Fix SD cards that send a bit-shifted CMD12 response (SdError 0XC,0X1F) - #1
Open
travisccook wants to merge 2 commits into
Open
travisccook wants to merge 2 commits into
travisccook wants to merge 2 commits into
Conversation
Verbatim copy of the SdFat library bundled with the Earle Philhower arduino-pico core 5.4.4 (greiman/SdFat cda0573), limited to src/, library.properties, LICENSE.md and README.md. Added unchanged so the following fix can be reviewed as a small diff. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U1RDDneTekTuqmAkSXEVGY
Some SD cards answer CMD12 (STOP_TRANSMISSION) in SPI mode with a
correct R1 that is not byte-aligned. SdFat reads it byte-aligned, sees
a bogus status and fails every multi-block read stop, so the Audio
Trigger halts at boot with "SdError: 0XC,0X1F" ("SD card not detected").
Seen on a 16GB SDHC card (MID 0x9C, OEM "SO", product "USD00"): MISO
after CMD12 is 3F E0 1F FF, i.e. R1 = 0x00 starting 3 bits late, at
400 kHz, 4 MHz and 25 MHz alike. Raw CMD17/CMD18 reads of the same card
work at all speeds.
For CMD12 only, realign a bit-shifted response the way the Linux
mmc_spi driver does. Byte-aligned responses are handled as before.
Point PlatformIO at the patched copy with lib_extra_dirs and document
the change and Arduino IDE usage in CHIRP_PATCH.md.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U1RDDneTekTuqmAkSXEVGY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Some microSD cards make the CHIRP Audio Trigger halt at boot with 3 flashing red LEDs and "SD card not detected", even though the card is fine and freshly formatted:
The cause is a card quirk that SdFat doesn't handle. This PR adds a patched copy of the core's SdFat so these cards work.
Cause
These cards answer CMD12 (STOP_TRANSMISSION) with a correct R1 response (
0x00) that is not byte-aligned: it starts 3 bits late. SdFat reads responses byte-aligned, sees0x1F, and fails every multi-block read stop. SdFat reports0x0C(CMD18) because the failed CMD12 happens inside the next read'ssyncDevice();0x1Fis the misread CMD12 response.Raw MISO bytes captured on a Rev.B board right after CMD12:
The Linux
mmc_spidriver already handles this ("ugly card with a bit-shifted response"). SdFat 2.3.1 (bundled with arduino-pico 5.4.4,greiman/SdFatcda0573, which is also the latest upstream) does not.How it was narrowed down
On a Rev.B board (firmware v20260308) with a 16GB SDHC card (CID MID
0x9C, OEM "SO", product "USD00", 2018-07), using raw SPI test builds:0x87CRC; SdFat built withUSE_SD_CRC=1still fails3F E0 1F FFafter CMD12, identical at 400 kHz, 4 MHz and 25 MHz, with valid or0x87CRCA 1GB SDSC card works with stock firmware, which is why this doesn't show up with every card.
Changes
CHIRP_Audio_Trigger/Arduino_Sketches/libraries/SdFat/— SdFat 2.3.1 copied verbatim from arduino-pico 5.4.4 (first commit, unmodified, so the fix is a small diff), plus a 17-line change inSdSpiCard::cardCommand()(second commit). For CMD12 only: skip0xFFbytes, and if the first non-0xFFbyte has its top bit set, combine it with the next byte and shift out the leading one bits. Byte-aligned responses are handled as before; all other commands are untouched.CHIRP_Audio/platformio.ini—lib_extra_dirs = ../librariesso PlatformIO builds use the patched copy.libraries/SdFat/CHIRP_PATCH.md— the problem, the evidence, the diff, Arduino IDE instructions, and how to drop the copy if SdFat fixes this upstream.Arduino IDE: it doesn't read
platformio.ini. Copylibraries/SdFatinto the sketchbooklibrariesfolder; a sketchbook library outranks a platform-bundled one of the same name and architecture per the Arduino CLI dependency resolution rules.Testing
SdError: 0XC,0X1F; patched firmware printsInitializing SD Card... OK (25MHz), reads/writesCHIRP.INI, scans banks and reachesSystem ReadyMUSB) with the 16GB card — not yet testedNotes
CMD18/0x1Ferror with other cards. If it gets fixed upstream and arduino-pico picks it up,libraries/SdFatand thelib_extra_dirsline can be removed.🤖 Generated with Claude Code
https://claude.ai/code/session_01U1RDDneTekTuqmAkSXEVGY