|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 3 | + |
| 4 | +/* This example expose SD card as mass storage using |
| 5 | + * default SdFat Library |
| 6 | + */ |
| 7 | + |
| 8 | +#include "SPI.h" |
| 9 | +#include "SdFat.h" |
| 10 | + |
| 11 | +#include "Adafruit_TinyUSB.h" |
| 12 | + |
| 13 | +const int chipSelect = 10; |
| 14 | +const int spi_freq_mhz = 50; |
| 15 | + |
| 16 | +Adafruit_USBD_MSC usb_msc; |
| 17 | + |
| 18 | +SdFat sd; |
| 19 | + |
| 20 | +// the setup function runs once when you press reset or power the board |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 24 | + usb_msc.setID("Adafruit", "SD Card", "1.0"); |
| 25 | + |
| 26 | + // Set read write callback |
| 27 | + usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
| 28 | + |
| 29 | + // Still initialize MSC but tell usb stack that MSC is not ready to read/write |
| 30 | + // If we don't initialize, board will be enumerated as CDC only |
| 31 | + usb_msc.setUnitReady(false); |
| 32 | + usb_msc.begin(); |
| 33 | + |
| 34 | + Serial.begin(115200); |
| 35 | + while ( !Serial ) delay(10); // wait for native usb |
| 36 | + |
| 37 | + Serial.println("Adafruit TinyUSB Mass Storage SD Card example"); |
| 38 | + |
| 39 | + Serial.print("\nInitializing SD card ... "); |
| 40 | + Serial.print(" CS = "); Serial.println(chipSelect); |
| 41 | + |
| 42 | + if ( !sd.cardBegin(chipSelect, SD_SCK_MHZ(spi_freq_mhz)) ) |
| 43 | + { |
| 44 | + Serial.println("initialization failed. Things to check:"); |
| 45 | + Serial.println("* is a card inserted?"); |
| 46 | + Serial.println("* is your wiring correct?"); |
| 47 | + Serial.println("* did you change the chipSelect pin to match your shield or module?"); |
| 48 | + while (1) delay(1); |
| 49 | + } |
| 50 | + |
| 51 | + // Size in blocks (512 bytes) |
| 52 | + uint32_t block_count = sd.card()->cardSize(); |
| 53 | + |
| 54 | + Serial.print("Volume size (MB): "); |
| 55 | + Serial.println((block_count/2) / 1024); |
| 56 | + |
| 57 | + // Set disk size, SD block size is always 512 |
| 58 | + usb_msc.setCapacity(block_count, 512); |
| 59 | + |
| 60 | + // MSC is ready for read/write |
| 61 | + usb_msc.setUnitReady(true); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() |
| 65 | +{ |
| 66 | + // nothing to do |
| 67 | +} |
| 68 | + |
| 69 | +int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) |
| 70 | +{ |
| 71 | + (void) bufsize; |
| 72 | + return sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1; |
| 73 | +} |
| 74 | + |
| 75 | +// Callback invoked when received WRITE10 command. |
| 76 | +// Process data in buffer to disk's storage and |
| 77 | +// return number of written bytes (must be multiple of block size) |
| 78 | +int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) |
| 79 | +{ |
| 80 | + return sd.card()->writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1; |
| 81 | +} |
| 82 | + |
| 83 | +// Callback invoked when WRITE10 command is completed (status received and accepted by host). |
| 84 | +// used to flush any pending cache. |
| 85 | +void msc_flush_cb (void) |
| 86 | +{ |
| 87 | + sd.card()->syncBlocks(); |
| 88 | +} |
0 commit comments