diff --git a/src/rp2_common/pico_async_context/async_context_freertos.c b/src/rp2_common/pico_async_context/async_context_freertos.c index be7c08110..f6b1a5a1a 100644 --- a/src/rp2_common/pico_async_context/async_context_freertos.c +++ b/src/rp2_common/pico_async_context/async_context_freertos.c @@ -73,6 +73,7 @@ static void async_context_task(__unused void *vself) { async_context_freertos_release_lock(&self->core); __sev(); // it is possible regular code is waiting on a WFE on the other core } while (!self->task_should_exit); + xSemaphoreGive(self->task_complete_sem); vTaskDelete(NULL); } @@ -113,6 +114,7 @@ bool async_context_freertos_init(async_context_freertos_t *self, async_context_f assert(config->task_stack); self->lock_mutex = xSemaphoreCreateRecursiveMutexStatic(&self->lock_mutex_buf); self->work_needed_sem = xSemaphoreCreateBinaryStatic(&self->work_needed_sem_buf); + self->task_complete_sem = xSemaphoreCreateBinaryStatic(&self->task_complete_sem_buf); self->timer_handle = xTimerCreateStatic( "async_context_timer", // Just a text name, not used by the kernel. portMAX_DELAY, pdFALSE, // The timers will auto-reload themselves when they expire. @@ -129,6 +131,7 @@ bool async_context_freertos_init(async_context_freertos_t *self, async_context_f #else self->lock_mutex = xSemaphoreCreateRecursiveMutex(); self->work_needed_sem = xSemaphoreCreateBinary(); + self->task_complete_sem = xSemaphoreCreateBinary(); self->timer_handle = xTimerCreate( "async_context_timer", // Just a text name, not used by the kernel. portMAX_DELAY, pdFALSE, // The timers will auto-reload themselves when they expire. @@ -171,6 +174,9 @@ void async_context_freertos_deinit(async_context_t *self_base) { async_context_freertos_t *self = (async_context_freertos_t *)self_base; if (self->task_handle) { async_context_execute_sync(self_base, end_task_func, self_base); + if (self->task_complete_sem) { + xSemaphoreTake(self->task_complete_sem, portMAX_DELAY); + } } if (self->timer_handle) { xTimerDelete(self->timer_handle, 0); @@ -181,6 +187,9 @@ void async_context_freertos_deinit(async_context_t *self_base) { if (self->work_needed_sem) { vSemaphoreDelete(self->work_needed_sem); } + if (self->task_complete_sem) { + vSemaphoreDelete(self->task_complete_sem); + } memset(self, 0, sizeof(*self)); } diff --git a/src/rp2_common/pico_async_context/include/pico/async_context_freertos.h b/src/rp2_common/pico_async_context/include/pico/async_context_freertos.h index 273d653f2..0120410f7 100644 --- a/src/rp2_common/pico_async_context/include/pico/async_context_freertos.h +++ b/src/rp2_common/pico_async_context/include/pico/async_context_freertos.h @@ -75,11 +75,13 @@ struct async_context_freertos { async_context_t core; SemaphoreHandle_t lock_mutex; SemaphoreHandle_t work_needed_sem; + SemaphoreHandle_t task_complete_sem; TimerHandle_t timer_handle; TaskHandle_t task_handle; #if configSUPPORT_STATIC_ALLOCATION StaticSemaphore_t lock_mutex_buf; StaticSemaphore_t work_needed_sem_buf; + StaticSemaphore_t task_complete_sem_buf; StaticTimer_t timer_buf; StaticTask_t task_buf; #endif