Search code examples
embeddedstm32sd-cardspi

Intermittent behavior on SD Card initialization on STM32 board


I'm prototyping an application using a STM32F407 board that contains a SD Card slot, as depicted below:

SD Card Pic

The communication is set to be 1 Bit, following the SDIO specification.

The fact is that when I run the "HAL_SD_Init" function from HAL library during the board startup routine, sometimes it works perfectly and my application runs fine (read, write, all operations OK), but sometimes not and it seems totally random and intermittent.

I even surrounded the HAL_SD_Init method with a while statement (for testing purpose only) in a way that the board just continues the startup routine if the SD Initialization was succesful and, in the meanwhile, I keep toggling the card on the slot, inserting and removing it until it magically works and the code continues its execution.

while(HAL_SD_Init(&hsd) != HAL_OK);

Since the code seems fine, I connected the logic analyzer and tried to analyze an OK and a NOK case in the physical domain:

  • OK CASE:

    Main remarks:

    1. Command sequence in Command line (CMD) seems OK - CMD0 sent, CMD8 sent, CMD8 answered as expected, and then everything proceeds normally.
    2. The Data Line (D0) is maintained in a high-level, idling, as expected for this phase.

OK CASE

  • NOK CASE:

    Main remarks:

    1. Command sequence in Command line (CMD) seems OK but unanswered - CMD0 sent, CMD8 sent, but NO answer in command line.
    2. On the other hand, the Data Line (D0) is not idling, but seems to be communicating something (I didn't find anything about this in the SDIO spec).

enter image description here

Final thoughts and observations:

  • Once a NOK behavior is ongoing, it's not gonna work if I don't try to remove and reinsert the card physically, i.e., I can keep polling the card infinitely or even turn off the board, wait a few moments and turn it on again. Simply won't work.
  • I tried changing the SD CLK to higher and lower values during the initialization phase, but no effect. It continues somewhat random and intermittent.
  • I can't figure out what the SD card is trying to return when it doesn't answer my command in the CMD line but does answer something in the D0 line. If contacts were loose, I wouldn't expect the card to return anything.
  • The contacts doesn't seem loose or misaligned (visual analysis). I tried to insert it more to the left, more to the right, applying a down-up and up-down pressure, but no pattern detected. So far, no clue.
  • This card works fine when inserted into a Micro SD adapter and plugged into my PC.

Let me know if there are any questions.


Solution

  • After abandoning the project for a while and redoing the electrical architecture of my board, I found the issue.

    Part of the code for the SDIO / FATFS was being auto-generated by the STM32CubeIDE (handy tool, but not bug free).

    When setting up the FATFS, it was requiring me to use a "Card Detected" pin. For my board, this pin collides with the SDIO D3 pin. So, I was forcing one of the SD Card data pins to ground (although operaitng it in 1 bit). Thus, I was getting intermittent behavior.

    I removed this part of the code + removed totally this card detected pin, subtituting it by a software logic and leaaving the GPIO uninitialized. Now, it works like a charm!