10000 Fix FreeRTOS CoreMutex shim to handle ISRs (#1442) · esphome/arduino-pico@652f9f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 652f9f9

Browse files
Fix FreeRTOS CoreMutex shim to handle ISRs (earlephilhower#1442)
* Fix FreeRTOS CoreMutex shim to handle ISRs Automatically check, when in FreeRTOS, if we're in an ISR and if so call the correct mutex grab. Thanks to @caveman99 for finding and proposing a solution! Fixes earlephilhower#1441 * Fix the CoreMutex destructor, too
1 parent cac9eb0 commit 652f9f9

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

cores/rp2040/CoreMutex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ CoreMutex::CoreMutex(mutex_t *mutex, uint8_t option) {
3030
_option = option;
3131
if (__isFreeRTOS) {
3232
auto m = __get_freertos_mutex_for_ptr(mutex);
33-
if (_option & FromISR) {
33+
if (__freertos_check_if_in_isr()) {
3434
__freertos_mutex_take_from_isr(m);
3535
} else {
3636
if (!__freertos_mutex_try_take(m)) {
@@ -56,7 +56,7 @@ CoreMutex::~CoreMutex() {
5656
if (_acquired) {
10000
5757
if (__isFreeRTOS) {
5858
auto m = __get_freertos_mutex_for_ptr(_mutex);
59-
if (_option & FromISR) {
59+
if (__freertos_check_if_in_isr()) {
6060
__freertos_mutex_give_from_isr(m);
6161
} else {
6262
__freertos_mutex_give(m);

cores/rp2040/CoreMutex.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
#include "_freertos.h"
2828

2929
enum {
30-
DebugEnable = 1,
31-
FromISR = 1 << 1,
30+
DebugEnable = 1
3231
};
3332

3433
class CoreMutex {

cores/rp2040/_freertos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern "C" {
3737
typedef QueueHandle_t SemaphoreHandle_t;
3838
#endif
3939

40+
extern bool __freertos_check_if_in_isr() __attribute__((weak));
41+
4042
extern SemaphoreHandle_t __freertos_mutex_create() __attribute__((weak));
4143
extern SemaphoreHandle_t _freertos_recursive_mutex_create() __attribute__((weak));
4244

cores/rp2040/wiring_private.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static std::map<pin_size_t, CBInfo> _map;
7777
void _gpioInterruptDispatcher(uint gpio, uint32_t events) {
7878
(void) events;
7979
// Only need to lock around the std::map check, not the whole IRQ callback
80-
CoreMutex m(&_irqMutex, (FromISR | DebugEnable));
80+
CoreMutex m(&_irqMutex);
8181
if (m) {
8282
auto irq = _map.find(gpio);
8383
if (irq != _map.end()) {

libraries/FreeRTOS/src/variantHooks.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ extern "C" {
8585
void __freertos_recursive_mutex_give(SemaphoreHandle_t mtx) {
8686
xSemaphoreGiveRecursive(mtx);
8787
}
88+
89+
bool __freertos_check_if_in_isr() {
90+
return portCHECK_IF_IN_ISR();
91+
}
8892
}
8993

9094

@@ -488,5 +492,3 @@ void __USBStart() {
488492
xTaskCreate(__usb, "USB", 256, 0, configMAX_PRIORITIES - 2, &__usbTask);
489493
vTaskCoreAffinitySet(__usbTask, 1 << 0);
490494
}
491-
492-

0 commit comments

Comments
 (0)
0