From 89702417724115afe3c5516fde31df06a4aa72f6 Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Thu, 21 Nov 2024 12:24:17 -0500 Subject: [PATCH] Fix flash_safe_execute timeout under FreeRTOS (#2021) In some cases, the flash lockout task can start on the wrong core and not have time to move to the correct core before exit is called. This causes a timeout as the exit function is looking at the wrong core when checking for the lockout task. --- src/rp2_common/pico_flash/flash.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rp2_common/pico_flash/flash.c b/src/rp2_common/pico_flash/flash.c index 0d560674..aa98f980 100644 --- a/src/rp2_common/pico_flash/flash.c +++ b/src/rp2_common/pico_flash/flash.c @@ -142,16 +142,14 @@ static int default_enter_safe_zone_timeout_ms(__unused uint32_t timeout_ms) { // it only prevents the other core from also entering a critical section. // Therefore, we must do our own handshake which starts a task on the other core and have it disable interrupts uint core_num = get_core_num(); - // create at low priority + // create at low priority on other core TaskHandle_t task_handle; - if (pdPASS != xTaskCreate(flash_lockout_task, "flash lockout", configMINIMAL_STACK_SIZE, (void *)core_num, 0, &task_handle)) { + if (pdPASS != xTaskCreateAffinitySet(flash_lockout_task, "flash lockout", configMINIMAL_STACK_SIZE, (void *)core_num, 0, 1u << (core_num ^ 1), &task_handle)) { return PICO_ERROR_INSUFFICIENT_RESOURCES; } lockout_state[core_num] = FREERTOS_LOCKOUT_LOCKER_WAITING; __sev(); - // bind to other core - vTaskCoreAffinitySet(task_handle, 1u << (core_num ^ 1)); - // and make it super high priority + // make it super high priority vTaskPrioritySet(task_handle, configMAX_PRIORITIES -1); absolute_time_t until = make_timeout_time_ms(timeout_ms); while (lockout_state[core_num] != FREERTOS_LOCKOUT_LOCKEE_READY && !time_reached(until)) {