Hi! I'd double the variables, so there's a pair for each value. Plus a flag, which indicates which one of the pair is stable. When updating, I'd write to the "unstable" variable, and toggle the flag afterwards.
Greets, Kiste >________________________________ > Von: Alun Jones <[email protected]> >An: [email protected] >Gesendet: 15:02 Montag, 7.Januar 2013 >Betreff: [jallib] Advice regarding variable access from interrupt routines > > >Hi all, > >Quite often I need to access a variable both from inside an interrupt service >routine and from the main loop of a program. When this is a >byte value, it's presumably safe to just mark the variable as a volatile and >everything will be OK. > >When it's a word or a dword, there is presumably the risk of an interrupt >happening half way through an update or an access taking place in >the main loop. In this case, there is presumably a risk of getting a >half-updated value back. > >The example I'm looking at at the moment is reading an ADC value and returning >it via I2C (using i2c_hw_slave_msg). My main loop is doing >the ADC conversions and storing the value in a word variable. The I2C >interrupt-triggered procedure is picking up the most recent value and >returning it to the master. > >Now, if the I2C interrupt routine fires while the main loop is half way >through updating the variable, there's a good chance I'll send a bad value >to the master. > >This is just one example - I've also hit the same issue whilst trying to write >interrupt driven RTC routines. > >I can see various ways around it, but none of them feel particularly >satisfactory, especially where it's the main loop that's doing >the writes and the ISR needs to be able to read. > >So I thought I'd ask - what do the rest of you do in this general situation? >Is there any obvious (but not to me) solution? > >Cheers, >Alun. > > >-- >You received this message because you are subscribed to the Google Groups >"jallib" group. >To view this discussion on the web visit >https://groups.google.com/d/msg/jallib/-/UzAaRlyhNnIJ. >To post to this group, send email to [email protected]. >To unsubscribe from this group, send email to >[email protected]. >For more options, visit this group at >http://groups.google.com/group/jallib?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "jallib" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jallib?hl=en.
