USB MSC
About
USB Mass Storage Class API. This class makes the device accessible as a mass storage device and allows you to transfer data between the host and the device.
One of the examples for this mode is to flash the device by dropping the firmware binary like a flash memory device when connecting the ESP32 to the host computer.
APIs
begin
This function is used to start the peripheral using the default MSC configuration.
bool begin(uint32_t block_count, uint16_t block_size);
Where:
block_count
set the disk sector count.block_size
set the disk sector size.
This function will return true
if the configuration was successful.
end
This function will finish the peripheral as MSC and release all the allocated resources. After calling end
you need to use begin
again in order to initialize the USB MSC driver again.
void end();
vendorID
This function is used to define the vendor ID.
void vendorID(const char * vid);//max 8 chars
productID
This function is used to define the product ID.
void productID(const char * pid);//max 16 chars
productRevision
This function is used to define the product revision.
void productRevision(const char * ver);//max 4 chars
Example Code
Here is an example of how to use the USB MSC.
FirmwareMSC
#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 "USB.h"
#include "FirmwareMSC.h"
#if !ARDUINO_USB_MSC_ON_BOOT
FirmwareMSC MSC_Update;
#endif
static void usbEventCallback(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;
}
} else if (event_base == ARDUINO_FIRMWARE_MSC_EVENTS) {
arduino_firmware_msc_event_data_t *data = (arduino_firmware_msc_event_data_t *)event_data;
switch (event_id) {
case ARDUINO_FIRMWARE_MSC_START_EVENT: Serial.println("MSC Update Start"); break;
case ARDUINO_FIRMWARE_MSC_WRITE_EVENT:
//Serial.printf("MSC Update Write %u bytes at offset %u\n", data->write.size, data->write.offset);
Serial.print(".");
break;
case ARDUINO_FIRMWARE_MSC_END_EVENT: Serial.printf("\nMSC Update End: %u bytes\n", data->end.size); break;
case ARDUINO_FIRMWARE_MSC_ERROR_EVENT: Serial.printf("MSC Update ERROR! Progress: %u bytes\n", data->error.size); break;
case ARDUINO_FIRMWARE_MSC_POWER_EVENT:
Serial.printf("MSC Update Power: power: %u, start: %u, eject: %u", data->power.power_condition, data->power.start, data->power.load_eject);
break;
default: break;
}
}
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
USB.onEvent(usbEventCallback);
MSC_Update.onEvent(usbEventCallback);
MSC_Update.begin();
USB.begin();
}
void loop() {
// put your main code here, to run repeatedly
}
#endif /* ARDUINO_USB_MODE */