Skip to content

Commit

Permalink
Fix SDMMC timeout HAL bug
Browse files Browse the repository at this point in the history
  • Loading branch information
danngreen committed Jul 12, 2024
1 parent 671e3a6 commit 00f3236
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 8 additions & 2 deletions firmware/lib/stm32-hal/stm32mp1/include/stm32mp1xx_ll_sdmmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,14 @@ typedef struct
#define SDMMC_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000U)
#define SDMMC_CARD_LOCKED ((uint32_t)0x02000000U)

//#define SDMMC_DATATIMEOUT ((uint32_t)0xFFFFFFFFU)
#define SDMMC_DATATIMEOUT ((uint32_t)5000U) // 5 seconds
///////////////////
// See https://github.com/STMicroelectronics/STM32CubeF7/issues/20
//
// SDMMC_DATATIMEOUT: units is clock bus periods: max time DPSM can be in Wait_R or Busy before timeout status flag is set
#define SDMMC_DATATIMEOUT ((uint32_t)0x09300000) // about 200ms
// SDMMC_DATATIMEOUT_TICKS: units is ticks (1ms by default)
#define SDMMC_DATATIMEOUT_TICKS ((uint32_t)1000U) // 1 second
///////////////////

#define SDMMC_0TO7BITS ((uint32_t)0x000000FFU)
#define SDMMC_8TO15BITS ((uint32_t)0x0000FF00U)
Expand Down
16 changes: 8 additions & 8 deletions firmware/lib/stm32-hal/stm32mp1/src/stm32mp1xx_hal_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
tickstart = HAL_GetTick();
while((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
{
if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
hsd->State= HAL_SD_STATE_READY;
Expand Down Expand Up @@ -2724,7 +2724,7 @@ static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
/* Check to CKSTOP */
while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
{
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
return HAL_SD_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -2754,7 +2754,7 @@ static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
/* Check VSWEND Flag */
while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
{
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
return HAL_SD_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -2864,7 +2864,7 @@ static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
}
}

if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
return HAL_SD_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -2892,7 +2892,7 @@ static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
*pData = SDMMC_ReadFIFO(hsd->Instance);
pData++;

if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
return HAL_SD_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -3082,7 +3082,7 @@ static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
}


if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT_TICKS)
{
return HAL_SD_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -3239,7 +3239,7 @@ uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd)
loop ++;
}

if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT_TICKS)
{
hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
hsd->State= HAL_SD_STATE_READY;
Expand Down Expand Up @@ -3354,7 +3354,7 @@ uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd)
loop ++;
}

if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT_TICKS)
{
hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
hsd->State= HAL_SD_STATE_READY;
Expand Down

0 comments on commit 00f3236

Please sign in to comment.