Skip to content

Commit 5a94944

Browse files
Fix USB crashes in FreeRTOS (#1419)
Fixes #1402 The global USB mutex is auto-shadowed with a FreeRTOS semaphore while in FreeRTOS mode. Unfortunately, while the core was using the proper FreeRTOS semaphore to lock access to the USB port, the actual FreeRTOS USB task was using the naked Pico SDK mutex, leading to cases where it could acquire the mutex even though some other FreeRTOS task actually owned the shadowed mutex. Properly lock the shadowed Semaphore, not mutex_t, in the FreeRTOS USB periodic task.
1 parent a916695 commit 5a94944

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

cores/rp2040/CoreMutex.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ CoreMutex::CoreMutex(mutex_t *mutex, uint8_t option) {
3333
if (_option & FromISR) {
3434
__freertos_mutex_take_from_isr(m);
3535
} else {
36-
__freertos_mutex_take(m);
36+
if (!__freertos_mutex_try_take(m)) {
37+
return;
38+
}
3739
}
3840
} else {
3941
uint32_t owner;

libraries/FreeRTOS/src/variantHooks.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,10 @@ static void __usb(void *param) {
380380
__usbInitted = true;
381381

382382
while (true) {
383-
if (mutex_try_enter(&__usb_mutex, NULL)) {
383+
auto m = __get_freertos_mutex_for_ptr(&__usb_mutex);
384+
if (xSemaphoreTake(m, 0)) {
384385
tud_task();
385-
mutex_exit(&__usb_mutex);
386+
xSemaphoreGive(m);
386387
}
387388
vTaskDelay(1 / portTICK_PERIOD_MS);
388389
}

0 commit comments

Comments
 (0)