Skip to content

Commit d23e304

Browse files
committed
fix msc external flash example issue if flash is not formatted
examples/MassStorage/msc_external_flash_sdcard/msc_external_flash_sdcard.ino
1 parent 78f1dd2 commit d23e304

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

examples/MassStorage/msc_external_flash/msc_external_flash.ino

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ FatFile file;
7777
// USB Mass Storage object
7878
Adafruit_USBD_MSC usb_msc;
7979

80+
// Check if flash is formatted
81+
bool fs_formatted;
82+
8083
// Set to true when PC write to flash
8184
bool fs_changed;
8285

@@ -98,15 +101,20 @@ void setup()
98101

99102
// MSC is ready for read/write
100103
usb_msc.setUnitReady(true);
101-
104+
102105
usb_msc.begin();
103106

104107
// Init file system on the flash
105-
fatfs.begin(&flash);
108+
fs_formatted = fatfs.begin(&flash);
106109

107110
Serial.begin(115200);
108111
//while ( !Serial ) delay(10); // wait for native usb
109112

113+
if ( !fs_formatted )
114+
{
115+
Serial.println("Failed to init files system, flash may not be formatted");
116+
}
117+
110118
Serial.println("Adafruit TinyUSB Mass Storage External Flash example");
111119
Serial.print("JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);
112120
Serial.print("Flash size: "); Serial.print(flash.size() / 1024); Serial.println(" KB");
@@ -119,7 +127,18 @@ void loop()
119127
if ( fs_changed )
120128
{
121129
fs_changed = false;
122-
130+
131+
// check if host formatted disk
132+
if (!fs_formatted)
133+
{
134+
fs_formatted = fatfs.begin(&flash);
135+
}
136+
137+
// skip if still not formatted
138+
if (!fs_formatted) return;
139+
140+
Serial.println("Opening root");
141+
123142
if ( !root.open("/") )
124143
{
125144
Serial.println("open root failed");

examples/MassStorage/msc_external_flash_sdcard/msc_external_flash_sdcard.ino

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ Adafruit_USBD_MSC usb_msc;
9494
// Set to true when PC write to flash
9595
bool sd_changed = false;
9696
bool sd_inited = false;
97+
98+
bool flash_formatted = false;
9799
bool flash_changed = false;
98100

99101
// the setup function runs once when you press reset or power the board
100102
void setup()
101103
{
102104
pinMode(LED_BUILTIN, OUTPUT);
103-
104105
Serial.begin(115200);
105106

106107
// MSC with 2 Logical Units: LUN0: External Flash, LUN1: SDCard
@@ -117,7 +118,7 @@ void setup()
117118

118119
//------------- Lun 0 for external flash -------------//
119120
flash.begin();
120-
fatfs.begin(&flash);
121+
flash_formatted = fatfs.begin(&flash);
121122

122123
usb_msc.setCapacity(0, flash.size()/512, 512);
123124
usb_msc.setReadWriteCallback(0, external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
@@ -193,14 +194,23 @@ void loop()
193194
{
194195
if ( flash_changed )
195196
{
196-
File root;
197-
root = fatfs.open("/");
197+
if (!flash_formatted)
198+
{
199+
flash_formatted = fatfs.begin(&flash);
200+
}
198201

199-
Serial.println("Flash contents:");
200-
print_rootdir(&root);
201-
Serial.println();
202+
// skip if still not formatted
203+
if (flash_formatted)
204+
{
205+
File root;
206+
root = fatfs.open("/");
202207

203-
root.close();
208+
Serial.println("Flash contents:");
209+
print_rootdir(&root);
210+
Serial.println();
211+
212+
root.close();
213+
}
204214

205215
flash_changed = false;
206216
}

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
5050
void begin(uint32_t baud, uint8_t config);
5151
void end(void);
5252

53+
void setDebugOutput(bool en) {}
54+
5355
// return line coding set by host
5456
uint32_t baud(void);
5557
uint8_t stopbits(void);

0 commit comments

Comments
 (0)