Skip to content

update msc sdcad example to work with metro rp2040 #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ FatVolume fatfs;

#if defined(ARDUINO_PYPORTAL_M4) || defined(ARDUINO_PYPORTAL_M4_TITANO)
// PyPortal has on-board card reader
#define SDCARD_CS 32
#define SDCARD_DETECT 33
#define SDCARD_CS 32
#define SDCARD_DETECT 33
#define SDCARD_DETECT_ACTIVE HIGH
#elif defined(ARDUINO_ADAFRUIT_METRO_RP2040)
#define SDCARD_CS 23
#define SDCARD_DETECT 15
#define SDCARD_DETECT_ACTIVE LOW
#else
#define SDCARD_CS 10
// no detect
Expand Down Expand Up @@ -113,17 +118,15 @@ void setup() {
usb_msc.setUnitReady(1, true);
#endif

// while ( !Serial ) delay(10); // wait for native usb
// while ( !Serial ) delay(10); // wait for native usb
Serial.println("Adafruit TinyUSB Mass Storage External Flash + SD Card example");
delay(1000);
}

bool init_sdcard(void)
{
bool init_sdcard(void) {
Serial.print("Init SDCard ... ");

if ( !sd.begin(SDCARD_CS, SD_SCK_MHZ(50)) )
{
if (!sd.begin(SDCARD_CS, SD_SCK_MHZ(50))) {
Serial.print("Failed ");
sd.errorPrint("sd.begin() failed");

Expand All @@ -138,33 +141,29 @@ bool init_sdcard(void)
block_count = sd.card()->cardSize();
#endif


usb_msc.setCapacity(1, block_count, 512);
usb_msc.setReadWriteCallback(1, sdcard_read_cb, sdcard_write_cb, sdcard_flush_cb);

sd_changed = true; // to print contents initially

Serial.print("OK, Card size = ");
Serial.print((block_count / (1024*1024)) * 512);
Serial.print((block_count / (1024 * 1024)) * 512);
Serial.println(" MB");

return true;
}

void print_rootdir(File32* rdir)
{
void print_rootdir(File32* rdir) {
File32 file;

// 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(rdir, O_RDONLY) )
{
while (file.openNext(rdir, O_RDONLY)) {
file.printFileSize(&Serial);
Serial.write(' ');
file.printName(&Serial);
if ( file.isDir() )
{
if (file.isDir()) {
// Indicate a directory.
Serial.write('/');
}
Expand All @@ -173,18 +172,14 @@ void print_rootdir(File32* rdir)
}
}

void loop()
{
if ( flash_changed )
{
if (!flash_formatted)
{
void loop() {
if (flash_changed) {
if (!flash_formatted) {
flash_formatted = fatfs.begin(&flash);
}

// skip if still not formatted
if (flash_formatted)
{
if (flash_formatted) {
File32 root;
root = fatfs.open("/");

Expand All @@ -198,8 +193,7 @@ void loop()
flash_changed = false;
}

if ( sd_changed )
{
if (sd_changed) {
File32 root;
root = sd.open("/");

Expand Down Expand Up @@ -275,18 +269,14 @@ void sdcard_flush_cb (void)
#ifdef SDCARD_DETECT
// Invoked when received Test Unit Ready command.
// return true allowing host to read/write this LUN e.g SD card inserted
bool sdcard_ready_callback(void)
{
bool sdcard_ready_callback(void) {
// Card is inserted
if ( digitalRead(SDCARD_DETECT) == HIGH )
{
if (digitalRead(SDCARD_DETECT) == SDCARD_DETECT_ACTIVE) {
// init SD card if not already
if ( !sd_inited )
{
if (!sd_inited) {
sd_inited = init_sdcard();
}
}else
{
} else {
sd_inited = false;
usb_msc.setReadWriteCallback(1, NULL, NULL, NULL);
}
Expand Down
28 changes: 22 additions & 6 deletions examples/MassStorage/msc_sdfat/msc_sdfat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@
*********************************************************************/

/* This example expose SD card as mass storage using
* SdFat Library
* - SdFat https://github.com/adafruit/SdFat
*/

#include "SPI.h"
#include "SdFat_Adafruit_Fork.h"
#include "Adafruit_TinyUSB.h"

const int chipSelect = 10;
//--------------------------------------------------------------------+
// SDCard Config
//--------------------------------------------------------------------+

#if defined(ARDUINO_PYPORTAL_M4) || defined(ARDUINO_PYPORTAL_M4_TITANO)
// PyPortal has on-board card reader
#define SDCARD_CS 32
#define SDCARD_DETECT 33
#define SDCARD_DETECT_ACTIVE HIGH
#elif defined(ARDUINO_ADAFRUIT_METRO_RP2040)
#define SDCARD_CS 23
#define SDCARD_DETECT 15
#define SDCARD_DETECT_ACTIVE LOW
#else
#define SDCARD_CS 10
// no detect
#endif

// File system on SD Card
SdFat sd;
Expand Down Expand Up @@ -57,16 +73,16 @@ void setup() {
TinyUSBDevice.attach();
}

//while ( !Serial ) delay(10); // wait for native usb
while ( !Serial ) delay(10); // wait for native usb
Serial.println("Adafruit TinyUSB Mass Storage SD Card example");
Serial.print("\nInitializing SD card ... ");
Serial.print("CS = "); Serial.println(chipSelect);
Serial.print("CS = "); Serial.println(SDCARD_CS);

if ( !sd.begin(chipSelect, SD_SCK_MHZ(50)) ) {
if ( !sd.begin(SDCARD_CS, SD_SCK_MHZ(50)) ) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
Serial.println("* did you change the SDCARD_CS pin to match your shield or module?");
while (1) delay(1);
}

Expand Down