Skip to content

Devices on a shared SPI bus need different configuration. #830

Closed
@hydra

Description

@hydra

As noted from conversations in the embassy matrix chat, devices attached to an SPI bus often have different characteristics, such as maximum bus speed, polarity, phase and bit order. This config can also differ for different operations on the SAME device. e.g. some SPI devices have a maximum speed for initialisation commands and then a high speed for read commands. thus the config needs to be able to be changed for the device once it's created.

https://github.com/embassy-rs/embassy/blob/master/embassy-embedded-hal/src/shared_bus/spi.rs#L62

The signature of SpiBusDevice::new is currently:

pub fn new(bus: &'a Mutex<M, BUS>, cs: CS)

it feels like it should be:

pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: Config)

and that once you've got a an SpiBusDevice instance you should be able to supply a new config either a) on a per transaction basis, or b) arbitrarily.

equally, to be performant, the spi bus should not be reconfigured if the last config is the same as the one previously used. that is:
a) don't reconfigure the spi peripheral if the last transfer for the same device uses the same config.
b) don't reconfigure the spi peripheral if the last transfer for a different device uses a config with the same settings.
and perhaps: c) only reconfigure the aspects of the spi device that are actually different, e.g. if the speed is not the same change it, and don't change the polarity/phase if they are the same, but this checking might actually be slower and take more code space than just re-configuring the SPI peripheral registers...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions