Skip to content

Commit 00d939a

Browse files
committed
moved changes from SPI.cpp to esp32-hal-spi.c
1 parent b0941a0 commit 00d939a

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

cores/esp32/esp32-hal-spi.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
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+
259304
bool 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));

libraries/SPI/src/SPI.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#include "io_pin_remap.h"
2626
#include "esp32-hal-log.h"
2727

28-
#ifdef SOC_SDMMC_IO_POWER_EXTERNAL
29-
#include "sd_pwr_ctrl_by_on_chip_ldo.h"
30-
#include "soc/sdmmc_pins.h"
31-
#endif
32-
3328
#if !CONFIG_DISABLE_HAL_LOCKS
3429
#define SPI_PARAM_LOCK() \
3530
do { \
@@ -83,33 +78,6 @@ bool SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) {
8378
return false;
8479
}
8580

86-
#ifdef SOC_SDMMC_IO_POWER_EXTERNAL
87-
bool intersection = false;
88-
int8_t requested[] = {sck, miso, mosi, ss};
89-
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};
90-
for (int i=0; i<4; i++) {
91-
for (int j=0; j<6; j++) {
92-
if (requested[i] == ldo_ctrld[j]) {
93-
intersection = true;
94-
break;
95-
}
96-
}
97-
if (intersection) break;
98-
}
99-
if (intersection) {
100-
sd_pwr_ctrl_ldo_config_t ldo_config;
101-
ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL;
102-
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
103-
sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
104-
if (sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300)) {
105-
log_e("Unable to set power control to 3V3");
106-
return false;
107-
}
108-
pinMode(BOARD_SDMMC_POWER_PIN, OUTPUT);
109-
digitalWrite(BOARD_SDMMC_POWER_PIN, BOARD_SDMMC_POWER_ON_LEVEL);
110-
}
111-
#endif
112-
11381
if (sck == -1 && miso == -1 && mosi == -1 && ss == -1) {
11482
#if CONFIG_IDF_TARGET_ESP32
11583
_sck = (_spi_num == VSPI) ? SCK : 14;

0 commit comments

Comments
 (0)