Skip to content

Commit 281218e

Browse files
committed
update dual lun external flash + sd example
1 parent 4fafd00 commit 281218e

File tree

3 files changed

+116
-15
lines changed

3 files changed

+116
-15
lines changed

examples/MassStorage/msc_external_flash/msc_external_flash.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ bool changed;
3939
// the setup function runs once when you press reset or power the board
4040
void setup()
4141
{
42-
flash.begin();
43-
4442
pinMode(LED_BUILTIN, OUTPUT);
4543

44+
flash.begin();
45+
4646
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
4747
usb_msc.setID("Adafruit", "External Flash", "1.0");
4848

@@ -123,6 +123,8 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
123123
// return number of written bytes (must be multiple of block size)
124124
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
125125
{
126+
digitalWrite(LED_BUILTIN, HIGH);
127+
126128
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
127129
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
128130
return flash.writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
@@ -139,4 +141,6 @@ void msc_flush_cb (void)
139141
fatfs.cacheClear();
140142

141143
changed = true;
144+
145+
digitalWrite(LED_BUILTIN, LOW);
142146
}

examples/MassStorage/msc_external_flash_sdfat/msc_external_flash_sdfat.ino

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,119 @@
2424

2525
Adafruit_SPIFlash flash(&flashTransport);
2626

27+
// File system object on external flash from SdFat
28+
FatFileSystem fatfs;
29+
2730
const int chipSelect = 10;
31+
32+
// File system on SD Card
2833
SdFat sd;
2934

35+
// USB Mass Storage object
3036
Adafruit_USBD_MSC usb_msc;
3137

38+
// Set to true when PC write to flash
39+
bool sd_changed = false;
40+
bool flash_changed = false;
41+
3242
// the setup function runs once when you press reset or power the board
3343
void setup()
3444
{
45+
pinMode(LED_BUILTIN, OUTPUT);
46+
3547
// MSC with 2 Logical Units
3648
usb_msc.setMaxLun(2);
3749

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+
3859
//------------- Lun 0 for external flash -------------//
3960
flash.begin();
40-
usb_msc.setID(0, "Adafruit", "External Flash", "1.0");
61+
fatfs.begin(&flash);
62+
4163
usb_msc.setCapacity(0, flash.pageSize()*flash.numPages()/512, 512);
4264
usb_msc.setReadWriteCallback(0, external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
4365
usb_msc.setUnitReady(0, true);
4466

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
4868

49-
if ( sd.cardBegin(chipSelect, SD_SCK_MHZ(50)) )
69+
//------------- Lun 1 for SD card -------------//
70+
if ( sd.begin(chipSelect, SD_SCK_MHZ(50)) )
5071
{
5172
uint32_t block_count = sd.card()->cardSize();
5273
usb_msc.setCapacity(1, block_count, 512);
74+
usb_msc.setReadWriteCallback(1, sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);
5375
usb_msc.setUnitReady(1, true);
54-
}
5576

56-
usb_msc.begin();
77+
sd_changed = true; // to print contents initially
78+
}
5779

5880
Serial.begin(115200);
5981
while ( !Serial ) delay(10); // wait for native usb
6082

6183
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+
}
62107
}
63108

64109
void loop()
65110
{
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
67140
}
68141

69142

@@ -73,7 +146,6 @@ void loop()
73146

74147
int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
75148
{
76-
(void) bufsize;
77149
return sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
78150
}
79151

@@ -82,6 +154,8 @@ int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
82154
// return number of written bytes (must be multiple of block size)
83155
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
84156
{
157+
digitalWrite(LED_BUILTIN, HIGH);
158+
85159
return sd.card()->writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
86160
}
87161

@@ -90,6 +164,13 @@ int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
90164
void sdcard_flush_cb (void)
91165
{
92166
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);
93174
}
94175

95176
//--------------------------------------------------------------------+
@@ -111,6 +192,8 @@ int32_t external_flash_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
111192
// return number of written bytes (must be multiple of block size)
112193
int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
113194
{
195+
digitalWrite(LED_BUILTIN, HIGH);
196+
114197
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
115198
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
116199
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
121204
void external_flash_flush_cb (void)
122205
{
123206
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);
124214
}

examples/MassStorage/msc_sdfat/msc_sdfat.ino

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@
1111

1212
const int chipSelect = 10;
1313

14-
Adafruit_USBD_MSC usb_msc;
15-
14+
// File system on SD Card
1615
SdFat sd;
1716

1817
SdFile root;
1918
SdFile file;
2019

20+
// USB Mass Storage object
21+
Adafruit_USBD_MSC usb_msc;
22+
2123
// Set to true when PC write to flash
2224
bool changed;
2325

2426
// the setup function runs once when you press reset or power the board
2527
void setup()
2628
{
29+
pinMode(LED_BUILTIN, OUTPUT);
30+
2731
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
2832
usb_msc.setID("Adafruit", "SD Card", "1.0");
2933

@@ -72,7 +76,7 @@ void loop()
7276
if ( changed )
7377
{
7478
root.open("/");
75-
Serial.println("Flash contents:");
79+
Serial.println("SD contents:");
7680

7781
// Open next file in root.
7882
// Warning, openNext starts at the current directory position
@@ -105,7 +109,6 @@ void loop()
105109
// return number of copied bytes (must be multiple of block size)
106110
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
107111
{
108-
(void) bufsize;
109112
return sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
110113
}
111114

@@ -114,6 +117,8 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
114117
// return number of written bytes (must be multiple of block size)
115118
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
116119
{
120+
digitalWrite(LED_BUILTIN, HIGH);
121+
117122
return sd.card()->writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
118123
}
119124

@@ -127,4 +132,6 @@ void msc_flush_cb (void)
127132
sd.cacheClear();
128133

129134
changed = true;
135+
136+
digitalWrite(LED_BUILTIN, LOW);
130137
}

0 commit comments

Comments
 (0)