I'm prototyping an application using a STM32F407 board that contains a SD Card slot, as depicted below:
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:
NOK CASE:
Main remarks:
Final thoughts and observations:
Let me know if there are any questions.
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!