romu-usb-flash-storage.ino
Handles reading and writing to internal flash memory to save configurations or data.
The RP2354A has QSPI flash memory. This code uses pico-sdk functions to write to the end of memory. Warning: flash writing is slow and wears out. For full USB Mass Storage, use the TinyUSB MSC library.
romu-usb-flash-storage.ino
#include <Arduino.h>
#include "pico/stdlib.h"
#include "hardware/flash.h"
// Zone de stockage en flash (dernier secteur)
#define FLASH_SECTOR_SIZE 4096
#define STORAGE_OFFSET (2 * 1024 * 1024 - FLASH_SECTOR_SIZE)
uint8_t buffer[FLASH_SECTOR_SIZE];
void setup() {
Serial.begin(115200);
// Lecture initiale
memcpy(buffer, (uint8_t*)XIP_BASE + STORAGE_OFFSET, FLASH_SECTOR_SIZE);
Serial.print("First byte in flash: ");
Serial.println(buffer[0]);
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if (c == 'w') {
// Écriture exemple
memset(buffer, 0, FLASH_SECTOR_SIZE);
buffer[0] = 0xAA;
// Désactivation IRQ pour écriture flash
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(STORAGE_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(STORAGE_OFFSET, buffer, FLASH_SECTOR_SIZE);
restore_interrupts(ints);
Serial.println("Data written to flash.");
}
}
delay(100);
}