libraries/SPI/src/SPI.h: SPIClass: add method to get SS pin number (#5788)

* SPI.h add new call to return a SS pin number used.

As code example states, the SS pin needs to be explicitly set for output for SPI to work, but the pin number have to be coded in addition to the SPI logic in the library, which means this duplicates code. It is much better to just be able to get the pin number from library itself.

* Update SPI_Multiple_Buses.ino to use new pinSS method

Simplify the example case, to show usage of pinSS method. This also simplifies the example, removing duplicated code.
This commit is contained in:
michlv
2021-10-21 14:48:55 +01:00
committed by GitHub
parent 4413dbbd87
commit 951c8bece5
2 changed files with 11 additions and 22 deletions

View File

@ -76,36 +76,24 @@ void setup() {
//set up slave select pins as outputs as the Arduino API //set up slave select pins as outputs as the Arduino API
//doesn't handle automatically pulling SS low //doesn't handle automatically pulling SS low
pinMode(VSPI_SS, OUTPUT); //VSPI SS pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
pinMode(HSPI_SS, OUTPUT); //HSPI SS pinMode(hspi->pinSS(), OUTPUT); //HSPI SS
} }
// the loop function runs over and over again until power down or reset // the loop function runs over and over again until power down or reset
void loop() { void loop() {
//use the SPI buses //use the SPI buses
vspiCommand(); spiCommand(vspi, 0b01010101); // junk data to illustrate usage
hspiCommand(); spiCommand(hspi, 0b11001100);
delay(100); delay(100);
} }
void vspiCommand() { void spiCommand(SPIClass *spi, byte data) {
byte data = 0b01010101; // junk data to illustrate usage
//use it as you would the regular arduino SPI API //use it as you would the regular arduino SPI API
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0)); spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(VSPI_SS, LOW); //pull SS slow to prep other end for transfer digitalWrite(spi->pinSS(), LOW); //pull SS slow to prep other end for transfer
vspi->transfer(data); spi->transfer(data);
digitalWrite(VSPI_SS, HIGH); //pull ss high to signify end of data transfer digitalWrite(spi->pinSS(), HIGH); //pull ss high to signify end of data transfer
vspi->endTransaction(); spi->endTransaction();
}
void hspiCommand() {
byte stuff = 0b11001100;
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
hspi->transfer(stuff);
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
} }

View File

@ -83,6 +83,7 @@ public:
void writePattern(const uint8_t * data, uint8_t size, uint32_t repeat); void writePattern(const uint8_t * data, uint8_t size, uint32_t repeat);
spi_t * bus(){ return _spi; } spi_t * bus(){ return _spi; }
int8_t pinSS() { return _ss; }
}; };
extern SPIClass SPI; extern SPIClass SPI;