Skip to content

Commit ef5a4cd

Browse files
committed
feat(sdmmc): Add RAW disk functions
feat(sdmmc): fixed printf mismatches and missing callback feat(sdmmc): added ci.json Removed excess log_i
1 parent 297757a commit ef5a4cd

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

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

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

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+
}

Diff for: libraries/SD_MMC/src/SD_MMC.cpp

+22
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,8 @@ 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);
197+
194198

195199
if (!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC_CMD, (void *)(this), -1, -1)) {
196200
goto err;
@@ -280,5 +284,23 @@ uint64_t SDMMCFS::usedBytes() {
280284
return size;
281285
}
282286

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

Diff for: libraries/SD_MMC/src/SD_MMC.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
#include "sdkconfig.h"
1818
#include "soc/soc_caps.h"
19-
#ifdef SOC_SDMMC_HOST_SUPPORTED
19+
#ifndef SOC_SDMMC_HOST_SUPPORTED
20+
#error The SDMMC library requires a device with an SDIO Host
21+
#else
2022

2123
#include "FS.h"
2224
#include "driver/sdmmc_types.h"
@@ -40,6 +42,7 @@ class SDMMCFS : public FS {
4042
int8_t _pin_d1 = -1;
4143
int8_t _pin_d2 = -1;
4244
int8_t _pin_d3 = -1;
45+
uint8_t _pdrv = 0xFF;
4346
bool _mode1bit = false;
4447

4548
public:
@@ -55,6 +58,10 @@ class SDMMCFS : public FS {
5558
uint64_t cardSize();
5659
uint64_t totalBytes();
5760
uint64_t usedBytes();
61+
int sectorSize();
62+
int numSectors();
63+
bool readRAW(uint8_t *buffer, uint32_t sector);
64+
bool writeRAW(uint8_t *buffer, uint32_t sector);
5865

5966
private:
6067
static bool sdmmcDetachBus(void *bus_pointer);

0 commit comments

Comments
 (0)