romu-usb-flash-storage.ino

Carte de développement USB compacte Bitmerse Romu (RP2354A) · Fichier : romu-usb-flash-storage.ino · Langage : C++ (Arduino) · Usage : Utiliser la mémoire flash interne du RP2354A comme stockage de données persistant via USB Mass Storage.

Gère la lecture et l'écriture dans la mémoire flash interne pour sauvegarder des configurations ou des données.

Le RP2354A possède une mémoire flash QSPI. Ce code utilise les fonctions pico-sdk pour écrire à la fin de la mémoire. Attention : l'écriture flash est lente et usante. Pour un usage USB Mass Storage complet, utilisez la bibliothèque TinyUSB MSC.

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);
}

← Retour à la fiche