3535#include "esp_system.h"
3636#include "esp_intr_alloc.h"
3737
38+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
39+ #include "sd_pwr_ctrl_by_on_chip_ldo.h"
40+ #include "soc/sdmmc_pins.h"
41+ #endif
42+
3843#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
3944#include "soc/dport_reg.h"
4045#include "esp32/rom/ets_sys.h"
@@ -256,6 +261,46 @@ static bool spiDetachBus_SS(void *bus) {
256261 return true;
257262}
258263
264+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
265+ void setLDOPower (int8_t pin ) {
266+ if (pin < 0 ) {
267+ return ;
268+ }
269+
270+ #ifdef BOARD_SDMMC_POWER_PIN
271+ if (perimanPinIsValid (BOARD_SDMMC_POWER_PIN ) && perimanGetPinBusType (BOARD_SDMMC_POWER_PIN )) {
272+ if (strcmp (perimanGetPinBusExtraType (BOARD_SDMMC_POWER_PIN ), "SDMMC POWER" ) == 0 ) {
273+ return ;
274+ }
275+ }
276+ #endif
277+
278+ int8_t ldo_ctrld [] = {
279+ SDMMC_SLOT0_IOMUX_PIN_NUM_CLK , SDMMC_SLOT0_IOMUX_PIN_NUM_CMD ,
280+ SDMMC_SLOT0_IOMUX_PIN_NUM_D0 , SDMMC_SLOT0_IOMUX_PIN_NUM_D1 ,
281+ SDMMC_SLOT0_IOMUX_PIN_NUM_D2 , SDMMC_SLOT0_IOMUX_PIN_NUM_D3
282+ };
283+ for (int j = 0 ; j < 6 ; j ++ ) {
284+ if (pin == ldo_ctrld [j ]) {
285+ #ifdef BOARD_SDMMC_POWER_PIN
286+ pinMode (BOARD_SDMMC_POWER_PIN , OUTPUT );
287+ digitalWrite (BOARD_SDMMC_POWER_PIN , BOARD_SDMMC_POWER_ON_LEVEL );
288+ perimanSetPinBusExtraType (BOARD_SDMMC_POWER_PIN , "SDMMC POWER" );
289+ #endif
290+ sd_pwr_ctrl_ldo_config_t ldo_config ;
291+ ldo_config .ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL ;
292+ sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL ;
293+ sd_pwr_ctrl_new_on_chip_ldo (& ldo_config , & pwr_ctrl_handle );
294+ if (sd_pwr_ctrl_set_io_voltage (pwr_ctrl_handle , 3300 )) {
295+ log_e ("Unable to set power control to 3V3" );
296+ return ;
297+ }
298+ break ;
299+ }
300+ }
301+ }
302+ #endif
303+
259304bool spiAttachSCK (spi_t * spi , int8_t sck ) {
260305 if (!spi || sck < 0 ) {
261306 return false;
@@ -264,6 +309,9 @@ bool spiAttachSCK(spi_t *spi, int8_t sck) {
264309 if (bus != NULL && !perimanClearPinBus (sck )) {
265310 return false;
266311 }
312+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
313+ setLDOPower (sck );
314+ #endif
267315 pinMode (sck , OUTPUT );
268316 pinMatrixOutAttach (sck , SPI_CLK_IDX (spi -> num ), false, false);
269317 spi -> sck = sck ;
@@ -283,6 +331,9 @@ bool spiAttachMISO(spi_t *spi, int8_t miso) {
283331 if (bus != NULL && !perimanClearPinBus (miso )) {
284332 return false;
285333 }
334+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
335+ setLDOPower (miso );
336+ #endif
286337 pinMode (miso , INPUT );
287338 pinMatrixInAttach (miso , SPI_MISO_IDX (spi -> num ), false);
288339 spi -> miso = miso ;
@@ -302,6 +353,9 @@ bool spiAttachMOSI(spi_t *spi, int8_t mosi) {
302353 if (bus != NULL && !perimanClearPinBus (mosi )) {
303354 return false;
304355 }
356+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
357+ setLDOPower (mosi );
358+ #endif
305359 pinMode (mosi , OUTPUT );
306360 pinMatrixOutAttach (mosi , SPI_MOSI_IDX (spi -> num ), false, false);
307361 spi -> mosi = mosi ;
@@ -360,6 +414,9 @@ bool spiAttachSS(spi_t *spi, uint8_t ss_num, int8_t ss) {
360414 if (bus != NULL && !perimanClearPinBus (ss )) {
361415 return false;
362416 }
417+ #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4
418+ setLDOPower (ss );
419+ #endif
363420 pinMode (ss , OUTPUT );
364421 pinMatrixOutAttach (ss , SPI_SS_IDX (spi -> num , ss_num ), spi -> ss_invert , false);
365422 spiEnableSSPins (spi , (1 << ss_num ));
0 commit comments