Skip to content

Commit 0a1f667

Browse files
committed
i2c: implement all operations in terms of transaction.
1 parent 5901e91 commit 0a1f667

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

embedded-hal-async/src/i2c.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
4141
/// - `MAK` = master acknowledge
4242
/// - `NMAK` = master no acknowledge
4343
/// - `SP` = stop condition
44-
async fn read<'a>(&'a mut self, address: A, read: &'a mut [u8]) -> Result<(), Self::Error>;
44+
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
45+
self.transaction(address, &mut [Operation::Read(read)])
46+
.await
47+
}
4548

4649
/// Writes bytes to slave with address `address`
4750
///
@@ -59,7 +62,10 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
5962
/// - `SAK` = slave acknowledge
6063
/// - `Bi` = ith byte of data
6164
/// - `SP` = stop condition
62-
async fn write<'a>(&'a mut self, address: A, write: &'a [u8]) -> Result<(), Self::Error>;
65+
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
66+
self.transaction(address, &mut [Operation::Write(write)])
67+
.await
68+
}
6369

6470
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
6571
/// single transaction*.
@@ -83,12 +89,18 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
8389
/// - `MAK` = master acknowledge
8490
/// - `NMAK` = master no acknowledge
8591
/// - `SP` = stop condition
86-
async fn write_read<'a>(
87-
&'a mut self,
92+
async fn write_read(
93+
&mut self,
8894
address: A,
89-
write: &'a [u8],
90-
read: &'a mut [u8],
91-
) -> Result<(), Self::Error>;
95+
write: &[u8],
96+
read: &mut [u8],
97+
) -> Result<(), Self::Error> {
98+
self.transaction(
99+
address,
100+
&mut [Operation::Write(write), Operation::Read(read)],
101+
)
102+
.await
103+
}
92104

93105
/// Execute the provided operations on the I2C bus as a single transaction.
94106
///
@@ -103,35 +115,35 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
103115
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
104116
/// - `SR` = repeated start condition
105117
/// - `SP` = stop condition
106-
async fn transaction<'a, 'b>(
107-
&'a mut self,
118+
async fn transaction(
119+
&mut self,
108120
address: A,
109-
operations: &'a mut [Operation<'b>],
121+
operations: &mut [Operation<'_>],
110122
) -> Result<(), Self::Error>;
111123
}
112124

113125
impl<A: AddressMode, T: I2c<A>> I2c<A> for &mut T {
114-
async fn read<'a>(&'a mut self, address: A, buffer: &'a mut [u8]) -> Result<(), Self::Error> {
115-
T::read(self, address, buffer).await
126+
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
127+
T::read(self, address, read).await
116128
}
117129

118-
async fn write<'a>(&'a mut self, address: A, bytes: &'a [u8]) -> Result<(), Self::Error> {
119-
T::write(self, address, bytes).await
130+
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
131+
T::write(self, address, write).await
120132
}
121133

122-
async fn write_read<'a>(
123-
&'a mut self,
134+
async fn write_read(
135+
&mut self,
124136
address: A,
125-
bytes: &'a [u8],
126-
buffer: &'a mut [u8],
137+
write: &[u8],
138+
read: &mut [u8],
127139
) -> Result<(), Self::Error> {
128-
T::write_read(self, address, bytes, buffer).await
140+
T::write_read(self, address, write, read).await
129141
}
130142

131-
async fn transaction<'a, 'b>(
132-
&'a mut self,
143+
async fn transaction(
144+
&mut self,
133145
address: A,
134-
operations: &'a mut [Operation<'b>],
146+
operations: &mut [Operation<'_>],
135147
) -> Result<(), Self::Error> {
136148
T::transaction(self, address, operations).await
137149
}

embedded-hal/src/i2c.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! // ...
4242
//! # Ok(())
4343
//! }
44-
//! fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
44+
//! fn transaction(&mut self, address: u8, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
4545
//! // ...
4646
//! # Ok(())
4747
//! }
@@ -61,7 +61,7 @@
6161
//! // ...
6262
//! # Ok(())
6363
//! }
64-
//! fn transaction<'a>(&mut self, address: u16, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
64+
//! fn transaction(&mut self, address: u16, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
6565
//! // ...
6666
//! # Ok(())
6767
//! }
@@ -232,7 +232,7 @@ impl AddressMode for SevenBitAddress {}
232232

233233
impl AddressMode for TenBitAddress {}
234234

235-
/// Transactional I2C operation.
235+
/// I2C operation.
236236
///
237237
/// Several operations can be combined as part of a transaction.
238238
#[derive(Debug, PartialEq, Eq)]
@@ -245,7 +245,7 @@ pub enum Operation<'a> {
245245

246246
/// Blocking I2C
247247
pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
248-
/// Reads enough bytes from slave with `address` to fill `buffer`
248+
/// Reads enough bytes from slave with `address` to fill `read`
249249
///
250250
/// # I2C Events (contract)
251251
///
@@ -263,7 +263,9 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
263263
/// - `MAK` = master acknowledge
264264
/// - `NMAK` = master no acknowledge
265265
/// - `SP` = stop condition
266-
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error>;
266+
fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
267+
self.transaction(address, &mut [Operation::Read(read)])
268+
}
267269

268270
/// Writes bytes to slave with address `address`
269271
///
@@ -281,9 +283,11 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
281283
/// - `SAK` = slave acknowledge
282284
/// - `Bi` = ith byte of data
283285
/// - `SP` = stop condition
284-
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error>;
286+
fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
287+
self.transaction(address, &mut [Operation::Write(write)])
288+
}
285289

286-
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
290+
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
287291
/// single transaction*
288292
///
289293
/// # I2C Events (contract)
@@ -305,12 +309,12 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
305309
/// - `MAK` = master acknowledge
306310
/// - `NMAK` = master no acknowledge
307311
/// - `SP` = stop condition
308-
fn write_read(
309-
&mut self,
310-
address: A,
311-
bytes: &[u8],
312-
buffer: &mut [u8],
313-
) -> Result<(), Self::Error>;
312+
fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
313+
self.transaction(
314+
address,
315+
&mut [Operation::Write(write), Operation::Read(read)],
316+
)
317+
}
314318

315319
/// Execute the provided operations on the I2C bus.
316320
///
@@ -325,10 +329,10 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
325329
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
326330
/// - `SR` = repeated start condition
327331
/// - `SP` = stop condition
328-
fn transaction<'a>(
332+
fn transaction(
329333
&mut self,
330334
address: A,
331-
operations: &mut [Operation<'a>],
335+
operations: &mut [Operation<'_>],
332336
) -> Result<(), Self::Error>;
333337
}
334338

@@ -350,10 +354,10 @@ impl<A: AddressMode, T: I2c<A>> I2c<A> for &mut T {
350354
T::write_read(self, address, bytes, buffer)
351355
}
352356

353-
fn transaction<'a>(
357+
fn transaction(
354358
&mut self,
355359
address: A,
356-
operations: &mut [Operation<'a>],
360+
operations: &mut [Operation<'_>],
357361
) -> Result<(), Self::Error> {
358362
T::transaction(self, address, operations)
359363
}

0 commit comments

Comments
 (0)