Skip to content

Commit e467531

Browse files
committed
update msc example to work with both sdfat v1 and v2
1 parent cdf49a2 commit e467531

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

examples/MassStorage/msc_esp32_file_browser/msc_esp32_file_browser.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Adafruit_FlashTransport_ESP32 flashTransport;
6060
Adafruit_SPIFlash flash(&flashTransport);
6161

6262
// file system object from SdFat
63-
FatFileSystem fatfs;
63+
FatVolume fatfs;
6464

6565
// USB Mass Storage object
6666
Adafruit_USBD_MSC usb_msc;

examples/MassStorage/msc_external_flash/msc_external_flash.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
Adafruit_SPIFlash flash(&flashTransport);
7070

7171
// file system object from SdFat
72-
FatFileSystem fatfs;
72+
FatVolume fatfs;
7373

7474
FatFile root;
7575
FatFile file;

examples/MassStorage/msc_external_flash_sdcard/msc_external_flash_sdcard.ino

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
Adafruit_SPIFlash flash(&flashTransport);
7171

7272
// External Flash File system
73-
FatFileSystem fatfs;
73+
FatVolume fatfs;
7474

7575
//--------------------------------------------------------------------+
7676
// SDCard Config
@@ -150,12 +150,20 @@ bool init_sdcard(void)
150150
if ( !sd.begin(SDCARD_CS, SD_SCK_MHZ(50)) )
151151
{
152152
Serial.print("Failed ");
153-
sd.errorPrint();
153+
sd.errorPrint("sd.begin() failed");
154154

155155
return false;
156156
}
157157

158-
uint32_t block_count = sd.card()->cardSize();
158+
uint32_t block_count;
159+
160+
#if SD_FAT_VERSION >= 20000
161+
block_count = sd.card()->sectorCount();
162+
#else
163+
block_count = sd.card()->cardSize();
164+
#endif
165+
166+
159167
usb_msc.setCapacity(1, block_count, 512);
160168
usb_msc.setReadWriteCallback(1, sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
161169

@@ -239,24 +247,44 @@ void loop()
239247

240248
int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
241249
{
242-
return sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
250+
bool rc;
251+
252+
#if SD_FAT_VERSION >= 20000
253+
rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
254+
#else
255+
rc = sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512);
256+
#endif
257+
258+
return rc ? bufsize : -1;
243259
}
244260

245261
// Callback invoked when received WRITE10 command.
246262
// Process data in buffer to disk's storage and
247263
// return number of written bytes (must be multiple of block size)
248264
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
249265
{
266+
bool rc;
267+
250268
digitalWrite(LED_BUILTIN, HIGH);
251269

252-
return sd.card()->writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
270+
#if SD_FAT_VERSION >= 20000
271+
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
272+
#else
273+
rc = sd.card()->writeBlocks(lba, buffer, bufsize/512);
274+
#endif
275+
276+
return rc ? bufsize : -1;
253277
}
254278

255279
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
256280
// used to flush any pending cache.
257281
void sdcard_flush_cb (void)
258282
{
283+
#if SD_FAT_VERSION >= 20000
284+
sd.card()->syncDevice();
285+
#else
259286
sd.card()->syncBlocks();
287+
#endif
260288

261289
// clear file system's cache to force refresh
262290
sd.cacheClear();

examples/MassStorage/msc_internal_flash_samd/msc_internal_flash_samd.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Adafruit_InternalFlash flash(INTERNAL_FLASH_FILESYSTEM_START_ADDR, INTERNAL_FLASH_FILESYSTEM_SIZE);
2424

2525
// file system object from SdFat
26-
FatFileSystem fatfs;
26+
FatVolume fatfs;
2727

2828
FatFile root;
2929
FatFile file;

examples/MassStorage/msc_sdfat/msc_sdfat.ino

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ void setup()
6565
}
6666

6767
// Size in blocks (512 bytes)
68+
#if SD_FAT_VERSION >= 20000
69+
uint32_t block_count = sd.card()->sectorCount();
70+
#else
6871
uint32_t block_count = sd.card()->cardSize();
72+
#endif
6973

7074
Serial.print("Volume size (MB): ");
7175
Serial.println((block_count/2) / 1024);
@@ -117,24 +121,44 @@ void loop()
117121
// return number of copied bytes (must be multiple of block size)
118122
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
119123
{
120-
return sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
124+
bool rc;
125+
126+
#if SD_FAT_VERSION >= 20000
127+
rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
128+
#else
129+
rc = sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512);
130+
#endif
131+
132+
return rc ? bufsize : -1;
121133
}
122134

123135
// Callback invoked when received WRITE10 command.
124136
// Process data in buffer to disk's storage and
125137
// return number of written bytes (must be multiple of block size)
126138
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
127139
{
140+
bool rc;
141+
128142
digitalWrite(LED_BUILTIN, HIGH);
129143

130-
return sd.card()->writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
144+
#if SD_FAT_VERSION >= 20000
145+
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
146+
#else
147+
rc = sd.card()->writeBlocks(lba, buffer, bufsize/512);
148+
#endif
149+
150+
return rc ? bufsize : -1;
131151
}
132152

133153
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
134154
// used to flush any pending cache.
135155
void msc_flush_cb (void)
136156
{
157+
#if SD_FAT_VERSION >= 20000
158+
sd.card()->syncDevice();
159+
#else
137160
sd.card()->syncBlocks();
161+
#endif
138162

139163
// clear file system's cache to force refresh
140164
sd.cacheClear();

0 commit comments

Comments
 (0)