diff --git a/examples/esp32_sdio_load_ram_example/main/main.c b/examples/esp32_sdio_load_ram_example/main/main.c index a320cba..efd6c67 100644 --- a/examples/esp32_sdio_load_ram_example/main/main.c +++ b/examples/esp32_sdio_load_ram_example/main/main.c @@ -83,6 +83,7 @@ void app_main(void) get_example_ram_app_binary(esp_loader_get_target(), &bin); ESP_LOGI(TAG, "Loading app to RAM ..."); esp_loader_error_t err = load_ram_binary(bin.ram_app.data); + loader_port_esp32_sdio_deinit(); if (err == ESP_LOADER_SUCCESS) { // Forward slave's serial output ESP_LOGI(TAG, "********************************************"); diff --git a/port/esp32_sdio_port.c b/port/esp32_sdio_port.c index cb2bd03..0b317f4 100644 --- a/port/esp32_sdio_port.c +++ b/port/esp32_sdio_port.c @@ -54,6 +54,16 @@ esp_loader_error_t loader_port_esp32_sdio_init(const loader_esp32_sdio_config_t s_card_config.max_freq_khz = config->max_freq_khz; s_card_config.slot = config->slot; +#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE + /* + The buffer and lenght of the data passed to SDMMC Host Driver needs to be aligned. + The new L1 cache requires 64 bytes aligning of a buffer, + this flag ensures that SDMMC Driver allocates custom aligned buffer and memcpy the data to it. + This should be reworked when the new sdmmc driver-ng comes out as it would not need same alignment for size and buffer. + */ + s_card_config.flags = SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF; +#endif + if (!config->dont_initialize_host_driver) { if (sdmmc_host_init() != ESP_OK) { return ESP_LOADER_ERROR_FAIL; @@ -61,7 +71,7 @@ esp_loader_error_t loader_port_esp32_sdio_init(const loader_esp32_sdio_config_t sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); slot_config.width = 4; -#if SOC_SDMMC_USE_GPIO_MATRIX && !SOC_SDMMC_USE_IOMUX +#if SOC_SDMMC_USE_GPIO_MATRIX slot_config.clk = config->sdio_clk_pin; slot_config.cmd = config->sdio_cmd_pin; slot_config.d0 = config->sdio_d0_pin; @@ -92,6 +102,9 @@ esp_loader_error_t loader_port_esp32_sdio_init(const loader_esp32_sdio_config_t void loader_port_esp32_sdio_deinit(void) { if (host_driver_needs_deinit) { +#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE + free(s_card.host.dma_aligned_buffer); +#endif sdmmc_host_deinit(); host_driver_needs_deinit = false; } diff --git a/src/protocol_sdio.c b/src/protocol_sdio.c index 9eb4fa3..47ced19 100644 --- a/src/protocol_sdio.c +++ b/src/protocol_sdio.c @@ -149,7 +149,7 @@ static esp_loader_error_t slave_write_register(const uint32_t addr, uint32_t reg static esp_loader_error_t slave_wait_ready(const uint32_t timeout) { - uint8_t reg = 0; + uint8_t reg __attribute__((aligned(4))) = 0; loader_port_start_timer(timeout); while ((reg & SD_IO_CCR_FN_ENABLE_FUNC1_EN) == 0) { @@ -251,14 +251,18 @@ static esp_loader_error_t slave_init_link(void) esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args) { + esp_loader_error_t err = ESP_LOADER_ERROR_FAIL; for (uint8_t trial = 0; trial < connect_args->trials; trial++) { - - if (loader_port_sdio_card_init() != ESP_LOADER_SUCCESS) { - loader_port_debug_print("Retrying card connection..."); - loader_port_delay_ms(100); - } else { + err = loader_port_sdio_card_init(); + if (err == ESP_LOADER_SUCCESS) { break; } + loader_port_debug_print("Retrying card connection..."); + loader_port_delay_ms(100); + } + + if (err != ESP_LOADER_SUCCESS) { + return err; } RETURN_ON_ERROR(slave_init_io());