Page 1 of 1

race condition with BLE - noInterrupts() not working

Posted: Wed Sep 26, 2018 9:19 am
by sterwen
Hello,

Working on a system using BLE and I need to protect some critical section of code, but haven't found the solution.
The BLE handling functions (onRead/OnWrite) are interrupting my code even if I have protected the critical section with noInterrupts() / interrupts().

Any hint, or low level specific function on ESP32 that I need to use ?

Thanks

Re: race condition with BLE - noInterrupts() not working

Posted: Wed Sep 26, 2018 3:24 pm
by ESP_igrr
It is possible that BLE callbacks are being called on CPU0, while your code is running on CPU1. In this case, disabling interrupts on CPU1 will not create a critical section.

If you need to protect access to some variables, you can use a mutex or (for very short pieces of code) a spinlock (portENTER_CRITICAL/portEXIT_CRITICAL).

Re: race condition with BLE - noInterrupts() not working

Posted: Thu Sep 27, 2018 8:45 am
by sterwen
Hello,

Thanks for the hint. I will look in to the documentation further, but can you give me some directions ?

1) How to control (and what are the rules) for CPU assignment ?
2) I don't believe that this is Arduino level, so I need to jump into the native ESP development environment. Is that fully compatible ?

Thanks for your help

Re: race condition with BLE - noInterrupts() not working

Posted: Thu Sep 27, 2018 9:52 am
by ESP_igrr
CPU assignment happens when tasks are created. For example, when Arduino core creates task which runs your "setup" and "loop" functions, it pins this task to CPU1: https://github.com/espressif/arduino-es ... in.cpp#L24

Similarly, when BT libraries create a task where callbacks are dispatched, they will also assign which CPU this task will run on. I'm not familiar with Arduino bluetooth libraries to point at the code line, though.

With regards to critical sections and mutexes, this should all be accessible from Arduino. At most, you will need to include some header files (like "freertos/FreeRTOS.h", "freertos/semphr.h").