Skip to content

Commit 09745da

Browse files
committed
Update embedded-hal to 1.0.0-alpha.10
1 parent 1f97fd4 commit 09745da

File tree

10 files changed

+155
-113
lines changed

10 files changed

+155
-113
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.59.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.59.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.59.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: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,70 @@ fn state_to_value(state: embedded_hal::digital::PinState, is_active_low: bool) -
9494
}
9595
}
9696

97+
/// Error type wrapping [gpio_cdev::errors::Error](gpio_cdev::errors::Error) to implement [embedded_hal::digital::Error]
98+
#[derive(Debug)]
99+
pub struct CdevPinError {
100+
err: gpio_cdev::errors::Error,
101+
}
102+
103+
impl CdevPinError {
104+
/// Fetch inner (concrete) [`gpio_cdev::errors::Error`]
105+
pub fn inner(&self) -> &gpio_cdev::errors::Error {
106+
&self.err
107+
}
108+
}
109+
110+
impl From<gpio_cdev::errors::Error> for CdevPinError {
111+
fn from(err: gpio_cdev::errors::Error) -> Self {
112+
Self { err }
113+
}
114+
}
115+
116+
impl embedded_hal::digital::Error for CdevPinError {
117+
fn kind(&self) -> embedded_hal::digital::ErrorKind {
118+
use embedded_hal::digital::ErrorKind;
119+
ErrorKind::Other
120+
}
121+
}
122+
97123
impl embedded_hal::digital::ErrorType for CdevPin {
98-
type Error = gpio_cdev::errors::Error;
124+
type Error = CdevPinError;
99125
}
100126

101127
impl embedded_hal::digital::OutputPin for CdevPin {
102-
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-
))
128+
fn set_low(&mut self) -> Result<(), CdevPinError> {
129+
self.0
130+
.set_value(state_to_value(
131+
embedded_hal::digital::PinState::Low,
132+
self.1.is_active_low(),
133+
))
134+
.map_err(CdevPinError::from)
107135
}
108136

109-
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-
))
137+
fn set_high(&mut self) -> Result<(), CdevPinError> {
138+
self.0
139+
.set_value(state_to_value(
140+
embedded_hal::digital::PinState::High,
141+
self.1.is_active_low(),
142+
))
143+
.map_err(CdevPinError::from)
114144
}
115145
}
116146

117147
impl embedded_hal::digital::InputPin for CdevPin {
118-
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-
})
148+
fn is_high(&self) -> Result<bool, CdevPinError> {
149+
self.0
150+
.get_value()
151+
.map(|val| {
152+
val == state_to_value(
153+
embedded_hal::digital::PinState::High,
154+
self.1.is_active_low(),
155+
)
156+
})
157+
.map_err(CdevPinError::from)
125158
}
126159

127-
fn is_low(&self) -> Result<bool, Self::Error> {
160+
fn is_low(&self) -> Result<bool, CdevPinError> {
128161
self.is_high().map(|val| !val)
129162
}
130163
}

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: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use embedded_hal::i2c::NoAcknowledgeSource;
1313
pub struct I2cdev {
1414
inner: i2cdev::linux::LinuxI2CDevice,
1515
path: PathBuf,
16-
address: Option<u8>,
16+
address: Option<u16>,
1717
}
1818

1919
impl I2cdev {
@@ -32,9 +32,9 @@ impl I2cdev {
3232
Ok(dev)
3333
}
3434

35-
fn set_address(&mut self, address: u8) -> Result<(), i2cdev::linux::LinuxI2CError> {
35+
fn set_address(&mut self, address: u16) -> Result<(), i2cdev::linux::LinuxI2CError> {
3636
if self.address != Some(address) {
37-
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, u16::from(address))?;
37+
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, address)?;
3838
self.address = Some(address);
3939
}
4040
Ok(())
@@ -58,65 +58,17 @@ impl ops::DerefMut for I2cdev {
5858
mod embedded_hal_impl {
5959
use super::*;
6060
use embedded_hal::i2c::ErrorType;
61-
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
62-
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
61+
use embedded_hal::i2c::{I2c, Operation as I2cOperation, SevenBitAddress, TenBitAddress};
62+
use i2cdev::core::{I2CMessage, I2CTransfer};
6363
use i2cdev::linux::LinuxI2CMessage;
6464
impl ErrorType for I2cdev {
6565
type Error = I2CError;
6666
}
6767

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-
68+
impl I2c<TenBitAddress> for I2cdev {
11769
fn transaction(
11870
&mut self,
119-
address: u8,
71+
address: u16,
12072
operations: &mut [I2cOperation],
12173
) -> Result<(), Self::Error> {
12274
// Map operations from generic to linux objects
@@ -135,13 +87,15 @@ mod embedded_hal_impl {
13587
.map(drop)
13688
.map_err(|err| I2CError { err })
13789
}
90+
}
13891

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)
92+
impl I2c<SevenBitAddress> for I2cdev {
93+
fn transaction(
94+
&mut self,
95+
address: u8,
96+
operations: &mut [I2cOperation],
97+
) -> Result<(), Self::Error> {
98+
I2c::<TenBitAddress>::transaction(self, u16::from(address), operations)
14599
}
146100
}
147101
}

src/spi.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ impl ops::DerefMut for Spidev {
4141
mod embedded_hal_impl {
4242
use super::*;
4343
use embedded_hal::spi::ErrorType;
44-
use embedded_hal::spi::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
44+
use embedded_hal::spi::{
45+
Operation as SpiOperation, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice,
46+
SpiDeviceRead, SpiDeviceWrite,
47+
};
4548
use spidev::SpidevTransfer;
4649
use std::io::{Read, Write};
4750

@@ -82,16 +85,39 @@ mod embedded_hal_impl {
8285
}
8386
}
8487

85-
impl SpiDevice for Spidev {
86-
type Bus = Spidev;
88+
impl SpiDeviceRead for Spidev {
89+
fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
90+
for mut buf in operations {
91+
SpiBusRead::read(self, &mut buf)?;
92+
}
93+
self.flush()?;
94+
Ok(())
95+
}
96+
}
8797

88-
fn transaction<R>(
98+
impl SpiDeviceWrite for Spidev {
99+
fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
100+
for buf in operations {
101+
SpiBusWrite::write(self, buf)?;
102+
}
103+
self.flush()?;
104+
Ok(())
105+
}
106+
}
107+
108+
impl SpiDevice for Spidev {
109+
fn transaction(
89110
&mut self,
90-
f: impl FnOnce(&mut Self::Bus) -> Result<R, <Self::Bus as ErrorType>::Error>,
91-
) -> Result<R, Self::Error> {
92-
let result = f(self)?;
111+
operations: &mut [SpiOperation<'_, u8>],
112+
) -> Result<(), Self::Error> {
113+
operations.iter_mut().try_for_each(|op| match op {
114+
SpiOperation::Read(buf) => SpiBusRead::read(self, buf),
115+
SpiOperation::Write(buf) => SpiBusWrite::write(self, buf),
116+
SpiOperation::Transfer(read, write) => SpiBus::transfer(self, read, write),
117+
SpiOperation::TransferInPlace(buf) => SpiBus::transfer_in_place(self, buf),
118+
})?;
93119
self.flush()?;
94-
Ok(result)
120+
Ok(())
95121
}
96122
}
97123
}

0 commit comments

Comments
 (0)