Skip to content

Add register-based SPI slave support with example code #3039

Description

@philrittenhouse

Is your feature request/improvement related to a problem? Please describe.

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

Describe the solution you'd like

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.

Describe alternatives you've considered

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.

Additional context

I have put together a fork with the necessary changes along with some sample code to demonstrated it. I have also added timeouts to the new method so that the slave does not get stuck in an infinite while loop if something goes wrong.

I also added a method that can be used to reset the spi interface so that the slave can recover if it falls behind or detects other errors that cause it to fall out sync with the master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions