Skip to main content
Associate II
August 6, 2026
Solved

SDIO Gets stuck

  • August 6, 2026
  • 8 replies
  • 148 views

hi 

I am trying to get an sd card to work with my custom board, uses STM32F412RET6.

when i try to mount or do anything other than init in 1 bit mode the code gets stuck at this point. I have been able to read block size and number from the SD card but that is it.

 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT))
{
if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
{
*(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
index++;
}
else if(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXACT))
{
break;
}

if((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
{
return HAL_SD_ERROR_TIMEOUT;
}
}

 

Best answer by AScha.3

Hi,

so you just have the command - communication, not the data in-out on D0 .

Typical a hardware problem, to long wires cpu to sd-card, or bad setting of pin speed.

  • wires: keep all lines to sd-card at about same length and  shorter than 50mm 
  • set all cpu pins to sd-card to medium speed + pullups ON 
  • set the sd clock to < 50 MHz (...clock divide..), try at 20MHz and 1-bit mode
  • set using no DMA , hardw.control.flow ON, give sdmmc a good priority interrupt (and enable it ! in NVIC)
  • try only mount (…, 1) = mount NOW , or a read of file;  NO write or format !!!

 

8 replies

TDK
August 6, 2026

The SDIO peripheral has race conditions where data needs read out quickly enough otherwise it overflows. Use a higher clock prescaler to see if that solves the problem. If it does, the solution would be to use DMA or rewrite code to be faster. Using Release mode can be a bandaid.

"If you feel a post has answered your question, please click ""Accept as Solution""."
drJonesyAuthor
Associate II
August 6, 2026

DMA is enabled and prescaler is set to 250

 

ST Technical Moderator
August 10, 2026

Hello ​@drJonesy 

The execution is not expected to stay stuck in this loop indefinitely, because the code contains an internal timeout protection.
If the SDIO transfer does not progress and none of the expected status flags are detected, the timeout condition is reached:

if((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
{
return HAL_SD_ERROR_TIMEOUT;
}

 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
AScha.3
Super User
August 10, 2026

Right, ​@Saket_Om  , and the timeout is : ?

 

afaik is on : SDMMC_ERROR_TIMEOUT ((uint32_t)0x80000000U) /*!< Timeout error ,

so at systick 1ms , → 24.8 days . 

But ok, just wait a little...

If you feel a post has answered your question, please click on " Best Answer ".
TDK
August 10, 2026

It’s actually hilariously worse than that.

https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/1f6451c3e07728b4c830744de380e56bf5bc0026/Inc/stm32f4xx_ll_sdmmc.h#L308

#ifndef SDMMC_DATATIMEOUT /*Hardware Data Timeout (ms) */
#define SDMMC_DATATIMEOUT ((uint32_t)0xFFFFFFFFU)
#endif /* SDMMC_DATATIMEOUT */

#ifndef SDMMC_SWDATATIMEOUT /*Software Data Timeout (ms) */
#define SDMMC_SWDATATIMEOUT SDMMC_DATATIMEOUT
#endif /* SDMMC_SWDATATIMEOUT */

So yes you can change it, but by default it’s 49.7 days. 🤣 And if your program is busy during that particular 1ms, you’ll have to wait another 49.7 days for the timeout to trigger. And if you’re not using the default 1ms tick, but 10ms or 100ms instead, it will never timeout.

"If you feel a post has answered your question, please click ""Accept as Solution""."
AScha.3
AScha.3Best answer
Super User
August 10, 2026

Hi,

so you just have the command - communication, not the data in-out on D0 .

Typical a hardware problem, to long wires cpu to sd-card, or bad setting of pin speed.

  • wires: keep all lines to sd-card at about same length and  shorter than 50mm 
  • set all cpu pins to sd-card to medium speed + pullups ON 
  • set the sd clock to < 50 MHz (...clock divide..), try at 20MHz and 1-bit mode
  • set using no DMA , hardw.control.flow ON, give sdmmc a good priority interrupt (and enable it ! in NVIC)
  • try only mount (…, 1) = mount NOW , or a read of file;  NO write or format !!!

 

If you feel a post has answered your question, please click on " Best Answer ".
drJonesyAuthor
Associate II
August 10, 2026

Hi 

Thanks for the heads up about what the problem may be. I have set GPIO to medium speed and pull up but that doesn’t help.  I will focus on the circuit design and check for wire lengths. 

 

Just as I started writing this, I checked my circuit, for Errors and low and behold. The problem was on the eclamp pin 16. it is set dat0 in the schematic but Dat1 in data sheet. Very disappointed in myself for not checking.

 

Thanks for the heads up. I will modify my circuit and see if i get a fix.

drJonesyAuthor
Associate II
August 10, 2026

Hi All

 

I am now reading the SD CARD. Problem as above. Thank you AScha.3 for your help with this issue. You can now close this issue.