From b0941a0032f5078f2399bdc49e29e03289199fdc Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 29 Nov 2025 10:45:44 -1000 Subject: [PATCH 1/4] feat(spi): Set power channel on ESP32P4 --- libraries/SPI/src/SPI.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 673301a8e15..3dac9063d30 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -25,6 +25,11 @@ #include "io_pin_remap.h" #include "esp32-hal-log.h" +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL +#include "sd_pwr_ctrl_by_on_chip_ldo.h" +#include "soc/sdmmc_pins.h" +#endif + #if !CONFIG_DISABLE_HAL_LOCKS #define SPI_PARAM_LOCK() \ do { \ @@ -78,6 +83,33 @@ bool SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { return false; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL + bool intersection = false; + int8_t requested[] = {sck, miso, mosi, ss}; + int8_t ldo_ctrld[] = {SDMMC_SLOT0_IOMUX_PIN_NUM_CLK, SDMMC_SLOT0_IOMUX_PIN_NUM_CMD, SDMMC_SLOT0_IOMUX_PIN_NUM_D0, SDMMC_SLOT0_IOMUX_PIN_NUM_D1, SDMMC_SLOT0_IOMUX_PIN_NUM_D2, SDMMC_SLOT0_IOMUX_PIN_NUM_D3}; + for (int i=0; i<4; i++) { + for (int j=0; j<6; j++) { + if (requested[i] == ldo_ctrld[j]) { + intersection = true; + break; + } + } + if (intersection) break; + } + if (intersection) { + sd_pwr_ctrl_ldo_config_t ldo_config; + ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL; + sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL; + sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle); + if (sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300)) { + log_e("Unable to set power control to 3V3"); + return false; + } + pinMode(BOARD_SDMMC_POWER_PIN, OUTPUT); + digitalWrite(BOARD_SDMMC_POWER_PIN, BOARD_SDMMC_POWER_ON_LEVEL); + } +#endif + if (sck == -1 && miso == -1 && mosi == -1 && ss == -1) { #if CONFIG_IDF_TARGET_ESP32 _sck = (_spi_num == VSPI) ? SCK : 14; From 00d939a256219e2b19e14d7b1610e45af4b3e9d1 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 3 Dec 2025 16:07:18 -1000 Subject: [PATCH 2/4] moved changes from SPI.cpp to esp32-hal-spi.c --- cores/esp32/esp32-hal-spi.c | 57 +++++++++++++++++++++++++++++++++++++ libraries/SPI/src/SPI.cpp | 32 --------------------- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index 378ae587858..b7a1b29bb36 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -35,6 +35,11 @@ #include "esp_system.h" #include "esp_intr_alloc.h" +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 +#include "sd_pwr_ctrl_by_on_chip_ldo.h" +#include "soc/sdmmc_pins.h" +#endif + #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "soc/dport_reg.h" #include "esp32/rom/ets_sys.h" @@ -256,6 +261,46 @@ static bool spiDetachBus_SS(void *bus) { return true; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 +void setLDOPower(int8_t pin) { + if (pin<0) { + return; + } + +#ifdef BOARD_SDMMC_POWER_PIN + if (perimanPinIsValid(BOARD_SDMMC_POWER_PIN) && perimanGetPinBusType(BOARD_SDMMC_POWER_PIN)) { + if (strcmp(perimanGetPinBusExtraType(BOARD_SDMMC_POWER_PIN), "SDMMC POWER") == 0) { + return; + } + } +#endif + + int8_t ldo_ctrld[] = { + SDMMC_SLOT0_IOMUX_PIN_NUM_CLK, SDMMC_SLOT0_IOMUX_PIN_NUM_CMD, + SDMMC_SLOT0_IOMUX_PIN_NUM_D0, SDMMC_SLOT0_IOMUX_PIN_NUM_D1, + SDMMC_SLOT0_IOMUX_PIN_NUM_D2, SDMMC_SLOT0_IOMUX_PIN_NUM_D3 + }; + for (int j=0; j<6; j++) { + if (pin == ldo_ctrld[j]) { +#ifdef BOARD_SDMMC_POWER_PIN + pinMode(BOARD_SDMMC_POWER_PIN, OUTPUT); + digitalWrite(BOARD_SDMMC_POWER_PIN, BOARD_SDMMC_POWER_ON_LEVEL); + perimanSetPinBusExtraType(BOARD_SDMMC_POWER_PIN, "SDMMC POWER"); +#endif + sd_pwr_ctrl_ldo_config_t ldo_config; + ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL; + sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL; + sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle); + if (sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300)) { + log_e("Unable to set power control to 3V3"); + return; + } + break; + } + } +} +#endif + bool spiAttachSCK(spi_t *spi, int8_t sck) { if (!spi || sck < 0) { return false; @@ -264,6 +309,9 @@ bool spiAttachSCK(spi_t *spi, int8_t sck) { if (bus != NULL && !perimanClearPinBus(sck)) { return false; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 + setLDOPower(sck); +#endif pinMode(sck, OUTPUT); pinMatrixOutAttach(sck, SPI_CLK_IDX(spi->num), false, false); spi->sck = sck; @@ -283,6 +331,9 @@ bool spiAttachMISO(spi_t *spi, int8_t miso) { if (bus != NULL && !perimanClearPinBus(miso)) { return false; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 + setLDOPower(miso); +#endif pinMode(miso, INPUT); pinMatrixInAttach(miso, SPI_MISO_IDX(spi->num), false); spi->miso = miso; @@ -302,6 +353,9 @@ bool spiAttachMOSI(spi_t *spi, int8_t mosi) { if (bus != NULL && !perimanClearPinBus(mosi)) { return false; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 + setLDOPower(mosi); +#endif pinMode(mosi, OUTPUT); pinMatrixOutAttach(mosi, SPI_MOSI_IDX(spi->num), false, false); spi->mosi = mosi; @@ -360,6 +414,9 @@ bool spiAttachSS(spi_t *spi, uint8_t ss_num, int8_t ss) { if (bus != NULL && !perimanClearPinBus(ss)) { return false; } +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 + setLDOPower(ss); +#endif pinMode(ss, OUTPUT); pinMatrixOutAttach(ss, SPI_SS_IDX(spi->num, ss_num), spi->ss_invert, false); spiEnableSSPins(spi, (1 << ss_num)); diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 3dac9063d30..673301a8e15 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -25,11 +25,6 @@ #include "io_pin_remap.h" #include "esp32-hal-log.h" -#ifdef SOC_SDMMC_IO_POWER_EXTERNAL -#include "sd_pwr_ctrl_by_on_chip_ldo.h" -#include "soc/sdmmc_pins.h" -#endif - #if !CONFIG_DISABLE_HAL_LOCKS #define SPI_PARAM_LOCK() \ do { \ @@ -83,33 +78,6 @@ bool SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { return false; } -#ifdef SOC_SDMMC_IO_POWER_EXTERNAL - bool intersection = false; - int8_t requested[] = {sck, miso, mosi, ss}; - int8_t ldo_ctrld[] = {SDMMC_SLOT0_IOMUX_PIN_NUM_CLK, SDMMC_SLOT0_IOMUX_PIN_NUM_CMD, SDMMC_SLOT0_IOMUX_PIN_NUM_D0, SDMMC_SLOT0_IOMUX_PIN_NUM_D1, SDMMC_SLOT0_IOMUX_PIN_NUM_D2, SDMMC_SLOT0_IOMUX_PIN_NUM_D3}; - for (int i=0; i<4; i++) { - for (int j=0; j<6; j++) { - if (requested[i] == ldo_ctrld[j]) { - intersection = true; - break; - } - } - if (intersection) break; - } - if (intersection) { - sd_pwr_ctrl_ldo_config_t ldo_config; - ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL; - sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL; - sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle); - if (sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300)) { - log_e("Unable to set power control to 3V3"); - return false; - } - pinMode(BOARD_SDMMC_POWER_PIN, OUTPUT); - digitalWrite(BOARD_SDMMC_POWER_PIN, BOARD_SDMMC_POWER_ON_LEVEL); - } -#endif - if (sck == -1 && miso == -1 && mosi == -1 && ss == -1) { #if CONFIG_IDF_TARGET_ESP32 _sck = (_spi_num == VSPI) ? SCK : 14; From 7a150bfba4abb16bea977d15b3688aa10cd9ab12 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Fri, 5 Dec 2025 11:17:16 -1000 Subject: [PATCH 3/4] Check all 10 of the pins on LDO4 --- cores/esp32/esp32-hal-spi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index b7a1b29bb36..64786f46f60 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -278,9 +278,11 @@ void setLDOPower(int8_t pin) { int8_t ldo_ctrld[] = { SDMMC_SLOT0_IOMUX_PIN_NUM_CLK, SDMMC_SLOT0_IOMUX_PIN_NUM_CMD, SDMMC_SLOT0_IOMUX_PIN_NUM_D0, SDMMC_SLOT0_IOMUX_PIN_NUM_D1, - SDMMC_SLOT0_IOMUX_PIN_NUM_D2, SDMMC_SLOT0_IOMUX_PIN_NUM_D3 + SDMMC_SLOT0_IOMUX_PIN_NUM_D2, SDMMC_SLOT0_IOMUX_PIN_NUM_D3, + SDMMC_SLOT0_IOMUX_PIN_NUM_D4, SDMMC_SLOT0_IOMUX_PIN_NUM_D5, + SDMMC_SLOT0_IOMUX_PIN_NUM_D6, SDMMC_SLOT0_IOMUX_PIN_NUM_D7 }; - for (int j=0; j<6; j++) { + for (int j=0; j<10; j++) { if (pin == ldo_ctrld[j]) { #ifdef BOARD_SDMMC_POWER_PIN pinMode(BOARD_SDMMC_POWER_PIN, OUTPUT); From 234df8c0fc956798ab19c096ca879579684f721b Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Mon, 8 Dec 2025 08:43:38 -1000 Subject: [PATCH 4/4] make setLDO function static --- cores/esp32/esp32-hal-spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index 64786f46f60..5f72fe0dd4d 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -262,7 +262,7 @@ static bool spiDetachBus_SS(void *bus) { } #ifdef SOC_SDMMC_IO_POWER_EXTERNAL //ESP32-P4 -void setLDOPower(int8_t pin) { +static void setLDOPower(int8_t pin) { if (pin<0) { return; }