Skip to content

sdcard: Compute CRC7 for all SPI commands.#1088

Merged
dpgeorge merged 2 commits intomicropython:masterfrom
kwagyeman:kwabena/sdcard_fixes
Mar 2, 2026
Merged

sdcard: Compute CRC7 for all SPI commands.#1088
dpgeorge merged 2 commits intomicropython:masterfrom
kwagyeman:kwabena/sdcard_fixes

Conversation

@kwagyeman
Copy link
Contributor

@kwagyeman kwagyeman commented Feb 25, 2026

Some SD cards (e.g., SDXC) permanently enable CRC checking in SPI mode. Sending commands with CRC=0x00 causes them to return ILLEGAL_COMMAND, making initialisation fail. Fixed this by computing a valid CRC7 for every command. Most of my SD cards did not work with the SPI code. After adding CRC support all the cards worked.

Also added a performance test to the test script to benchmark things (using baudrate of 20MHz):

OpenMV 071b46ba24; MicroPython 366b6bd242; OpenMV-AE3 with AE302F80F55D5AE
Type "help()" for more information.
>>> 
10811 bytes read

Single block read/write
11 bytes written
11 bytes read

Write speed test
528 KB/s
Read speed test
263 KB/s

Verifying data read back
Large file Pass
Small file Pass

Tests passed
OpenMV 071b46ba24; MicroPython 366b6bd242; OpenMV-AE3 with AE302F80F55D5AE
Type "help()" for more information.
>>> 

@kwagyeman kwagyeman changed the title Kwabena/sdcard fixes sdcard: Compute CRC7 for all SPI commands. Feb 25, 2026
@kwagyeman kwagyeman force-pushed the kwabena/sdcard_fixes branch from d1cb87a to 95f72a7 Compare February 25, 2026 22:25
@kwagyeman
Copy link
Contributor Author

@dpgeorge - This fix is needed to make the SD card work on the AE3 for the battery shield: https://openmv.io/collections/openmv-ae3-accessories/products/openmv-ae3-battery-shield?variant=42433436385374

@dpgeorge
Copy link
Member

See #765, which does this and other improvements. I gave a lot of feedback on that PR and was waiting for it to be addressed, but it looks like that might never happen...

@kwagyeman
Copy link
Contributor Author

kwagyeman commented Feb 26, 2026

Oh... yeah, way to many changes in one commit in that PR. I tried to make sure this one just fixes what was causing problems. Nothing else to make it easy to review.

If that PR is in limbo, then would appreciate this being considered instead. The only change that needed to be done was to add CRC7 support.

@benwynn
Copy link
Contributor

benwynn commented Mar 1, 2026

...that PR's been hanging out for three years now. This is at least the 3rd independent re-implementation of the crc7 stuff for this module i've seen so far; because people need it to make this module actually work. Lets get this in so people can stop re-inventing wheels. Perfect is the enemy of good.

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this in!

Some SD cards (e.g. SDXC) have CRC checking permanently enabled in SPI
mode. Sending commands with CRC=0x00 causes them to return ILLEGAL_COMMAND,
making initialisation fail. Fix this by computing a valid CRC7 for every
command.

Signed-off-by: Kwabena W. Agyeman <kwagyeman@live.com>
Writes and reads 1 MB through the FAT filesystem and prints throughput
in KB/s.

Signed-off-by: Kwabena W. Agyeman <kwagyeman@live.com>
@dpgeorge dpgeorge force-pushed the kwabena/sdcard_fixes branch from 95f72a7 to d0511a4 Compare March 2, 2026 13:30
@dpgeorge dpgeorge merged commit d0511a4 into micropython:master Mar 2, 2026
5 checks passed
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.

3 participants