Skip to content
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

Add SPI slave mode support #2050

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

patricklaf
Copy link
Contributor

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.

@fpistm fpistm self-requested a review August 30, 2023 15:09
@fpistm fpistm added the enhancement New feature or request label Aug 30, 2023
Signed-off-by: patricklaf <[email protected]>
Co-authored-by: Frederic Pillon <[email protected]>
@fpistm
Copy link
Member

fpistm commented Dec 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

2 participants