hi there! i want to use my ESP32S3 as both Mass Storage Device and want to control/list the files on ESP side uploaded to ESP32. So i can delete the restricted files from MSC. Is that possible? i wrote the code below but MSC is unstable and listing files returns zero. Can I use SPIFFS in this way? thanks in advanced.
[Codebox]
#include "USB.h"
#include "USBMSC.h"
#include "FFat.h"
USBMSC MSC;
boolean currentButtonState;
boolean previousButtonState;
boolean buttonReleased;
// Storage parameters
const uint32_t SECTOR_SIZE = 512;
const uint32_t SECTOR_COUNT = 2.5 * 1024; // 1.5MB storage
// MSC read callback
int32_t onRead(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
File f = FFat.open("/disk.img", "r");
// File f = FFat.open("/disk/", "r");
if (!f) return 0;
f.seek(lba * SECTOR_SIZE + offset);
return f.read((uint8_t*)buffer, bufsize);
}
// MSC write callback
int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
File f = FFat.open("/disk.img", "r+");
// File f = FFat.open("/disk/", "r+");
if (!f) return 0;
f.seek(lba * SECTOR_SIZE + offset);
return f.write(buffer, bufsize);
}
void listDir(fs::FS& fs, const char* dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("- failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
listDir(fs, file.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void setup() {
Serial.begin(115200);
// Initialize FFAT
if (!FFat.begin(true)) {
Serial.println("FFat Mount Failed");
return;
}
// Create disk image file if not exists
if (!FFat.exists("/disk.img")) {
File f = FFat.open("/disk.img", "w");
// if (!FFat.exists("/disk/")) {
// File f = FFat.open("/disk/", "w");
if (f) {
for (uint32_t i = 0; i < SECTOR_COUNT * SECTOR_SIZE; i++) {
f.write(0);
}
f.close();
}
}
// Configure MSC
MSC.vendorID("ESP32");
MSC.productID("USB Drive");
MSC.productRevision("1.0");
MSC.onRead(onRead);
MSC.onWrite(onWrite);
MSC.mediaPresent(true);
MSC.begin(SECTOR_COUNT, SECTOR_SIZE);
// Start USB
USB.begin();
Serial.println("USB MSC device started");
}
unsigned long lastTime = 0;
void loop() {
if (millis() - lastTime > 3000) {
listDir(FFat, "/disk.img", 0);
lastTime = millis();
}
}
[/Codebox]
ESP32S3 USB Mass Storage Device data control
-
- Posts: 3
- Joined: Sat Feb 08, 2025 5:38 pm
-
- Posts: 56
- Joined: Sat Jan 18, 2025 2:31 pm
Re: ESP32S3 USB Mass Storage Device data control
FFat (FAT on Flash) is not designed for rapid read/write access, which MSC requires. Instead, try LittleFS, which is optimized for flash memory:
Then initialize LittleFS:
Code: Select all
#include "LittleFS.h"
#define FS LittleFS // Replace FFat with LittleFS
Code: Select all
if (!FS.begin()) {
Serial.println("LittleFS Mount Failed");
return;
}
-
- Posts: 916
- Joined: Mon Jul 22, 2019 3:20 pm
Re: ESP32S3 USB Mass Storage Device data control
USB_MSC is accessing the partition as a block device, so the host is managing the file system separately. @chegewara has built a demonstration app. You may be able to simultaneously have FFat mounted, but must put in some guards (probably a single access mutex) to prevent race conditions.
-
- Posts: 3
- Joined: Sat Feb 08, 2025 5:38 pm
Re: ESP32S3 USB Mass Storage Device data control
Thank you all. i will try littlefs and demo.
-
- Posts: 3
- Joined: Sat Feb 08, 2025 5:38 pm
Re: ESP32S3 USB Mass Storage Device data control
I tried LittleFS but windows says insert disk. it's not recognized by the PC. And i can not uderstand the demo. So i don't know how can i achive.
Who is online
Users browsing this forum: No registered users and 99 guests