Add flash_commit_sync() to force a sync when committing.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-06-21 20:40:39 +02:00
parent 14191e1aee
commit f38da1c762
4 changed files with 33 additions and 0 deletions
+1
View File
@@ -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)
+5
View File
@@ -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);
}
+1
View File
@@ -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
+26
View File
@@ -18,6 +18,7 @@
#include "picokeys.h"
#include "serial.h"
#include "crypto_utils.h"
#include "pico_time.h"
#include <stdio.h>
#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;