cpu/esp32: critcial section handling changed in FreeRTOS adaptation layer#11947
Conversation
|
@JulianHolzwarth May I ask you to take look on this? This might be interesting for your work. |
|
Thank you for letting me know. |
Probably not, since a spin-lock would not improve the current mechanism and could block an ISR too long. I will remove the comment with the spin lock, but I would prefer to leave the
I know, FreeRTOS doesn't define the mutex parameter. Unfortunatly, the ESP32 port of FreeRTOS does. And even more, SDK headers and SDK libraries require this interface. Therefore, I would leave this parameter there for the moment, even it is not used. When you have finished your FreeRTOS adaptation layer, we have to think about it again. |
|
Ok. Thank you. |
4be4260 to
146ad38
Compare
Using a mutex for critical section handling with portENTER_CRITICAL and portEXIT_CRITICAL does not work for RIOT, as this function can also be called in the interrupt context. Therefore, the given mutex is not used. Instead, the basic default FreeRTOS mechanism for critical sections is used by simply disabling interrupts. Since context switches for the ESP32 are also based on interrupts, there is no possibility that another thread will enter the critical section once the interrupts are disabled.
146ad38 to
8c5de9b
Compare
|
@benpicco All lights are green 😄 |
|
@benpicco Thanks again for reviewing, testing and merging 😄 |
Contribution description
This PR changes the critical section handling of the FreeRTOS adaptation layer that is required by ESP32 SDK libraries for the WiFi interface.
Background
ESP32 SDK libraries for the WiFi interface require a number of FreeRTOS functions to work, e.g., crittical section handling, queues, mutexes and semaphores. To provide the ESP SDK libraries with these required FreeRTOS functions, an adaptation layer module in
cpu/esp32/freertosrealizes a mapping of these functions to RIOT's mechansims.Critical section handling
The critical section handling of FreeRTOS for ESP32 realized by the
portENTER_CRITICAL,taskENTER_CRITICAL,portEXIT_CRITICALandtaskEXIT_CRITICALmacros/functions uses mutexes. However, since these macros/functions are also called in interrupt context, the mutex based implementation of critical section handling does not work in RIOT.This led to a number of instability problems when the WiFi interface was used (modules
esp_wifiand/oresp_now), e.g., the problem with the multiheap corruption described in issue #11941. Therefore, the former mutex based implementation of critical section handling had to be changed.Change
The mutex that is given as parameter for critical section handling macros/function is not used any longer. Instead, the basic default FreeRTOS mechanism for critical sections is realized by simply disabling interrupts. Once the interrupt is disabled by calling
portENTER_CRITICALortaskENTER_CRITICAL, the execution in critical section can't be interrupted. Since context switches for the ESP32 are also based on interrupts, there is no possibility that another thread will enter the critical section once the interrupts are disabled, even if an action within the critical section would trigger athread_yield_higher.I have tested the changes with ESP32 border router test configuration as described in #10929. While the CORRUPT multiheap error message as described in #11941 occured within several minutes without these changes, the configuration is working with the changes since more than one day without any error message.
Testing procedure
Setup an ESP-NOW network of two ESP32 nodes. For that purpose compile and flash two nodes with:
After that, connect to the nodes with a terminal program and use some commands like
ifconfigandping6and wait. Without the changes in this PR, aCORRUPTmessage should come after several minutes. With the changes the same configuration should work stable.Issues/PRs references
Fixes issue #11941.
Makes PR #11942 obsolete.