You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "SD Card", "1.0");
// Set read write callback
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
// Still initialize MSC but tell usb stack that MSC is not ready to read/write
// If we don't initialize, board will be enumerated as CDC only
usb_msc.setUnitReady(false);
usb_msc.begin();
// Open next file in root.
// Warning, openNext starts at the current directory position
// so a rewind of the directory may be required.
while ( file.openNext(&root, O_RDONLY) )
{
file.printFileSize(&Serial);
Serial.write(' ');
file.printName(&Serial);
if ( file.isDir() )
{
// Indicate a directory.
Serial.write('/');
}
Serial.println();
file.close();
}
root.close();
Serial.println();
fs_changed = false;
delay(500);
}
}
// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and
// return number of copied bytes (must be multiple of block size)
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
{
bool rc;
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
{
bool rc;
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void msc_flush_cb (void)
{
#if SD_FAT_VERSION >= 20000
sd.card()->syncDevice();
#else
sd.card()->syncBlocks();
#endif
// clear file system's cache to force refresh
//sd.cacheClear();
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I am having issues trying to run the SD-FAT version for exposing the SD card as Mass Storage.
More of a weird behaviour.
I am using the TTGO T3-S3 (ESP32S3FH4R2) and coding with Arduino IDE 2.3.2 and the latest Espressif framework.
I have made few modifications to the original code (SD card is started in a HSPI interface).
It compiles and uploads without a hitch, but then the weird part begins. What I observed:
Now...the board has a RESET switch, but the only thing that works is physicaly pulling the cable off and plugging it back in.
I am not sure whether there are code modifications I can try, or if this is purely board related...
Perhaps @lewisxhe could weight in here.
It would be nice to have a workaround for this. My intended application doesn't allow for someone to pull the plug and plug it back when needed.
Thanks in advance.
`#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_TinyUSB.h>
#define SDCARD_MOSI 11
#define SDCARD_MISO 2
#define SDCARD_SCLK 14
#define SDCARD_CS 13
SPIClass SDSPI(HSPI);
SdFat sd;
SdFile root;
SdFile file;
Adafruit_USBD_MSC usb_msc;
bool fs_changed;
void setup()
{
Serial.begin(115200);
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "SD Card", "1.0");
// Set read write callback
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
// Still initialize MSC but tell usb stack that MSC is not ready to read/write
// If we don't initialize, board will be enumerated as CDC only
usb_msc.setUnitReady(false);
usb_msc.begin();
//SPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS);
SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS);
SdSpiConfig config(SDCARD_CS, DEDICATED_SPI, SD_SCK_MHZ(25), &SDSPI);
//if (!sd.begin(SDCARD_CS))
if (!sd.begin(config))
{
Serial.println("SD FAILED!");
}
else
{
Serial.println("SD OK!");
}
// Size in blocks (512 bytes)
#if SD_FAT_VERSION >= 20000
uint32_t block_count = sd.card()->sectorCount();
#else
uint32_t block_count = sd.card()->cardSize();
#endif
Serial.print("Volume size (MB): ");
Serial.println((block_count/2) / 1024);
usb_msc.setCapacity(block_count, 512);
usb_msc.setUnitReady(true);
fs_changed = true;
}
void loop()
{
if ( fs_changed )
{
root.open("/");
Serial.println("SD contents:");
}
}
// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and
// return number of copied bytes (must be multiple of block size)
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
{
bool rc;
#if SD_FAT_VERSION >= 20000
rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
#else
rc = sd.card()->readBlocks(lba, (uint8_t*) buffer, bufsize/512);
#endif
return rc ? bufsize : -1;
}
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
{
bool rc;
digitalWrite(LED_BUILTIN, HIGH);
#if SD_FAT_VERSION >= 20000
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
#else
rc = sd.card()->writeBlocks(lba, buffer, bufsize/512);
#endif
return rc ? bufsize : -1;
}
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void msc_flush_cb (void)
{
#if SD_FAT_VERSION >= 20000
sd.card()->syncDevice();
#else
sd.card()->syncBlocks();
#endif
// clear file system's cache to force refresh
//sd.cacheClear();
fs_changed = true;
digitalWrite(LED_BUILTIN, LOW);
}`
Beta Was this translation helpful? Give feedback.
All reactions