@@ -247,7 +247,7 @@ pub enum Operation<'a> {
247
247
/// Transactional I2C interface.
248
248
///
249
249
/// This allows combining operations within an I2C transaction.
250
- pub trait Transactional {
250
+ pub trait Transactional < A : AddressMode = SevenBitAddress > {
251
251
/// Error type
252
252
type Error ;
253
253
@@ -266,15 +266,15 @@ pub trait Transactional {
266
266
/// - `SP` = stop condition
267
267
fn try_exec < ' a > (
268
268
& mut self ,
269
- address : u8 ,
269
+ address : A ,
270
270
operations : & mut [ Operation < ' a > ] ,
271
271
) -> Result < ( ) , Self :: Error > ;
272
272
}
273
273
274
274
/// Transactional I2C interface (iterator version).
275
275
///
276
276
/// This allows combining operation within an I2C transaction.
277
- pub trait TransactionalIter {
277
+ pub trait TransactionalIter < A : AddressMode = SevenBitAddress > {
278
278
/// Error type
279
279
type Error ;
280
280
@@ -291,51 +291,85 @@ pub trait TransactionalIter {
291
291
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
292
292
/// - `SR` = repeated start condition
293
293
/// - `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 >
295
295
where
296
296
O : IntoIterator < Item = Operation < ' a > > ;
297
297
}
298
298
299
299
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
300
300
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
301
+ ///
302
+ /// If you implement `blocking::i2c::Transactional` for your I2C peripheral,
303
+ /// you can use this default implementation so that you do not need to implement
304
+ /// the `blocking::i2c::Write`, `blocking::i2c::Read` and `blocking::i2c::WriteRead`
305
+ /// traits as well.
306
+ /// ```
307
+ /// use embedded_hal::blocking::i2c;
308
+ ///
309
+ /// struct I2c1;
310
+ ///
311
+ /// impl i2c::Transactional<i2c::SevenBitAddress> for I2c1 {
312
+ /// # type Error = ();
313
+ /// fn try_exec<'a>(
314
+ /// &mut self,
315
+ /// address: i2c::SevenBitAddress,
316
+ /// operations: &mut [i2c::Operation<'a>],
317
+ /// ) -> Result<(), Self::Error> {
318
+ /// // ...
319
+ /// # Ok(())
320
+ /// }
321
+ /// }
322
+ ///
323
+ /// // This is all you need to do:
324
+ /// impl i2c::transactional::Default<i2c::SevenBitAddress> for I2c1 {};
325
+ ///
326
+ /// // Then you can use `Write` and so on:
327
+ /// use i2c::Write;
328
+ ///
329
+ /// let mut i2c1 = I2c1{};
330
+ /// i2c1.try_write(0x01, &[0xAB, 0xCD]).unwrap();
331
+ /// ```
301
332
pub mod transactional {
302
- use super :: { Operation , Read , Transactional , Write , WriteRead } ;
333
+ use super :: { AddressMode , Operation , Read , Transactional , Write , WriteRead } ;
303
334
304
335
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
305
336
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
306
- pub trait Default < E > { }
337
+ pub trait Default < A : AddressMode > : Transactional < A > { }
307
338
308
- impl < E , S > Write for S
339
+ impl < A , E , S > Write < A > for S
309
340
where
310
- S : self :: Default < E > + Transactional < Error = E > ,
341
+ A : AddressMode ,
342
+ S : self :: Default < A > + Transactional < A , Error = E > ,
311
343
{
312
344
type Error = E ;
313
345
314
- fn try_write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
346
+ fn try_write ( & mut self , address : A , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
315
347
self . try_exec ( address, & mut [ Operation :: Write ( bytes) ] )
316
348
}
317
349
}
318
350
319
- impl < E , S > Read for S
351
+ impl < A , E , S > Read < A > for S
320
352
where
321
- S : self :: Default < E > + Transactional < Error = E > ,
353
+ A : AddressMode ,
354
+ S : self :: Default < A > + Transactional < A , Error = E > ,
322
355
{
323
356
type Error = E ;
324
357
325
- fn try_read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
358
+ fn try_read ( & mut self , address : A , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
326
359
self . try_exec ( address, & mut [ Operation :: Read ( buffer) ] )
327
360
}
328
361
}
329
362
330
- impl < E , S > WriteRead for S
363
+ impl < A , E , S > WriteRead < A > for S
331
364
where
332
- S : self :: Default < E > + Transactional < Error = E > ,
365
+ A : AddressMode ,
366
+ S : self :: Default < A > + Transactional < A , Error = E > ,
333
367
{
334
368
type Error = E ;
335
369
336
370
fn try_write_read (
337
371
& mut self ,
338
- address : u8 ,
372
+ address : A ,
339
373
bytes : & [ u8 ] ,
340
374
buffer : & mut [ u8 ] ,
341
375
) -> Result < ( ) , Self :: Error > {
0 commit comments