Skip to content

Commit 951c8be

Browse files
authored
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.
1 parent 4413dbb commit 951c8be

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

Diff for: libraries/SPI/examples/SPI_Multiple_Buses/SPI_Multiple_Buses.ino

+10-22
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,24 @@ void setup() {
7676

7777
//set up slave select pins as outputs as the Arduino API
7878
//doesn't handle automatically pulling SS low
79-
pinMode(VSPI_SS, OUTPUT); //VSPI SS
80-
pinMode(HSPI_SS, OUTPUT); //HSPI SS
79+
pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
80+
pinMode(hspi->pinSS(), OUTPUT); //HSPI SS
8181

8282
}
8383

8484
// the loop function runs over and over again until power down or reset
8585
void loop() {
8686
//use the SPI buses
87-
vspiCommand();
88-
hspiCommand();
87+
spiCommand(vspi, 0b01010101); // junk data to illustrate usage
88+
spiCommand(hspi, 0b11001100);
8989
delay(100);
9090
}
9191

92-
void vspiCommand() {
93-
byte data = 0b01010101; // junk data to illustrate usage
94-
92+
void spiCommand(SPIClass *spi, byte data) {
9593
//use it as you would the regular arduino SPI API
96-
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
97-
digitalWrite(VSPI_SS, LOW); //pull SS slow to prep other end for transfer
98-
vspi->transfer(data);
99-
digitalWrite(VSPI_SS, HIGH); //pull ss high to signify end of data transfer
100-
vspi->endTransaction();
101-
}
102-
103-
void hspiCommand() {
104-
byte stuff = 0b11001100;
105-
106-
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
107-
digitalWrite(HSPI_SS, LOW);
108-
hspi->transfer(stuff);
109-
digitalWrite(HSPI_SS, HIGH);
110-
hspi->endTransaction();
94+
spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
95+
digitalWrite(spi->pinSS(), LOW); //pull SS slow to prep other end for transfer
96+
spi->transfer(data);
97+
digitalWrite(spi->pinSS(), HIGH); //pull ss high to signify end of data transfer
98+
spi->endTransaction();
11199
}

Diff for: libraries/SPI/src/SPI.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class SPIClass
8383
void writePattern(const uint8_t * data, uint8_t size, uint32_t repeat);
8484

8585
spi_t * bus(){ return _spi; }
86+
int8_t pinSS() { return _ss; }
8687
};
8788

8889
extern SPIClass SPI;

0 commit comments

Comments
 (0)