gremain sd fat - a debugging journey
Posted: Fri Feb 05, 2021 5:50 pm
this is somewhat off-topic or perhaps it belong to (steve's) libmaple and libraries
but as sd fat is used rather heavily for mass data storage, i'd like to post a pretty involved debugging session
related to gremain's sd fat and steve's libmaple core - actually the spi.h, spi.cpp Arduino-ish apis
https://github.com/greiman/SdFat
in a heavily patched up local copy of Sdinfo, i made it print the commands issued to the sd card over SPI
https://github.com/greiman/SdFat/tree/m ... les/SdInfo
A run looks like this
to get to the gist of the problem, the manufacturer, card id and card size information is completely incorrect!
all the sectors partition tables and data read etc are zeros. the CMD sd card commands are inserted/patched into the codes using Serial.print() statements to trace execution.
it got me into a deep dive into the 'simplified' sd specs
https://www.sdcard.org/downloads/pls/
http://elm-chan.org/docs/mmc/mmc_e.html
the things as i learned are if you are using the spi interface (not the full sdio), the part and commands to read is only the spi portion of the specs.
the sdcard protocols are pretty complicated as it caters to sd 4 parallel channels, single channel and a fallback spi mode. the spi mode specs is one part to read while doing just spi. things used for the main 4 channel sd protocols are different from that of spi mode. the gist of this protocol specifcally for spi is something like this:
- CMD0 is like a soft reset command and this needs to be issued with CS held low. if that isn't done the card likely initialize to something other than spi mode. this means when you hit trouble with sd spi mode, the cs pin and setup is 1 important thing to look out for.
- CMD:8, CMD:55, ACMD:41, CMD 58 are initialization commands the details of which can be found in the 'simplified specs' (read the spi portion, the non-spi parts are different). CMD 58 that in part tells between types of sd cards e.g. SD (v1), SD2, SDHC etc
- CMD 10 reads card ID, CMD 9 reads CSD that contains the volume/size information - it is returning zeros in the above case.
that got me wondering what is wrong digging into the possible causes
- initially i started thinking it is the cs pin, then in doing many tests, i tried assigning different pins for cs. same problem zeros.
then one of the test i did is to simply disconnect cs pin. it stopped working. that kind of proves i'm still doing spi mode somehow.
- next i thought it is some series resistors (on the ili9341 lcd module with the sd card slot), i literally removed the resistors. same zeros
- next i thought maybe connectivity problems, so i tried printing the commands and more importantly responses. what i'm looking for is *non zero* responses, yup i got that. i actually patched my copy to issue cmd2 (get card ids) and cmd3 (card relative address) which are not intended for spi.
i got 4 which is an error code. looks good it at least confirms that i'm doing spi
- next reduced spi frequency / baud rate to 1mhz
- next i thought my sd cards do not compatibly support the spi mode - bummer (it is quite notorious some cards do not comply with the spi fallback).
then several different cards (sandisk, some less known brands etc) all give zeros. i'm about to setup a logic analyzer to dig further in
- there are some results with some data. it turns out if i memset() those buffers to zeros, that's what i got, zeros. i.e. nothing read from the sd card
but as sd fat is used rather heavily for mass data storage, i'd like to post a pretty involved debugging session
related to gremain's sd fat and steve's libmaple core - actually the spi.h, spi.cpp Arduino-ish apis
https://github.com/greiman/SdFat
in a heavily patched up local copy of Sdinfo, i made it print the commands issued to the sd card over SPI
https://github.com/greiman/SdFat/tree/m ... les/SdInfo
A run looks like this
Code: Select all
SdFat version: 2.0.4
Assuming the SD is the only SPI device.
Edit DISABLE_CS_PIN to disable an SPI device.
Assuming the SD chip select pin is: 4
Edit SD_CS_PIN to change the SD chip select pin.
type any character to start
CMD:0
1
CMD:59
1
CMD:8
1
CMD:55
1
CMD:41
1
CMD:55
1
CMD:41
0
CMD:58
0
init time: 808 ms
CMD:2
4
CMD:3
4
CMD:10
0
000000000000000000000000000000004653
CMD:9
0
0000000000000000000000000000000032
CMD:58
0
Card type: SDHC
Manufacturer ID: 0X0
OEM ID:
Product:
Version: 0.0
Serial number: 0X0
Manufacturing date: 0/2000
cardSize: 0.00 MB (MB = 1,000,000 bytes)
flashEraseSize: 1 blocks
eraseSingleBlock: false
OCR: 0XC0FF8000
CMD:18
0
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
... ...
000000000
SD Partition Table
part,boot,bgnCHS[3],type,endCHS[3],start,length
1,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0,0
2,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0,0
3,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0,0
4,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0X0,0,0
CMD:12
0
CMD:18
0
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
...
000000000
CMD:12
0
CMD:18
0
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
.. ...
000000000
volumeBegin failed. Is the card formatted?
SD errorCode: SD_CARD_ERROR_READ_REG = 0X1B
SD errorData = 0X0
type any character to start
all the sectors partition tables and data read etc are zeros. the CMD sd card commands are inserted/patched into the codes using Serial.print() statements to trace execution.
it got me into a deep dive into the 'simplified' sd specs
https://www.sdcard.org/downloads/pls/
http://elm-chan.org/docs/mmc/mmc_e.html
the things as i learned are if you are using the spi interface (not the full sdio), the part and commands to read is only the spi portion of the specs.
the sdcard protocols are pretty complicated as it caters to sd 4 parallel channels, single channel and a fallback spi mode. the spi mode specs is one part to read while doing just spi. things used for the main 4 channel sd protocols are different from that of spi mode. the gist of this protocol specifcally for spi is something like this:
- CMD0 is like a soft reset command and this needs to be issued with CS held low. if that isn't done the card likely initialize to something other than spi mode. this means when you hit trouble with sd spi mode, the cs pin and setup is 1 important thing to look out for.
- CMD:8, CMD:55, ACMD:41, CMD 58 are initialization commands the details of which can be found in the 'simplified specs' (read the spi portion, the non-spi parts are different). CMD 58 that in part tells between types of sd cards e.g. SD (v1), SD2, SDHC etc
- CMD 10 reads card ID, CMD 9 reads CSD that contains the volume/size information - it is returning zeros in the above case.
that got me wondering what is wrong digging into the possible causes
- initially i started thinking it is the cs pin, then in doing many tests, i tried assigning different pins for cs. same problem zeros.
then one of the test i did is to simply disconnect cs pin. it stopped working. that kind of proves i'm still doing spi mode somehow.
- next i thought it is some series resistors (on the ili9341 lcd module with the sd card slot), i literally removed the resistors. same zeros
- next i thought maybe connectivity problems, so i tried printing the commands and more importantly responses. what i'm looking for is *non zero* responses, yup i got that. i actually patched my copy to issue cmd2 (get card ids) and cmd3 (card relative address) which are not intended for spi.
i got 4 which is an error code. looks good it at least confirms that i'm doing spi
- next reduced spi frequency / baud rate to 1mhz
- next i thought my sd cards do not compatibly support the spi mode - bummer (it is quite notorious some cards do not comply with the spi fallback).
then several different cards (sandisk, some less known brands etc) all give zeros. i'm about to setup a logic analyzer to dig further in
- there are some results with some data. it turns out if i memset() those buffers to zeros, that's what i got, zeros. i.e. nothing read from the sd card