SD/MMC over SPI: Empty block reads. - Page 1
Forum section: electronics / general

author: utkumaden
author_rank: Newbie
timestamp: August 03, 2020, 09:48:56 pm
content: I am working as the programmer in a student project I am working on. One of the requirements of the project is to store weather data read from off the shelf sensors in an SD card. Since I don't have a tell in the specifics of the micro controller choice, I am stuck with implementing my own SD over SPI driver. I have tried a couple ready made drivers using STMs HAL library with no avail.Followinghttp://elm-chan.org/docs/mmc/mmc_e.html, I have created some of the necessary code to at least initialize an SD card correctly.Clock with the cardnot selectedSend CMD0 to put it in idle state.Try SEND_IF_CONDITION to deduce the type of card plugged in (even though I can't control the voltage)APP_SEND_OP_COND to initialize the card in SD modeRead the High Capacity field in CSDSet block length if the card is not high capacity.However when I send a read command two things come into play: The address must be 512 byte aligned, else it is rejected with an address error (even if the card is HC), and if the command isn't rejected, every sector read return null (all zeros). I have also tried setting the block size myself with SET_BLOCK_LEN to 512 IIRC. Since I don't have access to a proper scope right now, I am stuck with using a cheap logic analyzer I bought from the internet, which sometimes, somehow interferes with the MISO line. I try to verify with the analyzer plugged in and unplugged.This phenomena isn't limited to READ_SINGLE_BLOCK either, it happens on READ_MULTIPLE_BLOCK too. In fact it isn't limited to what I have written either. This has happened with a random library I found, where the read block was all zeros.Has anyone ever come across this issue? Is the SD card I have been using the issue or is there an arcane step I am missing? Could it be the another one of those breadboard failures?If anyone wishes to analyze my code or try it themselves, I can supply the code upon request. (I do not wish to post it immediately).Hardware information:A SanDisk 32GB microSD card (if relevant)Any bog standard SD breakout moduleThe "BluePill" board with an STM32F103C8T6 microcontrollerA cheap logic analyzerI am starting to think data transfers and communication is my kryptonite.

author: bson
author_rank: Unknown
timestamp: August 04, 2020, 02:06:18 am
content: You do know CMD17: READ_SINGLE_BLOCK expects a byte address with SDSC cards, but a 512-byte block number with SDHC/SDXC? I'm asking because if you give SDHC/SDXC a byte address the behavior you see wouldn't be unreasonable.Also, might be worth checking that the card isn't locked. (CMD42: LOCK_UNLOCK)Grab a copy of the Simplified Physical Layer spec athttps://www.sdcard.org/downloads/pls/index.htmlSome cards are odd, also. I've seen SDHC cards that ignore SET_BLOCKLEN and contrary to the spec always use 1024 bytes! Better ask the card and use the value it reports.

author: utkumaden
author_rank: Newbie
timestamp: August 04, 2020, 08:37:37 am
content: Quote from: bson on August 04, 2020, 02:06:18 amYes I am aware of this. From what I understand setting the block length will change the addressing mode of the CMD17/18 and the write commands to block addressing. I have debugged my code further to determine if I am identifying the card correctly. There was a small error, but I am confused even more. Despite the fact I set the HC flag in ACMD41, the flag is unset in the OCR register after initialization. Now the code does set the block length.Quote from: bson on August 04, 2020, 02:06:18 amI am pretty sure the card is not locked, as it is a microsd and it never had any locked sectors. I'll try this.Quote from: bson on August 04, 2020, 02:06:18 amHow can I determine the default block length of the card? I skimmed through the simplified spec and couldn't find a method.I am attaching my logic analyzer output.

author: bson
author_rank: Unknown
timestamp: August 04, 2020, 11:07:06 pm
content: Quote from: utkumaden on August 04, 2020, 08:37:37 amThis was in 2014 or thereabouts, so not exactly fresh. I believe I set the blocklen to 512, then tried to read 512 bytes. If that resulted in an error (BLOCK_LEN_ERROR), try 1024, and if that fails try 2048 and then give up. Remember the size (and address shift count). Those were all very early HC cards I believe, or possibly 'oversize' SDSC. I still have a collection of old cards stashed somewhere.

author: utkumaden
author_rank: Newbie
timestamp: August 05, 2020, 09:52:53 pm
content: It seems I have figured it out. I think it was a power issue, because the module had some ICs, the buffer on it wasn't getting enough voltage. 5V works like a charm (the board also has a regulator).