-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(sdmmc): Add RAW disk functions #9796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+146
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef5a4cd
feat(sdmmc): Add RAW disk functions
lbernstone d22d5d1
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] 83ff659
Fixed sdmmc host check to pass CI
lbernstone ad27a17
Merge branch 'master' into sdmmc_raw
lucasssvaz c92bd43
feat(sdmmc): fixed example USB check
lbernstone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#if !SOC_USB_OTG_SUPPORTED || ARDUINO_USB_MODE | ||
#error Device does not support USB_OTG or native USB CDC/JTAG is selected | ||
#endif | ||
|
||
#include <USB.h> | ||
#include <USBMSC.h> | ||
#include <SD_MMC.h> | ||
|
||
// USB Mass Storage Class (MSC) object | ||
USBMSC msc; | ||
|
||
int clk = 36; | ||
int cmd = 35; | ||
int d0 = 37; | ||
int d1 = 38; | ||
int d2 = 33; | ||
int d3 = 34; | ||
bool onebit = true; // set to false for 4-bit. 1-bit will ignore the d1-d3 pins (but d3 must be pulled high) | ||
|
||
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { | ||
uint32_t secSize = SD_MMC.sectorSize(); | ||
if (!secSize) { | ||
return false; // disk error | ||
} | ||
log_v("Write lba: %ld\toffset: %ld\tbufsize: %ld", lba, offset, bufsize); | ||
for (int x = 0; x < bufsize / secSize; x++) { | ||
uint8_t blkbuffer[secSize]; | ||
memcpy(blkbuffer, (uint8_t *)buffer + secSize * x, secSize); | ||
if (!SD_MMC.writeRAW(blkbuffer, lba + x)) { | ||
return false; | ||
} | ||
} | ||
return bufsize; | ||
} | ||
|
||
static int32_t onRead(uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { | ||
uint32_t secSize = SD_MMC.sectorSize(); | ||
if (!secSize) { | ||
return false; // disk error | ||
} | ||
log_v("Read lba: %ld\toffset: %ld\tbufsize: %ld\tsector: %lu", lba, offset, bufsize, secSize); | ||
for (int x = 0; x < bufsize / secSize; x++) { | ||
if (!SD_MMC.readRAW((uint8_t *)buffer + (x * secSize), lba + x)) { | ||
return false; // outside of volume boundary | ||
} | ||
} | ||
return bufsize; | ||
} | ||
|
||
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject) { | ||
log_i("Start/Stop power: %u\tstart: %d\teject: %d", power_condition, start, load_eject); | ||
return true; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println("Starting Serial"); | ||
|
||
Serial.println("Mounting SDcard"); | ||
SD_MMC.setPins(clk, cmd, d0, d1, d2, d3); | ||
if (!SD_MMC.begin("/sdcard", onebit)) { | ||
Serial.println("Mount Failed"); | ||
return; | ||
} | ||
|
||
Serial.println("Initializing MSC"); | ||
// Initialize USB metadata and callbacks for MSC (Mass Storage Class) | ||
msc.vendorID("ESP32"); | ||
msc.productID("USB_MSC"); | ||
msc.productRevision("1.0"); | ||
msc.onRead(onRead); | ||
msc.onWrite(onWrite); | ||
msc.onStartStop(onStartStop); | ||
msc.mediaPresent(true); | ||
msc.begin(SD_MMC.numSectors(), SD_MMC.sectorSize()); | ||
|
||
Serial.println("Initializing USB"); | ||
|
||
USB.begin(); | ||
USB.onEvent(usbEventCallback); | ||
|
||
Serial.printf("Card Size: %lluMB\n", SD_MMC.totalBytes() / 1024 / 1024); | ||
Serial.printf("Sector: %d\tCount: %d\n", SD_MMC.sectorSize(), SD_MMC.numSectors()); | ||
} | ||
|
||
void loop() { | ||
delay(-1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"targets": { | ||
"esp32": false, | ||
"esp32s2": false, | ||
"esp32c3": false, | ||
"esp32c6": false, | ||
"esp32h2": false | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.