🛠 Exposer un stockage de masse USB (TinyUSB MSC)

Arduino Nano ESP32 · avance · Schéma : IMG-nano-esp32-avance-1-nanoesp32-tinyusb-msd.svg — La carte énumérée comme clé USB dans l'explorateur, image disque stockée dans la flash de 16 Mo.

Objectives

Expose a USB mass storage device (MSC class from the TinyUSB stack of the ESP32 core): the board appears as a USB drive containing a formatted image, read from the 16 MB flash. Useful for retrieving data without a network. >

Hardware

Complete Code

// Inspiré de l'exemple USBMassStorage du core ESP32-S3 (à valider sur Nano ESP32)
#include "FS.h"
#include "SPIFFS.h"
#include "USB.h"
#include "USBMassStorage.h"

USBMassStorage MassStorage;
const uint32_t tailleCluster = 512;
const uint32_t nbClusters = 2048;   // ~1 Mo

void setup {
  Serial.begin(115200);
  if (!SPIFFS.begin(true)) { Serial.println("SPIFFS KO"); return; }
  File img = SPIFFS.open("/disque.bin", "w+");
  if (!img) { Serial.println("image KO"); return; }
  uint8_t vide[512] = {0};
  for (uint32_t i = 0; i < nbClusters; i++) img.write(vide, tailleCluster);
  img.close;
  MassStorage.begin("/disque.bin");   // avant USB.begin
  USB.begin;                        // énumère la carte en « clé USB »
  Serial.println("MSC actif : disque.bin exposé");
}

void loop { delay(1000); }

Expected Verification

After USB re-enumeration, the file explorer shows a removable volume backed by disk.bin; a file copied to it persists after a reset. The native serial port may disappear while MSC occupies the USB: plan for a reset to regain upload capability.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials