|
| 1 | +/********************************************************************* |
| 2 | + Adafruit invests time and resources providing this open source code, |
| 3 | + please support Adafruit and open-source hardware by purchasing |
| 4 | + products from Adafruit! |
| 5 | +
|
| 6 | + MIT license, check LICENSE for more information |
| 7 | + Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 8 | + All text above, and the splash screen below must be included in |
| 9 | + any redistribution |
| 10 | +*********************************************************************/ |
| 11 | + |
| 12 | +/* This example demo how to expose on-board external Flash as USB Mass Storage. |
| 13 | + * Following library is required |
| 14 | + * - Adafruit_SPIFlash https://github.com/adafruit/Adafruit_SPIFlash |
| 15 | + * - SdFat https://github.com/adafruit/SdFat |
| 16 | + * |
| 17 | + * Note: Adafruit fork of SdFat enabled ENABLE_EXTENDED_TRANSFER_CLASS and FAT12_SUPPORT |
| 18 | + * in SdFatConfig.h, which is needed to run SdFat on external flash. You can use original |
| 19 | + * SdFat library and manually change those macros |
| 20 | + */ |
| 21 | + |
| 22 | +#include "SPI.h" |
| 23 | +#include "SdFat.h" |
| 24 | +#include "Adafruit_SPIFlash.h" |
| 25 | +#include "Adafruit_TinyUSB.h" |
| 26 | + |
| 27 | +// ESP32 use same flash device that store code. |
| 28 | +// Therefore there is no need to specify the SPI and SS |
| 29 | +Adafruit_FlashTransport_ESP32 flashTransport; |
| 30 | +Adafruit_SPIFlash flash(&flashTransport); |
| 31 | + |
| 32 | +// file system object from SdFat |
| 33 | +FatFileSystem fatfs; |
| 34 | + |
| 35 | +FatFile root; |
| 36 | +FatFile file; |
| 37 | + |
| 38 | +// USB Mass Storage object |
| 39 | +Adafruit_USBD_MSC usb_msc; |
| 40 | + |
| 41 | +// Check if flash is formatted |
| 42 | +bool formatted; |
| 43 | + |
| 44 | +// Set to true when PC write to flash |
| 45 | +bool changed; |
| 46 | + |
| 47 | +// the setup function runs once when you press reset or power the board |
| 48 | +void setup() |
| 49 | +{ |
| 50 | + pinMode(LED_BUILTIN, OUTPUT); |
| 51 | + Serial.begin(115200); |
| 52 | + |
| 53 | + flash.begin(); |
| 54 | + |
| 55 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 56 | + usb_msc.setID("Adafruit", "External Flash", "1.0"); |
| 57 | + |
| 58 | + // Set callback |
| 59 | + usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
| 60 | + |
| 61 | + // Set disk size, block size should be 512 regardless of spi flash page size |
| 62 | + usb_msc.setCapacity(flash.size()/512, 512); |
| 63 | + |
| 64 | + // MSC is ready for read/write |
| 65 | + usb_msc.setUnitReady(true); |
| 66 | + |
| 67 | + usb_msc.begin(); |
| 68 | + |
| 69 | + // Init file system on the flash |
| 70 | + formatted = fatfs.begin(&flash); |
| 71 | + |
| 72 | +// while ( !Serial ) delay(10); // wait for native usb |
| 73 | + if ( !formatted ) |
| 74 | + { |
| 75 | + Serial.println("Failed to init files system, flash may not be formatted"); |
| 76 | + } |
| 77 | + |
| 78 | + Serial.println("Adafruit TinyUSB Mass Storage External Flash example"); |
| 79 | + Serial.print("JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX); |
| 80 | + Serial.print("Flash size: "); Serial.print(flash.size() / 1024); Serial.println(" KB"); |
| 81 | + |
| 82 | + changed = true; // to print contents initially |
| 83 | +} |
| 84 | + |
| 85 | +void loop() |
| 86 | +{ |
| 87 | + if ( changed ) |
| 88 | + { |
| 89 | + changed = false; |
| 90 | + |
| 91 | + // check if host formatted disk |
| 92 | + if (!formatted) |
| 93 | + { |
| 94 | + formatted = fatfs.begin(&flash); |
| 95 | + } |
| 96 | + |
| 97 | + // skip if still not formatted |
| 98 | + if (!formatted) return; |
| 99 | + |
| 100 | + Serial.println("Opening root"); |
| 101 | + |
| 102 | + if ( !root.open("/") ) |
| 103 | + { |
| 104 | + Serial.println("open root failed"); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + Serial.println("Flash contents:"); |
| 109 | + |
| 110 | + // Open next file in root. |
| 111 | + // Warning, openNext starts at the current directory position |
| 112 | + // so a rewind of the directory may be required. |
| 113 | + while ( file.openNext(&root, O_RDONLY) ) |
| 114 | + { |
| 115 | + file.printFileSize(&Serial); |
| 116 | + Serial.write(' '); |
| 117 | + file.printName(&Serial); |
| 118 | + if ( file.isDir() ) |
| 119 | + { |
| 120 | + // Indicate a directory. |
| 121 | + Serial.write('/'); |
| 122 | + } |
| 123 | + Serial.println(); |
| 124 | + file.close(); |
| 125 | + } |
| 126 | + |
| 127 | + root.close(); |
| 128 | + |
| 129 | + Serial.println(); |
| 130 | + delay(1000); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Callback invoked when received READ10 command. |
| 135 | +// Copy disk's data to buffer (up to bufsize) and |
| 136 | +// return number of copied bytes (must be multiple of block size) |
| 137 | +int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) |
| 138 | +{ |
| 139 | + // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks |
| 140 | + // already include 4K sector caching internally. We don't need to cache it, yahhhh!! |
| 141 | + return flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1; |
| 142 | +} |
| 143 | + |
| 144 | +// Callback invoked when received WRITE10 command. |
| 145 | +// Process data in buffer to disk's storage and |
| 146 | +// return number of written bytes (must be multiple of block size) |
| 147 | +int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) |
| 148 | +{ |
| 149 | + digitalWrite(LED_BUILTIN, HIGH); |
| 150 | + |
| 151 | + // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks |
| 152 | + // already include 4K sector caching internally. We don't need to cache it, yahhhh!! |
| 153 | + return flash.writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1; |
| 154 | +} |
| 155 | + |
| 156 | +// Callback invoked when WRITE10 command is completed (status received and accepted by host). |
| 157 | +// used to flush any pending cache. |
| 158 | +void msc_flush_cb (void) |
| 159 | +{ |
| 160 | + // sync with flash |
| 161 | + flash.syncBlocks(); |
| 162 | + |
| 163 | + // clear file system's cache to force refresh |
| 164 | + fatfs.cacheClear(); |
| 165 | + |
| 166 | + changed = true; |
| 167 | + |
| 168 | + digitalWrite(LED_BUILTIN, LOW); |
| 169 | +} |
0 commit comments