Skip to content

Commit

Permalink
stm32/sdcard: Drop the pyb.SDCard timeout from 60 to 30 seconds.
Browse files Browse the repository at this point in the history
60 seconds is long enough that the USB serial connection drops out before
it times out (at least on my computer).

Also refactor out the timeout argument from sdcard_wait_finished, to try
and save a little code size.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <[email protected]>
  • Loading branch information
projectgus authored and dpgeorge committed Feb 25, 2025
1 parent 731f7ad commit 78728dc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions ports/stm32/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@

static uint8_t pyb_sdmmc_flags;

#define TIMEOUT_MS 30000

// TODO: I think that as an optimization, we can allocate these dynamically
// if an sd card is detected. This will save approx 260 bytes of RAM
// when no sdcard was being used.
Expand Down Expand Up @@ -441,7 +443,7 @@ static void sdcard_reset_periph(void) {
SDIO->ICR = SDMMC_STATIC_FLAGS;
}

static HAL_StatusTypeDef sdcard_wait_finished(uint32_t timeout) {
static HAL_StatusTypeDef sdcard_wait_finished(void) {
// Wait for HAL driver to be ready (eg for DMA to finish)
uint32_t start = HAL_GetTick();
for (;;) {
Expand All @@ -463,7 +465,7 @@ static HAL_StatusTypeDef sdcard_wait_finished(uint32_t timeout) {
}
__WFI();
enable_irq(irq_state);
if (HAL_GetTick() - start >= timeout) {
if (HAL_GetTick() - start >= TIMEOUT_MS) {
return HAL_TIMEOUT;
}
}
Expand All @@ -490,7 +492,7 @@ static HAL_StatusTypeDef sdcard_wait_finished(uint32_t timeout) {
if (!(state == HAL_SD_CARD_SENDING || state == HAL_SD_CARD_RECEIVING || state == HAL_SD_CARD_PROGRAMMING)) {
return HAL_ERROR;
}
if (HAL_GetTick() - start >= timeout) {
if (HAL_GetTick() - start >= TIMEOUT_MS) {
return HAL_TIMEOUT;
}
__WFI();
Expand Down Expand Up @@ -569,7 +571,7 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
err = HAL_SD_ReadBlocks_DMA(&sdmmc_handle.sd, dest, block_num, num_blocks);
}
if (err == HAL_OK) {
err = sdcard_wait_finished(60000);
err = sdcard_wait_finished();
}

#if SDIO_USE_GPDMA
Expand All @@ -588,14 +590,14 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
} else {
#if MICROPY_HW_ENABLE_MMCARD
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
err = HAL_MMC_ReadBlocks(&sdmmc_handle.mmc, dest, block_num, num_blocks, 60000);
err = HAL_MMC_ReadBlocks(&sdmmc_handle.mmc, dest, block_num, num_blocks, TIMEOUT_MS);
} else
#endif
{
err = HAL_SD_ReadBlocks(&sdmmc_handle.sd, dest, block_num, num_blocks, 60000);
err = HAL_SD_ReadBlocks(&sdmmc_handle.sd, dest, block_num, num_blocks, TIMEOUT_MS);
}
if (err == HAL_OK) {
err = sdcard_wait_finished(60000);
err = sdcard_wait_finished();
}
}

Expand Down Expand Up @@ -662,7 +664,7 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
err = HAL_SD_WriteBlocks_DMA(&sdmmc_handle.sd, (uint8_t *)src, block_num, num_blocks);
}
if (err == HAL_OK) {
err = sdcard_wait_finished(60000);
err = sdcard_wait_finished();
}

#if SDIO_USE_GPDMA
Expand All @@ -681,14 +683,14 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
} else {
#if MICROPY_HW_ENABLE_MMCARD
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
err = HAL_MMC_WriteBlocks(&sdmmc_handle.mmc, (uint8_t *)src, block_num, num_blocks, 60000);
err = HAL_MMC_WriteBlocks(&sdmmc_handle.mmc, (uint8_t *)src, block_num, num_blocks, TIMEOUT_MS);
} else
#endif
{
err = HAL_SD_WriteBlocks(&sdmmc_handle.sd, (uint8_t *)src, block_num, num_blocks, 60000);
err = HAL_SD_WriteBlocks(&sdmmc_handle.sd, (uint8_t *)src, block_num, num_blocks, TIMEOUT_MS);
}
if (err == HAL_OK) {
err = sdcard_wait_finished(60000);
err = sdcard_wait_finished();
}
}

Expand Down

0 comments on commit 78728dc

Please sign in to comment.