Struct kernel::services::i2c::Transaction
source · pub struct Transaction {
addr: Addr,
tx: KProducer<Transfer>,
rsp_rx: Reusable<Result<FixedVec<u8>, ErrorKind>>,
ended: bool,
}
Expand description
A transaction on the I²C bus.
This type represents a transaction consisting of a series of read and write
operations to a target device on an I²C bus with a given
Addr
. This type is part of a lower-level interface for I²C
bus transactions, and is returned by the I2cClient::start_transaction
method.
Once a Transaction
has been created by I2cClient::start_transaction
,
data can be written to the target device using the Transaction::write
method, and read from the target device using the Transaction::read
method. Any number of read and write operations may be performed within a
Transaction
until an operation with end: true
is performed. This
completes the transaction.
While a Transaction
is in progress, the I²C bus is “locked”
by the client that is performing that transaction. Other clients calling
I2cClient::start_transaction
(or using the
embedded_hal_async::i2c::I2c
interface) will wait until the current
transaction has completed before beginning their own transactions.
Fields§
§addr: Addr
§tx: KProducer<Transfer>
§rsp_rx: Reusable<Result<FixedVec<u8>, ErrorKind>>
§ended: bool
Implementations§
source§impl Transaction
impl Transaction
sourcepub async fn new(__arg0: StartTransaction) -> (Self, KConsumer<Transfer>)
pub async fn new(__arg0: StartTransaction) -> (Self, KConsumer<Transfer>)
Constructs a new Transaction
from the provided StartTransaction
message, returning the Transaction
and a KConsumer
for receiving
Transfer
s within that Transaction
.
This is intended to be used by server implementations of the
I2cService
when handling StartTransaction
messages.
sourcepub async fn read(
&mut self,
buf: FixedVec<u8>,
len: usize,
end: bool,
) -> Result<FixedVec<u8>, I2cError>
pub async fn read( &mut self, buf: FixedVec<u8>, len: usize, end: bool, ) -> Result<FixedVec<u8>, I2cError>
Read len
bytes from the I²C bus into buf
.
Note that, rather than always filling the entire buffer, this method
takes a len
argument which specifies the number of bytes to read. This
is intended to allow callers to reuse the same FixedVec
for multiple
read
and write
operations.
§Arguments
buf
: aFixedVec
buffer into which bytes read from the I²C bus will be writtenlen
: the number of bytes to read. This must be less than or equal tobuf.len()
.end
: whether or not to end the transaction. If this istrue
, aSTOP
condition will be sent on the bus oncelen
bytes have been read. If this isfalse
, a repeatedSTART
condition will be sent on the bus oncelen
bytes have been read.
§Errors
- If an error occurs performing the I²C bus transaction.
- If
len
is greater thanbuf.capacity()
-buf.len()
. - If there is no
I2cService
running.
§Cancelation Safety
If this future is dropped, the underlying I²C bus read operation may still be performed.
sourcepub async fn write(
&mut self,
buf: FixedVec<u8>,
end: bool,
) -> Result<FixedVec<u8>, I2cError>
pub async fn write( &mut self, buf: FixedVec<u8>, end: bool, ) -> Result<FixedVec<u8>, I2cError>
Write bytes from buf
to the I²C.
§Arguments
buf
: aFixedVec
buffer containing the bytes to write to the I²C bus.end
: whether or not to end the transaction. If this istrue
, aSTOP
condition will be sent on the bus once the entire buffer has been sent. If this isfalse
, a repeatedSTART
condition will be sent on the bus once the entire buffer has been written.
§Errors
- If an error occurs performing the I²C bus transaction.
- If there is no
I2cService
running.
§Cancelation Safety
If this future is dropped, the underlying I²C bus write operation may still be performed.