Arduino_Core_STM32
Arduino_Core_STM32 copied to clipboard
Add SPI slave mode support
Summary
This PR implements SPI slave mode for the SPI library.
There is an existing PR, #1612, but it is not actively developped and it uses HAL and LL functions from user code.
How to use
There is no change to the API for master mode. Existing code should just work without any change.
This is an exemple of a slave device:
volatile bool rx = false;
void SPI_ISR() {
rx = true;
}
void setup() {
// Add SPI_SLAVE to begin function
SPI.begin(SS, SPI_SLAVE);
// Add an ISR to the SS pin to detect device selection by SPI master
SPI.attachSlaveInterrupt(SS, SPI_ISR);
}
void loop() {
if (rx) {
// Use the various SPI.transfer() functions to read and write data
rx = false;
}
}
Limitation
-
The read and write operations of the slave must be as fast as possible or data will be lost.
-
An SPI device is only slave or master.
begin()andend()can be called with different device mode to allow role switch.
Hi @patricklaf I've rebased and reworked your PR to match latest SPI changes. I guess, It would be fine to add examples.