Arduino_Core_STM32 icon indicating copy to clipboard operation
Arduino_Core_STM32 copied to clipboard

Add SPI slave mode support

Open patricklaf opened this issue 2 years ago • 1 comments

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() and end() can be called with different device mode to allow role switch.

patricklaf avatar Jun 29 '23 14:06 patricklaf

Hi @patricklaf I've rebased and reworked your PR to match latest SPI changes. I guess, It would be fine to add examples.

fpistm avatar Dec 06 '23 09:12 fpistm