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::blocking::i2c::{SevenBitAddress, TenBitAddress, Write};
25
+ //! # use embedded_hal::blocking::i2c::{Error, ErrorKind, SevenBitAddress, TenBitAddress, Write};
26
26
//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
27
27
//! pub struct I2c0;
28
28
//!
29
+ //! # #[derive(Debug)]
30
+ //! # pub struct I2cError;
31
+ //! # impl Error for I2cError {
32
+ //! # fn kind(&self) -> ErrorKind {
33
+ //! # unreachable!()
34
+ //! # }
35
+ //! # }
36
+ //! #
29
37
//! impl Write<SevenBitAddress> for I2c0
30
38
//! {
31
- //! # type Error = () ;
39
+ //! # type Error = I2cError ;
32
40
//! #
33
41
//! fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
34
42
//! // ...
38
46
//!
39
47
//! impl Write<TenBitAddress> for I2c0
40
48
//! {
41
- //! # type Error = () ;
49
+ //! # type Error = I2cError ;
42
50
//! #
43
51
//! fn write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> {
44
52
//! // ...
52
60
//! For demonstration purposes the address mode parameter has been omitted in this example.
53
61
//!
54
62
//! ```
55
- //! # use embedded_hal::blocking::i2c::WriteRead;
63
+ //! # use embedded_hal::blocking::i2c::{Error, WriteRead} ;
56
64
//! const ADDR: u8 = 0x15;
57
65
//! # const TEMP_REGISTER: u8 = 0x1;
58
66
//! pub struct TemperatureSensorDriver<I2C> {
62
70
//! impl<I2C, E> TemperatureSensorDriver<I2C>
63
71
//! where
64
72
//! I2C: WriteRead<Error = E>,
73
+ //! E: Error,
65
74
//! {
66
75
//! pub fn read_temperature(&mut self) -> Result<u8, E> {
67
76
//! let mut temp = [0];
75
84
//! ### Device driver compatible only with 10-bit addresses
76
85
//!
77
86
//! ```
78
- //! # use embedded_hal::blocking::i2c::{TenBitAddress, WriteRead};
87
+ //! # use embedded_hal::blocking::i2c::{Error, TenBitAddress, WriteRead};
79
88
//! const ADDR: u16 = 0x158;
80
89
//! # const TEMP_REGISTER: u8 = 0x1;
81
90
//! pub struct TemperatureSensorDriver<I2C> {
85
94
//! impl<I2C, E> TemperatureSensorDriver<I2C>
86
95
//! where
87
96
//! I2C: WriteRead<TenBitAddress, Error = E>,
97
+ //! E: Error,
88
98
//! {
89
99
//! pub fn read_temperature(&mut self) -> Result<u8, E> {
90
100
//! let mut temp = [0];
@@ -112,10 +122,12 @@ impl AddressMode for SevenBitAddress {}
112
122
113
123
impl AddressMode for TenBitAddress { }
114
124
125
+ pub use crate :: errors:: i2c:: { Error , ErrorKind } ;
126
+
115
127
/// Blocking read
116
128
pub trait Read < A : AddressMode = SevenBitAddress > {
117
129
/// Error type
118
- type Error ;
130
+ type Error : Error ;
119
131
120
132
/// Reads enough bytes from slave with `address` to fill `buffer`
121
133
///
@@ -141,7 +153,7 @@ pub trait Read<A: AddressMode = SevenBitAddress> {
141
153
/// Blocking write
142
154
pub trait Write < A : AddressMode = SevenBitAddress > {
143
155
/// Error type
144
- type Error ;
156
+ type Error : Error ;
145
157
146
158
/// Writes bytes to slave with address `address`
147
159
///
@@ -165,7 +177,7 @@ pub trait Write<A: AddressMode = SevenBitAddress> {
165
177
/// Blocking write (iterator version)
166
178
pub trait WriteIter < A : AddressMode = SevenBitAddress > {
167
179
/// Error type
168
- type Error ;
180
+ type Error : Error ;
169
181
170
182
/// Writes bytes to slave with address `address`
171
183
///
@@ -180,7 +192,7 @@ pub trait WriteIter<A: AddressMode = SevenBitAddress> {
180
192
/// Blocking write + read
181
193
pub trait WriteRead < A : AddressMode = SevenBitAddress > {
182
194
/// Error type
183
- type Error ;
195
+ type Error : Error ;
184
196
185
197
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
186
198
/// single transaction*
@@ -215,7 +227,7 @@ pub trait WriteRead<A: AddressMode = SevenBitAddress> {
215
227
/// Blocking write (iterator version) + read
216
228
pub trait WriteIterRead < A : AddressMode = SevenBitAddress > {
217
229
/// Error type
218
- type Error ;
230
+ type Error : Error ;
219
231
220
232
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
221
233
/// single transaction*
@@ -249,7 +261,7 @@ pub enum Operation<'a> {
249
261
/// This allows combining operations within an I2C transaction.
250
262
pub trait Transactional < A : AddressMode = SevenBitAddress > {
251
263
/// Error type
252
- type Error ;
264
+ type Error : Error ;
253
265
254
266
/// Execute the provided operations on the I2C bus.
255
267
///
@@ -273,7 +285,7 @@ pub trait Transactional<A: AddressMode = SevenBitAddress> {
273
285
/// This allows combining operation within an I2C transaction.
274
286
pub trait TransactionalIter < A : AddressMode = SevenBitAddress > {
275
287
/// Error type
276
- type Error ;
288
+ type Error : Error ;
277
289
278
290
/// Execute the provided operations on the I2C bus (iterator version).
279
291
///
@@ -304,9 +316,16 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
304
316
/// use embedded_hal::blocking::i2c;
305
317
///
306
318
/// struct I2c1;
319
+ /// # #[derive(Debug)]
320
+ /// # pub struct I2cError;
321
+ /// # impl i2c::Error for I2cError {
322
+ /// # fn kind(&self) -> i2c::ErrorKind {
323
+ /// # unreachable!()
324
+ /// # }
325
+ /// # }
307
326
///
308
327
/// impl i2c::Transactional<i2c::SevenBitAddress> for I2c1 {
309
- /// # type Error = () ;
328
+ /// # type Error = I2cError ;
310
329
/// fn exec<'a>(
311
330
/// &mut self,
312
331
/// address: i2c::SevenBitAddress,
@@ -327,7 +346,7 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
327
346
/// i2c1.write(0x01, &[0xAB, 0xCD]).unwrap();
328
347
/// ```
329
348
pub mod transactional {
330
- use super :: { AddressMode , Operation , Read , Transactional , Write , WriteRead } ;
349
+ use super :: { AddressMode , Error , Operation , Read , Transactional , Write , WriteRead } ;
331
350
332
351
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
333
352
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
@@ -337,6 +356,7 @@ pub mod transactional {
337
356
where
338
357
A : AddressMode ,
339
358
S : self :: Default < A > + Transactional < A , Error = E > ,
359
+ E : Error ,
340
360
{
341
361
type Error = E ;
342
362
@@ -349,6 +369,7 @@ pub mod transactional {
349
369
where
350
370
A : AddressMode ,
351
371
S : self :: Default < A > + Transactional < A , Error = E > ,
372
+ E : Error ,
352
373
{
353
374
type Error = E ;
354
375
@@ -361,6 +382,7 @@ pub mod transactional {
361
382
where
362
383
A : AddressMode ,
363
384
S : self :: Default < A > + Transactional < A , Error = E > ,
385
+ E : Error ,
364
386
{
365
387
type Error = E ;
366
388
0 commit comments