Update to the SD card saga, hopefully not a final update but a big update nonetheless. TL;DR - i was writing Rust code using Embassy for an STM32 microcontroller (ARM) to interface an SD Card via the SPI protocol and had a lot of trouble along the way.
I finally sat down and refactored all of my messy code into some basic Command, Response, Channel structs and put some effort into working with the new SD card that i bough fresh off the shelf. It still had some issues that needed overcoming, but it was 1000x more pleasant to work with than the ancient 15 year old card i tried working with prior.
The solution is not yet perfect and it needs a lot of refactoring and design modifications but i managed to:
Mount the SD card on my linux using whatever filesystem was there by default (vFat i think or Fat32)
Create a file with simple contents to find (repeated "Hello World!" to make it larger than 1 block (512 bytes))
Find the byte offset of the file using "hexdump -C /dev/sda"
Divide the byte offset by the size of a block (512) to get the block offset
(I had to do all that because obviously my code does not have filesystem-parsing ability lol)
Unplug the card from my linux and plug it into my stm32 devboard as before
Hardcode the block address in my code in an aptly named constant
Run the code
And lo, behold, here it is:
(there's a sea of debug messages prior to this ofc and the hex is cutoff but ohwell)
I managed to read one single block of 512 bytes form my SD card and it contained my file's contents, which means that it works!
This really seems like a small result but honestly i had to fight tooth and nail through the complex SD spec and navigate hardware inconsistencies and deviations from said spec and even when things were working it was still kind of difficult to pack everything into nice abstractions so i dont end up with 2000 lines of spi.blocking_transfer(&mut buf).
There's still a lot of work ahead of me - writing to the card would be nice, duh - but im super stoked because i started this a long time ago and it's an accomplishment for me. I've never communicated with anything using SPI, i haven't used any storage devices before (no flash, eeprom chips etc - storage built into the microcontroller doesnt count!). This is a pretty big deal and im very happy about the result.
Hopefully i will continue that in the future, i'd like to get writing working but at this stage it sounds like a piece of cake, since the mechanism is relatively similar (unless the card misbehaves once again lmao).
















