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 agoDon't forget to specify variables as "volatile" to prevent them being optimised out.
Sometimes I do tricks such as implementing S/R Flip Flops or counters in hardware, with different memory addresses to set/reset increment/decrement/read them. For simple FIFO buffers I'll do e.g. "ISR owns write pointer", "main thread owns read pointer" as Scott says. I will also use a "main thread is using this" flag as you've suggested. So//shared
volatile int MT_Access;
//main thread
MT_Access = 1;
<do something>
MT_Access = 0;
//ISR
if (0 == MT_Access)
{
<update something>
}