From f38da1c76277bbed0b97d833656f56cbb286be26 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 21 Jun 2026 20:14:57 +0200 Subject: [PATCH] Add flash_commit_sync() to force a sync when committing. Signed-off-by: Pol Henarejos --- src/compat/esp_compat.h | 1 + src/fs/flash.c | 5 +++++ src/fs/flash.h | 1 + src/fs/low_flash.c | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/compat/esp_compat.h b/src/compat/esp_compat.h index 800ea4a..a49016b 100644 --- a/src/compat/esp_compat.h +++ b/src/compat/esp_compat.h @@ -22,6 +22,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/queue.h" +#include "freertos/task.h" typedef QueueHandle_t queue_t; #define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) #define queue_add_blocking(a,b) xQueueSend(*(a), b, portMAX_DELAY) diff --git a/src/fs/flash.c b/src/fs/flash.c index 51cee85..724f80b 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -37,6 +37,7 @@ uint32_t FLASH_SIZE_BYTES = (2 * 1024 * 1024); extern void low_flash_task(void); extern void low_flash_commit(void); +extern bool low_flash_commit_sync(uint32_t timeout_ms); /* * ------------------------------------------------------ @@ -193,3 +194,7 @@ void flash_task(void) { void flash_commit(void) { low_flash_commit(); } + +bool flash_commit_sync(uint32_t timeout_ms) { + return low_flash_commit_sync(timeout_ms); +} diff --git a/src/fs/flash.h b/src/fs/flash.h index afd00ea..fceb164 100644 --- a/src/fs/flash.h +++ b/src/fs/flash.h @@ -42,5 +42,6 @@ extern bool flash_check_blank(const uint8_t *p_start, size_t size); extern void flash_task(void); extern void low_flash_init(void); extern void flash_commit(void); +extern bool flash_commit_sync(uint32_t timeout_ms); #endif // _FLASH_H diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index 54f3139..d680b60 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -18,6 +18,7 @@ #include "picokeys.h" #include "serial.h" #include "crypto_utils.h" +#include "pico_time.h" #include #ifdef PICO_PLATFORM #include "hardware/flash.h" @@ -101,6 +102,7 @@ bool flash_available = false; //this function has to be called from the core 0 void low_flash_task(void); void low_flash_commit(void); +bool low_flash_commit_sync(uint32_t timeout_ms); void low_flash_task(void){ if (mutex_try_enter(&mtx_flash, NULL) == true) { @@ -256,6 +258,30 @@ void low_flash_commit(void) { mutex_exit(&mtx_flash); } +static bool low_flash_available(void) { + mutex_enter_blocking(&mtx_flash); + bool available = flash_available; + mutex_exit(&mtx_flash); + return available; +} + +bool low_flash_commit_sync(uint32_t timeout_ms) { + low_flash_commit(); + + uint32_t start = board_millis(); + while (low_flash_available()) { + if (board_millis() - start >= timeout_ms) { + return false; + } +#if defined(PICO_PLATFORM) + tight_loop_contents(); +#elif defined(ESP_PLATFORM) + vTaskDelay(1); +#endif + } + return true; +} + static page_flash_t *find_free_page(uintptr_t addr) { uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE; page_flash_t *p = NULL;