spark-flashee-eeprom icon indicating copy to clipboard operation
spark-flashee-eeprom copied to clipboard

make circular buffer usable from a ISR and main loop

Open m-mcgowan opened this issue 10 years ago • 2 comments

At present, the read and write pointers in circular buffer is not volatile, so will not work correctly when used form a ISR.

Filling the buffer from a ISR is useful when it needs to happen asynchronously from the main loop, such as with gathering and storing frequent sensor data.

m-mcgowan avatar Jun 24 '14 14:06 m-mcgowan

Hi @m-mcgowan,

Do you have any update in this issue? Because before i read this issue i have tried to use the circular buffer, and use the interruptions methods of https://github.com/pkourany/SparkIntervalTimer the spark starts to blink red.

aaraujo11 avatar Oct 30 '14 12:10 aaraujo11

It is more involved than just changing variables to volatile. If you want to use flash both from the main thread and also from an interrupt handler, then you all access to flash should be done using a critical section, to prevent both the main loop and the ISR from accessing at the same time. But then, when the ISR interrupts the main loop that is using flash, the ISR will block indefinitely waiting to enter the critical section (which is only released when the loop code is run, but that doesn't happen until the ISR returns...). A possible workaround is to have the Critical Section disable interrupts, which may work.

It at all possible, rather than reading/writing to flash from a ISR, it's best to set a flag, and handle all flash read/writes in the main loop.

m-mcgowan avatar Oct 30 '14 13:10 m-mcgowan