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

Conversation

hathach
Copy link
Member

@hathach hathach commented May 30, 2025

  • use SDIO (pio) for rp2040/rp2350
  • remove printing flash/sdcard contents to Serial since it conflict with host, which can cause conflict in spi/sdio transport and/or correupt cache,sector
  • This "fix" completely remove the sdcard contents dump. a better fix would be marking the SDcard as not ready (return false in TEST_UNIT_READY) while performming self-reading, but that would maybe take a bit of time. I will do it later if needed.
  • supercede and close macos support for msc #532 fix Mass Storage Controller microSD on Metro RP2040 #455

@mikeysklar let me know if this works for you (you need to update Sdfat to v2.3.50)

PS: The conflict happen with macos more often since macos does lots of hidden writing of meta data file e.g .DS_Store

- use SDIO (pio) for rp2040/rp2350
- remove printing flash/sdcard contents to Serial since it conflict with host, which can cause conflict in spi/sdio transport and/or correupt cache,sector
@hathach hathach mentioned this pull request May 30, 2025
@mikeysklar
Copy link

I'm not seeing the SD card on MacOS, but the flash continues to be shared successfullly. Just to confirm I'm running this PR's updated sketch and updated SdFat.

| Component                 | Version        |
|--------------------------|----------------|
| Operating System         | macOS Sequoia 15.5 |
| Arduino IDE              | 2.3.6          |
| Board                    | Adafruit Metro RP2040 |
| SdFat                    | 2.3.50         |
| Adafruit SPIFlash        | 5.1.1          |
| Adafruit TinyUSB Library | 3.6.0          |

I can dig more into what macOS thinks is going on.

Moving the same board to my Ubuntu laptop I see the microSD being shared. I can read / write files without issue.

sklarm@prego:/media/sklarm/SDCARD1$ df -H | grep sd..
/dev/sda1        16M   31k   16M   1% /media/sklarm/EXT FLASH
/dev/sdb1        16G  2.0M   16G   1% /media/sklarm/SDCARD1

@mikeysklar
Copy link

mikeysklar commented May 30, 2025

After removing power from the Metro RP2040 and bringing it back to macOS a working read/write auto-mounted microSD appeared. I'm guessing a hard reset is necessary since the board is correctly sharing the drive after being unplugged / replugged. I had been doing soft resets.

I did a quick drive test with a 10MB file going through the Metro RP2040 microSD and compared with with a directly connected (remove the controller and use a USB adapter). Probably what you expect to see. Read speed is crazy fast, but that must be caching so not a fair comparison.

| Access Method                 | Write Speed | Read Speed    | Write Gain |
|------------------------------|-------------|---------------|------------|
| RP2040 (mounted via USB MSC) | 0.72 MB/s   | 1.24 MB/s     | 1×         |
| USB-C Adapter (macOS)        | 4.59 MB/s   | 4666.04 MB/s* | ~6.4×      |

@hathach
Copy link
Member Author

hathach commented May 30, 2025

Great, thank you for testing.

you can try usign the old SPI by comment out the SDIO_CMD/CLK/DATA_PIN and define the SDCARD_CS to see if the speed is actual improved using new SDIO (pio)

@mikeysklar
Copy link

I disabled SDIO and gave SPI a try. That was a good test.

SPI is 6x slower than SDIO
SDIO is 6x slower than direct connect.

| Access Method           | Write Speed | Read Speed    | Write Gain |
|-------------------------|-------------|---------------|------------|
| RP2040 SPI MSC          | 0.11 MB/s   | 0.43 MB/s     | 1×         |
| RP2040 SDIO MSC         | 0.72 MB/s   | 1.24 MB/s     | ~6.5×      |
| USB-C Adapter (macOS)   | 4.59 MB/s   | 4666.04 MB/s* | ~42×       |

@hathach
Copy link
Member Author

hathach commented May 31, 2025

Card reader is usb highspeed while rp2040 is full speed 12 Mbit/s i.e 1.5 MB/s so 1.25Mbyte is as good as you can get. I think we can merge this now. Thank you very much for your effort to try and test this issue

