🛠 Exposer un stockage de masse USB (TinyUSB MSC)
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
- 1 × Arduino Nano ESP32 + USB-C cable
- Recent "Arduino ESP32 Boards" package (TinyUSB stack included)
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
- Forgetting MassStorage.begin before USB.begin: enumeration presents no disk.
- Uninitialized image (zero size): the OS rejects the volume.
- Loss of serial port during MSC enumeration: use the double-press reset (DFU bootloader) or replug the board.
- >
Other tutorials for this board
Code examples for this board
connexion-wifi.inoArduinopoint-acces.inoArduinoble-capteur.inoArduinoneoled-rgb.inoArduinoadc-12bits.inoArduinopreferences.inoArduino