L053R8 Nucleo SD Card HelloWorld - can't init SD card

Hi,

I’m on an L053R8 Nucleo board, hence on MBed 2 version (can’t upgrade; or haven’t found an easy way to do so!), and trying to implement a basic SD-Card handling program.

I have tried this:
https://os.mbed.com/handbook/SDFileSystem
but I get, with a 2Gb older card:

No disk, or could not put SD card in to SPI idle state

while on a 8Gb, or another 16Gb SDHC card:

Timeout waiting for v2.x card
Fail to initialize card
Timeout waiting for v2.x card
Fail to initialize card
Could not open file for write

Having different results for the two cards makes me think the physical wiring is correct, as I get some response form the SPI. I’m using SPI1 (D4, D5, D3, D2 for mosi, miso, sclk, cs, respectively) from the board.

I also tried a different lib (following suggestions from an older similar post):

But on the 8Gb I still get

Mounting SD card...failed!

I’ve also compared code with the MBed6 version of SDBlockDevice.cpp and tried importing the init section into my SD libs:

still without success: from 2Gb card I get a response 0x00 at CMD0, from 8Gb I get 0xFF.

What else could I try?
Thanks in advance

Hello,

I tried the old (your first link) one and SDFileSystem.cpp at line 127 contains a settings _init_sck = 100000; in my case it was necessary to set it to 350000.

I had issue with "Could not open file for write", but it disappeared without any change of code.

SDHC 4GB connected to Nucleo-L432KC.

BR, Jan

Hi Jan, thanks for your suggestion.
Indeed, init frequency should be set beyond 125KHz, or the spi isn’t initialized at all. Infact my tests happen @400KHz.
I’m suspecting the powering scheme, having read somewhere that the board 3.3V doesn’t provide enough current, especially if the board is powered through the USB only (as I’m doing). How did you power your L432KC?

Hello Fabio,

My board is powered only via USB and the SD card breakout board is powered from Nucleo’s 5V pin.

Br, Jan

I made a fundamental mistake: powering the adapter with 3.3V!
Thanks, for making me realize that.

Hello Jan,
I am having a similar issue init’ing the SDBlockDevice. I have tried 6 different SPI SD Card adapters, and many microSDs of different sizes, and from different manufacturers and of different standards (SD/SDHC/SDXC). Still, the init() stage fails for me.

Switching the SPI Adapter VCC between 3V and 5V off the STM32 does not seem to make a difference. I have also experimented with different init frequencies (100KHz, 150KHz, 400KHz) but these did not seem to affect the init sequence eventual result either. They all seem to fail right after CMD9 in reading back the card-specific data.

Any thoughts on what could cause this? My debug logs (for 3 different configurations and runs) are below. Can you see anything amiss?

Similar to your setup, I am powering my NUCLEO F767ZI off the USB only.
Thanks
Nuertey

DEBUG LOGS:

============================================================
HiLetgo 5pcs Micro SD TF Card Adater Reader Module 6Pin SPI Interface Driver Module

init frequency of 150 KHz (and 400 KHz) gives same result.

micro SDCARD, 1GB => Connected its VCC to +3V GPIO on STM32.

Mbed OS version: 6.16.0

Built: Jul 30 2022, 12:17:52

Calling the SDBlockDevice init() method…
*Running SDBlockDevice::_initialise_card() … *
CMD:0 arg:0x0 Response:0x1
CMD:8 arg:0x1aa Response:0x1
V2-Version Card
R3/R7: 0x1aa
CMD:58 arg:0x0 Response:0x1
R3/R7: 0xff8000
Indicating to SDCard that we support HC/XC capacity cards in ACMD41…
CMD:41 arg:0x40000000 Response:0x1
CMD:41 arg:0x40000000 Response:0x0
Card Initialized: Version 2.x Card
CMD:58 arg:0x0 Response:0x0
R3/R7: 0x80ff8000
*Card Initialized: Standard Capacity Card: Version 2.x *
CMD:59 arg:0x0 Response:0x0
init card = 1
*Running SDBlockDevice::_sd_sectors() … *
CMD:9 arg:0x0 Response:0x0
Nuertey Expecting 0xFE here: 0x40
Nuerkie Expecting 0xFE here: 0x26
*Running SDBlockDevice::_read_bytes() … *
*Running SDBlockDevice::_wait_token() … *
_wait_token: timeout
Read timeout
Couldn’t read csd response from disk
CMD9 failed as: “0 == _sectors”
After SDBlockDevice init() method…
Error! SDBlockDevice Init failed: [-4001] → “device specific error”

