Skip to content

Commit 780f072

Browse files
Jason2866lbernstonepre-commit-ci-lite[bot]lucasssvaz
authored
feat(sdmmc): Add RAW disk functions (espressif#9796) (#431)
* feat(sdmmc): Add RAW disk functions feat(sdmmc): fixed printf mismatches and missing callback feat(sdmmc): added ci.json Removed excess log_i * ci(pre-commit): Apply automatic fixes * Fixed sdmmc host check to pass CI * feat(sdmmc): fixed example USB check --------- Co-authored-by: lbernstone <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Lucas Saavedra Vaz <[email protected]>
1 parent aceae95 commit 780f072

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Diff for: libraries/SD_MMC/examples/SD2USBMSC/SD2USBMSC.ino

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#if !SOC_USB_OTG_SUPPORTED || ARDUINO_USB_MODE
2+
#error Device does not support USB_OTG or native USB CDC/JTAG is selected
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) {
23+
return false; // disk error
24+
}
25+
log_v("Write lba: %ld\toffset: %ld\tbufsize: %ld", lba, offset, bufsize);
26+
for (int x = 0; x < bufsize / secSize; x++) {
27+
uint8_t blkbuffer[secSize];
28+
memcpy(blkbuffer, (uint8_t *)buffer + secSize * x, secSize);
29+
if (!SD_MMC.writeRAW(blkbuffer, lba + x)) {
30+
return false;
31+
}
32+
}
33+
return bufsize;
34+
}
35+
36+
static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
37+
uint32_t secSize = SD_MMC.sectorSize();
38+
if (!secSize) {
39+
return false; // disk error
40+
}
41+
log_v("Read lba: %ld\toffset: %ld\tbufsize: %ld\tsector: %lu", lba, offset, bufsize, secSize);
42+
for (int x = 0; x < bufsize / secSize; x++) {
43+
if (!SD_MMC.readRAW((uint8_t *)buffer + (x * secSize), lba + x)) {
44+
return false; // outside of volume boundary
45+
}
46+
}
47+
return bufsize;
48+
}
49+
50+
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject) {
51+
log_i("Start/Stop power: %u\tstart: %d\teject: %d", power_condition, start, load_eject);
52+
return true;
53+
}
54+
55+
static void usbEventCallback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
56+
if (event_base == ARDUINO_USB_EVENTS) {
57+
arduino_usb_event_data_t *data = (arduino_usb_event_data_t *)event_data;
58+
switch (event_id) {
59+
case ARDUINO_USB_STARTED_EVENT: Serial.println("USB PLUGGED"); break;
60+
case ARDUINO_USB_STOPPED_EVENT: Serial.println("USB UNPLUGGED"); break;
61+
case ARDUINO_USB_SUSPEND_EVENT: Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en); break;
62+
case ARDUINO_USB_RESUME_EVENT: Serial.println("USB RESUMED"); break;
63+
64+
default: break;
65+
}
66+
}
67+
}
68+
69+
void setup() {
70+
Serial.begin(115200);
71+
Serial.println("Starting Serial");
72+
73+
Serial.println("Mounting SDcard");
74+
SD_MMC.setPins(clk, cmd, d0, d1, d2, d3);
75+
if (!SD_MMC.begin("/sdcard", onebit)) {
76+
Serial.println("Mount Failed");
77+
return;
78+
}
79+
80+
Serial.println("Initializing MSC");
81+
// Initialize USB metadata and callbacks for MSC (Mass Storage Class)
82+
msc.vendorID("ESP32");
83+
msc.productID("USB_MSC");
84+
msc.productRevision("1.0");
85+
msc.onRead(onRead);
86+
msc.onWrite(onWrite);
87+
msc.onStartStop(onStartStop);
88+
msc.mediaPresent(true);
89+
msc.begin(SD_MMC.numSectors(), SD_MMC.sectorSize());
90+
91+
Serial.println("Initializing USB");
92+
93+
USB.begin();
94+
USB.onEvent(usbEventCallback);
95+
96+
Serial.printf("Card Size: %lluMB\n", SD_MMC.totalBytes() / 1024 / 1024);
97+
Serial.printf("Sector: %d\tCount: %d\n", SD_MMC.sectorSize(), SD_MMC.numSectors());
98+
}
99+
100+
void loop() {
101+
delay(-1);
102+
}

Diff for: libraries/SD_MMC/examples/SD2USBMSC/ci.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"targets": {
3+
"esp32": false,
4+
"esp32s2": false,
5+
"esp32c3": false,
6+
"esp32c6": false,
7+
"esp32h2": false
8+
}
9+
}

0 commit comments

Comments
 (0)