Skip to content

Add support register-based SPI slave - #3040

Open
philrittenhouse wants to merge 1 commit into
stm32duino:mainfrom
philrittenhouse:main
Open

Add support register-based SPI slave#3040
philrittenhouse wants to merge 1 commit into
stm32duino:mainfrom
philrittenhouse:main

Conversation

@philrittenhouse

Copy link
Copy Markdown

By adding a method to the SPI library to read and write one byte at a time, the slave's code can implement a complete register-based SPI slave. The new method includes timeouts so that the slave does not get stuck in an infinite while loop if something goes wrong.

An additional reset() method can be used to reset the spi interface This allows the slave to recover if it falls behind or detects other errors that cause it to fall out sync with the master.

Example code showing a simple register-based slave is included in the SPI/examples/RegisterSlave folder along with a python script that runs on a RPi and acts as the master.

Resolves: #3039
See Also: #2050

Pull Request template

Summary

This PR fixes/implements the following bugs/features

  • Add support for register-based SPI slave
  • Add SPI slave example code

The current SPI slave support needs to expose additional functionality in order for the user code to implement a register-based SPI slave.

In a register-based SPI slave, the slave maintains a list of registers that the master can read or write. The registers can be read to return state (e.g. temperature or mode) or written to change state (e.g. set mode, control outputs).

By adding a method to read and write one byte at a time, the slave's code can implement a complete register-based SPI slave. The slave code works by waiting for an interrupt from the master on SS. It then read a byte containing the read/write bit and an address. It then either reads the data to be written to the given address, or returns the data read from the given address.

Using the spi_transfer() method doesn’t work for this use case because the slave does not know in advance if the master is starting a read or a write request and must determine that on the fly.

Validation

  • Ensure CI build is passed.
  • Demonstrate the code is solid. [e.g. Provide a sketch]

Code formatting

  • Ensure AStyle check is passed thanks CI

Closing issues

Fixes #3039
Closes: #3039

By adding a method to read and write one byte at a time, the slave's
code can implement a complete register-based SPI slave.
The new method includes timeouts so that the slave does not get stuck
in an infinite while loop if something goes wrong.

An additional reset() method can be used to reset the spi interface
This allows the slave to recover if it falls behind or detects other errors
that cause it to fall out sync with the master.

Example code showing a simple register-based slave is included in the
SPI/examples/RegisterSlave folder along with a python script that runs
on a RPi and acts as the master.
@fpistm

fpistm commented Aug 11, 2026

Copy link
Copy Markdown
Member

Hi @philrittenhouse
Honestly, I do not understand well the goal of your PR.
Seems to me it is a workaround and does not follow the SPI protocol.

In your example you unconditionally call reset.
Reset function is closed to the deinit one except clock are not disable and hal not deinit but as the init is called in your reset function why not simple call deinit then init? I'm not confident that the HAL spi handle state will be ok after several call.

The read/write function seems very than the transfet one except the last step to wait EOT.

About example, it is very specific, requires STM32FreeRTOS and RPi to run examples.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new SPI APIs intended to enable register-based SPI slave implementations by supporting single-byte full-duplex transfers with timeouts, plus a reset capability for recovery. This extends the existing STM32 Arduino SPI abstraction and provides a working end-to-end example (including a Raspberry Pi master script).

Changes:

  • Add low-level spi_read_write_byte() and spi_reset() utilities in the SPI C layer.
  • Expose new SPIClass::read_write_byte() and SPIClass::reset() methods in the C++ SPI API.
  • Add a RegisterSlave example sketch and a Python master demo script.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
libraries/SPI/src/utility/spi_com.h Declares new low-level reset and single-byte transfer APIs.
libraries/SPI/src/utility/spi_com.c Implements SPI reset and 1-byte transfer-with-timeout logic.
libraries/SPI/src/SPI.h Exposes new reset() and read_write_byte() methods on SPIClass.
libraries/SPI/src/SPI.cpp Implements the new SPIClass methods calling into the C layer.
libraries/SPI/examples/RegisterSlave/SPI_slave_demo.ino Adds a FreeRTOS-based register-style SPI slave example.
libraries/SPI/examples/RegisterSlave/spi_demo.py Adds a Raspberry Pi spidev script to exercise the example.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +566 to +571
SPI_HandleTypeDef *handle = &(obj->handle);

#if defined SPI1_BASE
// Reset SPI
if (handle->Instance == SPI1) {
__HAL_RCC_SPI1_FORCE_RESET();
Comment on lines +708 to +715
spi_status_e spi_read_write_byte(spi_t *obj, uint8_t tx, uint8_t *rx)
{
spi_status_e ret = SPI_OK;
int8_t tmp;
uint32_t tickstart;
SPI_TypeDef *_SPI = obj->handle.Instance;

tickstart = HAL_GetTick();
Comment thread libraries/SPI/src/SPI.cpp
Comment on lines +106 to +110
void SPIClass::reset(void)
{
_spi.handle.State = HAL_SPI_STATE_RESET;
spi_reset(&_spi);
}
Comment thread libraries/SPI/src/SPI.cpp
Comment on lines +195 to +203
/**
* @brief Helper to perform a single byte transaction (read and write).
* begin() or beginTransaction() must be called at least once before.
* @param
* @param
* @param tx: byte to send
* @param *rx: byte received. If NULL the received byte will be discarded.
* @return true on success.
*/
Comment on lines +699 to +707
/**
* @brief This function is used to send/receive one byte on SPI
* @param
* @param obj : pointer to spi_t structure
* @param tx: byte to send
* @param rx: pointer to byte received. If NULL the received byte will be discarded
* @param
* @retval status. SPI_OK = 0
*/
Comment on lines +55 to +60
void SPI_ISR() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;

SPI.reset(); // Reset the SPI hardware interface to flush any stale data:

// At this point xTaskToNotify should not be NULL
Comment on lines +17 to +22
* The following pins should be conneced to the corresponding pins on the master
* PA4 - NSS
* PA5 - SCK
* PA6 - MISO
* PA7 - MOSI
* GND - GND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add register-based SPI slave support with example code

3 participants