============================================================
Same SPI adapter as previous, same init frequency of 150 KHz.

but rather microSDXC UHS-I CARD, 64GB => Connected its VCC to +3V GPIO on STM32.

Mbed OS version: 6.16.0

Built: Jul 30 2022, 12:17:52

Calling the SDBlockDevice init() method…
*Running SDBlockDevice::_initialise_card() … *
CMD:0 arg:0x0 Response:0x1
CMD:8 arg:0x1aa Response:0x1
V2-Version Card
R3/R7: 0x1aa
CMD:58 arg:0x0 Response:0x1
R3/R7: 0xff8000
Indicating to SDCard that we support HC/XC capacity cards in ACMD41…
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
Card Initialized: Version 2.x Card
CMD:58 arg:0x0 Response:0x0
R3/R7: 0x80ff8000
*Card Initialized: Standard Capacity Card: Version 2.x *
CMD:59 arg:0x0 Response:0x0
init card = 1
*Running SDBlockDevice::_sd_sectors() … *
CMD:9 arg:0x0 Response:0x0
Nuertey Expecting 0xFE here: 0x4f
Nuerkie Expecting 0xFE here: 0x20
*Running SDBlockDevice::_read_bytes() … *
*Running SDBlockDevice::_wait_token() … *
_wait_token: timeout
Read timeout
Couldn’t read csd response from disk
CMD9 failed as: “0 == _sectors”
After SDBlockDevice init() method…
Error! SDBlockDevice Init failed: [-4001] → “device specific error”

============================================================
Maxmoral 2PCS Micro SD Storage Board Memory Shield Expansion Module 6 Pin SPI Interface Mini TF Card Adapter Reader

init frequency of 150 KHz (and 400 KHz) gives same result.

micro SDCARD, 1GB => Connected its VCC to +5V GPIO on STM32.

Mbed OS version: 6.16.0

Built: Aug 9 2022, 19:59:53

Calling the SDBlockDevice init() method…
*Running SDBlockDevice::_initialise_card() … *
CMD:0 arg:0x0 Response:0x1
CMD:8 arg:0x1aa Response:0x1
V2-Version Card
R3/R7: 0x1aa
CMD:58 arg:0x0 Response:0x1
R3/R7: 0xff8000
Indicating to SDCard that we support HC/XC capacity cards in ACMD41…
CMD:41 arg:0x40000000 Response:0x1
CMD:41 arg:0x40000000 Response:0x1
CMD:41 arg:0x40000000 Response:0x0
Card Initialized: Version 2.x Card
CMD:58 arg:0x0 Response:0x0
R3/R7: 0x80ff8000
*Card Initialized: Standard Capacity Card: Version 2.x *
CMD:59 arg:0x0 Response:0x0
init card = 1
*Running SDBlockDevice::_sd_sectors() … *
CMD:9 arg:0x0 Response:0x0
Nuertey Expecting 0xFE here: 0x0
Nuerkie Expecting 0xFE here: 0x26
*Running SDBlockDevice::_read_bytes() … *
*Running SDBlockDevice::_wait_token() … *
_wait_token: timeout
Read timeout
Couldn’t read csd response from disk
CMD9 failed as: “0 == _sectors”
After SDBlockDevice init() method…
Error! SDBlockDevice Init failed: [-4001] → “device specific error”

Hello,

I have also the Nucleo-F767ZI and I never had any issue with SD card. Please be so kind and fill additional info.

  • The logs above are from official example for SD card? Or please share code and settings (also with used SPI pins) what you used.
  • What a tool do you use? - MbedCLI, Online compiler, Mbed Studio, Keil Studio cloud or something else.
  • What a compiler do you use? - GCC_ARM or AC6 (not related to Online compiler and Keil Studio cloud).

BR, Jan

Excellent! It is a great MCU.

