Skip to content

Commit 341744a

Browse files
committed
add msc_sdfat example
1 parent 949a81b commit 341744a

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ before_install:
2626
- source install.sh
2727

2828
install:
29-
- arduino --install-library "Adafruit SPIFlash","Adafruit QSPI"
29+
- arduino --install-library "Adafruit SPIFlash","Adafruit QSPI","SdFat"
3030
- pip3 install --user adafruit-nrfutil
3131

3232
script:

examples/MassStorage/msc_sdcard/msc_sdcard.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// The MIT License (MIT)
22
// Copyright (c) 2019 Ha Thach for Adafruit Industries
33

4-
#include "Adafruit_TinyUSB.h"
4+
/* This example expose SD card as mass storage using
5+
* default SD Library
6+
*/
7+
58
#include "SD.h"
9+
#include "Adafruit_TinyUSB.h"
610

7-
#ifdef ARDUINO_NRF52832_FEATHER
8-
const int chipSelect = 11;
9-
#else
1011
const int chipSelect = 10;
11-
#endif
1212

1313
Adafruit_USBD_MSC usb_msc;
1414

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)