|
| 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 "SD.h" |
| 27 | + |
| 28 | +#ifdef ARDUINO_NRF52832_FEATHER |
| 29 | +const int chipSelect = 11; |
| 30 | +#else |
| 31 | +const int chipSelect = 10; |
| 32 | +#endif |
| 33 | + |
| 34 | +Adafruit_USBD_MSC usb_msc; |
| 35 | + |
| 36 | +Sd2Card card; |
| 37 | +SdVolume volume; |
| 38 | + |
| 39 | +// the setup function runs once when you press reset or power the board |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 43 | + usb_msc.setID(0, "Adafruit", "SD Card", "1.0"); |
| 44 | + |
| 45 | + // Set read write callback |
| 46 | + usb_msc.setReadWriteCallback(0, sd_read_cb, sd_write_cb, sd_flush_cb); |
| 47 | + |
| 48 | + // Still initialize MSC but tell usb stack that MSC is not ready to read/write |
| 49 | + // If we don't initialize, board will be enumerated as CDC only |
| 50 | + usb_msc.setUnitReady(0, false); |
| 51 | + usb_msc.begin(); |
| 52 | + |
| 53 | + Serial.begin(115200); |
| 54 | + while ( !Serial ) delay(10); // wait for native usb |
| 55 | + |
| 56 | + Serial.println("Adafruit TinyUSB SD Card Reader example"); |
| 57 | + |
| 58 | + Serial.println("\nInitializing SD card..."); |
| 59 | + |
| 60 | + if ( !card.init(SPI_HALF_SPEED, chipSelect) ) |
| 61 | + { |
| 62 | + Serial.println("initialization failed. Things to check:"); |
| 63 | + Serial.println("* is a card inserted?"); |
| 64 | + Serial.println("* is your wiring correct?"); |
| 65 | + Serial.println("* did you change the chipSelect pin to match your shield or module?"); |
| 66 | + while (1) delay(1); |
| 67 | + } |
| 68 | + |
| 69 | + // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 |
| 70 | + if (!volume.init(card)) { |
| 71 | + Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); |
| 72 | + while (1) delay(1); |
| 73 | + } |
| 74 | + |
| 75 | + uint32_t block_count = volume.blocksPerCluster()*volume.clusterCount(); |
| 76 | + |
| 77 | + Serial.print("Volume size (MB): "); |
| 78 | + Serial.println((block_count/2) / 1024); |
| 79 | + |
| 80 | + // Set disk size, SD block size is always 512 |
| 81 | + usb_msc.setCapacity(0, block_count, 512); |
| 82 | + |
| 83 | + // MSC is ready for read/write |
| 84 | + usb_msc.setUnitReady(0, true); |
| 85 | +} |
| 86 | + |
| 87 | +void loop() |
| 88 | +{ |
| 89 | + // nothing to do |
| 90 | +} |
| 91 | + |
| 92 | +int32_t sd_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) |
| 93 | +{ |
| 94 | + (void) bufsize; |
| 95 | + return card.readBlock(lba, (uint8_t*) buffer) ? 512 : -1; |
| 96 | +} |
| 97 | + |
| 98 | +// Callback invoked when received WRITE10 command. |
| 99 | +// Process data in buffer to disk's storage and |
| 100 | +// return number of written bytes (must be multiple of block size) |
| 101 | +int32_t sd_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) |
| 102 | +{ |
| 103 | + return card.writeBlock(lba, buffer) ? 512 : -1; |
| 104 | +} |
| 105 | + |
| 106 | +// Callback invoked when WRITE10 command is completed (status received and accepted by host). |
| 107 | +// used to flush any pending cache. |
| 108 | +void sd_flush_cb (void) |
| 109 | +{ |
| 110 | + // nothing to do |
| 111 | +} |
0 commit comments