24
24
25
25
Adafruit_SPIFlash flash (&flashTransport);
26
26
27
+ // File system object on external flash from SdFat
28
+ FatFileSystem fatfs;
29
+
27
30
const int chipSelect = 10 ;
31
+
32
+ // File system on SD Card
28
33
SdFat sd;
29
34
35
+ // USB Mass Storage object
30
36
Adafruit_USBD_MSC usb_msc;
31
37
38
+ // Set to true when PC write to flash
39
+ bool sd_changed = false ;
40
+ bool flash_changed = false ;
41
+
32
42
// the setup function runs once when you press reset or power the board
33
43
void setup ()
34
44
{
45
+ pinMode (LED_BUILTIN, OUTPUT);
46
+
35
47
// MSC with 2 Logical Units
36
48
usb_msc.setMaxLun (2 );
37
49
50
+ usb_msc.setID (0 , " Adafruit" , " External Flash" , " 1.0" );
51
+ usb_msc.setID (1 , " Adafruit" , " SD Card" , " 1.0" );
52
+
53
+ // Since initialize both external flash and SD card can take time.
54
+ // If it takes too long, our board could be enumerated as CDC device only
55
+ // i.e without Mass Storage. To prevent this, we call Mass Storage begin first
56
+ // LUN readiness will always be set later on
57
+ usb_msc.begin ();
58
+
38
59
// ------------- Lun 0 for external flash -------------//
39
60
flash.begin ();
40
- usb_msc.setID (0 , " Adafruit" , " External Flash" , " 1.0" );
61
+ fatfs.begin (&flash);
62
+
41
63
usb_msc.setCapacity (0 , flash.pageSize ()*flash.numPages ()/512 , 512 );
42
64
usb_msc.setReadWriteCallback (0 , external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
43
65
usb_msc.setUnitReady (0 , true );
44
66
45
- // ------------- Lun 1 for SD card -------------//
46
- usb_msc.setID (1 , " Adafruit" , " SD Card" , " 1.0" );
47
- usb_msc.setReadWriteCallback (1 , sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
67
+ flash_changed = true ; // to print contents initially
48
68
49
- if ( sd.cardBegin (chipSelect, SD_SCK_MHZ (50 )) )
69
+ // ------------- Lun 1 for SD card -------------//
70
+ if ( sd.begin (chipSelect, SD_SCK_MHZ (50 )) )
50
71
{
51
72
uint32_t block_count = sd.card ()->cardSize ();
52
73
usb_msc.setCapacity (1 , block_count, 512 );
74
+ usb_msc.setReadWriteCallback (1 , sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
53
75
usb_msc.setUnitReady (1 , true );
54
- }
55
76
56
- usb_msc.begin ();
77
+ sd_changed = true ; // to print contents initially
78
+ }
57
79
58
80
Serial.begin (115200 );
59
81
while ( !Serial ) delay (10 ); // wait for native usb
60
82
61
83
Serial.println (" Adafruit TinyUSB Mass Storage External Flash + SD Card example" );
84
+ delay (1000 );
85
+ }
86
+
87
+ void print_rootdir (File* rdir)
88
+ {
89
+ File file;
90
+
91
+ // Open next file in root.
92
+ // Warning, openNext starts at the current directory position
93
+ // so a rewind of the directory may be required.
94
+ while ( file.openNext (rdir, O_RDONLY) )
95
+ {
96
+ file.printFileSize (&Serial);
97
+ Serial.write (' ' );
98
+ file.printName (&Serial);
99
+ if ( file.isDir () )
100
+ {
101
+ // Indicate a directory.
102
+ Serial.write (' /' );
103
+ }
104
+ Serial.println ();
105
+ file.close ();
106
+ }
62
107
}
63
108
64
109
void loop ()
65
110
{
66
- // nothing to do
111
+ if ( flash_changed )
112
+ {
113
+ File root;
114
+ root = fatfs.open (" /" );
115
+
116
+ Serial.println (" Flash contents:" );
117
+ print_rootdir (&root);
118
+ Serial.println ();
119
+
120
+ root.close ();
121
+
122
+ flash_changed = false ;
123
+ }
124
+
125
+ if ( sd_changed )
126
+ {
127
+ File root;
128
+ root = sd.open (" /" );
129
+
130
+ Serial.println (" SD contents:" );
131
+ print_rootdir (&root);
132
+ Serial.println ();
133
+
134
+ root.close ();
135
+
136
+ sd_changed = false ;
137
+ }
138
+
139
+ delay (1000 ); // refresh every 1 second
67
140
}
68
141
69
142
@@ -73,7 +146,6 @@ void loop()
73
146
74
147
int32_t sdcard_read_cb (uint32_t lba, void * buffer, uint32_t bufsize)
75
148
{
76
- (void ) bufsize;
77
149
return sd.card ()->readBlocks (lba, (uint8_t *) buffer, bufsize/512 ) ? bufsize : -1 ;
78
150
}
79
151
@@ -82,6 +154,8 @@ int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
82
154
// return number of written bytes (must be multiple of block size)
83
155
int32_t sdcard_write_cb (uint32_t lba, uint8_t * buffer, uint32_t bufsize)
84
156
{
157
+ digitalWrite (LED_BUILTIN, HIGH);
158
+
85
159
return sd.card ()->writeBlocks (lba, buffer, bufsize/512 ) ? bufsize : -1 ;
86
160
}
87
161
@@ -90,6 +164,13 @@ int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
90
164
void sdcard_flush_cb (void )
91
165
{
92
166
sd.card ()->syncBlocks ();
167
+
168
+ // clear file system's cache to force refresh
169
+ sd.cacheClear ();
170
+
171
+ sd_changed = true ;
172
+
173
+ digitalWrite (LED_BUILTIN, LOW);
93
174
}
94
175
95
176
// --------------------------------------------------------------------+
@@ -111,6 +192,8 @@ int32_t external_flash_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
111
192
// return number of written bytes (must be multiple of block size)
112
193
int32_t external_flash_write_cb (uint32_t lba, uint8_t * buffer, uint32_t bufsize)
113
194
{
195
+ digitalWrite (LED_BUILTIN, HIGH);
196
+
114
197
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
115
198
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
116
199
return flash.writeBlocks (lba, buffer, bufsize/512 ) ? bufsize : -1 ;
@@ -121,4 +204,11 @@ int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize
121
204
void external_flash_flush_cb (void )
122
205
{
123
206
flash.syncBlocks ();
207
+
208
+ // clear file system's cache to force refresh
209
+ fatfs.cacheClear ();
210
+
211
+ flash_changed = true ;
212
+
213
+ digitalWrite (LED_BUILTIN, LOW);
124
214
}
0 commit comments