@ladyada we get at least 3x for sdcard using pio as sdio. Note that somehow I cannot get metro rp2350 working with it. Maybe I miss some settings, will try to fix it next week

@hathach hathach merged commit 44053c4 into master May 31, 2025
24 checks passed
@hathach hathach deleted the fix-metro-rp2040-msc-sdfat-macos-conflict branch May 31, 2025 04:03
@ladyada
Copy link
Member

ladyada commented May 31, 2025

hmm it should work but to be honest we didn't try it, do you want to buy https://www.adafruit.com/product/4682 and try wiring it up to any other pins

@hathach
Copy link
Member Author

hathach commented Jun 3, 2025

yeah, not sure why, will test it further, and make an PR to fix later.

@hathach
Copy link
Member Author

hathach commented Jun 5, 2025

@ladyada I used an exterrnal sdcard breakout with pin 22,23,8,9,10,11 and it seems to work well. I think the upstream SdFat currently does not support GPIO that is larger than 31/32

https://github.com/greiman/SdFat/blob/a4ecec60554e8071e3f15a4407cad3908519c06e/src/SdCard/Rp2040Sdio/PioSdioCard.cpp#L329-L330

  g_pio->input_sync_bypass |=
      (1 << g_clkPin) | (1 << g_cmdPin) | (0XF << g_dat0Pin);

I will file an issue and try to see if we can make a PR to upstream

IMG_1800

@hathach
Copy link
Member Author

hathach commented Jun 9, 2025

@ladyada once I have manage to fix the GPIO > 32 issue with adafruit/SdFat#35, I just realize that upstream just fixed it a few days ago, though it is pushed to SdFat-beta repo greiman/SdFat-beta@e8208b1 which is scheduled to release as 2.3.1 soon. Instead of waiting, I think we can just go ahead and merge with SdFat-beta for now and do the sync later on if there is any other commit.

@ladyada
Copy link
Member

ladyada commented Jun 9, 2025

yes please! thank you :)

@hathach
Copy link
Member Author

hathach commented Jun 9, 2025

update: I am backing off from SdFat-beta, since there is lots of refactor and rename, which is time-consuming to cherry pick and re-sync. It is probably faster to apply simple fix in adafruit/SdFat#35 and do simple sync/merge with upstream later :)

@ladyada
Copy link
Member

ladyada commented Jun 9, 2025

thats fine

@hathach
Copy link
Member Author

hathach commented Jun 10, 2025

https://github.com/adafruit/SdFat/releases/tag/2.3.53 is released and fixed issue with high GPIO for now, we will sync with upstream when it is released later.

@mikeysklar
Copy link

mikeysklar commented Jun 11, 2025

I had the opportunity to do an updated read / write test using the RP2350 changes @hathach has recently made to the Adafruit SdFat. I tried using my fastest microSD.

| Component                 | Version/Details           |
|--------------------------|---------------------------|
| Operating System         | macOS Sequoia 15.5        |
| Arduino IDE              | 2.3.6                     |
| Board                    | Adafruit Metro RP2350     |
| BSP (Philhower)          | 4.5.4                     |
| SdFat Library            | Adafruit Fork 2.3.53      |
| Adafruit SPIFlash        | 5.1.1                     |
| Adafruit TinyUSB Library | 3.6.1                     |
| microSD Card             | PNY A1, U3, V30           |
| Access Method           | Write Speed | Read Speed     | Write Gain |
|-------------------------|-------------|----------------|------------|
| USB-C Adapter (macOS)   | 18.44 MB/s  | 5102.56 MB/s*  | ~168×      |
| RP2350 SDIO MSC         | 0.81 MB/s   | 1.62 MB/s      | ~7.4×      |
| RP2040 SDIO MSC         | 0.72 MB/s   | 1.24 MB/s      | ~6.5×      |
| RP2040 SPI MSC          | 0.11 MB/s   | 0.43 MB/s      | 1×         |

@hathach
Copy link
Member Author

hathach commented Jun 12, 2025

superb! thank you @mikeysklar for detail testing.

@ladyada
Copy link
Member

ladyada commented Jun 12, 2025

yay thanks for the thorough test. cc @caternuson ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mass Storage Controller microSD on Metro RP2040
3 participants