Skip to content

Commit 5068535

Browse files
committed
Add error traits for communication interfaces
1 parent 1b2ae70 commit 5068535

File tree

8 files changed

+189
-28
lines changed

8 files changed

+189
-28
lines changed

src/blocking/i2c.rs

+36-14
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@
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
//! // ...
@@ -38,7 +46,7 @@
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
//! // ...
@@ -52,7 +60,7 @@
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> {
@@ -62,6 +70,7 @@
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];
@@ -75,7 +84,7 @@
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> {
@@ -85,6 +94,7 @@
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

113123
impl AddressMode for TenBitAddress {}
114124

125+
pub use crate::errors::i2c::{Error, ErrorKind};
126+
115127
/// Blocking read
116128
pub 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
142154
pub 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)
166178
pub 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
181193
pub 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
216228
pub 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.
250262
pub 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.
274286
pub 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
/// ```
329348
pub 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

src/blocking/serial.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Blocking serial API
22
3+
pub use crate::errors::serial::{Error, ErrorKind};
4+
35
/// Write half of a serial interface (blocking variant)
46
pub trait Write<Word> {
57
/// The type of error that can occur when writing
6-
type Error;
8+
type Error: Error;
79

810
/// Writes a slice, blocking until everything has been written
911
///
@@ -34,6 +36,7 @@ pub mod write {
3436
impl<S, Word> crate::blocking::serial::Write<Word> for S
3537
where
3638
S: Default<Word>,
39+
S::Error: crate::nb::serial::Error,
3740
Word: Clone,
3841
{
3942
type Error = S::Error;

src/blocking/spi.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Blocking SPI API
22
3+
pub use crate::errors::spi::{Error, ErrorKind};
4+
35
/// Blocking transfer
46
pub trait Transfer<W> {
57
/// Error type
6-
type Error;
8+
type Error: Error;
79

810
/// Writes `words` to the slave. Returns the `words` received from the slave
911
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
@@ -12,7 +14,7 @@ pub trait Transfer<W> {
1214
/// Blocking write
1315
pub trait Write<W> {
1416
/// Error type
15-
type Error;
17+
type Error: Error;
1618

1719
/// Writes `words` to the slave, ignoring all the incoming words
1820
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
@@ -21,7 +23,7 @@ pub trait Write<W> {
2123
/// Blocking write (iterator version)
2224
pub trait WriteIter<W> {
2325
/// Error type
24-
type Error;
26+
type Error: Error;
2527

2628
/// Writes `words` to the slave, ignoring all the incoming words
2729
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
@@ -38,6 +40,7 @@ pub mod transfer {
3840
impl<W, S> crate::blocking::spi::Transfer<W> for S
3941
where
4042
S: Default<W>,
43+
S::Error: crate::blocking::spi::Error,
4144
W: Clone,
4245
{
4346
type Error = S::Error;
@@ -62,6 +65,7 @@ pub mod write {
6265
impl<W, S> crate::blocking::spi::Write<W> for S
6366
where
6467
S: Default<W>,
68+
S::Error: crate::blocking::spi::Error,
6569
W: Clone,
6670
{
6771
type Error = S::Error;
@@ -86,6 +90,7 @@ pub mod write_iter {
8690
impl<W, S> crate::blocking::spi::WriteIter<W> for S
8791
where
8892
S: Default<W>,
93+
S::Error: crate::blocking::spi::Error,
8994
W: Clone,
9095
{
9196
type Error = S::Error;
@@ -119,15 +124,15 @@ pub enum Operation<'a, W: 'static> {
119124
/// as part of a single SPI transaction
120125
pub trait Transactional<W: 'static> {
121126
/// Associated error type
122-
type Error;
127+
type Error: Error;
123128

124129
/// Execute the provided transactions
125130
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
126131
}
127132

128133
/// Blocking transactional impl over spi::Write and spi::Transfer
129134
pub mod transactional {
130-
use super::{Operation, Transfer, Write};
135+
use super::{Error, Operation, Transfer, Write};
131136

132137
/// Default implementation of `blocking::spi::Transactional<W>` for implementers of
133138
/// `spi::Write<W>` and `spi::Transfer<W>`
@@ -136,6 +141,7 @@ pub mod transactional {
136141
impl<W: 'static, E, S> super::Transactional<W> for S
137142
where
138143
S: self::Default<W> + Write<W, Error = E> + Transfer<W, Error = E>,
144+
E: Error,
139145
W: Copy + Clone,
140146
{
141147
type Error = E;

0 commit comments

Comments
 (0)