Awful timeout implementation in SDMMC
SDMMC HAL driver implementation is full of timeouts like this:
uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U / 1000U);
do
{
if (count-- == 0U)
{
return SDMMC_ERROR_TIMEOUT;
}
sta_reg = SDMMCx->STA;
} while (SOME_FLAG_NOT_SET);It may work in a single-threaded application on some basic CPU which runs fixed clocks per cycle, but it's not accurate on CPUs with dual-issue pipeline. All preempting interrupts and in case RTOS is used, all higher priority tasks, prolong this timeout. And vice versa - the longer this timeout gets the more it blocks lower priority tasks and interrupts. And the timeout isn't even in microseconds to justify it. SDMMC_CMDTIMEOUT value is 5000 milliseconds (by the comment). It's bad implementation by any standard.
It's been so for a long time in many STM32 series (F4, H7, H7RS, N6, etc.). It's reported at least 6 years ago: I may have encountered a bug in SDMMC_GetCmdResp1 ... - STMicroelectronics Community
I didn't check all the posts in forum, but I bet this implementation has caused problem for a lot of engineers.
How about fixing it? With HAL_GetTick() for start. Those while loops all over HAL checking for HAL_GetTick() aren't good for RTOS use either, but at least the timing would be correct.
Created second post about the general issue: HAL blocking lower priority tasks and interrupts - STMicroelectronics Community
