From e14e1508cbcbd75ddb89b59dc8575bc286616d9e Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Sun, 4 Feb 2018 02:18:46 +0800 Subject: [PATCH] fix(sdmmc_host): fix the issue when slot and host flag are not compatible. --- components/driver/include/driver/sdmmc_host.h | 9 +++++++++ components/driver/include/driver/sdmmc_types.h | 1 + components/driver/sdmmc_host.c | 8 ++++++++ components/sdmmc/sdmmc_cmd.c | 13 ++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/driver/include/driver/sdmmc_host.h b/components/driver/include/driver/sdmmc_host.h index 63023e9030..c298889ed3 100644 --- a/components/driver/include/driver/sdmmc_host.h +++ b/components/driver/include/driver/sdmmc_host.h @@ -39,6 +39,7 @@ extern "C" { .io_voltage = 3.3f, \ .init = &sdmmc_host_init, \ .set_bus_width = &sdmmc_host_set_bus_width, \ + .get_bus_width = &sdmmc_host_get_slot_width, \ .set_card_clk = &sdmmc_host_set_card_clk, \ .do_transaction = &sdmmc_host_do_transaction, \ .deinit = &sdmmc_host_deinit, \ @@ -115,6 +116,14 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config) */ esp_err_t sdmmc_host_set_bus_width(int slot, size_t width); +/** + * @brief Get bus width configured in ``sdmmc_host_init_slot`` to be used for data transfer + * + * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1) + * @return configured bus width of the specified slot. + */ +size_t sdmmc_host_get_slot_width(int slot); + /** * @brief Set card clock frequency * diff --git a/components/driver/include/driver/sdmmc_types.h b/components/driver/include/driver/sdmmc_types.h index 835eaa3fb7..cece4174ef 100644 --- a/components/driver/include/driver/sdmmc_types.h +++ b/components/driver/include/driver/sdmmc_types.h @@ -125,6 +125,7 @@ typedef struct { float io_voltage; /*!< I/O voltage used by the controller (voltage switching is not supported) */ esp_err_t (*init)(void); /*!< Host function to initialize the driver */ esp_err_t (*set_bus_width)(int slot, size_t width); /*!< host function to set bus width */ + size_t (*get_bus_width)(int slot); /*!< host function to get bus width */ esp_err_t (*set_card_clk)(int slot, uint32_t freq_khz); /*!< host function to set card clock frequency */ esp_err_t (*do_transaction)(int slot, sdmmc_command_t* cmdinfo); /*!< host function to do a transaction */ esp_err_t (*deinit)(void); /*!< host function to deinitialize the driver */ diff --git a/components/driver/sdmmc_host.c b/components/driver/sdmmc_host.c index e2a4c94732..4c297a58f7 100644 --- a/components/driver/sdmmc_host.c +++ b/components/driver/sdmmc_host.c @@ -85,6 +85,7 @@ static const char* TAG = "sdmmc_periph"; static intr_handle_t s_intr_handle; static QueueHandle_t s_event_queue; +size_t s_slot_width[2] = {1,1}; void sdmmc_host_reset() { @@ -327,6 +328,7 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config) else if (slot_width > pslot->width) { return ESP_ERR_INVALID_ARG; } + s_slot_width[slot] = slot_width; configure_pin(pslot->clk); configure_pin(pslot->cmd); @@ -428,6 +430,12 @@ esp_err_t sdmmc_host_set_bus_width(int slot, size_t width) return ESP_OK; } +size_t sdmmc_host_get_slot_width(int slot) +{ + assert( slot == 0 || slot == 1 ); + return s_slot_width[slot]; +} + static void sdmmc_host_dma_init() { SDMMC.ctrl.dma_enable = 1; diff --git a/components/sdmmc/sdmmc_cmd.c b/components/sdmmc/sdmmc_cmd.c index a55440225e..594e469767 100644 --- a/components/sdmmc/sdmmc_cmd.c +++ b/components/sdmmc/sdmmc_cmd.c @@ -82,6 +82,17 @@ esp_err_t sdmmc_card_init(const sdmmc_host_t* config, sdmmc_card_t* card) memcpy(&card->host, config, sizeof(*config)); const bool is_spi = host_is_spi(card); + if ( !is_spi ) { + //check HOST flags compatible with slot configuration. + int slot_bit_width = config->get_bus_width(config->slot); + if ( slot_bit_width == 1 && (config->flags & (SDMMC_HOST_FLAG_4BIT|SDMMC_HOST_FLAG_8BIT))) { + ESP_LOGW(TAG, "HOST slot only enables 1-bit."); + card->host.flags = ((card->host.flags&(~(SDMMC_HOST_FLAG_4BIT|SDMMC_HOST_FLAG_8BIT)))|SDMMC_HOST_FLAG_1BIT); + } else if ( slot_bit_width == 4 && (config->flags & SDMMC_HOST_FLAG_8BIT)){ + ESP_LOGW(TAG, "HOST slot only enables 4-bit."); + card->host.flags = ((card->host.flags&(~(SDMMC_HOST_FLAG_1BIT|SDMMC_HOST_FLAG_8BIT)))|SDMMC_HOST_FLAG_4BIT); + } + } /* GO_IDLE_STATE (CMD0) command resets the card */ esp_err_t err = sdmmc_send_cmd_go_idle_state(card); if (err != ESP_OK) { @@ -218,7 +229,7 @@ esp_err_t sdmmc_card_init(const sdmmc_host_t* config, sdmmc_card_t* card) /* If the host has been initialized with 4-bit bus support, and the card * supports 4-bit bus, switch to 4-bit bus now. */ - if ((config->flags & SDMMC_HOST_FLAG_4BIT) && + if ((card->host.flags & SDMMC_HOST_FLAG_4BIT) && (card->scr.bus_width & SCR_SD_BUS_WIDTHS_4BIT)) { ESP_LOGD(TAG, "switching to 4-bit bus mode"); err = sdmmc_send_cmd_set_bus_width(card, 4);