Skip to content

Commit 4bc2d59

Browse files
committed
embedded-hal: 0.2 -> 1
1 parent 8ea3d00 commit 4bc2d59

File tree

7 files changed

+404
-109
lines changed

7 files changed

+404
-109
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Added re-exports for `libftd2xx` and `ftdi` when the respective feature is used.
1010

11+
### Changed
12+
- Updated `embedded-hal` from `0.2.4` to `1.0.0`.
13+
1114
## [0.11.0] - 2022-01-18
1215
### Added
1316
- Added support for input pins.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ default = []
1717

1818
[dependencies]
1919
nb = "^1"
20-
embedded-hal = { version = "~0.2.4", features = ["unproven"] }
20+
embedded-hal = "1.0.0-alpha.8"
2121
ftdi-mpsse = "^0.1"
2222
libftd2xx = { version = "0.32.0", optional = true }
2323
ftdi = { version = "0.1.3", optional = true }

src/delay.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,16 @@ impl Default for Delay {
3131
}
3232
}
3333

34-
macro_rules! impl_delay_for {
35-
($UXX:ty) => {
36-
impl embedded_hal::blocking::delay::DelayMs<$UXX> for Delay {
37-
fn delay_ms(&mut self, ms: $UXX) {
38-
std::thread::sleep(std::time::Duration::from_millis(ms.into()))
39-
}
40-
}
34+
impl embedded_hal::delay::blocking::DelayUs for Delay {
35+
type Error = std::convert::Infallible;
4136

42-
impl embedded_hal::blocking::delay::DelayUs<$UXX> for Delay {
43-
fn delay_us(&mut self, us: $UXX) {
44-
std::thread::sleep(std::time::Duration::from_micros(us.into()))
45-
}
46-
}
47-
};
48-
}
37+
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
38+
std::thread::sleep(std::time::Duration::from_micros(us.into()));
39+
Ok(())
40+
}
4941

50-
impl_delay_for!(u8);
51-
impl_delay_for!(u16);
52-
impl_delay_for!(u32);
53-
impl_delay_for!(u64);
42+
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
43+
std::thread::sleep(std::time::Duration::from_millis(ms.into()));
44+
Ok(())
45+
}
46+
}

src/gpio.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::error::Error;
22
use crate::{FtInner, PinUse};
33
use ftdi_mpsse::{MpsseCmdBuilder, MpsseCmdExecutor};
4-
use std::result::Result;
5-
use std::{cell::RefCell, sync::Mutex};
4+
use std::borrow::BorrowMut;
5+
use std::sync::{Arc, Mutex};
66

77
/// FTDI output pin.
88
///
@@ -13,7 +13,7 @@ use std::{cell::RefCell, sync::Mutex};
1313
#[derive(Debug)]
1414
pub struct OutputPin<'a, Device: MpsseCmdExecutor> {
1515
/// Parent FTDI device.
16-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
16+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
1717
/// GPIO pin index. 0-7 for the FT232H.
1818
idx: u8,
1919
}
@@ -25,11 +25,11 @@ where
2525
Error<E>: From<E>,
2626
{
2727
pub(crate) fn new(
28-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
28+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
2929
idx: u8,
3030
) -> Result<OutputPin<'a, Device>, Error<E>> {
31-
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
32-
let mut inner = lock.borrow_mut();
31+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
32+
let inner = lock.borrow_mut();
3333
inner.direction |= 1 << idx;
3434
inner.allocate_pin(idx, PinUse::Output);
3535
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
@@ -40,8 +40,8 @@ where
4040
}
4141

