SDBlockDevice fails to initialize

Hi
I am using latest Mbed OS5, and I try to use SDBlockDevice API to access a HC micro SD card.
I have followed block device configuration, but the micro SD card fails to initialize.
The micro SD card seems to be fine. I can use a SD card reader to read / write / format the card.
The failure is due to a mismatch in “Voltage accepted” field in R7 response. I use a multimeter to probe the micro SD card housing, and confirm the reading is at 3.3V, so I am quite puzzle why “Voltage accepted” register values are different.
Below is the debug message when I turn on SD_DBG and SD_CMD_TRACE in SDBlockDevice.cpp
I got the same error regardless the clock speeds although I have tried SCK = 10kHz, 100kHz, 400kHz, 1MHz, 10MHz, 20MHz, 25MHz.
Really appreciate if someone can help shed some light on what I might be missing and what I can investigate further.

CMD:0    arg:0x0         Response:0x1
CMD:8    arg:0x1aa       Response:0x1
V2-Version Card
R3/R7: 0x8a8283aa
CMD8 Pattern mismatch 0x1aa : 0x8a8283aa
Fail to initialize card

If I intentionally bypass “Voltage accepted” values, it would still fail due to unable to read the CSD register

CMD:0    arg:0x0         Response:0x1
CMD:8    arg:0x1aa       Response:0x1
V2-Version Card
R3/R7: 0x8a8283aa
CMD:58   arg:0x0         Response:0x1
R3/R7: 0x8aff8200
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x1
CMD:41   arg:0x40000000          Response:0x0
CMD:58   arg:0x0         Response:0x0
R3/R7: 0xcaff8200
Card Initialized: High Capacity Card
CMD:59   arg:0x0         Response:0x0
init card = 1
CMD:9    arg:0x0         Response:0x0
_wait_token: timeout
Read timeout
Couldn't read csd response from disk

Thanks,
Dennis

I use something like this all the time and works every time (using Mbed-os 5.15)…

#include "mbed.h"
#include "BlockDevice.h"
#include "FATFileSystem.h"

// This will take the system's default block device
BlockDevice *bd = BlockDevice::get_default_instance();
FATFileSystem fs("sd");


int main() { 

int err = fs.mount(bd);
    printf("%s\r\n", (err ? "SD card Fail :(" : "SD card OK"));  
    if(err){
        return 0;
        }

}

This is part of my mbed_app.json file to specify the sd card pins.


    "target_overrides": {
        "*": {
            "platform.stdio-baud-rate": 115200,
            "platform.stdio-convert-newlines": true,
            "mbed-trace.enable": true
        },
        "K22F": {
            "target.features_add": ["STORAGE"],
            "target.components_add": ["SD"],
            "sd.SPI_MOSI": "PTD6",
            "sd.SPI_MISO": "PTD7",
            "sd.SPI_CLK": "PTD5",
            "sd.SPI_CS": "PTD4"
        }
    }
1 Like

Hi Paul,
Thanks for the quick reply over the weekend.
I was using the following instead of getting the default block device as you suggested.

BlockDevice *bd = new SDBlockDevice (SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_CLK, SD_SPI_CS);
FATFileSystem fs("sd");

I modified the micro SD card pins directly in the following file, and I think it is ok since I am able to access the micro SD card as a block device (mentioned a bit further below)
mbed-os\components\storage\blockdevice\COMPONENT_SD\mbed_lib.json

However, I am getting the following assertion error while mounting (I have enable MBED_CONF_FAT_CHAN_FFS_DBG to help debugging).

Mounting [fs] on ffs drive [0:]
disk_initialize on pdrv [0]
disk_ioctl(2)


++ MbedOS Error Info ++
Error Status: 0x80FF0144 Code: 324 Module: 255
Error Message: Assertion failed: sector_size <= WORD(-1)
Location: 0x1000B22D
File: .\mbed-os\features\storage\filesystem\fat\FATFileSystem.cpp+175
Error Value: 0x0

Incidentally, if I do the following without mount, I can read / write to the micro SD card as a block device., but the size information doesn’t look correct. In addition, I do intent to set up a file system on the micro SD card. Am I missing anything from this set up so far?

  err = bd->init();
  if (err) {
      printf("Can't init block device");
  }
  printf("sd size: %llu\n",         bd->size()); // 67108864 on terminal vs actual: 4GB
  printf("sd read size: %llu\n",    bd->get_read_size()); // 1 on terminal
  printf("sd program size: %llu\n", bd->get_program_size()); // 512 on terminal
  printf("sd erase size: %llu\n",   bd->get_erase_size()); // 262144 on terminal

  uint8_t block[512] = "Hello World!\n";       
  if ( 0 == bd->program(block, 0, 512)) {
      // read the data block from the device
      if ( 0 == bd->read(block, 0, 512)) {
          // print the contents of the block
          printf("Message read: %s", block);
      }
  } else {
      printf("SD Block Device Fail to read/write");
  }

Update as of 5/12/20:
As I debug further, I noticed that the default block device was set to “QSPIF” (by calling BlockDevice::get_type()) instead of “SD”. Thus, I modified custom_target.json with “components_remove: [“QSPIF”]” and re-ran the block device example, but still failed due to the same “Voltage accepted” mismatch.

It turned out that I was using GCC_ARM compiler, and it would do sign extend on numerical comparison, so I have to explicitly cast the operands to have the correct behavior.

Good that you got it working Dennis,
I only use Mbed-online or Mbed-Studio so would not have picked up on your issue.

Hi Paul,

I am using Mbed Studio + Mbed-os 6.0 to access an external SD card. I notice SDBlockDevice.h is not recognized but BlockDevice.h is fine.

I use the following code:
BlockDevice *bd = BlockDevice::get_default_instance(); // ok
FATFileSystem fs(“sd”); // ok
BlockDevice sd (p5, p6, p7, p16); // not OK, error

My target board is mbed LPC1768. Any idea if latest Mbed-os 6.0 can support SD card functions ?
Also did not see mbed_app.json file next to main.cpp file.

  • Glennon
1 Like

Hello Glennon,

Did you managed to overcome this problem?

Hello, I am having the same problem. @dcjw in your last comment I assumed you resolved the issue, so can you please mention what steps you followed?