ESP32S3 MSC failure

hi there. i want to use ESP32S as Mass Storage device and modified example code. code compiled with no error but Windows 10 says "insert disk".

ESP32S3 Dev Module Selected
USB-Mode -> USB OTG (tiny USB)
USB CDC Disabled
Firmware Enabled

Can you help me?

#ifndef ARDUINO_USB_MODE
#error This ESP32 SoC has no Native USB interface
#elif ARDUINO_USB_MODE == 1
#warning This sketch should be used when USB is in OTG mode
void setup() {}
void loop() {}
#else

#include "Arduino.h"
#include "USB.h"
#include "USBMSC.h"
#include "FFat.h"


USBMSC usb_msc;  // USB Mass Storage nesnesi

static void onUSBChange(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
  if (event_base == ARDUINO_USB_EVENTS) {
    arduino_usb_event_data_t *data = (arduino_usb_event_data_t *)event_data;
    switch (event_id) {
      case ARDUINO_USB_STARTED_EVENT: Serial.println("USB PLUGGED"); break;
      case ARDUINO_USB_STOPPED_EVENT: Serial.println("USB UNPLUGGED"); break;
      case ARDUINO_USB_SUSPEND_EVENT: Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en); break;
      case ARDUINO_USB_RESUME_EVENT:  Serial.println("USB RESUMED"); break;

      default: break;
    }
  }
 }
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject) {
  Serial.printf("MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
  return true;
}

static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
  Serial.printf("MSC WRITE: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
  File file = FFat.open("/mscfile");
  if (file) {
    file.seek(lba * 512 + offset);
    file.write(buffer, bufsize);
    file.close();
  } else {
    Serial.println("Failed to open file for writing");
    return -1;
  }

  Serial.printf("MSC WRITE: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
  return bufsize;
}
static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
  Serial.printf("MSC READ: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
  File file = FFat.open("/mscfile");
  if (file) {
    file.seek(lba * 512 + offset);
    file.read((uint8_t*)buffer, bufsize);
    file.close();
  } else {
    Serial.println("Failed to open file for reading");
    return -1;
  }

  Serial.printf("MSC READ: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
  return bufsize;
}

void setup() {
  Serial.begin(115200);

  if (!FFat.begin(true)) {  // true = format if needed
    Serial.println("FFat cannot be started!");
    return;
  }
  Serial.println("FFat started!");
  if (!FFat.format()) {
    Serial.println("formatted");
    return;
  }
  else {
    Serial.println("NOT formatted ");
  }
  Serial.print("fat size ");
  Serial.println(FFat.totalBytes());
  Serial.print("usable  ");
  Serial.println(FFat.freeBytes());

  File file = FFat.open("/mscfile", FILE_WRITE);
  if (file) {
    file.close();
    Serial.println("mscfile created.");
  } else {
    Serial.println("mscfile cannot be created!");
    return;
  }
  // USB Mass Storage başlatma
  usb_msc.vendorID("ESP32");       //max 8 chars
  usb_msc.productID("USB_MSC");    //max 16 chars
  usb_msc.productRevision("1.0");  //max 4 chars
  usb_msc.onStartStop(onStartStop);
  usb_msc.onRead(onRead);
  usb_msc.onWrite(onWrite);
  Serial.println("written!");

  usb_msc.mediaPresent(true);
  usb_msc.isWritable(true);  // true if writable, false if read-only

  usb_msc.begin(32, 512); // Root dizinini paylaş
  USB.onEvent(onUSBChange);  // USB bağlantı durumunu takip et
  USB.begin();

  Serial.println("USB MSC started!");
}

void loop() {

}
#endif

It looks like the Pc finds Your device being a CD player or similar, a read only device. Check what code/status Your device is sending to describe itself.

but CDC is disabled.

What remains being enabled? CDR should not allowed either.

im using arduino IDE 1.8 and there is no selection like CDR. only CDC and USB Mode can be selected. i selected USB OTG enabled and USB firmware MSC on boot enabled

Sorry im newbee on this topic

This is beyond my experience but just wait for a more fit helper to step in.

Um, at the risk of being obvious, insert the 'mass storage device'

ok. thank you.

1 Like

I think he misread your CD as CDC. And you misread his CDC (CommDeviceClass) as a line signal :)))

Your thinking is wrong.

finally i found the solution.
IDE options;
ESP32-S3-US-OTG, 4 MB with FFat, USB mode USB-OTG.
I'm searching for access and control the files stored in MSC now.
here is the code works perfectly.

#include "USB.h"
#include "USBMSC.h"
#include "FFat.h"

USBMSC MSC;

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.