Skip to content

resolve msc sdcard example conflict with host espcially macos #533

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 31, 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 @@ -33,43 +33,54 @@

// for flashTransport definition
#include "flash_config.h"

Adafruit_SPIFlash flash(&flashTransport);

// External Flash File system
FatVolume fatfs;

//--------------------------------------------------------------------+
// 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 SDIO_CLK_PIN 18
#define SDIO_CMD_PIN 19 // MOSI
#define SDIO_DAT0_PIN 20 // DAT1: 21, DAT2: 22, DAT3: 23

#define SDCARD_DETECT 15
#define SDCARD_DETECT_ACTIVE LOW

#elif defined(ARDUINO_ADAFRUIT_METRO_RP2350)
// Note: not working yet (need troubleshoot later)
#define SDIO_CLK_PIN 34
#define SDIO_CMD_PIN 35 // MOSI
#define SDIO_DAT0_PIN 36 // DAT1: 37, DAT2: 38, DAT3: 39

#define SDCARD_DETECT 40
#define SDCARD_DETECT_ACTIVE LOW

#else
// Use SPI, no detect
#define SDCARD_CS 10
// no detect
#endif

// SDCard File system
#if defined(SDIO_CLK_PIN) && defined(SDIO_CMD_PIN) && defined(SDIO_DAT0_PIN)
#define SD_CONFIG SdioConfig(SDIO_CLK_PIN, SDIO_CMD_PIN, SDIO_DAT0_PIN)
#else
#define SD_CONFIG SdSpiConfig(SDCARD_CS, SHARED_SPI, SD_SCK_MHZ(50))
#endif

// File system on SD Card
SdFat sd;

// USB Mass Storage object
Adafruit_USBD_MSC usb_msc;

// Set to true when PC write to flash
bool sd_changed = false;
bool sd_inited = false;

bool flash_formatted = false;
bool flash_changed = false;

// the setup function runs once when you press reset or power the board
void setup() {
#ifdef LED_BUILTIN
Expand Down Expand Up @@ -98,14 +109,11 @@ void setup() {

//------------- Lun 0 for external flash -------------//
flash.begin();
flash_formatted = fatfs.begin(&flash);

usb_msc.setCapacity(0, flash.size()/512, 512);
usb_msc.setReadWriteCallback(0, external_flash_read_cb, external_flash_write_cb, external_flash_flush_cb);
usb_msc.setUnitReady(0, true);

flash_changed = true; // to print contents initially

//------------- Lun 1 for SD card -------------//
#ifdef SDCARD_DETECT
// DETECT pin is available, detect card present on the fly with test unit ready
Expand All @@ -126,26 +134,18 @@ void setup() {
bool init_sdcard(void) {
Serial.print("Init SDCard ... ");

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

if (!sd.begin(SD_CONFIG)) {
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 SDCARD_CS or SDIO pin to match your shield or module?");
return false;
}

uint32_t block_count;

#if SD_FAT_VERSION >= 20000
block_count = sd.card()->sectorCount();
#else
block_count = sd.card()->cardSize();
#endif

uint32_t block_count = sd.card()->sectorCount();
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.println(" MB");
Expand Down Expand Up @@ -173,93 +173,35 @@ void print_rootdir(File32* rdir) {
}

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

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

Serial.println("Flash contents:");
print_rootdir(&root);
Serial.println();

root.close();
}

flash_changed = false;
}

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

Serial.println("SD contents:");
print_rootdir(&root);
Serial.println();

root.close();

sd_changed = false;
}

delay(1000); // refresh every 1 second
// nothing to do
}


//--------------------------------------------------------------------+
// SD Card callbacks
//--------------------------------------------------------------------+

int32_t sdcard_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

int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
bool rc = sd.card()->readSectors(lba, (uint8_t*) buffer, bufsize/512);
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 sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
bool rc;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

#if SD_FAT_VERSION >= 20000
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
#else
rc = sd.card()->writeBlocks(lba, buffer, bufsize/512);
#endif

bool rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
return rc ? bufsize : -1;
}

// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void sdcard_flush_cb (void)
{
#if SD_FAT_VERSION >= 20000
void sdcard_flush_cb (void) {
sd.card()->syncDevice();
#else
sd.card()->syncBlocks();
#endif

// clear file system's cache to force refresh
sd.cacheClear();

sd_changed = true;
sd.cacheClear(); // clear file system's cache to force refresh

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
Expand All @@ -281,8 +223,6 @@ bool sdcard_ready_callback(void) {
usb_msc.setReadWriteCallback(1, NULL, NULL, NULL);
}

Serial.println(sd_inited);

return sd_inited;
}
#endif
Expand Down Expand Up @@ -317,12 +257,6 @@ int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize
// used to flush any pending cache.
void external_flash_flush_cb (void) {
flash.syncBlocks();

// clear file system's cache to force refresh
fatfs.cacheClear();

flash_changed = true;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
#endif
Expand Down
Loading