race condition with BLE - noInterrupts() not working

sterwen
Posts: 6
Joined: Sun Sep 02, 2018 12:17 pm

race condition with BLE - noInterrupts() not working

Postby sterwen » Wed Sep 26, 2018 9:19 am

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

ESP_igrr
Posts: 1264
Joined: Tue Dec 01, 2015 8:37 am

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

Postby ESP_igrr » Wed Sep 26, 2018 3:24 pm

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).

sterwen
Posts: 6
Joined: Sun Sep 02, 2018 12:17 pm

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

Postby sterwen » Thu Sep 27, 2018 8:45 am

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

ESP_igrr
Posts: 1264
Joined: Tue Dec 01, 2015 8:37 am

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

Postby ESP_igrr » Thu Sep 27, 2018 9:52 am

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").

Who is online

Users browsing this forum: No registered users and 4 guests