romu-internal-flash-storage.ino
Uses RP2354 internal Flash memory to store persistent data without external EEPROM.
RP2354 has integrated Flash. This example shows how to write and read from a specific area. Beware of limited write cycles (100k). Use for saving HID device configuration. Requires hardware/flash.h header.
romu-internal-flash-storage.ino
#include "pico/stdlib.h"
#include "pico/bootrom.h"
#include "hardware/flash.h"
// Adresse de début de la zone de stockage (dernière page de flash, par exemple)
#define STORAGE_START 0x101FF000 // Dépend de la taille de la flash RP2354
#define PAGE_SIZE 256
typedef struct {
uint32_t magic_word;
uint32_t counter;
char name[32];
} UserConfig;
void save_config(UserConfig *config) {
// Effacer la page
flash_range_erase(STORAGE_START - XIP_BASE, PAGE_SIZE);
// Écrire les données
flash_range_program(STORAGE_START - XIP_BASE, (const uint8_t *)config, sizeof(UserConfig));
}
bool load_config(UserConfig *config) {
uint32_t *flash_ptr = (uint32_t *)STORAGE_START;
if (flash_ptr[0] == 0xDEADBEEF) {
memcpy(config, (void *)STORAGE_START, sizeof(UserConfig));
return true;
}
return false;
}
int main() {
stdio_init_all();
UserConfig config;
if (load_config(&config)) {
printf("Loaded: Name=%s, Count=%lu\n", config.name, config.counter);
config.counter++;
} else {
printf("No config found, initializing...\n");
config.magic_word = 0xDEADBEEF;
config.counter = 1;
strcpy(config.name, "RomuUser");
}
save_config(&config);
printf("Saved new count: %lu\n", config.counter);
while (true) {
sleep_ms(1000);
}
}