Skip to content

Commit 9ca33ea

Browse files
committed
macos support for msc
Changing from multi-block writes to single block worked for macos and ubuntu. I was able to mount read and write both flash and microSD using a Metro RP2040.
1 parent 805aa2d commit 9ca33ea

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

examples/MassStorage/.DS_Store

6 KB
Binary file not shown.

examples/MassStorage/msc_external_flash_sdcard/msc_external_flash_sdcard.ino

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,17 @@ void loop() {
214214
// SD Card callbacks
215215
//--------------------------------------------------------------------+
216216

217-
int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
218-
{
219-
bool rc;
220-
221-
#if SD_FAT_VERSION >= 20000
222-
rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
223-
#else
224-
rc = sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512);
225-
#endif
217+
int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
218+
bool rc = true;
219+
uint8_t* buf = (uint8_t*)buffer;
220+
221+
// Perform each sector with a single-block CMD17
222+
for (uint32_t i = 0; i < bufsize/512; i++) {
223+
if (!sd.card()->readBlock(lba + i, buf + i*512)) {
224+
rc = false;
225+
break;
226+
}
227+
}
226228

227229
return rc ? bufsize : -1;
228230
}
@@ -231,17 +233,19 @@ int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
231233
// Process data in buffer to disk's storage and
232234
// return number of written bytes (must be multiple of block size)
233235
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
234-
bool rc;
236+
bool rc = true;
235237

236238
#ifdef LED_BUILTIN
237239
digitalWrite(LED_BUILTIN, HIGH);
238240
#endif
239241

240-
#if SD_FAT_VERSION >= 20000
241-
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
242-
#else
243-
rc = sd.card()->writeBlocks(lba, buffer, bufsize/512);
244-
#endif
242+
// Perform each 512-byte sector as a single-block CMD24
243+
for (uint32_t i = 0; i < bufsize/512; i++) {
244+
if (!sd.card()->writeBlock(lba + i, buffer + i*512)) {
245+
rc = false;
246+
break;
247+
}
248+
}
245249

246250
return rc ? bufsize : -1;
247251
}

0 commit comments

Comments
 (0)