CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

a conflict variable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stefsun



Joined: 23 May 2007
Posts: 22

View user's profile Send private message

a conflict variable
PostPosted: Tue Apr 29, 2008 2:18 am     Reply with quote

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 );

RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: a conflict variable
PostPosted: Tue Apr 29, 2008 5:45 am     Reply with quote

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
Matro
Guest







Re: a conflict variable
PostPosted: Tue Apr 29, 2008 6:04 am     Reply with quote

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.
stefsun



Joined: 23 May 2007
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 7:14 am     Reply with quote

thanks Matro and Robert Scott
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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