ESP32 critical zone example using FreeRTOS / PlatformIO
In order to enter a critical zone on the ESP32 using FreeRTOS, you have to do the following:
Globally declare a spinlock:
portMUX_TYPE mySpinlock;
In setup()
, initialize the spinlock:
spinlock_initialize(&mySpinlock);
Now, wherever you want to enter a critical zone, run:
portENTER_CRITICAL(&mySpinlock);
// TODO Your critical code goes here!
portEXIT_CRITICAL(&mySpinlock);
When using this **in an interrupt handler,**use this instead:
portENTER_CRITICAL_ISR(&mySpinlock);
// TODO Your critical code goes here!
portEXIT_CRITICAL_ISR(&mySpinlock);
FreeRTOS will ensure that no two threads using mySpinlock
are run at the same time.