Skip to content

Commit eaa9bfd

Browse files
committed
update msc_sdfat.ino and msc_external_flash_sdcard.ino to
- use SDIO (pio) for rp2040/rp2350 - remove printing flash/sdcard contents to Serial since it conflict with host, which can cause conflict in spi/sdio transport and/or correupt cache,sector
1 parent 9806375 commit eaa9bfd

File tree

2 files changed

+67
-177
lines changed

2 files changed

+67
-177
lines changed

examples/MassStorage/msc_external_flash_sdcard/msc_external_flash_sdcard.ino

Lines changed: 35 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,54 @@
3333

3434
// for flashTransport definition
3535
#include "flash_config.h"
36-
3736
Adafruit_SPIFlash flash(&flashTransport);
3837

39-
// External Flash File system
40-
FatVolume fatfs;
41-
4238
//--------------------------------------------------------------------+
4339
// SDCard Config
4440
//--------------------------------------------------------------------+
45-
4641
#if defined(ARDUINO_PYPORTAL_M4) || defined(ARDUINO_PYPORTAL_M4_TITANO)
4742
// PyPortal has on-board card reader
4843
#define SDCARD_CS 32
4944
#define SDCARD_DETECT 33
5045
#define SDCARD_DETECT_ACTIVE HIGH
46+
5147
#elif defined(ARDUINO_ADAFRUIT_METRO_RP2040)
52-
#define SDCARD_CS 23
48+
#define SDIO_CLK_PIN 18
49+
#define SDIO_CMD_PIN 19 // MOSI
50+
#define SDIO_DAT0_PIN 20 // DAT1: 21, DAT2: 22, DAT3: 23
51+
5352
#define SDCARD_DETECT 15
5453
#define SDCARD_DETECT_ACTIVE LOW
54+
55+
#elif defined(ARDUINO_ADAFRUIT_METRO_RP2350)
56+
// Note: not working yet (need troubleshoot later)
57+
#define SDIO_CLK_PIN 34
58+
#define SDIO_CMD_PIN 35 // MOSI
59+
#define SDIO_DAT0_PIN 36 // DAT1: 37, DAT2: 38, DAT3: 39
60+
61+
#define SDCARD_DETECT 40
62+
#define SDCARD_DETECT_ACTIVE LOW
63+
5564
#else
65+
// Use SPI, no detect
5666
#define SDCARD_CS 10
57-
// no detect
5867
#endif
5968

60-
// SDCard File system
69+
#if defined(SDIO_CLK_PIN) && defined(SDIO_CMD_PIN) && defined(SDIO_DAT0_PIN)
70+
#define SD_CONFIG SdioConfig(SDIO_CLK_PIN, SDIO_CMD_PIN, SDIO_DAT0_PIN)
71+
#else
72+
#define SD_CONFIG SdSpiConfig(SDCARD_CS, SHARED_SPI, SD_SCK_MHZ(50))
73+
#endif
74+
75+
// File system on SD Card
6176
SdFat sd;
6277

6378
// USB Mass Storage object
6479
Adafruit_USBD_MSC usb_msc;
6580

6681
// Set to true when PC write to flash
67-
bool sd_changed = false;
6882
bool sd_inited = false;
6983

