Skip to content

Commit

Permalink
Merge branch 'fix/sdio_esp32p4_as_host' into 'master'
Browse files Browse the repository at this point in the history
fix: INVALID_ARG error when ESP32P4 used as a host

Closes ESF-199

See merge request espressif/esp-serial-flasher!135
  • Loading branch information
dobairoland committed Jan 14, 2025
2 parents c7b8433 + b42d1d8 commit 76978c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/esp32_sdio_load_ram_example/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "********************************************");
Expand Down
15 changes: 14 additions & 1 deletion port/esp32_sdio_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ 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;
}

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;
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions src/protocol_sdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 76978c2

Please sign in to comment.