7
7
8
8
#include " SPI.h"
9
9
#include " SdFat.h"
10
-
11
10
#include " Adafruit_TinyUSB.h"
12
11
13
12
const int chipSelect = 10 ;
@@ -16,6 +15,12 @@ Adafruit_USBD_MSC usb_msc;
16
15
17
16
SdFat sd;
18
17
18
+ SdFile root;
19
+ SdFile file;
20
+
21
+ // Set to true when PC write to flash
22
+ bool changed;
23
+
19
24
// the setup function runs once when you press reset or power the board
20
25
void setup ()
21
26
{
@@ -38,7 +43,7 @@ void setup()
38
43
Serial.print (" \n Initializing SD card ... " );
39
44
Serial.print (" CS = " ); Serial.println (chipSelect);
40
45
41
- if ( !sd.cardBegin (chipSelect, SD_SCK_MHZ (50 )) )
46
+ if ( !sd.begin (chipSelect, SD_SCK_MHZ (50 )) )
42
47
{
43
48
Serial.println (" initialization failed. Things to check:" );
44
49
Serial.println (" * is a card inserted?" );
@@ -58,11 +63,41 @@ void setup()
58
63
59
64
// MSC is ready for read/write
60
65
usb_msc.setUnitReady (true );
66
+
67
+ changed = true ; // to print contents initially
61
68
}
62
69
63
70
void loop ()
64
71
{
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
+ }
66
101
}
67
102
68
103
// Callback invoked when received READ10 command.
@@ -87,4 +122,9 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
87
122
void msc_flush_cb (void )
88
123
{
89
124
sd.card ()->syncBlocks ();
125
+
126
+ // clear file system's cache to force refresh
127
+ sd.cacheClear ();
128
+
129
+ changed = true ;
90
130
}
0 commit comments