Skip to content

Commit 2affe1b

Browse files
committed
print flash contents (root only) for external flash example
1 parent 27e1e02 commit 2affe1b

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

examples/MassStorage/msc_external_flash/msc_external_flash.ino

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@
2424

2525
Adafruit_SPIFlash flash(&flashTransport);
2626

27+
// file system object from SdFat
28+
FatFileSystem fatfs;
29+
30+
FatFile root;
31+
FatFile file;
32+
33+
// USB Mass Storage object
2734
Adafruit_USBD_MSC usb_msc;
2835

36+
// Set to true when PC write to flash
37+
bool changed;
38+
2939
// the setup function runs once when you press reset or power the board
3040
void setup()
3141
{
@@ -53,11 +63,49 @@ void setup()
5363
Serial.println("Adafruit TinyUSB Mass Storage SPI Flash example");
5464
Serial.print("JEDEC ID: "); Serial.println(flash.getJEDECID(), HEX);
5565
Serial.print("Flash size: "); Serial.println(flash.size());
66+
67+
// Init file system on the flash
68+
fatfs.begin(&flash);
69+
70+
changed = true; // to print contents initially
5671
}
5772

5873
void loop()
5974
{
60-
// nothing to do
75+
if ( changed )
76+
{
77+
if ( !root.open("/") )
78+
{
79+
Serial.println("open root failed");
80+
return;
81+
}
82+
83+
Serial.println("Flash contents:");
84+
85+
// Open next file in root.
86+
// Warning, openNext starts at the current directory position
87+
// so a rewind of the directory may be required.
88+
while ( file.openNext(&root, O_RDONLY) )
89+
{
90+
file.printFileSize(&Serial);
91+
Serial.write(' ');
92+
file.printName(&Serial);
93+
if ( file.isDir() )
94+
{
95+
// Indicate a directory.
96+
Serial.write('/');
97+
}
98+
Serial.println();
99+
file.close();
100+
}
101+
102+
root.close();
103+
104+
Serial.println();
105+
106+
changed = false;
107+
delay(1000); // refresh every 1 second
108+
}
61109
}
62110

63111
// Callback invoked when received READ10 command.
@@ -85,4 +133,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
85133
void msc_flush_cb (void)
86134
{
87135
flash.syncBlocks();
136+
137+
changed = true;
88138
}

0 commit comments

Comments
 (0)