|
24 | 24 |
|
25 | 25 | Adafruit_SPIFlash flash(&flashTransport);
|
26 | 26 |
|
| 27 | +// file system object from SdFat |
| 28 | +FatFileSystem fatfs; |
| 29 | + |
| 30 | +FatFile root; |
| 31 | +FatFile file; |
| 32 | + |
| 33 | +// USB Mass Storage object |
27 | 34 | Adafruit_USBD_MSC usb_msc;
|
28 | 35 |
|
| 36 | +// Set to true when PC write to flash |
| 37 | +bool changed; |
| 38 | + |
29 | 39 | // the setup function runs once when you press reset or power the board
|
30 | 40 | void setup()
|
31 | 41 | {
|
@@ -53,11 +63,49 @@ void setup()
|
53 | 63 | Serial.println("Adafruit TinyUSB Mass Storage SPI Flash example");
|
54 | 64 | Serial.print("JEDEC ID: "); Serial.println(flash.getJEDECID(), HEX);
|
55 | 65 | 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 |
56 | 71 | }
|
57 | 72 |
|
58 | 73 | void loop()
|
59 | 74 | {
|
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 | + } |
61 | 109 | }
|
62 | 110 |
|
63 | 111 | // Callback invoked when received READ10 command.
|
@@ -85,4 +133,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
|
85 | 133 | void msc_flush_cb (void)
|
86 | 134 | {
|
87 | 135 | flash.syncBlocks();
|
| 136 | + |
| 137 | + changed = true; |
88 | 138 | }
|
0 commit comments