stm32/sdcard: Support configuring the SD/MMC bus width to 1 or 4 bits.

Some SD/MMC breakout boards don't support 4-bit bus mode.  This adds a new
macro MICROPY_HW_SDMMC_BUS_WIDTH that allows each board to define the width
of the SD/MMC bus interface used on that board, defaulting to 4 bits.
This commit is contained in:
Chris Wilson 2019-08-01 19:54:32 -07:00 committed by Damien George
parent efdcd6baa7
commit 3d02ebb4e8
2 changed files with 17 additions and 4 deletions

View File

@ -102,6 +102,11 @@
#define MICROPY_HW_ENABLE_MMCARD (0)
#endif
// SD/MMC interface bus width (defaults to 4 bits)
#ifndef MICROPY_HW_SDMMC_BUS_WIDTH
#define MICROPY_HW_SDMMC_BUS_WIDTH (4)
#endif
// Whether to automatically mount (and boot from) the SD card if it's present
#ifndef MICROPY_HW_SDCARD_MOUNT_AT_BOOT
#define MICROPY_HW_SDCARD_MOUNT_AT_BOOT (MICROPY_HW_ENABLE_SDCARD)

View File

@ -156,17 +156,21 @@ void sdcard_init(void) {
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_CK);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_CMD);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D0);
#if MICROPY_HW_SDMMC_BUS_WIDTH == 4
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D1);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D2);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC2_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC2_D3);
#endif
#else
// Default SDIO/SDMMC1 config
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CK);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CMD);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D0);
#if MICROPY_HW_SDMMC_BUS_WIDTH == 4
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D1);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D2);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_D3);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CK);
mp_hal_pin_config_alt_static(MICROPY_HW_SDMMC_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDMMC_CMD);
#endif
#endif
// configure the SD card detect pin
@ -252,12 +256,14 @@ STATIC HAL_StatusTypeDef sdmmc_init_sd(void) {
mp_hal_delay_ms(50);
}
// configure the SD bus width for wide operation
#if MICROPY_HW_SDMMC_BUS_WIDTH == 4
// configure the SD bus width for 4-bit wide operation
status = HAL_SD_ConfigWideBusOperation(&sdmmc_handle.sd, SDIO_BUS_WIDE_4B);
if (status != HAL_OK) {
HAL_SD_DeInit(&sdmmc_handle.sd);
return status;
}
#endif
return HAL_OK;
}
@ -285,7 +291,8 @@ STATIC HAL_StatusTypeDef sdmmc_init_mmc(void) {
// As this is an eMMC card, overwrite LogBlockNbr with actual value
sdmmc_handle.mmc.MmcCard.LogBlockNbr = 7469056 + 2048;
// Configure the SDIO bus width for wide operation
#if MICROPY_HW_SDMMC_BUS_WIDTH == 4
// Configure the SDIO bus width for 4-bit wide operation
#ifdef STM32F7
sdmmc_handle.mmc.Init.ClockBypass = SDIO_CLOCK_BYPASS_ENABLE;
#endif
@ -294,6 +301,7 @@ STATIC HAL_StatusTypeDef sdmmc_init_mmc(void) {
HAL_MMC_DeInit(&sdmmc_handle.mmc);
return status;
}
#endif
return HAL_OK;
}