m-mcgowan / spark-flashee-eeprom

Eeprom emulation using external flash on Particle devices. Includes unit tests cross compiled to regular gcc, and on-device integration tests.
GNU Affero General Public License v3.0
34 stars 8 forks source link

make circular buffer usable from a ISR and main loop #1

Open m-mcgowan opened 10 years ago

m-mcgowan commented 10 years ago

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.

aaraujo11 commented 9 years ago

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.

m-mcgowan commented 9 years ago

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.