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 agoHi eliben,
> What is the best way to ensure safe access to shared data between an > ISR and the main code ? That depends on what you are trying to do -- there's usually not a single "best way". > Is disabling interrupts during the main's code reading of the data the best option ? In most cases, no. This usually leads to those "mysterious" bugs that show up when baud rates, cpu clocks, etc. change. > What are the common guideline when dealing with data synchronization > in ISRs for Nios ? Here's a good reference: http://www.xml.com/ldd/chapter/book/ch09.html#t8 (http://www.xml.com/ldd/chapter/book/ch09.html#t8) A very simple and common technique is to use a circular buffer. The size of the buffer can be adjusted to meet your requirements ... provided you have adequate memory resources. Here's a basic overview: 1. The isr reads a "put" pointer, and writes its data. Then updates the put pointer. 2. The app compares the put pointer to a "get" pointer, when unequal, there is data. 3. The app reads (copies, consumes, whatever) the data, then updates the get pointer. Regards, --Scott