![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
stefsun
Joined: 23 May 2007 Posts: 22
|
a conflict variable |
Posted: Tue Apr 29, 2008 2:18 am |
|
|
A variable can be read, and also can write, if there is a conflict, one is reading the variable, and the other is writing the variable. In the case,error will be there. How to solve the problem ?
I know that VC can use the mutex or Critical Section the problem, but in CCS, I do not know how to solve the problem.
for example
Code: |
main
adc_value = read_adc();
#INT_TBE
putc( adc_value );
|
|
|
![](templates/subSilver/images/spacer.gif) |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: a conflict variable |
Posted: Tue Apr 29, 2008 5:45 am |
|
|
There is nothing like a mutex or Critical Region provided by the CCS compiler. But you can provide your own if you need to. Normally you don't need these formal objects.
In your example,if adc_val is a single byte, then there is no conflict. Whenever INT_TBE gets control, it will use the most recent value of adc_val that was written by the main program. But if the shared variable is two or more bytes, then you may have to take some precautions. There are several ways to resolve the conflict, depending on the needs of your application:
1. Disable the interrupt in main() before writing the variable, and then re-enable the interrupt afterwards.
2. Set a flag variable in main before writing the variable and clear that flag afterwards. Then design the interrupt code so that is does not use the shared variable if the flag is set. What it does do when the flag is set is up to you. Perhaps it is OK to do nothing. Or else it could use an old value saved from before.
3. If the interrupt is periodic, then wait in main until just after the interrupt hits before writing the shared variable. Then the interrupt will not hit during the writing of the variable.
And there may be other solutions.
Robert Scott
Real-Time Specialties |
|
![](templates/subSilver/images/spacer.gif) |
Matro Guest
|
Re: a conflict variable |
Posted: Tue Apr 29, 2008 6:04 am |
|
|
stefsun wrote: | A variable can be readed, and also can write, if there is a conflict,one is reading the variable,and the other is writing the variable. In the case,error will be there.how to solve the problem.
I know that the VC can use the mutex or Critical solute the problem, ccs, do not I know how to solve the problem?
for example
Code: |
main
adc_value = read_adc();
INT_TBE
putc( adc_value );
|
|
There can't be any problem since all is done sequentially in a MCU.
So either a variable is read or it is written. But it can be both.
Mutex are not used for this kind of reason for more high-level ressource accesses.
In your example, the 2 instructions can't be executed exactly at the same time. That's not physically possible. So you will never have a conflict because a read and a write happen at the same time.
If for some reason you want a "protection", you can disable interrupts or use a flag.
Matro. |
|
![](templates/subSilver/images/spacer.gif) |
stefsun
Joined: 23 May 2007 Posts: 22
|
|
Posted: Tue Apr 29, 2008 7:14 am |
|
|
thanks Matro and Robert Scott |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|