2222//! Here is an example of an embedded-hal implementation of the `Write` trait
2323//! for both modes:
2424//! ```
25- //! # use embedded_hal::blocking::i2c::{SevenBitAddress, TenBitAddress, Write};
25+ //! # use embedded_hal::blocking::i2c::{Error, ErrorKind, SevenBitAddress, TenBitAddress, Write};
2626//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
2727//! pub struct I2c0;
2828//!
29+ //! # #[derive(Debug)]
30+ //! # pub struct I2cError;
31+ //! # impl Error for I2cError {
32+ //! # fn kind(&self) -> ErrorKind {
33+ //! # unreachable!()
34+ //! # }
35+ //! # }
36+ //! #
2937//! impl Write<SevenBitAddress> for I2c0
3038//! {
31- //! # type Error = () ;
39+ //! # type Error = I2cError ;
3240//! #
3341//! fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
3442//! // ...
3846//!
3947//! impl Write<TenBitAddress> for I2c0
4048//! {
41- //! # type Error = () ;
49+ //! # type Error = I2cError ;
4250//! #
4351//! fn write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> {
4452//! // ...
5260//! For demonstration purposes the address mode parameter has been omitted in this example.
5361//!
5462//! ```
55- //! # use embedded_hal::blocking::i2c::WriteRead;
63+ //! # use embedded_hal::blocking::i2c::{Error, WriteRead} ;
5664//! const ADDR: u8 = 0x15;
5765//! # const TEMP_REGISTER: u8 = 0x1;
5866//! pub struct TemperatureSensorDriver<I2C> {
6270//! impl<I2C, E> TemperatureSensorDriver<I2C>
6371//! where
6472//! I2C: WriteRead<Error = E>,
73+ //! E: Error,
6574//! {
6675//! pub fn read_temperature(&mut self) -> Result<u8, E> {
6776//! let mut temp = [0];
7584//! ### Device driver compatible only with 10-bit addresses
7685//!
7786//! ```
78- //! # use embedded_hal::blocking::i2c::{TenBitAddress, WriteRead};
87+ //! # use embedded_hal::blocking::i2c::{Error, TenBitAddress, WriteRead};
7988//! const ADDR: u16 = 0x158;
8089//! # const TEMP_REGISTER: u8 = 0x1;
8190//! pub struct TemperatureSensorDriver<I2C> {
8594//! impl<I2C, E> TemperatureSensorDriver<I2C>
8695//! where
8796//! I2C: WriteRead<TenBitAddress, Error = E>,
97+ //! E: Error,
8898//! {
8999//! pub fn read_temperature(&mut self) -> Result<u8, E> {
90100//! let mut temp = [0];
@@ -112,10 +122,12 @@ impl AddressMode for SevenBitAddress {}
112122
113123impl AddressMode for TenBitAddress { }
114124
125+ pub use crate :: errors:: i2c:: { Error , ErrorKind } ;
126+
115127/// Blocking read
116128pub trait Read < A : AddressMode = SevenBitAddress > {
117129 /// Error type
118- type Error ;
130+ type Error : Error ;
119131
120132 /// Reads enough bytes from slave with `address` to fill `buffer`
121133 ///
@@ -141,7 +153,7 @@ pub trait Read<A: AddressMode = SevenBitAddress> {
141153/// Blocking write
142154pub trait Write < A : AddressMode = SevenBitAddress > {
143155 /// Error type
144- type Error ;
156+ type Error : Error ;
145157
146158 /// Writes bytes to slave with address `address`
147159 ///
@@ -165,7 +177,7 @@ pub trait Write<A: AddressMode = SevenBitAddress> {
165177/// Blocking write (iterator version)
166178pub trait WriteIter < A : AddressMode = SevenBitAddress > {
167179 /// Error type
168- type Error ;
180+ type Error : Error ;
169181
170182 /// Writes bytes to slave with address `address`
171183 ///
@@ -180,7 +192,7 @@ pub trait WriteIter<A: AddressMode = SevenBitAddress> {
180192/// Blocking write + read
181193pub trait WriteRead < A : AddressMode = SevenBitAddress > {
182194 /// Error type
183- type Error ;
195+ type Error : Error ;
184196
185197 /// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
186198 /// single transaction*
@@ -215,7 +227,7 @@ pub trait WriteRead<A: AddressMode = SevenBitAddress> {
215227/// Blocking write (iterator version) + read
216228pub trait WriteIterRead < A : AddressMode = SevenBitAddress > {
217229 /// Error type
218- type Error ;
230+ type Error : Error ;
219231
220232 /// Writes bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
221233 /// single transaction*
@@ -249,7 +261,7 @@ pub enum Operation<'a> {
249261/// This allows combining operations within an I2C transaction.
250262pub trait Transactional < A : AddressMode = SevenBitAddress > {
251263 /// Error type
252- type Error ;
264+ type Error : Error ;
253265
254266 /// Execute the provided operations on the I2C bus.
255267 ///
@@ -273,7 +285,7 @@ pub trait Transactional<A: AddressMode = SevenBitAddress> {
273285/// This allows combining operation within an I2C transaction.
274286pub trait TransactionalIter < A : AddressMode = SevenBitAddress > {
275287 /// Error type
276- type Error ;
288+ type Error : Error ;
277289
278290 /// Execute the provided operations on the I2C bus (iterator version).
279291 ///
@@ -304,9 +316,16 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
304316/// use embedded_hal::blocking::i2c;
305317///
306318/// 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+ /// # }
307326///
308327/// impl i2c::Transactional<i2c::SevenBitAddress> for I2c1 {
309- /// # type Error = () ;
328+ /// # type Error = I2cError ;
310329/// fn exec<'a>(
311330/// &mut self,
312331/// address: i2c::SevenBitAddress,
@@ -327,7 +346,7 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
327346/// i2c1.write(0x01, &[0xAB, 0xCD]).unwrap();
328347/// ```
329348pub mod transactional {
330- use super :: { AddressMode , Operation , Read , Transactional , Write , WriteRead } ;
349+ use super :: { AddressMode , Error , Operation , Read , Transactional , Write , WriteRead } ;
331350
332351 /// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
333352 /// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
@@ -337,6 +356,7 @@ pub mod transactional {
337356 where
338357 A : AddressMode ,
339358 S : self :: Default < A > + Transactional < A , Error = E > ,
359+ E : Error ,
340360 {
341361 type Error = E ;
342362
@@ -349,6 +369,7 @@ pub mod transactional {
349369 where
350370 A : AddressMode ,
351371 S : self :: Default < A > + Transactional < A , Error = E > ,
372+ E : Error ,
352373 {
353374 type Error = E ;
354375
@@ -361,6 +382,7 @@ pub mod transactional {
361382 where
362383 A : AddressMode ,
363384 S : self :: Default < A > + Transactional < A , Error = E > ,
385+ E : Error ,
364386 {
365387 type Error = E ;
366388
0 commit comments