70-
bool flash_formatted = false;
71-
bool flash_changed = false;
72-
7384
// the setup function runs once when you press reset or power the board
7485
void setup() {
7586
#ifdef LED_BUILTIN
@@ -98,14 +109,11 @@ void setup() {
98109

99110
//------------- Lun 0 for external flash -------------//
100111
flash.begin();
101-
flash_formatted = fatfs.begin(&flash);
102112

103113
usb_msc.setCapacity(0, flash.size()/512, 512);
104114
usb_msc.setReadWriteCallback(0, external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
105115
usb_msc.setUnitReady(0, true);
106116

107-
flash_changed = true; // to print contents initially
108-
109117
//------------- Lun 1 for SD card -------------//
110118
#ifdef SDCARD_DETECT
111119
// DETECT pin is available, detect card present on the fly with test unit ready
@@ -126,26 +134,18 @@ void setup() {
126134
bool init_sdcard(void) {
127135
Serial.print("Init SDCard ... ");
128136

129-
if (!sd.begin(SDCARD_CS, SD_SCK_MHZ(50))) {
130-
Serial.print("Failed ");
131-
sd.errorPrint("sd.begin() failed");
132-
137+
if (!sd.begin(SD_CONFIG)) {
138+
Serial.println("initialization failed. Things to check:");
139+
Serial.println("- is a card inserted?");
140+
Serial.println("- is your wiring correct?");
141+
Serial.println("- did you change the SDCARD_CS or SDIO pin to match your shield or module?");
133142
return false;
134143
}
135144

136-
uint32_t block_count;
137-
138-
#if SD_FAT_VERSION >= 20000
139-
block_count = sd.card()->sectorCount();
140-
#else
141-
block_count = sd.card()->cardSize();
142-
#endif
143-
145+
uint32_t block_count = sd.card()->sectorCount();
144146
usb_msc.setCapacity(1, block_count, 512);
145147
usb_msc.setReadWriteCallback(1, sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
146148

147-
sd_changed = true; // to print contents initially
148-
149149
Serial.print("OK, Card size = ");
150150
Serial.print((block_count / (1024 * 1024)) * 512);
151151
Serial.println(" MB");
@@ -173,93 +173,35 @@ void print_rootdir(File32* rdir) {
173173
}
174174

175175
void loop() {
176-
if (flash_changed) {
177-
if (!flash_formatted) {
178-
flash_formatted = fatfs.begin(&flash);
179-
}
180-
181-
// skip if still not formatted
182-
if (flash_formatted) {
183-
File32 root;
184-
root = fatfs.open("/");
185-
186-
Serial.println("Flash contents:");
187-
print_rootdir(&root);
188-
Serial.println();
189-
190-
root.close();
191-
}
192-
193-
flash_changed = false;
194-
}
195-
196-
if (sd_changed) {
197-
File32 root;
198-
root = sd.open("/");
199-
200-
Serial.println("SD contents:");
201-
print_rootdir(&root);
202-
Serial.println();
203-
204-
root.close();
205-
206-
sd_changed = false;
207-
}
208-
209-
delay(1000); // refresh every 1 second
176+
// nothing to do
210177
}
211178

212179

213180
//--------------------------------------------------------------------+
214181
// SD Card callbacks
215182
//--------------------------------------------------------------------+
216183

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
226-
184+
int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
185+
bool rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
227186
return rc ? bufsize : -1;
228187
}
229188

230189
// Callback invoked when received WRITE10 command.
231190
// Process data in buffer to disk's storage and
232191
// return number of written bytes (must be multiple of block size)
233192
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
234-
bool rc;
235-
236193
#ifdef LED_BUILTIN
237194
digitalWrite(LED_BUILTIN, HIGH);
238195
#endif
239-
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
245-
196+
bool rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
246197
return rc ? bufsize : -1;
247198
}
248199

249200
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
250201
// used to flush any pending cache.
251-
void sdcard_flush_cb (void)
252-
{
253-
#if SD_FAT_VERSION >= 20000
202+
void sdcard_flush_cb (void) {
254203
sd.card()->syncDevice();
255-
#else
256-
sd.card()->syncBlocks();
257-
#endif
258-
259-
// clear file system's cache to force refresh
260-
sd.cacheClear();
261-
262-
sd_changed = true;
204+
sd.cacheClear(); // clear file system's cache to force refresh
263205

264206
#ifdef LED_BUILTIN
265207
digitalWrite(LED_BUILTIN, LOW);
@@ -281,8 +223,6 @@ bool sdcard_ready_callback(void) {
281223
usb_msc.setReadWriteCallback(1, NULL, NULL, NULL);
282224
}
283225

284-
Serial.println(sd_inited);
285-
286226
return sd_inited;
287227
}
288228
#endif
@@ -317,12 +257,6 @@ int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize
317257
// used to flush any pending cache.
318258
void external_flash_flush_cb (void) {
319259
flash.syncBlocks();
320-
321-
// clear file system's cache to force refresh
322-
fatfs.cacheClear();
323-
324-
flash_changed = true;
325-
326260
#ifdef LED_BUILTIN
327261
digitalWrite(LED_BUILTIN, LOW);
328262
#endif

0 commit comments

Comments
 (0)