Skip to content

Commit 21de3bc

Browse files
Morgan RoffMorganR
Morgan Roff
authored andcommitted
Merge remote-tracking branch 'upstream/master'
2 parents 3e76395 + ee0e6af commit 21de3bc

14 files changed

+422
-340
lines changed

.github/bors.toml

+1-1
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.36.0, x86_64-unknown-linux-gnu)",
7+
"CI (1.46.0, x86_64-unknown-linux-gnu)",
88
]

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

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

2727
# Test nightly but don't fail

CHANGELOG.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10-
### Changed
10+
### Added
1111

1212
- Implement `embedded_hal::digital::IoPin` for `CdevPin` and `SysfsPin`
13+
14+
## [v0.4.0-alpha.1] - 2021-09-27
15+
16+
### Changed
17+
18+
- Modified `OutputPin` behavior for active-low pins to match `InputPin` behavior.
1319
- Set default features to build both sysfs and cdev pin types.
1420
- Removed `Pin` export, use `CdevPin` or `SysfsPin`.
15-
- Increased the Minimum Supported Rust Version to `1.36.0` due to an update of `gpio_cdev`.
16-
- Adapted to `embedded-hal` `1.0.0-alpha.3` release.
21+
- Adapted to `embedded-hal` `1.0.0-alpha.5` release.
22+
- Increased the Minimum Supported Rust Version to `1.46.0` due to an update of `bitflags`.
23+
- Updated `spidev` to version `0.5`.
24+
- Updated `i2cdev` to version `0.5`.
25+
- Updated `gpio-cdev` to version `0.5`.
26+
- Updated `sysfs_gpio` to version `0.6`.
1727
- Updated `nb` to version `1`.
1828

1929
## [v0.3.0] - 2019-11-25
@@ -66,8 +76,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6676

6777
Initial release
6878

