33
33
34
34
// for flashTransport definition
35
35
#include " flash_config.h"
36
-
37
36
Adafruit_SPIFlash flash (&flashTransport);
38
37
39
- // External Flash File system
40
- FatVolume fatfs;
41
-
42
38
// --------------------------------------------------------------------+
43
39
// SDCard Config
44
40
// --------------------------------------------------------------------+
45
-
46
41
#if defined(ARDUINO_PYPORTAL_M4) || defined(ARDUINO_PYPORTAL_M4_TITANO)
47
42
// PyPortal has on-board card reader
48
43
#define SDCARD_CS 32
49
44
#define SDCARD_DETECT 33
50
45
#define SDCARD_DETECT_ACTIVE HIGH
46
+
51
47
#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
+
53
52
#define SDCARD_DETECT 15
54
53
#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
+
55
64
#else
65
+ // Use SPI, no detect
56
66
#define SDCARD_CS 10
57
- // no detect
58
67
#endif
59
68
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
61
76
SdFat sd;
62
77
63
78
// USB Mass Storage object
64
79
Adafruit_USBD_MSC usb_msc;
65
80
66
81
// Set to true when PC write to flash
67
- bool sd_changed = false ;
68
82
bool sd_inited = false ;
69
83
70
- bool flash_formatted = false ;
71
- bool flash_changed = false ;
72
-
73
84
// the setup function runs once when you press reset or power the board
74
85
void setup () {
75
86
#ifdef LED_BUILTIN
@@ -98,14 +109,11 @@ void setup() {
98
109
99
110
// ------------- Lun 0 for external flash -------------//
100
111
flash.begin ();
101
- flash_formatted = fatfs.begin (&flash);
102
112
103
113
usb_msc.setCapacity (0 , flash.size ()/512 , 512 );
104
114
usb_msc.setReadWriteCallback (0 , external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
105
115
usb_msc.setUnitReady (0 , true );
106
116
107
- flash_changed = true ; // to print contents initially
108
-
109
117
// ------------- Lun 1 for SD card -------------//
110
118
#ifdef SDCARD_DETECT
111
119
// DETECT pin is available, detect card present on the fly with test unit ready
@@ -126,26 +134,18 @@ void setup() {
126
134
bool init_sdcard (void ) {
127
135
Serial.print (" Init SDCard ... " );
128
136
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?" );
133
142
return false ;
134
143
}
135
144
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 ();
144
146
usb_msc.setCapacity (1 , block_count, 512 );
145
147
usb_msc.setReadWriteCallback (1 , sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
146
148
147
- sd_changed = true ; // to print contents initially
148
-
149
149
Serial.print (" OK, Card size = " );
150
150
Serial.print ((block_count / (1024 * 1024 )) * 512 );
151
151
Serial.println (" MB" );
@@ -173,93 +173,35 @@ void print_rootdir(File32* rdir) {
173
173
}
174
174
175
175
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
210
177
}
211
178
212
179
213
180
// --------------------------------------------------------------------+
214
181
// SD Card callbacks
215
182
// --------------------------------------------------------------------+
216
183
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 );
227
186
return rc ? bufsize : -1 ;
228
187
}
229
188
230
189
// Callback invoked when received WRITE10 command.
231
190
// Process data in buffer to disk's storage and
232
191
// return number of written bytes (must be multiple of block size)
233
192
int32_t sdcard_write_cb (uint32_t lba, uint8_t * buffer, uint32_t bufsize) {
234
- bool rc;
235
-
236
193
#ifdef LED_BUILTIN
237
194
digitalWrite (LED_BUILTIN, HIGH);
238
195
#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 );
246
197
return rc ? bufsize : -1 ;
247
198
}
248
199
249
200
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
250
201
// 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 ) {
254
203
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
263
205
264
206
#ifdef LED_BUILTIN
265
207
digitalWrite (LED_BUILTIN, LOW);
@@ -281,8 +223,6 @@ bool sdcard_ready_callback(void) {
281
223
usb_msc.setReadWriteCallback (1 , NULL , NULL , NULL );
282
224
}
283
225
284
- Serial.println (sd_inited);
285
-
286
226
return sd_inited;
287
227
}
288
228
#endif
@@ -317,12 +257,6 @@ int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize
317
257
// used to flush any pending cache.
318
258
void external_flash_flush_cb (void ) {
319
259
flash.syncBlocks ();
320
-
321
- // clear file system's cache to force refresh
322
- fatfs.cacheClear ();
323
-
324
- flash_changed = true ;
325
-
326
260
#ifdef LED_BUILTIN
327
261
digitalWrite (LED_BUILTIN, LOW);
328
262
#endif
0 commit comments