Yes, initially I started with the official GitHub - ARMmbed/mbed-os-snippet-USBAudio_wav_audio_player: A mirrored example snippet from mbed-os-examples-docs_only but when it would not work after compilation, I modified it heavily and added further debug commands to even the mbed-os library on a branch. Here is a link to my version if you want to try it out:

https://github.com/nuertey/Nuertey-USB-Audio-Player

And the debug-modified mbed-os library relevant files …

storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h
storage/blockdevice/COMPONENT_SD/source/SDBlockDevice.cpp

… have been uploaded to my general-purpose repo below so that you can add them to your mbed-os library baseline so as to try them out with the exact same codebase as mine:

https://github.com/nuertey/RandomArtifacts/blob/master/SDBlockDevice.h
https://github.com/nuertey/RandomArtifacts/blob/master/SDBlockDevice.cpp

Baseline version of mbed-os is listed in Nuertey-USB-Audio-Player/mbed-os.lib at main · nuertey/Nuertey-USB-Audio-Player · GitHub

Further info from “mbed ls”

[mbed] Working path ".../Workspace/Nuertey-USB-Audio-Player" (library)
[mbed] Program path ".../Workspace/Nuertey-USB-Audio-Player"
Nuertey-USB-Audio-Player (#5cbb39900bb2)
|- AudioPlayer (#82a84021c997)
`- mbed-os (#f0173f382102) [***]

*** This commit hash simply refers to my local changes of mbed-os for the two files above on my local branch:

commit f0173f3821021741bcd9630335e51d8060a56b0a
Author: Nuertey Odzeyem <nuertey_odzeyem@hotmail.com>
Date:   Sat Jul 30 10:59:33 2022 -0500

    Debugging SDCard not working by increasing the init frequency and adding further debug logs.

commit 91b793c4fd1ad7f02b8db5a2e0d77daf63e5c836
Merge: daf6112 e42fa8f
Author: Martin Kojtal <martin.kojtal@arm.com>
Date:   Mon Jul 4 14:55:12 2022 +0100

    Merge pull request #15306 from horeich/dev
    
    Add missing unlock() in file_truncate in FATFileSystem

I am compiling with Mbed CLI 1 and for GCC_ARM like so:

mbed compile -m nucleo_f767zi -t GCC_ARM --profile my_profile.json -c

Settings for my profile are in Nuertey-USB-Audio-Player/my_profile.json at main · nuertey/Nuertey-USB-Audio-Player · GitHub

The pins are also defined in Nuertey-USB-Audio-Player/mbed_app.json at main · nuertey/Nuertey-USB-Audio-Player · GitHub
Note that I am using GPIO PG_1 as my chip select as SPI_A on my MCU setup is already wired up to a different circuitry. I have documented this choice in main.cpp lines 46-111. So I am using SPI_B pins.

Thanks so much for the help.
Nuertey

Ok

  1. I tried code of this example and also the first USBAudio example from Mbed’s documentation - USBAudio - API references and tutorials | Mbed OS 6 Documentation.
    Both were working, now I know I can play a sound over USB.
    I did small modification (modificaion of exmaple form doc) because the original sound was very boring.
    //samples[i] = square_wave(100, 5000, cur_time);// original is boring
    samples[i] = square_wave(50+i, 5000, cur_time);
  1. Then I tried the example what you share as first. The compilation was really not possible because for some strange reason there missing library AudioPlayer. After import this library the example is working.
    I know I can play a wave file song from SD card, now.

  2. I have imported your code, compile with standard Mbed OS6.16 without any modification and your code is working as well too. However, how I replace the original source with your version SDBlockDevice files, then I have same result like you with same SD card.

     // Mbed SDBlockDevice
    Nuertey-USB-Audio-Player Application - Beginning... 
    
    Mbed OS version: 6.16.0
    
    Built: Aug 10 2022, 18:12:57
    
    Calling the SDBlockDevice init() method...
    After SDBlockDevice init() method...
    SDCard size: 4026531840
    SDCard read size: 512
    SDCard program size: 512
    SDCard erase size: 512
    About to commence playing audio file through USB connection...
    Song Completed.
    
    
    Nuertey-USB-Audio-Player - Exiting.

    //-----------------------------------------------//
    // Your SDBlockDevice
    Nuertey-USB-Audio-Player Application - Beginning... 
    
    Mbed OS version: 6.16.0
    
    Built: Aug 10 2022, 18:30:01
    
    Calling the SDBlockDevice init() method...
    Running SDBlockDevice::_initialise_card() ... 
    CMD:0    arg:0x0         Response:0x1
    CMD:8    arg:0x1aa       Response:0x1
    V2-Version Card
    R3/R7: 0x1aa
    CMD:58   arg:0x0         Response:0x1
    R3/R7: 0xc0ff8000
    Indicating to SDCard that we support HC/XC capacity cards in ACMD41...
    CMD:41   arg:0x40000000          Response:0x1
    CMD:41   arg:0x40000000          Response:0x1
    CMD:41   arg:0x40000000          Response:0x0
    Card Initialized: Version 2.x Card
    CMD:58   arg:0x0         Response:0x0
    R3/R7: 0xc0ff8000
    Card Initialized: High Capacity Card 
    CMD:59   arg:0x0         Response:0x0
    init card = 1
    Running SDBlockDevice::_sd_sectors() ... 
    CMD:9    arg:0x0         Response:0x0
    Nuertey Expecting 0xFE here: 0x40
    Nuerkie Expecting 0xFE here: 0xe
    Running SDBlockDevice::_read_bytes() ... 
    Running SDBlockDevice::_wait_token() ... 
    _wait_token: timeout
    Read timeout
    Couldn't read csd response from disk
    CMD9 failed as: "0 == _sectors"
    After SDBlockDevice init() method...
    Error! SDBlockDevice Init failed: [-4001] -> "device specific error"

So I do not see any issue in the official MbedOS release.

Notes:

  • All examples was copy&paseted.
  • Compiled for MbedOS 6.16 (currently latest)
  • Tool was MbedStudio 1.4.4 with AC6 /GCC_ARM
  • Catalex MicroSD card Adapter
  • Intenso Micro SDHC class 10, 4GB from Aliexpress

BR, Jan

I tested another two SD cards

  • Kingston MicroSDHC class10, 16GB from regular shop
  • Kingston MicroSDHC class10, 32GB from regular shop

Your code is also working with both of them.

I my case were all SD cards (3 in total) powered from 5V because my Catalex MicroSD card Adapter has voltage regulator from 5V to 3V3. With 3v3 power supply is still a voltage drop on the regulator and the SD card could not be powered correctly - verified, small one Ok and both bigger not.

Some adapters has pins for both voltages (5V pin through regulator and 3V3 pin directly to SD card), so check your adapters.

BR, Jan

Aha! Excellent catch. So my version of SDBlockDevice files with the added debug prints must be introducing some sorta delay or glitch that messes up with the SD Card Physical Layer Simplified Specification protocol over SPI. Great. The takeaway from your find then is that timing is important to the protocol, and dont mess with the official release :). Great.

I will revert back to the official release and try 1. 2. and 3. as you did and report back with the results.
Thanks so much! You have saved me weeks of debug.
Nuertey

Hi Jan,
So it seems that what I am observing with 2. is that mounting of the FATFileSystem itself fails, and depending on the particular card, fails for a different reason. Here are 3 logs below for that for 3 different cards. I modified the code slightly so as to mount explicitly instead of implicitly during construction and trapped the error log.

I will continue trying other SD Cards. Meanwhile if you have any insights, kindly let me know. FYI, I have settled on powering the SPI Adapter with 5V, so I don’t think we have an issue with an underpowered peripheral.
Thanks Nuertey

SDCard size: 988282880
SDCard read size: 512
SDCard program size: 512
SDCard erase size: 512
Mounting [sd] on ffs drive [0:]
disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
SD Card Mount Fail :frowning:
Error! Mounting SD Card failed: [-5] → I/O error
Opening the root directory… disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
f_opendir() failed: 1
Fail :frowning:

++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x8000613
Error Value: 0x0
Current Thread: main Id: 0x20006A34 Entry: 0x800B405 StackSize: 0x1000 StackMem: 0x20005A10 SP: 0x20006288
For more info, visit: mbedos-error
– MbedOS Error Info –
error: I/O error (-5)



SDCard size: 63900221440
SDCard read size: 512
SDCard program size: 512
SDCard erase size: 512
Mounting [sd] on ffs drive [0:]
disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
disk_read(sector 32768, count 1) on pdrv [0]
SD Card Mount Fail :frowning:
Error! Mounting SD Card failed: [-22] → Invalid argument
Opening the root directory… disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
disk_read(sector 32768, count 1) on pdrv [0]
f_opendir() failed: 13
Fail :frowning:

++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x8000613
Error Value: 0x0
Current Thread: main Id: 0x20006A34 Entry: 0x800B405 StackSize: 0x1000 StackMem: 0x20005A10 SP: 0x20006288
For more info, visit: mbedos-error
– MbedOS Error Info –
error: Invalid argument (-22)



SDCard size: 255869321216
SDCard read size: 512
SDCard program size: 512
SDCard erase size: 512
Mounting [sd] on ffs drive [0:]
disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
disk_read(sector 65536, count 1) on pdrv [0]
SD Card Mount Fail :frowning:
Error! Mounting SD Card failed: [-22] → Invalid argument
Opening the root directory… disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
disk_read(sector 65536, count 1) on pdrv [0]
f_opendir() failed: 13
Fail :frowning:

++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x8000613
Error Value: 0x0
Current Thread: main Id: 0x20006A34 Entry: 0x800B405 StackSize: 0x1000 StackMem: 0x20005A10 SP: 0x20006288
For more info, visit: mbedos-error
– MbedOS Error Info –
error: Invalid argument (-22)

Yeah, it seems like Blockdevice does not have problem with your SD card, but FatFileSystem can not mount it for some reason.
Try format the SD card with recommended Windows app - SD card formatter.

BR, Jan

Hi Jan,
Wanted to let you know yesterday that one card was successful! but could not post as I had already 3 replies until you had responded. Transcend microSD 2GB worked [for my SPI Adapter]! Yay.

So it was my other SD Cards that were somehow the issue. Those were:

SanDisk 1GB microSD
SanDisk Ultra 64GB microSDXC
SanDisk Ultra 256GB microSDXC

Thanks so much. And I am on Linux so cannot use SD card formatter but will keep in mind to reformat those other cards if I have to reuse them in the future.

Here are the success logs below for reference. It might help others in the future for their debugging.
Regards
Nuertey

DEBUG ENABLED

SDCard size: 2008023040
SDCard read size: 512
SDCard program size: 512
SDCard erase size: 512
Mounting [sd] on ffs drive [0:]
disk_initialize on pdrv [0]
disk_ioctl(2)
disk_read(sector 0, count 1) on pdrv [0]
disk_read(sector 255, count 1) on pdrv [0]
SD Card Mount Success :slight_smile:
Opening the root directory… disk_status on pdrv [0]
OK
root directory:
disk_status on pdrv [0]
disk_read(sector 736, count 1) on pdrv [0]
songs
disk_status on pdrv [0]
Closing the root directory… disk_status on pdrv [0]
OK
open(songs/Bach-minuet-in-g.wav) on filesystem [sd], drv [0]
disk_status on pdrv [0]
disk_read(sector 832, count 1) on pdrv [0]
disk_status on pdrv [0]
disk_read(sector 896, count 1) on pdrv [0]
disk_status on pdrv [0]
disk_status on pdrv [0]
disk_status on pdrv [0]
disk_status on pdrv [0]
disk_status on pdrv [0]
disk_status on pdrv [0]
Playing Audio
disk_status on pdrv [0]
disk_read(sector 897, count 1) on pdrv [0]
disk_status on pdrv [0]
disk_read(sector 898, count 1) on pdrv [0]

DEBUG DISABLED

SDCard size: 2008023040
SDCard read size: 512
SDCard program size: 512
SDCard erase size: 512
SD Card Mount Success :slight_smile:
Opening the root directory… OK
root directory:
songs
Closing the root directory… OK
Playing Audio
Song Over

Ok, that is good :slight_smile:

Maybe exist some relevant alternative for Linux

BR, Jan