SD card access shenanigans

Unless you’re a real SD/SPI guru then please quietly ignore this - I won’t mind - I do have a solution that’s not perfect, but good enough for now…

Scenario:

I have a little bare-metal “thing” which access the SD card and I’m struggling to make it work on my PicoCalc. (Fitted with RP2350 - Pimoroni Pico Plus 2W) With the exception of a few things like the FAT32 code it’s all my own code - a lot of assembler and some C, with nothing from the SDK. It’s also RISC-V, but that’s probably not important.

However, if I change the SD card out for another that I know works on other systems then it works just fine. I can read/write files, etc.

The other systems are an Adafruit Fruit Jam board - RP2350 and my own 65C816 SBC which uses an AVR mcu for SD card access. The code (in C) is the same in all cases - the underlying SPI driver is obviously different on the AVR but identical other than pin number changes on the Adafruit and PicoCalc boards. The code is conditionally assembled/compiled between the RP2350 platforms, but in reality the only difference is the pin numbers. Both are on SPI 0.

As far as I can tell, my code is essentially the same as the code I find all over the place - I’ve used it for over 8 years now on the AVR before migrating it to the RP2350 recently… You reset the card by running the clock, with CE and MOSI high for ~80 clock cycles then send it a CMD0 command to reset it into SPI mode then off you go…

But with the supplied SD card, a nice 32GB Sandisk SDHC-I (10) it fails to respond, but a micro SD card in a holder works just fine. (I tried a Samsung and another Sandisk card, both OK).

I can’t try the card in the other systems as they both take micro SDs - however it does work just fine in the PicoCalc with other uf2’s loaded and it reads/writes OK in a cheap little USB SD card reader I have which is very old and quite slow, but works OK.

The odd things is that sometimes the card will start to pass the early checks - putting it in to SPI mode then time-out at ACMD41 (e.g. if I hot plug it in) but it’ll fail from CMD0 after that.

I do know that SPI mode is being phased out but starting with the bigger/faster cards for now from what I gather. I’m sure this card will still work just fine in SPI mode but maybe I’m missing some arcane command or trick to make it work

So if anyone has a clue and has written their own code rather than use something from the SDK and has an “A-Ha!” moment the please let me know.

Thanks

-Gordon

I have had similar problems. You can look at my sdcard,c driver that seems to work reliably across all the SD Cards I have.

Thanks for the reply.

I did find your code and have looked it at in detail. Thanks for sharing. It’s really no different from mine. One thing you do is clock out 0xFF after asserting & de-asserting the CE signal. I did try that and it didn’t help.

I do find it odd that my code does work elsewhere (in another RP2350 system) and in my PicoCalc with a different SD card. I’m sure there will be one little subtlety that I’ve missed or overlooked somewhere. What I can’t do it put the supplied SD card into the other systems I have as they all use micro SDs, so I currently have a micro SD in a full-size adapter in the PicoCalc.

I’m also using this site as a reference too

and again, my code appears to be doing the right thing.

I can put in the supplied SD card and it fails, swap it out for another, hit the reset button and it boots up just fine.

It’s quite perplexing and it gets stuck right at the start I output the 80 clocks of high on CE and MOSI, send the CMD0 command and get back.. nothing.

I’ll keep digging…

Cheers,

-Gordon

Well… I seem to have it working with the original supplied SD card…

And I wish I could say what the issue is, but in reality I can’t. I think it may be to do with power, or power-up on the SD card, or maybe it’s as I removed some debugging and the timings changed, or that I tidied up my code a little and mad it look “pretty” (to me, at least), but there it is.

Hit reset and…

RP2350: ClockworkPi: PicoCalc
SPI Pins: SCLK:18, MOSI:19, MISO:16, CS:17, DET:22
In C Main now
Testing for SD card … PicoCalc
Enabling LED on pin 2
Done sdCardSetup
Done setupFS
.ls
Files:
BellLabs_Fine.mp3
bifdiag.bas
Chessnovice_johnybot.nes
lorenz.bas
mand.bas
picocalc.bmp
bootloader_pico.uf2
BOOT2040.uf2
MACHIKAP.INI
README.md
out.bmp
test.txt
main.lua

Directories:
cc
fonts
lua
pico1-apps
picoware
BCPL
.cat mand.bas
Unknown command
.type mand.bas
'Mandelbrot set

Option EXPLICIT

Const W = 320
Const H = 320
Const X_MIN = -2.0
Const X_MAX = 1.0
Const Y_MIN = -1.5
Const Y_MAX = 1.5

… and so on…

Heisenbug or just something else, who knows… And so onwards and upwards!

Cheers,

-Gordon

1 Like