4242
pub(crate) fn set(&self, state: bool) -> Result<(), Error<E>> {
43-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
44-
let mut inner = lock.borrow_mut();
43+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
44+
let inner = lock.borrow_mut();
4545

4646
if state {
4747
inner.value |= self.mask();
@@ -65,14 +65,21 @@ impl<'a, Device: MpsseCmdExecutor> OutputPin<'a, Device> {
6565
}
6666
}
6767

68-
impl<'a, Device, E> embedded_hal::digital::v2::OutputPin for OutputPin<'a, Device>
68+
impl<'a, Device, E> embedded_hal::digital::ErrorType for OutputPin<'a, Device>
6969
where
7070
Device: MpsseCmdExecutor<Error = E>,
7171
E: std::error::Error,
7272
Error<E>: From<E>,
7373
{
7474
type Error = Error<E>;
75+
}
7576

77+
impl<'a, Device, E> embedded_hal::digital::blocking::OutputPin for OutputPin<'a, Device>
78+
where
79+
Device: MpsseCmdExecutor<Error = E>,
80+
E: std::error::Error,
81+
Error<E>: From<E>,
82+
{
7683
fn set_low(&mut self) -> Result<(), Error<E>> {
7784
self.set(false)
7885
}
@@ -91,7 +98,7 @@ where
9198
#[derive(Debug)]
9299
pub struct InputPin<'a, Device: MpsseCmdExecutor> {
93100
/// Parent FTDI device.
94-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
101+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
95102
/// GPIO pin index. 0-7 for the FT232H.
96103
idx: u8,
97104
}
@@ -103,11 +110,11 @@ where
103110
Error<E>: From<E>,
104111
{
105112
pub(crate) fn new(
106-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
113+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
107114
idx: u8,
108115
) -> Result<InputPin<'a, Device>, Error<E>> {
109-
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
110-
let mut inner = lock.borrow_mut();
116+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
117+
let inner = lock.borrow_mut();
111118
inner.direction &= !(1 << idx);
112119
inner.allocate_pin(idx, PinUse::Input);
113120
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
@@ -118,8 +125,8 @@ where
118125
}
119126

120127
pub(crate) fn get(&self) -> Result<bool, Error<E>> {
121-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
122-
let mut inner = lock.borrow_mut();
128+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
129+
let inner = lock.borrow_mut();
123130

124131
let mut buffer = [0u8; 1];
125132
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new().gpio_lower().send_immediate();
@@ -137,14 +144,21 @@ impl<'a, Device: MpsseCmdExecutor> InputPin<'a, Device> {
137144
}
138145
}
139146

140-
impl<'a, Device, E> embedded_hal::digital::v2::InputPin for InputPin<'a, Device>
147+
impl<'a, Device, E> embedded_hal::digital::ErrorType for InputPin<'a, Device>
141148
where
142149
Device: MpsseCmdExecutor<Error = E>,
143150
E: std::error::Error,
144151
Error<E>: From<E>,
145152
{
146153
type Error = Error<E>;
154+
}
147155

156+
impl<'a, Device, E> embedded_hal::digital::blocking::InputPin for InputPin<'a, Device>
157+
where
158+
Device: MpsseCmdExecutor<Error = E>,
159+
E: std::error::Error,
160+
Error<E>: From<E>,
161+
{
148162
fn is_high(&self) -> Result<bool, Self::Error> {
149163
self.get()
150164
}

src/i2c.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::error::Error;
22
use crate::error::ErrorKind::I2cNoAck;
33
use crate::{FtInner, PinUse};
44
use ftdi_mpsse::{ClockBitsIn, ClockBitsOut, MpsseCmdBuilder, MpsseCmdExecutor};
5-
use std::result::Result;
6-
use std::{cell::RefCell, sync::Mutex};
5+
use std::borrow::BorrowMut;
6+
use std::sync::{Arc, Mutex};
77

88
/// SCL bitmask
99
const SCL: u8 = 1 << 0;
@@ -21,7 +21,7 @@ const BITS_OUT: ClockBitsOut = ClockBitsOut::MsbNeg;
2121
#[derive(Debug)]
2222
pub struct I2c<'a, Device: MpsseCmdExecutor> {
2323
/// Parent FTDI device.
24-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
24+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
2525
/// Length of the start, repeated start, and stop conditions.
2626
///
2727
/// The units for these are dimensionless number of MPSSE commands.
@@ -37,9 +37,9 @@ where
3737
E: std::error::Error,
3838
Error<E>: From<E>,
3939
{
40-
pub(crate) fn new(mtx: &Mutex<RefCell<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
41-
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
42-
let mut inner = lock.borrow_mut();
40+
pub(crate) fn new(mtx: &Arc<Mutex<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
41+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
42+
let inner = lock.borrow_mut();
4343
inner.allocate_pin(0, PinUse::I2c);
4444
inner.allocate_pin(1, PinUse::I2c);
4545
inner.allocate_pin(2, PinUse::I2c);
@@ -127,8 +127,8 @@ where
127127
fn read_fast(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error<E>> {
128128
assert!(!buffer.is_empty(), "buffer must be a non-empty slice");
129129

130-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
131-
let mut inner = lock.borrow_mut();
130+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
131+
let inner = lock.borrow_mut();
132132

133133
// ST
134134
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();
@@ -198,8 +198,8 @@ where
198198
fn read_slow(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error<E>> {
199199
assert!(!buffer.is_empty(), "buffer must be a non-empty slice");
200200

201-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
202-
let mut inner = lock.borrow_mut();
201+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
202+
let inner = lock.borrow_mut();
203203

204204
// ST
205205
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();
@@ -272,8 +272,8 @@ where
272272
fn write_fast(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error<E>> {
273273
assert!(!bytes.is_empty(), "bytes must be a non-empty slice");
274274

275-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
276-
let mut inner = lock.borrow_mut();
275+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
276+
let inner = lock.borrow_mut();
277277
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();
278278

279279
// ST
@@ -333,8 +333,8 @@ where
333333
fn write_slow(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error<E>> {
334334
assert!(!bytes.is_empty(), "bytes must be a non-empty slice");
335335

336-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
337-
let mut inner = lock.borrow_mut();
336+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
337+
let inner = lock.borrow_mut();
338338

339339
// ST
340340
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();
@@ -414,8 +414,8 @@ where
414414

415415
// lock at the start to prevent GPIO from being modified while we build
416416
// the MPSSE command
417-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
418-
let mut inner = lock.borrow_mut();
417+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
418+
let inner = lock.borrow_mut();
419419

420420
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();
421421

@@ -522,8 +522,8 @@ where
522522

523523
// lock at the start to prevent GPIO from being modified while we build
524524
// the MPSSE command
525-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
526-
let mut inner = lock.borrow_mut();
525+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
526+
let inner = lock.borrow_mut();
527527

528528
// ST
529529
let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder::new();

0 commit comments

Comments
 (0)