|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "Adafruit_TinyUSB.h" |
| 26 | +#include "Adafruit_SPIFlash.h" |
| 27 | + |
| 28 | +// Configuration of the flash chip pins and flash fatfs object. |
| 29 | +// You don't normally need to change these if using a Feather/Metro |
| 30 | +// M0 express board. |
| 31 | +#define FLASH_TYPE SPIFLASHTYPE_W25Q16BV // Flash chip type. |
| 32 | + // If you change this be |
| 33 | + // sure to change the fatfs |
| 34 | + // object type below to match. |
| 35 | + |
| 36 | + |
| 37 | +#if defined(__SAMD51__) |
| 38 | + // Alternatively you can define and use non-SPI pins, QSPI isnt on a sercom |
| 39 | + Adafruit_SPIFlash flash(PIN_QSPI_SCK, PIN_QSPI_IO1, PIN_QSPI_IO0, PIN_QSPI_CS); |
| 40 | +#else |
| 41 | + #if (SPI_INTERFACES_COUNT == 1) |
| 42 | + #define FLASH_SS SS // Flash chip SS pin. |
| 43 | + #define FLASH_SPI_PORT SPI // What SPI port is Flash on? |
| 44 | + #else |
| 45 | + #define FLASH_SS SS1 // Flash chip SS pin. |
| 46 | + #define FLASH_SPI_PORT SPI1 // What SPI port is Flash on? |
| 47 | + #endif |
| 48 | + |
| 49 | +Adafruit_SPIFlash flash(FLASH_SS, &FLASH_SPI_PORT); // Use hardware SPI |
| 50 | +#endif |
| 51 | + |
| 52 | +Adafruit_USBD_MSC usb_msc; |
| 53 | + |
| 54 | +// the setup function runs once when you press reset or power the board |
| 55 | +void setup() |
| 56 | +{ |
| 57 | + flash.begin(FLASH_TYPE); |
| 58 | + |
| 59 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 60 | + usb_msc.setID("Adafruit", "SPI Flash", "1.0"); |
| 61 | + |
| 62 | + // Set callback |
| 63 | + usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
| 64 | + |
| 65 | + // Set disk size, block size should be 512 regardless of spi flash page size |
| 66 | + usb_msc.setCapacity(flash.pageSize()*flash.numPages()/512, 512); |
| 67 | + |
| 68 | + // MSC is ready for read/write |
| 69 | + usb_msc.setUnitReady(true); |
| 70 | + |
| 71 | + usb_msc.begin(); |
| 72 | + |
| 73 | + Serial.begin(115200); |
| 74 | + while ( !Serial ) delay(10); // wait for native usb |
| 75 | + |
| 76 | + Serial.println("Mass Storage SPI Flash example"); |
| 77 | + Serial.print("Page size: "); Serial.println(flash.pageSize()); |
| 78 | + Serial.print("Page num : "); Serial.println(flash.numPages()); |
| 79 | +} |
| 80 | + |
| 81 | +void loop() |
| 82 | +{ |
| 83 | + // nothing to do |
| 84 | +} |
| 85 | + |
| 86 | +// Callback invoked when received READ10 command. |
| 87 | +// Copy disk's data to buffer (up to bufsize) and |
| 88 | +// return number of copied bytes (must be multiple of block size) |
| 89 | +int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) |
| 90 | +{ |
| 91 | + const uint32_t addr = lba*512; |
| 92 | + return flash.readBuffer(addr, (uint8_t*) buffer, bufsize); |
| 93 | +} |
| 94 | + |
| 95 | +// Callback invoked when received WRITE10 command. |
| 96 | +// Process data in buffer to disk's storage and |
| 97 | +// return number of written bytes (must be multiple of block size) |
| 98 | +int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) |
| 99 | +{ |
| 100 | + // need to erase & caching write back |
| 101 | + const uint32_t addr = lba*512; |
| 102 | + return flash.writeBuffer(addr, buffer, bufsize); |
| 103 | +} |
| 104 | + |
| 105 | +// Callback invoked when WRITE10 command is completed (status received and accepted by host). |
| 106 | +// used to flush any pending cache. |
| 107 | +void msc_flush_cb (void) |
| 108 | +{ |
| 109 | + // nothing to do |
| 110 | +} |
0 commit comments