22
22
//! Here is an example of an embedded-hal implementation of the `Write` trait
23
23
//! for both modes:
24
24
//! ```
25
- //! # use embedded_hal::i2c::{ErrorKind, ErrorType, SevenBitAddress, TenBitAddress, blocking::Write };
25
+ //! # use embedded_hal::i2c::{ErrorKind, ErrorType, SevenBitAddress, TenBitAddress, blocking::{I2c, Operation} };
26
26
//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
27
27
//! pub struct I2c0;
28
28
//!
29
29
//! # impl ErrorType for I2c0 { type Error = ErrorKind; }
30
- //! impl Write <SevenBitAddress> for I2c0
30
+ //! impl I2c <SevenBitAddress> for I2c0
31
31
//! {
32
- //! fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
32
+ //! fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
33
+ //! // ...
34
+ //! # Ok(())
35
+ //! }
36
+ //! fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
37
+ //! // ...
38
+ //! # Ok(())
39
+ //! }
40
+ //! fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> {
41
+ //! // ...
42
+ //! # Ok(())
43
+ //! }
44
+ //! fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
45
+ //! // ...
46
+ //! # Ok(())
47
+ //! }
48
+ //! fn write_iter_read<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B, buffer: &mut [u8]) -> Result<(), Self::Error> {
49
+ //! // ...
50
+ //! # Ok(())
51
+ //! }
52
+ //! fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
53
+ //! // ...
54
+ //! # Ok(())
55
+ //! }
56
+ //! fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> {
33
57
//! // ...
34
58
//! # Ok(())
35
59
//! }
36
60
//! }
37
61
//!
38
- //! impl Write <TenBitAddress> for I2c0
62
+ //! impl I2c <TenBitAddress> for I2c0
39
63
//! {
40
- //! fn write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> {
64
+ //! fn read(&mut self, addr: u16, buffer: &mut [u8]) -> Result<(), Self::Error> {
65
+ //! // ...
66
+ //! # Ok(())
67
+ //! }
68
+ //! fn write(&mut self, addr: u16, bytes: &[u8]) -> Result<(), Self::Error> {
69
+ //! // ...
70
+ //! # Ok(())
71
+ //! }
72
+ //! fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u16, bytes: B) -> Result<(), Self::Error> {
73
+ //! // ...
74
+ //! # Ok(())
75
+ //! }
76
+ //! fn write_read(&mut self, addr: u16, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
77
+ //! // ...
78
+ //! # Ok(())
79
+ //! }
80
+ //! fn write_iter_read<B: IntoIterator<Item = u8>>(&mut self, addr: u16, bytes: B, buffer: &mut [u8]) -> Result<(), Self::Error> {
81
+ //! // ...
82
+ //! # Ok(())
83
+ //! }
84
+ //! fn transaction<'a>(&mut self, address: u16, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
85
+ //! // ...
86
+ //! # Ok(())
87
+ //! }
88
+ //! fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>(&mut self, address: u16, operations: O) -> Result<(), Self::Error> {
41
89
//! // ...
42
90
//! # Ok(())
43
91
//! }
49
97
//! For demonstration purposes the address mode parameter has been omitted in this example.
50
98
//!
51
99
//! ```
52
- //! # use embedded_hal::i2c::{blocking::WriteRead , Error};
100
+ //! # use embedded_hal::i2c::{blocking::I2c , Error};
53
101
//! const ADDR: u8 = 0x15;
54
102
//! # const TEMP_REGISTER: u8 = 0x1;
55
103
//! pub struct TemperatureSensorDriver<I2C> {
58
106
//!
59
107
//! impl<I2C, E: Error> TemperatureSensorDriver<I2C>
60
108
//! where
61
- //! I2C: WriteRead <Error = E>,
109
+ //! I2C: I2c <Error = E>,
62
110
//! {
63
111
//! pub fn read_temperature(&mut self) -> Result<u8, E> {
64
112
//! let mut temp = [0];
72
120
//! ### Device driver compatible only with 10-bit addresses
73
121
//!
74
122
//! ```
75
- //! # use embedded_hal::i2c::{Error, TenBitAddress, blocking::WriteRead };
123
+ //! # use embedded_hal::i2c::{Error, TenBitAddress, blocking::I2c };
76
124
//! const ADDR: u16 = 0x158;
77
125
//! # const TEMP_REGISTER: u8 = 0x1;
78
126
//! pub struct TemperatureSensorDriver<I2C> {
81
129
//!
82
130
//! impl<I2C, E: Error> TemperatureSensorDriver<I2C>
83
131
//! where
84
- //! I2C: WriteRead <TenBitAddress, Error = E>,
132
+ //! I2C: I2c <TenBitAddress, Error = E>,
85
133
//! {
86
134
//! pub fn read_temperature(&mut self) -> Result<u8, E> {
87
135
//! let mut temp = [0];
@@ -213,8 +261,19 @@ pub mod blocking {
213
261
214
262
use super :: { AddressMode , ErrorType , SevenBitAddress } ;
215
263
216
- /// Blocking read
217
- pub trait Read < A : AddressMode = SevenBitAddress > : ErrorType {
264
+ /// Transactional I2C operation.
265
+ ///
266
+ /// Several operations can be combined as part of a transaction.
267
+ #[ derive( Debug , PartialEq ) ]
268
+ pub enum Operation < ' a > {
269
+ /// Read data into the provided buffer
270
+ Read ( & ' a mut [ u8 ] ) ,
271
+ /// Write data from the provided buffer
272
+ Write ( & ' a [ u8 ] ) ,
273
+ }
274
+
275
+ /// Blocking I2C
276
+ pub trait I2c < A : AddressMode = SevenBitAddress > : ErrorType {
218
277
/// Reads enough bytes from slave with `address` to fill `buffer`
219
278
///
220
279
/// # I2C Events (contract)
@@ -234,16 +293,7 @@ pub mod blocking {
234
293
/// - `NMAK` = master no acknowledge
235
294
/// - `SP` = stop condition
236
295
fn read ( & mut self , address : A , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
237
- }
238
-
239
- impl < A : AddressMode , T : Read < A > > Read < A > for & mut T {
240
- fn read ( & mut self , address : A , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
241
- T :: read ( self , address, buffer)
242
- }
243
- }
244
296
245
- /// Blocking write
246
- pub trait Write < A : AddressMode = SevenBitAddress > : ErrorType {
247
297
/// Writes bytes to slave with address `address`
248
298
///
249
299
/// # I2C Events (contract)
@@ -261,37 +311,16 @@ pub mod blocking {
261
311
/// - `Bi` = ith byte of data
262
312
/// - `SP` = stop condition
263
313
fn write ( & mut self , address : A , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
264
- }
265
-
266
- impl < A : AddressMode , T : Write < A > > Write < A > for & mut T {
267
- fn write ( & mut self , address : A , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
268
- T :: write ( self , address, bytes)
269
- }
270
- }
271
314
272
- /// Blocking write (iterator version)
273
- pub trait WriteIter < A : AddressMode = SevenBitAddress > : ErrorType {
274
315
/// Writes bytes to slave with address `address`
275
316
///
276
317
/// # I2C Events (contract)
277
318
///
278
- /// Same as `Write`
319
+ /// Same as the `write` method
279
320
fn write_iter < B > ( & mut self , address : A , bytes : B ) -> Result < ( ) , Self :: Error >
280
321
where
281
322
B : IntoIterator < Item = u8 > ;
282
- }
283
-
284
- impl < A : AddressMode , T : WriteIter < A > > WriteIter < A > for & mut T {
285
- fn write_iter < B > ( & mut self , address : A , bytes : B ) -> Result < ( ) , Self :: Error >
286
- where
287
- B : IntoIterator < Item = u8 > ,
288
- {
289
- T :: write_iter ( self , address, bytes)
290
- }
291
- }
292
323
293
- /// Blocking write + read
294
- pub trait WriteRead < A : AddressMode = SevenBitAddress > : ErrorType {
295
324
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
296
325
/// single transaction*
297
326
///
@@ -320,27 +349,13 @@ pub mod blocking {
320
349
bytes : & [ u8 ] ,
321
350
buffer : & mut [ u8 ] ,
322
351
) -> Result < ( ) , Self :: Error > ;
323
- }
324
-
325
- impl < A : AddressMode , T : WriteRead < A > > WriteRead < A > for & mut T {
326
- fn write_read (
327
- & mut self ,
328
- address : A ,
329
- bytes : & [ u8 ] ,
330
- buffer : & mut [ u8 ] ,
331
- ) -> Result < ( ) , Self :: Error > {
332
- T :: write_read ( self , address, bytes, buffer)
333
- }
334
- }
335
352
336
- /// Blocking write (iterator version) + read
337
- pub trait WriteIterRead < A : AddressMode = SevenBitAddress > : ErrorType {
338
353
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
339
354
/// single transaction*
340
355
///
341
356
/// # I2C Events (contract)
342
357
///
343
- /// Same as the `WriteRead` trait
358
+ /// Same as the `write_read` method
344
359
fn write_iter_read < B > (
345
360
& mut self ,
346
361
address : A ,
@@ -349,37 +364,7 @@ pub mod blocking {
349
364
) -> Result < ( ) , Self :: Error >
350
365
where
351
366
B : IntoIterator < Item = u8 > ;
352
- }
353
-
354
- impl < A : AddressMode , T : WriteIterRead < A > > WriteIterRead < A > for & mut T {
355
- fn write_iter_read < B > (
356
- & mut self ,
357
- address : A ,
358
- bytes : B ,
359
- buffer : & mut [ u8 ] ,
360
- ) -> Result < ( ) , Self :: Error >
361
- where
362
- B : IntoIterator < Item = u8 > ,
363
- {
364
- T :: write_iter_read ( self , address, bytes, buffer)
365
- }
366
- }
367
367
368
- /// Transactional I2C operation.
369
- ///
370
- /// Several operations can be combined as part of a transaction.
371
- #[ derive( Debug , PartialEq ) ]
372
- pub enum Operation < ' a > {
373
- /// Read data into the provided buffer
374
- Read ( & ' a mut [ u8 ] ) ,
375
- /// Write data from the provided buffer
376
- Write ( & ' a [ u8 ] ) ,
377
- }
378
-
379
- /// Transactional I2C interface.
380
- ///
381
- /// This allows combining operations within an I2C transaction.
382
- pub trait Transactional < A : AddressMode = SevenBitAddress > : ErrorType {
383
368
/// Execute the provided operations on the I2C bus.
384
369
///
385
370
/// Transaction contract:
@@ -393,27 +378,12 @@ pub mod blocking {
393
378
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
394
379
/// - `SR` = repeated start condition
395
380
/// - `SP` = stop condition
396
- fn exec < ' a > (
381
+ fn transaction < ' a > (
397
382
& mut self ,
398
383
address : A ,
399
384
operations : & mut [ Operation < ' a > ] ,
400
385
) -> Result < ( ) , Self :: Error > ;
401
- }
402
386
403
- impl < A : AddressMode , T : Transactional < A > > Transactional < A > for & mut T {
404
- fn exec < ' a > (
405
- & mut self ,
406
- address : A ,
407
- operations : & mut [ Operation < ' a > ] ,
408
- ) -> Result < ( ) , Self :: Error > {
409
- T :: exec ( self , address, operations)
410
- }
411
- }
412
-
413
- /// Transactional I2C interface (iterator version).
414
- ///
415
- /// This allows combining operation within an I2C transaction.
416
- pub trait TransactionalIter < A : AddressMode = SevenBitAddress > : ErrorType {
417
387
/// Execute the provided operations on the I2C bus (iterator version).
418
388
///
419
389
/// Transaction contract:
@@ -427,17 +397,61 @@ pub mod blocking {
427
397
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
428
398
/// - `SR` = repeated start condition
429
399
/// - `SP` = stop condition
430
- fn exec_iter < ' a , O > ( & mut self , address : A , operations : O ) -> Result < ( ) , Self :: Error >
400
+ fn transaction_iter < ' a , O > ( & mut self , address : A , operations : O ) -> Result < ( ) , Self :: Error >
431
401
where
432
402
O : IntoIterator < Item = Operation < ' a > > ;
433
403
}
434
404
435
- impl < A : AddressMode , T : TransactionalIter < A > > TransactionalIter < A > for & mut T {
436
- fn exec_iter < ' a , O > ( & mut self , address : A , operations : O ) -> Result < ( ) , Self :: Error >
405
+ impl < A : AddressMode , T : I2c < A > > I2c < A > for & mut T {
406
+ fn read ( & mut self , address : A , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
407
+ T :: read ( self , address, buffer)
408
+ }
409
+
410
+ fn write ( & mut self , address : A , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
411
+ T :: write ( self , address, bytes)
412
+ }
413
+
414
+ fn write_iter < B > ( & mut self , address : A , bytes : B ) -> Result < ( ) , Self :: Error >
415
+ where
416
+ B : IntoIterator < Item = u8 > ,
417
+ {
418
+ T :: write_iter ( self , address, bytes)
419
+ }
420
+
421
+ fn write_read (
422
+ & mut self ,
423
+ address : A ,
424
+ bytes : & [ u8 ] ,
425
+ buffer : & mut [ u8 ] ,
426
+ ) -> Result < ( ) , Self :: Error > {
427
+ T :: write_read ( self , address, bytes, buffer)
428
+ }
429
+
430
+ fn write_iter_read < B > (
431
+ & mut self ,
432
+ address : A ,
433
+ bytes : B ,
434
+ buffer : & mut [ u8 ] ,
435
+ ) -> Result < ( ) , Self :: Error >
436
+ where
437
+ B : IntoIterator < Item = u8 > ,
438
+ {
439
+ T :: write_iter_read ( self , address, bytes, buffer)
440
+ }
441
+
442
+ fn transaction < ' a > (
443
+ & mut self ,
444
+ address : A ,
445
+ operations : & mut [ Operation < ' a > ] ,
446
+ ) -> Result < ( ) , Self :: Error > {
447
+ T :: transaction ( self , address, operations)
448
+ }
449
+
450
+ fn transaction_iter < ' a , O > ( & mut self , address : A , operations : O ) -> Result < ( ) , Self :: Error >
437
451
where
438
452
O : IntoIterator < Item = Operation < ' a > > ,
439
453
{
440
- T :: exec_iter ( self , address, operations)
454
+ T :: transaction_iter ( self , address, operations)
441
455
}
442
456
}
443
457
}
0 commit comments