Skip to content

New SPI invert hardware SS function in hall-spi and SPI library #11297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cores/esp32/esp32-hal-spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct spi_struct_t {
int8_t miso;
int8_t mosi;
int8_t ss;
bool invert_out;
Copy link
Collaborator

@lucasssvaz lucasssvaz Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to ss_invert or invert_ss to be more intuitive

};

#if CONFIG_IDF_TARGET_ESP32S2
Expand Down Expand Up @@ -356,6 +357,8 @@ bool spiDetachMOSI(spi_t *spi) {
return true;
}



bool spiAttachSS(spi_t *spi, uint8_t ss_num, int8_t ss) {
if (!spi || ss < 0 || ss_num > 2) {
return false;
Expand All @@ -365,7 +368,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->invert_out, 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)) {
Expand Down Expand Up @@ -435,6 +438,12 @@ void spiSSDisable(spi_t *spi) {
SPI_MUTEX_UNLOCK();
}

void spiSSInvertout(spi_t *spi, bool invert) {
if (spi) {
spi->invert_out = invert;
}
}

void spiSSSet(spi_t *spi) {
if (!spi) {
return;
Expand Down
2 changes: 2 additions & 0 deletions cores/esp32/esp32-hal-spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void spiSSSet(spi_t *spi);
void spiSSClear(spi_t *spi);

void spiWaitReady(spi_t *spi);
//invert hardware SS
void spiSSInvertout(spi_t *spi, bool invert);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the out of the function name. This will make it easier to read.


uint32_t spiGetClockDiv(spi_t *spi);
uint8_t spiGetDataMode(spi_t *spi);
Expand Down
6 changes: 6 additions & 0 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ void SPIClass::setHwCs(bool use) {
_use_hw_ss = use;
}

void SPIClass::setSSInvert(bool invert) {
if (_spi) {
spiSSInvertout(_spi, invert);
}
}

void SPIClass::setFrequency(uint32_t freq) {
SPI_PARAM_LOCK();
//check if last freq changed
Expand Down
3 changes: 2 additions & 1 deletion libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class SPIClass {
~SPIClass();
void begin(int8_t sck = -1, int8_t miso = -1, int8_t mosi = -1, int8_t ss = -1);
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);
Expand Down