From 32f60da3302378f7e663027b85d822894eaa77aa Mon Sep 17 00:00:00 2001 From: Roman Leonov Date: Wed, 11 Sep 2024 11:26:54 +0200 Subject: [PATCH] fix(tusb_msc): Fixed sdmmc init on ESP32P4 --- .../device/tusb_msc/main/Kconfig.projbuild | 17 +++++++++++++ .../usb/device/tusb_msc/main/tusb_msc_main.c | 25 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/examples/peripherals/usb/device/tusb_msc/main/Kconfig.projbuild b/examples/peripherals/usb/device/tusb_msc/main/Kconfig.projbuild index ea7997fb49..a43bb71aeb 100644 --- a/examples/peripherals/usb/device/tusb_msc/main/Kconfig.projbuild +++ b/examples/peripherals/usb/device/tusb_msc/main/Kconfig.projbuild @@ -78,6 +78,23 @@ menu "TinyUSB MSC Example Configuration" endif # SOC_SDMMC_USE_GPIO_MATRIX + config EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO + depends on SOC_SDMMC_IO_POWER_EXTERNAL + bool "SDMMC powered by internal LDO" + default y + help + Enable when internal LDO of ESP32-P4 is used as a power supply for SD card. Specify the LDO IO. + Disable when external power supply is used as a power supply for SD card. + + config EXAMPLE_SD_PWR_CTRL_LDO_IO_ID + depends on SOC_SDMMC_IO_POWER_EXTERNAL && EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO + int "LDO IO" + default 4 if IDF_TARGET_ESP32P4 + help + There are several IOs in ESP32-P4 which are in "SD card IO" power domain. + Power has to be supplied to this power domain externally (from outside the chip) via one of the pins. + Based on the schematic, specify the LDO IO pin. + endif # EXAMPLE_STORAGE_MEDIA_SDMMC endmenu diff --git a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c index 3f9c954ed9..c40b04e1f1 100644 --- a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c +++ b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c @@ -14,6 +14,7 @@ #include #include #include +#include "sdkconfig.h" #include "esp_console.h" #include "esp_check.h" #include "esp_partition.h" @@ -23,6 +24,9 @@ #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SDMMC #include "diskio_impl.h" #include "diskio_sdmmc.h" +#if CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO +#include "sd_pwr_ctrl_by_on_chip_ldo.h" +#endif // CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO #endif /* @@ -307,6 +311,23 @@ static esp_err_t storage_init_sdmmc(sdmmc_card_t **card) // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000; sdmmc_host_t host = SDMMC_HOST_DEFAULT(); + // For SoCs where the SD power can be supplied both via an internal or external (e.g. on-board LDO) power supply. + // When using specific IO pins (which can be used for ultra high-speed SDMMC) to connect to the SD card + // and the internal LDO power supply, we need to initialize the power supply first. +#if CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO + sd_pwr_ctrl_ldo_config_t ldo_config = { + .ldo_chan_id = CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_IO_ID, + }; + sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL; + + ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to create a new on-chip LDO power control driver"); + return ret; + } + host.pwr_ctrl_handle = pwr_ctrl_handle; +#endif + // This initializes the slot without card detect (CD) and write protect (WP) signals. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); @@ -369,6 +390,10 @@ clean: free(sd_card); sd_card = NULL; } +#if CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO + // We don't need to duplicate error here as all error messages are handled via sd_pwr_* call + sd_pwr_ctrl_del_on_chip_ldo(pwr_ctrl_handle); +#endif // CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_INTERNAL_IO return ret; } #endif // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH