Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e382746

Browse files
lbernstonepre-commit-ci-lite[bot]lucasssvaz
authored
feat(sdmmc): Add RAW disk functions (espressif#9796)
* 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: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
1 parent 575a415 commit e382746

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed
Lines changed: 102 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
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+
}

‎libraries/SD_MMC/src/SD_MMC.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "driver/sdmmc_host.h"
2727
#include "driver/sdmmc_defs.h"
2828
#include "sdmmc_cmd.h"
29+
#include "diskio_sdmmc.h"
30+
#include "diskio.h"
2931
#include "soc/sdmmc_pins.h"
3032
#include "ff.h"
3133
#include "esp32-hal-periman.h"
@@ -191,6 +193,7 @@ bool SDMMCFS::begin(const char *mountpoint, bool mode1bit, bool format_if_mount_
191193
return false;
192194
}
193195
_impl->mountpoint(mountpoint);
196+
_pdrv = ff_diskio_get_pdrv_card(_card);
194197

195198
if (!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC_CMD, (void *)(this), -1, -1)) {
196199
goto err;
@@ -280,5 +283,27 @@ uint64_t SDMMCFS::usedBytes() {
280283
return size;
281284
}
282285

286+
int SDMMCFS::sectorSize() {
287+
if (!_card) {
288+
return 0;
289+
}
290+
return _card->csd.sector_size;
291+
}
292+
293+
int SDMMCFS::numSectors() {
294+
if (!_card) {
295+
return 0;
296+
}
297+
return (totalBytes() / _card->csd.sector_size);
298+
}
299+
300+
bool SDMMCFS::readRAW(uint8_t *buffer, uint32_t sector) {
301+
return (disk_read(_pdrv, buffer, sector, 1) == 0);
302+
}
303+
304+
bool SDMMCFS::writeRAW(uint8_t *buffer, uint32_t sector) {
305+
return (disk_write(_pdrv, buffer, sector, 1) == 0);
306+
}
307+
283308
SDMMCFS SD_MMC = SDMMCFS(FSImplPtr(new VFSImpl()));
284309
#endif /* SOC_SDMMC_HOST_SUPPORTED */

‎libraries/SD_MMC/src/SD_MMC.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
#include "sdkconfig.h"
1818
#include "soc/soc_caps.h"
19-
#ifdef SOC_SDMMC_HOST_SUPPORTED
19+
#ifndef SOC_SDMMC_HOST_SUPPORTED
20+
#ifdef ARDUINO
21+
#warning The SDMMC library requires a device with an SDIO Host
22+
#endif
23+
#else
2024

2125
#include "FS.h"
2226
#include "driver/sdmmc_types.h"
@@ -40,6 +44,7 @@ class SDMMCFS : public FS {
4044
int8_t _pin_d1 = -1;
4145
int8_t _pin_d2 = -1;
4246
int8_t _pin_d3 = -1;
47+
uint8_t _pdrv = 0xFF;
4348
bool _mode1bit = false;
4449

4550
public:
@@ -55,6 +60,10 @@ class SDMMCFS : public FS {
5560
uint64_t cardSize();
5661
uint64_t totalBytes();
5762
uint64_t usedBytes();
63+
int sectorSize();
64+
int numSectors();
65+
bool readRAW(uint8_t *buffer, uint32_t sector);
66+
bool writeRAW(uint8_t *buffer, uint32_t sector);
5867

5968
private:
6069
static bool sdmmcDetachBus(void *bus_pointer);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /