Skip to content

Commit f467687

Browse files
committed
Fix support for addressing modes in I2C transactional traits
1 parent a5789bf commit f467687

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Changed
1313

14+
### Fixed
15+
- Support for I2C addressing modes in `Transactional` I2C traits.
1416

1517
## [v1.0.0-alpha.3] - 2020-11-04
1618

src/blocking/i2c.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub enum Operation<'a> {
247247
/// Transactional I2C interface.
248248
///
249249
/// This allows combining operations within an I2C transaction.
250-
pub trait Transactional {
250+
pub trait Transactional<A: AddressMode = SevenBitAddress> {
251251
/// Error type
252252
type Error;
253253

@@ -266,15 +266,15 @@ pub trait Transactional {
266266
/// - `SP` = stop condition
267267
fn try_exec<'a>(
268268
&mut self,
269-
address: u8,
269+
address: A,
270270
operations: &mut [Operation<'a>],
271271
) -> Result<(), Self::Error>;
272272
}
273273

274274
/// Transactional I2C interface (iterator version).
275275
///
276276
/// This allows combining operation within an I2C transaction.
277-
pub trait TransactionalIter {
277+
pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
278278
/// Error type
279279
type Error;
280280

@@ -291,51 +291,54 @@ pub trait TransactionalIter {
291291
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
292292
/// - `SR` = repeated start condition
293293
/// - `SP` = stop condition
294-
fn try_exec_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
294+
fn try_exec_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
295295
where
296296
O: IntoIterator<Item = Operation<'a>>;
297297
}
298298

299299
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
300300
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
301301
pub mod transactional {
302-
use super::{Operation, Read, Transactional, Write, WriteRead};
302+
use super::{AddressMode, Operation, Read, Transactional, Write, WriteRead};
303303

304304
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
305305
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
306-
pub trait Default<E> {}
306+
pub trait Default<A: AddressMode, E>: Transactional<A, Error = E> {}
307307

308-
impl<E, S> Write for S
308+
impl<A, E, S> Write<A> for S
309309
where
310-
S: self::Default<E> + Transactional<Error = E>,
310+
A: AddressMode,
311+
S: self::Default<A, E> + Transactional<A, Error = E>,
311312
{
312313
type Error = E;
313314

314-
fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
315+
fn try_write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error> {
315316
self.try_exec(address, &mut [Operation::Write(bytes)])
316317
}
317318
}
318319

319-
impl<E, S> Read for S
320+
impl<A, E, S> Read<A> for S
320321
where
321-
S: self::Default<E> + Transactional<Error = E>,
322+
A: AddressMode,
323+
S: self::Default<A, E> + Transactional<A, Error = E>,
322324
{
323325
type Error = E;
324326

325-
fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
327+
fn try_read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
326328
self.try_exec(address, &mut [Operation::Read(buffer)])
327329
}
328330
}
329331

330-
impl<E, S> WriteRead for S
332+
impl<A, E, S> WriteRead<A> for S
331333
where
332-
S: self::Default<E> + Transactional<Error = E>,
334+
A: AddressMode,
335+
S: self::Default<A, E> + Transactional<A, Error = E>,
333336
{
334337
type Error = E;
335338

336339
fn try_write_read(
337340
&mut self,
338-
address: u8,
341+
address: A,
339342
bytes: &[u8],
340343
buffer: &mut [u8],
341344
) -> Result<(), Self::Error> {

0 commit comments

Comments
 (0)