Skip to content

Commit 4fafd00

Browse files
committed
update msc sdfat example
1 parent 9c0ea7d commit 4fafd00

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

examples/MassStorage/msc_sdfat/msc_sdfat.ino

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "SPI.h"
99
#include "SdFat.h"
10-
1110
#include "Adafruit_TinyUSB.h"
1211

1312
const int chipSelect = 10;
@@ -16,6 +15,12 @@ Adafruit_USBD_MSC usb_msc;
1615

1716
SdFat sd;
1817

18+
SdFile root;
19+
SdFile file;
20+
21+
// Set to true when PC write to flash
22+
bool changed;
23+
1924
// the setup function runs once when you press reset or power the board
2025
void setup()
2126
{
@@ -38,7 +43,7 @@ void setup()
3843
Serial.print("\nInitializing SD card ... ");
3944
Serial.print("CS = "); Serial.println(chipSelect);
4045

41-
if ( !sd.cardBegin(chipSelect, SD_SCK_MHZ(50)) )
46+
if ( !sd.begin(chipSelect, SD_SCK_MHZ(50)) )
4247
{
4348
Serial.println("initialization failed. Things to check:");
4449
Serial.println("* is a card inserted?");
@@ -58,11 +63,41 @@ void setup()
5863

5964
// MSC is ready for read/write
6065
usb_msc.setUnitReady(true);
66+
67+
changed = true; // to print contents initially
6168
}
6269

6370
void loop()
6471
{
65-
// nothing to do
72+
if ( changed )
73+
{
74+
root.open("/");
75+
Serial.println("Flash contents:");
76+
77+
// Open next file in root.
78+
// Warning, openNext starts at the current directory position
79+
// so a rewind of the directory may be required.
80+
while ( file.openNext(&root, O_RDONLY) )
81+
{
82+
file.printFileSize(&Serial);
83+
Serial.write(' ');
84+
file.printName(&Serial);
85+
if ( file.isDir() )
86+
{
87+
// Indicate a directory.
88+
Serial.write('/');
89+
}
90+
Serial.println();
91+
file.close();
92+
}
93+
94+
root.close();
95+
96+
Serial.println();
97+
98+
changed = false;
99+
delay(1000); // refresh every 0.5 second
100+
}
66101
}
67102

68103
// Callback invoked when received READ10 command.
@@ -87,4 +122,9 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
87122
void msc_flush_cb (void)
88123
{
89124
sd.card()->syncBlocks();
125+
126+
// clear file system's cache to force refresh
127+
sd.cacheClear();
128+
129+
changed = true;
90130
}

0 commit comments

Comments
 (0)