Skip to content

Commit a0d986b

Browse files
Merge #91
91: Update embedded-hal to 1.0.0-alpha.10 r=eldruin a=LehMaxence I tested SPI and CDEV GPIO with hardware. The major change is on the SPI protocol, but I could test i2c with hardware in a few days. Co-authored-by: mlehurau <Max.Lehuraux@analog.com>
2 parents 1f97fd4 + f8c0754 commit a0d986b

File tree

12 files changed

+232
-109
lines changed

12 files changed

+232
-109
lines changed

.github/bors.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ required_approvals = 1
44
status = [
55
"CI (stable, x86_64-unknown-linux-gnu)",
66
"CI (stable, armv7-unknown-linux-gnueabihf)",
7-
"CI (1.54.0, x86_64-unknown-linux-gnu)",
7+
"CI (1.60.0, x86_64-unknown-linux-gnu)",
88
]

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
include:
2323
# Test MSRV
24-
- rust: 1.54.0
24+
- rust: 1.60.0
2525
TARGET: x86_64-unknown-linux-gnu
2626

2727
# Test nightly but don't fail

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111
- [breaking-change] Replace serial-rs with the serialport-rs crate. `Serial::open` now needs a baud-rate argument as well.
12+
- Updated to `embedded-hal` `1.0.0-alpha.10` release ([API changes](https://github.com/rust-embedded/embedded-hal/blob/master/embedded-hal/CHANGELOG.md#v100-alpha10---2023-04-04))
1213

1314
## [v0.4.0-alpha.3] - 2022-08-04
1415

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ spi = ["spidev"]
2222
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
2323

2424
[dependencies]
25-
embedded-hal = "=1.0.0-alpha.9"
26-
embedded-hal-nb = "=1.0.0-alpha.1"
25+
embedded-hal = "=1.0.0-alpha.10"
26+
embedded-hal-nb = "=1.0.0-alpha.2"
2727
gpio-cdev = { version = "0.5.1", optional = true }
2828
sysfs_gpio = { version = "0.6.1", optional = true }
2929
i2cdev = { version = "0.5.1", optional = true }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ With `default-features = false` you can enable the features `gpio_cdev`, `gpio_s
2929

3030
## Minimum Supported Rust Version (MSRV)
3131

32-
This crate is guaranteed to compile on stable Rust 1.54.0 and up. It *might*
32+
This crate is guaranteed to compile on stable Rust 1.60.0 and up. It *might*
3333
compile with older versions but that may change in any new patch release.
3434

3535
## License

src/cdev_pin.rs

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+
use std::fmt;
6+
57
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
68
///
79
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
@@ -33,7 +35,7 @@ impl CdevPin {
3335
} else if self.1.is_open_source() {
3436
flags.insert(gpio_cdev::LineRequestFlags::OPEN_SOURCE);
3537
}
36-
return flags;
38+
flags
3739
}
3840

3941
/// Set this pin to input mode
@@ -94,34 +96,79 @@ fn state_to_value(state: embedded_hal::digital::PinState, is_active_low: bool) -
9496
}
9597
}
9698

99+
/// Error type wrapping [gpio_cdev::errors::Error](gpio_cdev::errors::Error) to implement [embedded_hal::digital::Error]
100+
#[derive(Debug)]
101+
pub struct CdevPinError {
102+
err: gpio_cdev::errors::Error,
103+
}
104+
105+
impl CdevPinError {
106+
/// Fetch inner (concrete) [`gpio_cdev::errors::Error`]
107+
pub fn inner(&self) -> &gpio_cdev::errors::Error {
108+
&self.err
109+
}
110+
}
111+
112+
impl From<gpio_cdev::errors::Error> for CdevPinError {
113+
fn from(err: gpio_cdev::errors::Error) -> Self {
114+
Self { err }
115+
}
116+
}
117+
118+
impl fmt::Display for CdevPinError {
119+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120+
write!(f, "{}", self.err)
121+
}
122+
}
123+
124+
impl std::error::Error for CdevPinError {
125+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126+
Some(&self.err)
127+
}
128+
}
129+
130+
impl embedded_hal::digital::Error for CdevPinError {
131+
fn kind(&self) -> embedded_hal::digital::ErrorKind {
132+
use embedded_hal::digital::ErrorKind;
133+
ErrorKind::Other
134+
}
135+
}
136+
97137
impl embedded_hal::digital::ErrorType for CdevPin {
98-
type Error = gpio_cdev::errors::Error;
138+
type Error = CdevPinError;
99139
}
100140

101141
impl embedded_hal::digital::OutputPin for CdevPin {
102142
fn set_low(&mut self) -> Result<(), Self::Error> {
103-
self.0.set_value(state_to_value(
104-
embedded_hal::digital::PinState::Low,
105-
self.1.is_active_low(),
106-
))
143+
self.0
144+
.set_value(state_to_value(
145+
embedded_hal::digital::PinState::Low,
146+
self.1.is_active_low(),
147+
))
148+
.map_err(CdevPinError::from)
107149
}
108150

109151
fn set_high(&mut self) -> Result<(), Self::Error> {
110-
self.0.set_value(state_to_value(
111-
embedded_hal::digital::PinState::High,
112-
self.1.is_active_low(),
113-
))
152+
self.0
153+
.set_value(state_to_value(
154+
embedded_hal::digital::PinState::High,
155+
self.1.is_active_low(),
156+
))
157+
.map_err(CdevPinError::from)
114158
}
115159
}
116160

117161
impl embedded_hal::digital::InputPin for CdevPin {
118162
fn is_high(&self) -> Result<bool, Self::Error> {
119-
self.0.get_value().map(|val| {
120-
val == state_to_value(
121-
embedded_hal::digital::PinState::High,
122-
self.1.is_active_low(),
123-
)
124-
})
163+
self.0
164+
.get_value()
165+
.map(|val| {
166+
val == state_to_value(
167+
embedded_hal::digital::PinState::High,
168+
self.1.is_active_low(),
169+
)
170+
})
171+
.map_err(CdevPinError::from)
125172
}
126173

127174
fn is_low(&self) -> Result<bool, Self::Error> {

src/delay.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
55
use cast::u64;
6-
use core::convert::Infallible;
76
use embedded_hal::delay::DelayUs;
87
use std::thread;
98
use std::time::Duration;
@@ -12,13 +11,10 @@ use std::time::Duration;
1211
pub struct Delay;
1312

1413
impl DelayUs for Delay {
15-
type Error = Infallible;
16-
17-
fn delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
14+
fn delay_us(&mut self, n: u32) {
1815
let secs = n / 1_000_000;
1916
let nsecs = (n % 1_000_000) * 1_000;
2017

2118
thread::sleep(Duration::new(u64(secs), nsecs));
22-
Ok(())
2319
}
2420
}

src/i2c.rs

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+
use std::fmt;
56
use std::ops;
67
use std::path::{Path, PathBuf};
78

@@ -13,7 +14,7 @@ use embedded_hal::i2c::NoAcknowledgeSource;
1314
pub struct I2cdev {
1415
inner: i2cdev::linux::LinuxI2CDevice,
1516
path: PathBuf,
16-
address: Option<u8>,
17+
address: Option<u16>,
1718
}
1819

1920
impl I2cdev {
@@ -32,9 +33,9 @@ impl I2cdev {
3233
Ok(dev)
3334
}
3435

35-
fn set_address(&mut self, address: u8) -> Result<(), i2cdev::linux::LinuxI2CError> {
36+
fn set_address(&mut self, address: u16) -> Result<(), i2cdev::linux::LinuxI2CError> {
3637
if self.address != Some(address) {
37-
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, u16::from(address))?;
38+
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, address)?;
3839
self.address = Some(address);
3940
}
4041
Ok(())
@@ -58,65 +59,17 @@ impl ops::DerefMut for I2cdev {
5859
mod embedded_hal_impl {
5960
use super::*;
6061
use embedded_hal::i2c::ErrorType;
61-
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
62-
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
62+
use embedded_hal::i2c::{I2c, Operation as I2cOperation, SevenBitAddress, TenBitAddress};
63+
use i2cdev::core::{I2CMessage, I2CTransfer};
6364
use i2cdev::linux::LinuxI2CMessage;
6465
impl ErrorType for I2cdev {
6566
type Error = I2CError;
6667
}
6768

68-
impl I2c for I2cdev {
69-
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
70-
self.set_address(address)?;
71-
self.inner.read(buffer).map_err(|err| I2CError { err })
72-
}
73-
74-
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
75-
self.set_address(address)?;
76-
self.inner.write(bytes).map_err(|err| I2CError { err })
77-
}
78-
79-
fn write_iter<B>(&mut self, address: u8, bytes: B) -> Result<(), Self::Error>
80-
where
81-
B: IntoIterator<Item = u8>,
82-
{
83-
let bytes: Vec<_> = bytes.into_iter().collect();
84-
self.write(address, &bytes)
85-
}
86-
87-
fn write_read(
88-
&mut self,
89-
address: u8,
90-
bytes: &[u8],
91-
buffer: &mut [u8],
92-
) -> Result<(), Self::Error> {
93-
self.set_address(address)?;
94-
let mut messages = [LinuxI2CMessage::write(bytes), LinuxI2CMessage::read(buffer)];
95-
self.inner
96-
.transfer(&mut messages)
97-
.map(drop)
98-
.map_err(|err| I2CError { err })
99-
}
100-
101-
fn write_iter_read<B>(
102-
&mut self,
103-
address: u8,
104-
bytes: B,
105-
buffer: &mut [u8],
106-
) -> Result<(), Self::Error>
107-
where
108-
B: IntoIterator<Item = u8>,
109-
{
110-
let bytes: Vec<_> = bytes.into_iter().collect();
111-
self.transaction(
112-
address,
113-
&mut [I2cOperation::Write(&bytes), I2cOperation::Read(buffer)],
114-
)
115-
}
116-
69+
impl I2c<TenBitAddress> for I2cdev {
11770
fn transaction(
11871
&mut self,
119-
address: u8,
72+
address: u16,
12073
operations: &mut [I2cOperation],
12174
) -> Result<(), Self::Error> {
12275
// Map operations from generic to linux objects
@@ -135,13 +88,15 @@ mod embedded_hal_impl {
13588
.map(drop)
13689
.map_err(|err| I2CError { err })
13790
}
91+
}
13892

139-
fn transaction_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
140-
where
141-
O: IntoIterator<Item = I2cOperation<'a>>,
142-
{
143-
let mut ops: Vec<_> = operations.into_iter().collect();
144-
self.transaction(address, &mut ops)
93+
impl I2c<SevenBitAddress> for I2cdev {
94+
fn transaction(
95+
&mut self,
96+
address: u8,
97+
operations: &mut [I2cOperation],
98+
) -> Result<(), Self::Error> {
99+
I2c::<TenBitAddress>::transaction(self, u16::from(address), operations)
145100
}
146101
}
147102
}
@@ -165,6 +120,18 @@ impl From<i2cdev::linux::LinuxI2CError> for I2CError {
165120
}
166121
}
167122

123+
impl fmt::Display for I2CError {
124+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125+
write!(f, "{}", self.err)
126+
}
127+
}
128+
129+
impl std::error::Error for I2CError {
130+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
131+
Some(&self.err)
132+
}
133+
}
134+
168135
impl embedded_hal::i2c::Error for I2CError {
169136
fn kind(&self) -> embedded_hal::i2c::ErrorKind {
170137
use embedded_hal::i2c::ErrorKind;

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ mod cdev_pin;
3434

3535
#[cfg(feature = "gpio_cdev")]
3636
/// Cdev pin re-export
37-
pub use cdev_pin::CdevPin;
37+
pub use cdev_pin::{CdevPin, CdevPinError};
3838

3939
#[cfg(feature = "gpio_sysfs")]
4040
/// Sysfs pin re-export
41-
pub use sysfs_pin::SysfsPin;
41+
pub use sysfs_pin::{SysfsPin, SysfsPinError};
4242

4343
mod delay;
4444
#[cfg(feature = "i2c")]

src/serial.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//!
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5-
use nb;
65
use serialport::{SerialPortBuilder, TTYPort};
6+
use std::fmt;
77
use std::io::{ErrorKind as IoErrorKind, Read, Write};
88

99
/// Newtype around [`serialport::TTYPort`] that implements
@@ -72,6 +72,14 @@ impl SerialError {
7272
}
7373
}
7474

75+
impl fmt::Display for SerialError {
76+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77+
write!(f, "{}", self.err)
78+
}
79+
}
80+
81+
impl std::error::Error for SerialError {}
82+
7583
impl embedded_hal::serial::Error for SerialError {
7684
fn kind(&self) -> embedded_hal::serial::ErrorKind {
7785
use embedded_hal::serial::ErrorKind::*;

0 commit comments

Comments
 (0)