Enables data transfer with a specific SPI responder. This class combines a pw::spi::Initiator
(representing the physical SPI bus), its configuration data, and a pw::spi::ChipSelector
object to uniquely address a device. Transfers to a selected initiator are guarded against concurrent access through the use of the pw::sync::Borrowable
object.
pw::spi::Device
provides a wrapper for an injected pw::spi::Initiator
object, using its methods to configure the bus and perform individual SPI transfers. The injected pw::spi::ChipSelector
object is used internally to activate and de-activate the device on-demand from within the data transfer methods.
The Read()
, Write()
, and WriteRead()
methods provide support for performing individual transfers: Read()
and Write()
perform half-duplex operations, whereas WriteRead()
provides support for full-duplex transfers.
The StartTransaction()
method provides support for performing multi-part transfers consisting of a series of Read()
, Write()
, or WriteRead()
calls, during which the caller is guaranteed exclusive access to the underlying bus. The Transaction
objects returned from this method implements the RAII layer providing exclusive access to the bus; exclusive access locking is released when the pw::spi::Transaction
object is destroyed or goes out of scope.
Mutual-exclusion to the pw::spi::Initiator
object is provided by the use of the pw::sync::Borrowable
object, where the pw::spi::Initiator
object is "borrowed" for the duration of a transaction.
Classes | |
class | Transaction |
Public Member Functions | |
Device (sync::Borrowable< Initiator > initiator, const Config config, ChipSelector &selector) | |
Status | Read (ByteSpan read_buffer) |
Status | Write (ConstByteSpan write_buffer) |
Status | WriteRead (ConstByteSpan write_buffer, ByteSpan read_buffer) |
Transaction | StartTransaction (ChipSelectBehavior behavior) |
Synchronously reads data from the SPI responder until the provided read_buffer
is full.
This call will configure the bus and activate/deactivate chip select for the transfer.
OkStatus()
on success, and implementation-specific values on failure.
|
inline |
Begins a transaction with the SPI device. This creates an RAII Transaction
object that ensures that only one entity can access the underlying SPI bus (pw::spi::Initiator
) for the object's duration. The behavior
parameter provides a means for a client to select how the chip select signal will be applied on Read
, Write
, or WriteRead
calls taking place with the Transaction
object. A value of kPerWriteRead
will activate/deactivate chip select on each operation, while kPerTransaction
will hold the chip select active for the duration of the Transaction object.
|
inline |
Synchronously writes the contents of write_buffer
to the SPI responder.
This call will configure the bus and activate/deactivate chip select for the transfer.
OkStatus()
on success, and implementation-specific values on failure.
|
inline |
Performs a synchronous read/write transfer with the SPI responder. Data from the write_buffer
object is written to the bus, while the read_buffer
is populated with incoming data on the bus. In the event the read buffer is smaller than the write buffer (or zero-size), any additional input bytes are discarded. In the event the write buffer is smaller than the read buffer (or zero-size), the output is padded with 0-bits for the remainder of the transfer.
This call will configure the bus and activate/deactivate chip select for the transfer.
OkStatus()
on success, and implementation-specific values on failure.