Forum Discussion
Altera_Forum
Honored Contributor
20 years agoSynchronizing between ISR and main code
Hello all, What is the best way to ensure safe access to shared data between an ISR and the main code ? In my application, the ISR reads a packet from the FIFO of a custom UART and saves i...
Altera_Forum
Honored Contributor
20 years agoThe circular buffer is indeed useful, implementing a FIFO in software. I wanted, however, to avoid it and use something simpler, as I don't need a FIFO. I can, however, use a "trivial" FIFO which has depth 1 and "loses" all subsequent packets until the old ones has been read.
Which brings me to the question - are variable assignment and test atomic in Nios ? What I mean is:int ready;
// is this atomic ?
ready = 1;
// and is this atomic
if (ready == 1)
{
...
} I ask because I can make my main code "deassert" the 'ready' flag when it finished reading the packet, and the ISR to ignore a new packet if 'ready' is still asserted. This way no stomping occurs (overflow messages are lost, which is acceptable). So I wonder whether it's possible for the ISR to run in the middle of asserting 'ready' ? If "ready = 1" is atomic, it certainly can't.