69-
[Unreleased]: https://github.com/japaric/linux-embedded-hal/compare/v0.2.1...HEAD
70-
[v0.2.2]: https://github.com/japaric/linux-embedded-hal/compare/v0.2.1...v0.2.2
79+
[Unreleased]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.4.0-alpha.1...HEAD
80+
[v0.4.0-alpha.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.3.0...v0.4.0-alpha.1
81+
[v0.3.0]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.2...v0.3.0
82+
[v0.2.2]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.1...v0.2.2
7183
[v0.2.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.0...v0.2.1
7284
[v0.2.0]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.1.1...v0.2.0
73-
[v0.1.1]: https://github.com/japaric/linux-embedded-hal/compare/v0.1.0...v0.1.1
85+
[v0.1.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.1.0...v0.1.1

Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ keywords = ["Linux", "hal"]
99
license = "MIT OR Apache-2.0"
1010
name = "linux-embedded-hal"
1111
repository = "https://github.com/rust-embedded/linux-embedded-hal"
12-
version = "0.4.0-alpha.0"
12+
version = "0.4.0-alpha.1"
1313
edition = "2018"
1414

1515
[features]
@@ -20,20 +20,20 @@ async-tokio = ["gpio-cdev/async-tokio"]
2020
default = [ "gpio_cdev", "gpio_sysfs" ]
2121

2222
[dependencies]
23-
embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal" }
24-
gpio-cdev = { version = "0.4", optional = true }
25-
sysfs_gpio = { version = "0.5", optional = true }
23+
embedded-hal = "=1.0.0-alpha.5"
24+
gpio-cdev = { version = "0.5", optional = true }
25+
sysfs_gpio = { version = "0.6", optional = true }
2626

27-
i2cdev = "0.4.3"
27+
i2cdev = "0.5"
2828
nb = "1"
2929
serial-core = "0.4.0"
3030
serial-unix = "0.4.0"
31-
spidev = "0.4"
31+
spidev = "0.5"
3232

3333
[dev-dependencies]
3434
openpty = "0.1.0"
3535

3636
[dependencies.cast]
3737
# we don't need the `Error` implementation
3838
default-features = false
39-
version = "0.2.2"
39+
version = "0.3"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ linux-embedded-hal = { version = "0.3", features = ["gpio_cdev"] }
2727

2828
## Minimum Supported Rust Version (MSRV)
2929

30-
This crate is guaranteed to compile on stable Rust 1.36.0 and up. It *might*
30+
This crate is guaranteed to compile on stable Rust 1.46.0 and up. It *might*
3131
compile with older versions but that may change in any new patch release.
3232

3333
## License

examples/transactional-i2c.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
extern crate embedded_hal;
2-
extern crate linux_embedded_hal;
3-
use embedded_hal::blocking::i2c::{Operation as I2cOperation, Transactional};
1+
use embedded_hal::i2c::blocking::{Operation as I2cOperation, Transactional};
42
use linux_embedded_hal::I2cdev;
53

64
const ADDR: u8 = 0x12;

src/cdev_pin.rs

+48-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
//! Linux CDev pin type
1+
//! Implementation of [`embedded-hal`] digital input/output traits using a Linux CDev pin
2+
//!
3+
//! [`embedded-hal`]: https://docs.rs/embedded-hal
24
35
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
46
///
5-
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
6-
pub struct CdevPin(pub gpio_cdev::LineHandle, gpio_cdev::LineInfo);
7+
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
8+
pub struct CdevPin(pub gpio_cdev::LineHandle, pub gpio_cdev::LineInfo);
79

810
impl CdevPin {
911
/// See [`gpio_cdev::Line::request`][0] for details.
1012
///
11-
/// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
13+
/// [0]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.Line.html#method.request
1214
pub fn new(handle: gpio_cdev::LineHandle) -> Result<Self, gpio_cdev::errors::Error> {
1315
let info = handle.line().info()?;
1416
Ok(CdevPin(handle, info))
1517
}
1618

1719
fn get_input_flags(&self) -> gpio_cdev::LineRequestFlags {
18-
let mut flags = gpio_cdev::LineRequestFlags::INPUT;
1920
if self.1.is_active_low() {
20-
flags.insert(gpio_cdev::LineRequestFlags::ACTIVE_LOW);
21+
return gpio_cdev::LineRequestFlags::INPUT | gpio_cdev::LineRequestFlags::ACTIVE_LOW;
2122
}
22-
return flags;
23+
gpio_cdev::LineRequestFlags::INPUT
2324
}
2425

2526
fn get_output_flags(&self) -> gpio_cdev::LineRequestFlags {
2627
let mut flags = gpio_cdev::LineRequestFlags::OUTPUT;
28+
if self.1.is_active_low() {
29+
flags.insert(gpio_cdev::LineRequestFlags::ACTIVE_LOW);
30+
}
2731
if self.1.is_open_drain() {
2832
flags.insert(gpio_cdev::LineRequestFlags::OPEN_DRAIN);
2933
} else if self.1.is_open_source() {
@@ -33,35 +37,58 @@ impl CdevPin {
3337
}
3438
}
3539

36-
impl embedded_hal::digital::OutputPin for CdevPin {
40+
/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
41+
/// for the active_low condition.
42+
fn state_to_value(state: embedded_hal::digital::PinState, is_active_low: bool) -> u8 {
43+
if is_active_low {
44+
match state {
45+
embedded_hal::digital::PinState::High => 0,
46+
embedded_hal::digital::PinState::Low => 1,
47+
}
48+
} else {
49+
match state {
50+
embedded_hal::digital::PinState::High => 1,
51+
embedded_hal::digital::PinState::Low => 0,
52+
}
53+
}
54+
}
55+
56+
impl embedded_hal::digital::blocking::OutputPin for CdevPin {
3757
type Error = gpio_cdev::errors::Error;
3858

3959
fn set_low(&mut self) -> Result<(), Self::Error> {
40-
self.0.set_value(0)
60+
self.0.set_value(state_to_value(
61+
embedded_hal::digital::PinState::Low,
62+
self.1.is_active_low(),
63+
))
4164
}
4265

4366
fn set_high(&mut self) -> Result<(), Self::Error> {
44-
self.0.set_value(1)
67+
self.0.set_value(state_to_value(
68+
embedded_hal::digital::PinState::High,
69+
self.1.is_active_low(),
70+
))
4571
}
4672
}
4773

48-
impl embedded_hal::digital::InputPin for CdevPin {
74+
impl embedded_hal::digital::blocking::InputPin for CdevPin {
4975
type Error = gpio_cdev::errors::Error;
5076

5177
fn is_high(&self) -> Result<bool, Self::Error> {
52-
if !self.1.is_active_low() {
53-
self.0.get_value().map(|val| val != 0)
54-
} else {
55-
self.0.get_value().map(|val| val == 0)
56-
}
78+
self.0.get_value().map(|val| {
79+
val == state_to_value(
80+
embedded_hal::digital::PinState::High,
81+
self.1.is_active_low(),
82+
)
83+
})
5784
}
5885

5986
fn is_low(&self) -> Result<bool, Self::Error> {
6087
self.is_high().map(|val| !val)
6188
}
6289
}
6390

64-
impl embedded_hal::digital::IoPin<CdevPin, CdevPin> for CdevPin {
91+
impl embedded_hal::digital::blocking::IoPin<CdevPin, CdevPin> for CdevPin {
6592
type Error = gpio_cdev::errors::Error;
6693

6794
fn into_input_pin(self) -> Result<CdevPin, Self::Error> {
@@ -95,10 +122,10 @@ impl embedded_hal::digital::IoPin<CdevPin, CdevPin> for CdevPin {
95122

96123
CdevPin::new(line.request(
97124
output_flags,
98-
match state {
99-
embedded_hal::digital::PinState::High => 1,
100-
embedded_hal::digital::PinState::Low => 0,
101-
},
125+
state_to_value(
126+
state,
127+
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
128+
),
102129
&consumer,
103130
)?)
104131
}

src/delay.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Implementation of [`embedded-hal`] delay traits
2+
//!
3+
//! [`embedded-hal`]: https://docs.rs/embedded-hal
4+
5+
use cast::{u32, u64};
6+
use core::convert::Infallible;
7+
use embedded_hal::delay::blocking::{DelayMs, DelayUs};
8+
use std::thread;
9+
use std::time::Duration;
10+
11+
/// Empty struct that provides delay functionality on top of `thread::sleep`
12+
pub struct Delay;
13+
14+
impl DelayUs<u8> for Delay {
15+
type Error = Infallible;
16+
17+
fn delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
18+
thread::sleep(Duration::new(0, u32(n) * 1000));
19+
Ok(())
20+
}
21+
}
22+
23+
impl DelayUs<u16> for Delay {
24+
type Error = Infallible;
25+
26+
fn delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
27+
thread::sleep(Duration::new(0, u32(n) * 1000));
28+
Ok(())
29+
}
30+
}
31+
32+
impl DelayUs<u32> for Delay {
33+
type Error = Infallible;
34+
35+
fn delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
36+
let secs = n / 1_000_000;
37+
let nsecs = (n % 1_000_000) * 1_000;
38+
39+
thread::sleep(Duration::new(u64(secs), nsecs));
40+
Ok(())
41+
}
42+
}
43+
44+
impl DelayUs<u64> for Delay {
45+
type Error = Infallible;
46+
47+
fn delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
48+
let secs = n / 1_000_000;
49+
let nsecs = ((n % 1_000_000) * 1_000) as u32;
50+
51+
thread::sleep(Duration::new(secs, nsecs));
52+
Ok(())
53+
}
54+
}
55+
56+
impl DelayMs<u8> for Delay {
57+
type Error = Infallible;
58+
59+
fn delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
60+
thread::sleep(Duration::from_millis(u64(n)));
61+
Ok(())
62+
}
63+
}
64+
65+
impl DelayMs<u16> for Delay {
66+
type Error = Infallible;
67+
68+
fn delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
69+
thread::sleep(Duration::from_millis(u64(n)));
70+
Ok(())
71+
}
72+
}
73+
74+
impl DelayMs<u32> for Delay {
75+
type Error = Infallible;
76+
77+
fn delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
78+
thread::sleep(Duration::from_millis(u64(n)));
79+
Ok(())
80+
}
81+
}
82+
83+
impl DelayMs<u64> for Delay {
84+
type Error = Infallible;
85+
86+
fn delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
87+
thread::sleep(Duration::from_millis(n));
88+
Ok(())
89+
}
90+
}

0 commit comments

Comments
 (0)