Skip to content

Commit

Permalink
CoreMutex add portYieldFromISR for FreeRTOS (#1484)
Browse files Browse the repository at this point in the history
  • Loading branch information
hreintke authored Jun 7, 2023
1 parent 5204dab commit 3f475ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cores/rp2040/CoreMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ CoreMutex::CoreMutex(mutex_t *mutex, uint8_t option) {
_mutex = mutex;
_acquired = false;
_option = option;
_pxHigherPriorityTaskWoken = 0; // pdFALSE
if (__isFreeRTOS) {
auto m = __get_freertos_mutex_for_ptr(mutex);

if (__freertos_check_if_in_isr()) {
if (!__freertos_mutex_take_from_isr(m)) {
if (!__freertos_mutex_take_from_isr(m, &_pxHigherPriorityTaskWoken)) {
return;
}
// At this point we have the mutex in ISR
Expand Down Expand Up @@ -59,7 +61,7 @@ CoreMutex::~CoreMutex() {
if (__isFreeRTOS) {
auto m = __get_freertos_mutex_for_ptr(_mutex);
if (__freertos_check_if_in_isr()) {
__freertos_mutex_give_from_isr(m);
__freertos_mutex_give_from_isr(m, &_pxHigherPriorityTaskWoken);
} else {
__freertos_mutex_give(m);
}
Expand Down
1 change: 1 addition & 0 deletions cores/rp2040/CoreMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ class CoreMutex {
mutex_t *_mutex;
bool _acquired;
uint8_t _option;
BaseType_t _pxHigherPriorityTaskWoken;
};
6 changes: 4 additions & 2 deletions cores/rp2040/_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern "C" {
struct QueueDefinition; /* Using old naming convention so as not to break kernel aware debuggers. */
typedef struct QueueDefinition * QueueHandle_t;
typedef QueueHandle_t SemaphoreHandle_t;
typedef int32_t BaseType_t;
#endif

extern bool __freertos_check_if_in_isr() __attribute__((weak));
Expand All @@ -43,10 +44,11 @@ extern "C" {
extern SemaphoreHandle_t _freertos_recursive_mutex_create() __attribute__((weak));

extern void __freertos_mutex_take(SemaphoreHandle_t mtx) __attribute__((weak));
extern int __freertos_mutex_take_from_isr(SemaphoreHandle_t mtx) __attribute__((weak));

extern int __freertos_mutex_take_from_isr(SemaphoreHandle_t mtx, BaseType_t* pxHigherPriorityTaskWoken) __attribute__((weak));
extern int __freertos_mutex_try_take(SemaphoreHandle_t mtx) __attribute__((weak));
extern void __freertos_mutex_give(SemaphoreHandle_t mtx) __attribute__((weak));
extern void __freertos_mutex_give_from_isr(SemaphoreHandle_t mtx) __attribute__((weak));
extern void __freertos_mutex_give_from_isr(SemaphoreHandle_t mtx, BaseType_t* pxHigherPriorityTaskWoken) __attribute__((weak));

extern void __freertos_recursive_mutex_take(SemaphoreHandle_t mtx) __attribute__((weak));
extern int __freertos_recursive_mutex_try_take(SemaphoreHandle_t mtx) __attribute__((weak));
Expand Down
10 changes: 6 additions & 4 deletions libraries/FreeRTOS/src/variantHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ extern "C" {
xSemaphoreTake(mtx, portMAX_DELAY);
}

int __freertos_mutex_take_from_isr(SemaphoreHandle_t mtx) {
return xSemaphoreTakeFromISR(mtx, NULL);
int __freertos_mutex_take_from_isr(SemaphoreHandle_t mtx, BaseType_t* pxHigherPriorityTaskWoken) {
return xSemaphoreTakeFromISR(mtx, pxHigherPriorityTaskWoken);
}

int __freertos_mutex_try_take(SemaphoreHandle_t mtx) {
Expand All @@ -70,8 +70,10 @@ extern "C" {
xSemaphoreGive(mtx);
}

void __freertos_mutex_give_from_isr(SemaphoreHandle_t mtx) {
xSemaphoreGiveFromISR(mtx, NULL);
void __freertos_mutex_give_from_isr(SemaphoreHandle_t mtx, BaseType_t* pxHigherPriorityTaskWoken) {
BaseType_t hiPrio = pxHigherPriorityTaskWoken ? *pxHigherPriorityTaskWoken : pdFALSE;
xSemaphoreGiveFromISR(mtx, &hiPrio);
portYIELD_FROM_ISR(hiPrio);
}

void __freertos_recursive_mutex_take(SemaphoreHandle_t mtx) {
Expand Down

0 comments on commit 3f475ac

Please sign in to comment.