From b461e01e2214f4fbae62ee5e601609e75d48feb5 Mon Sep 17 00:00:00 2001 From: UltimumControl <70729135+UltimumControl@users.noreply.github.com> Date: Mon, 28 Apr 2025 13:45:31 -0400 Subject: [PATCH] New SPI invert hardware SS function in hall-spi and SPI library (#11297) * Add files via upload * Add files via upload * Update SPI.h * Update esp32-hal-spi.c renamed invert_out to ss_invert to be more intuitive * Update esp32-hal-spi.h Removed the out from the function name spiSSInvertout. * Update SPI.cpp Removed the out from the function name spiSSInvertout. * Update cores/esp32/esp32-hal-spi.c Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/esp32-hal-spi.c | 9 ++++++++- cores/esp32/esp32-hal-spi.h | 2 ++ libraries/SPI/src/SPI.cpp | 6 ++++++ libraries/SPI/src/SPI.h | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index 809283096..9ae10c661 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -74,6 +74,7 @@ struct spi_struct_t { int8_t miso; int8_t mosi; int8_t ss; + bool ss_invert; }; #if CONFIG_IDF_TARGET_ESP32S2 @@ -365,7 +366,7 @@ bool spiAttachSS(spi_t *spi, uint8_t ss_num, int8_t ss) { return false; } pinMode(ss, OUTPUT); - pinMatrixOutAttach(ss, SPI_SS_IDX(spi->num, ss_num), false, false); + pinMatrixOutAttach(ss, SPI_SS_IDX(spi->num, ss_num), spi->ss_invert, false); spiEnableSSPins(spi, (1 << ss_num)); spi->ss = ss; if (!perimanSetPinBus(ss, ESP32_BUS_TYPE_SPI_MASTER_SS, (void *)(spi->num + 1), spi->num, -1)) { @@ -435,6 +436,12 @@ void spiSSDisable(spi_t *spi) { SPI_MUTEX_UNLOCK(); } +void spiSSInvert(spi_t *spi, bool invert) { + if (spi) { + spi->ss_invert = invert; + } +} + void spiSSSet(spi_t *spi) { if (!spi) { return; diff --git a/cores/esp32/esp32-hal-spi.h b/cores/esp32/esp32-hal-spi.h index b77abff78..7d56f0820 100644 --- a/cores/esp32/esp32-hal-spi.h +++ b/cores/esp32/esp32-hal-spi.h @@ -97,6 +97,8 @@ void spiSSSet(spi_t *spi); void spiSSClear(spi_t *spi); void spiWaitReady(spi_t *spi); +//invert hardware SS +void spiSSInvert(spi_t *spi, bool invert); uint32_t spiGetClockDiv(spi_t *spi); uint8_t spiGetDataMode(spi_t *spi); diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 35e52f43e..ae207a7ff 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -144,6 +144,12 @@ void SPIClass::setHwCs(bool use) { _use_hw_ss = use; } +void SPIClass::setSSInvert(bool invert) { + if (_spi) { + spiSSInvert(_spi, invert); + } +} + void SPIClass::setFrequency(uint32_t freq) { SPI_PARAM_LOCK(); //check if last freq changed diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index adb3d1bc1..628c2190f 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -65,6 +65,7 @@ public: void end(); void setHwCs(bool use); + void setSSInvert(bool invert); //use before setHwCS for change to be used by setHwCs void setBitOrder(uint8_t bitOrder); void setDataMode(uint8_t dataMode); void setFrequency(uint32_t freq);