|
| 1 | +#ifndef ARDUINO_USB_MODE |
| 2 | +#error This sketch requires a device capable of USB-OTG |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <USB.h> |
| 6 | +#include <USBMSC.h> |
| 7 | +#include <SD_MMC.h> |
| 8 | + |
| 9 | +// USB Mass Storage Class (MSC) object |
| 10 | +USBMSC msc; |
| 11 | + |
| 12 | +int clk = 36; |
| 13 | +int cmd = 35; |
| 14 | +int d0 = 37; |
| 15 | +int d1 = 38; |
| 16 | +int d2 = 33; |
| 17 | +int d3 = 34; |
| 18 | +bool onebit = true; // set to false for 4-bit. 1-bit will ignore the d1-d3 pins (but d3 must be pulled high) |
| 19 | + |
| 20 | +static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize){ |
| 21 | + uint32_t secSize = SD_MMC.sectorSize(); |
| 22 | + if (!secSize) return false; // disk error |
| 23 | + log_v("Write lba: %ld\toffset: %ld\tbufsize: %ld", lba, offset, bufsize); |
| 24 | + for (int x=0; x< bufsize/secSize; x++) { |
| 25 | + uint8_t blkbuffer[secSize]; |
| 26 | + memcpy(blkbuffer, (uint8_t*)buffer + secSize*x, secSize); |
| 27 | + if (!SD_MMC.writeRAW(blkbuffer, lba + x)) return false; |
| 28 | + } |
| 29 | + return bufsize; |
| 30 | +} |
| 31 | + |
| 32 | +static int32_t onRead(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize){ |
| 33 | + uint32_t secSize = SD_MMC.sectorSize(); |
| 34 | + if (!secSize) return false; // disk error |
| 35 | + log_v("Read lba: %ld\toffset: %ld\tbufsize: %ld\tsector: %lu", lba, offset, bufsize, secSize); |
| 36 | + for (int x=0; x < bufsize/secSize; x++) { |
| 37 | + if (!SD_MMC.readRAW((uint8_t*)buffer + (x * secSize), lba + x)) return false; // outside of volume boundary |
| 38 | + } |
| 39 | + return bufsize; |
| 40 | +} |
| 41 | + |
| 42 | +static bool onStartStop(uint8_t power_condition, bool start, bool load_eject){ |
| 43 | + log_i("Start/Stop power: %u\tstart: %d\teject: %d", power_condition, start, load_eject); |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +static void usbEventCallback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { |
| 48 | + if (event_base == ARDUINO_USB_EVENTS) { |
| 49 | + arduino_usb_event_data_t *data = (arduino_usb_event_data_t *)event_data; |
| 50 | + switch (event_id) { |
| 51 | + case ARDUINO_USB_STARTED_EVENT: Serial.println("USB PLUGGED"); break; |
| 52 | + case ARDUINO_USB_STOPPED_EVENT: Serial.println("USB UNPLUGGED"); break; |
| 53 | + case ARDUINO_USB_SUSPEND_EVENT: Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en); break; |
| 54 | + case ARDUINO_USB_RESUME_EVENT: Serial.println("USB RESUMED"); break; |
| 55 | + |
| 56 | + default: break; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void setup(){ |
| 62 | + Serial.begin(115200); |
| 63 | + Serial.println("Starting Serial"); |
| 64 | + |
| 65 | + Serial.println("Mounting SDcard"); |
| 66 | + SD_MMC.setPins(clk, cmd, d0, d1, d2, d3); |
| 67 | + if(!SD_MMC.begin("/sdcard", onebit)){ |
| 68 | + Serial.println("Mount Failed"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + Serial.println("Initializing MSC"); |
| 73 | + // Initialize USB metadata and callbacks for MSC (Mass Storage Class) |
| 74 | + msc.vendorID("ESP32"); |
| 75 | + msc.productID("USB_MSC"); |
| 76 | + msc.productRevision("1.0"); |
| 77 | + msc.onRead(onRead); |
| 78 | + msc.onWrite(onWrite); |
| 79 | + msc.onStartStop(onStartStop); |
| 80 | + msc.mediaPresent(true); |
| 81 | + msc.begin(SD_MMC.numSectors(), SD_MMC.sectorSize()); |
| 82 | + |
| 83 | + Serial.println("Initializing USB"); |
| 84 | + |
| 85 | + USB.begin(); |
| 86 | + USB.onEvent(usbEventCallback); |
| 87 | + |
| 88 | + Serial.printf("Card Size: %lluMB\n", SD_MMC.totalBytes()/1024/1024); |
| 89 | + Serial.printf("Sector: %d\tCount: %d\n", SD_MMC.sectorSize(), SD_MMC.numSectors()); |
| 90 | +} |
| 91 | + |
| 92 | +void loop(){ |
| 93 | + delay(-1); |
| 94 | +} |
0 commit comments