Skip to content

Commit d3d240c

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

File tree

6 files changed

+132
-64
lines changed

6 files changed

+132
-64
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -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/lib.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ pub use libftd2xx;
154154
mod delay;
155155
mod error;
156156
mod gpio;
157-
mod i2c;
157+
// mod i2c;
158158
mod spi;
159159

160160
use crate::error::Error;
161161
pub use delay::Delay;
162162
pub use gpio::{InputPin, OutputPin};
163-
pub use i2c::I2c;
163+
// pub use i2c::I2c;
164164
pub use spi::Spi;
165165

166166
use ftdi_mpsse::{MpsseCmdExecutor, MpsseSettings};
@@ -169,7 +169,7 @@ use std::{cell::RefCell, sync::Mutex};
169169
/// State tracker for each pin on the FTDI chip.
170170
#[derive(Debug, Clone, Copy)]
171171
enum PinUse {
172-
I2c,
172+
// I2c,
173173
Spi,
174174
Output,
175175
Input,
@@ -178,7 +178,7 @@ enum PinUse {
178178
impl std::fmt::Display for PinUse {
179179
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180180
match self {
181-
PinUse::I2c => write!(f, "I2C"),
181+
// PinUse::I2c => write!(f, "I2C"),
182182
PinUse::Spi => write!(f, "SPI"),
183183
PinUse::Output => write!(f, "OUTPUT"),
184184
PinUse::Input => write!(f, "INPUT"),
@@ -372,36 +372,36 @@ where
372372
Spi::new(&self.mtx)
373373
}
374374

375-
/// Aquire the I2C peripheral for the FT232H.
376-
///
377-
/// Pin assignments:
378-
/// * AD0 => SCL
379-
/// * AD1 => SDA
380-
/// * AD2 => SDA
381-
///
382-
/// Yes, AD1 and AD2 are both SDA.
383-
/// These pins must be shorted together for I2C operation.
384-
///
385-
/// # Panics
386-
///
387-
/// Panics if pin 0, 1, or 2 are already in use.
388-
///
389-
/// # Example
390-
///
391-
/// ```no_run
392-
/// use ftdi_embedded_hal as hal;
393-
///
394-
/// # #[cfg(feature = "libftd2xx")]
395-
/// # {
396-
/// let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A")?;
397-
/// let hal = hal::FtHal::init_freq(device, 3_000_000)?;
398-
/// let i2c = hal.i2c()?;
399-
/// # }
400-
/// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
401-
/// ```
402-
pub fn i2c(&self) -> Result<I2c<Device>, Error<E>> {
403-
I2c::new(&self.mtx)
404-
}
375+
// /// Aquire the I2C peripheral for the FT232H.
376+
// ///
377+
// /// Pin assignments:
378+
// /// * AD0 => SCL
379+
// /// * AD1 => SDA
380+
// /// * AD2 => SDA
381+
// ///
382+
// /// Yes, AD1 and AD2 are both SDA.
383+
// /// These pins must be shorted together for I2C operation.
384+
// ///
385+
// /// # Panics
386+
// ///
387+
// /// Panics if pin 0, 1, or 2 are already in use.
388+
// ///
389+
// /// # Example
390+
// ///
391+
// /// ```no_run
392+
// /// use ftdi_embedded_hal as hal;
393+
// ///
394+
// /// # #[cfg(feature = "libftd2xx")]
395+
// /// # {
396+
// /// let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A")?;
397+
// /// let hal = hal::FtHal::init_freq(device, 3_000_000)?;
398+
// /// let i2c = hal.i2c()?;
399+
// /// # }
400+
// /// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
401+
// /// ```
402+
// pub fn i2c(&self) -> Result<I2c<Device>, Error<E>> {
403+
// I2c::new(&self.mtx)
404+
// }
405405

406406
/// Aquire the digital output pin 0 for the FT232H.
407407
///

src/spi.rs

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,42 @@ where
8888
}
8989
}
9090

91-
impl<'a, Device, E> embedded_hal::blocking::spi::Write<u8> for Spi<'a, Device>
91+
impl<E> embedded_hal::spi::Error for Error<E>
92+
where
93+
E: std::error::Error,
94+
Error<E>: From<E>,
95+
{
96+
fn kind(&self) -> embedded_hal::spi::ErrorKind {
97+
embedded_hal::spi::ErrorKind::Other
98+
}
99+
}
100+
101+
impl<'a, Device, E> embedded_hal::spi::ErrorType for Spi<'a, Device>
92102
where
93103
Device: MpsseCmdExecutor<Error = E>,
94104
E: std::error::Error,
95105
Error<E>: From<E>,
96106
{
97107
type Error = Error<E>;
108+
}
109+
110+
impl<'a, Device, E> embedded_hal::spi::blocking::SpiBusFlush for Spi<'a, Device>
111+
where
112+
Device: MpsseCmdExecutor<Error = E>,
113+
E: std::error::Error,
114+
Error<E>: From<E>,
115+
{
116+
fn flush(&mut self) -> Result<(), Self::Error> {
117+
Ok(())
118+
}
119+
}
98120

121+
impl<'a, Device, E> embedded_hal::spi::blocking::SpiBusWrite<u8> for Spi<'a, Device>
122+
where
123+
Device: MpsseCmdExecutor<Error = E>,
124+
E: std::error::Error,
125+
Error<E>: From<E>,
126+
{
99127
fn write(&mut self, words: &[u8]) -> Result<(), Error<E>> {
100128
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
101129
.clock_data_out(self.clk_out, words)
@@ -110,15 +138,34 @@ where
110138
}
111139
}
112140

113-
impl<'a, Device, E> embedded_hal::blocking::spi::Transfer<u8> for Spi<'a, Device>
141+
impl<'a, Device, E> embedded_hal::spi::blocking::SpiBusRead<u8> for Spi<'a, Device>
114142
where
115143
Device: MpsseCmdExecutor<Error = E>,
116144
E: std::error::Error,
117145
Error<E>: From<E>,
118146
{
119-
type Error = Error<E>;
147+
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
148+
let data_out: Vec<u8> = vec![0; words.len()];
149+
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
150+
.clock_data(self.clk, &data_out)
151+
.send_immediate();
152+
153+
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
154+
let mut inner = lock.borrow_mut();
155+
inner.ft.send(cmd.as_slice())?;
156+
inner.ft.recv(words)?;
120157

121-
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error<E>> {
158+
Ok(())
159+
}
160+
}
161+
162+
impl<'a, Device, E> embedded_hal::spi::blocking::SpiBus<u8> for Spi<'a, Device>
163+
where
164+
Device: MpsseCmdExecutor<Error = E>,
165+
E: std::error::Error,
166+
Error<E>: From<E>,
167+
{
168+
fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Error<E>> {
122169
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
123170
.clock_data(self.clk, words)
124171
.send_immediate();
@@ -128,18 +175,29 @@ where
128175
inner.ft.send(cmd.as_slice())?;
129176
inner.ft.recv(words)?;
130177

131-
Ok(words)
178+
Ok(())
179+
}
180+
181+
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error<E>> {
182+
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
183+
.clock_data(self.clk, write)
184+
.send_immediate();
185+
186+
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
187+
let mut inner = lock.borrow_mut();
188+
inner.ft.send(cmd.as_slice())?;
189+
inner.ft.recv(read)?;
190+
191+
Ok(())
132192
}
133193
}
134194

135-
impl<'a, Device, E> embedded_hal::spi::FullDuplex<u8> for Spi<'a, Device>
195+
impl<'a, Device, E> embedded_hal::spi::nb::FullDuplex<u8> for Spi<'a, Device>
136196
where
137197
Device: MpsseCmdExecutor<Error = E>,
138198
E: std::error::Error,
139199
Error<E>: From<E>,
140200
{
141-
type Error = Error<E>;
142-
143201
fn read(&mut self) -> nb::Result<u8, Error<E>> {
144202
let mut buf: [u8; 1] = [0];
145203
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
@@ -155,7 +213,7 @@ where
155213
}
156214
}
157215

158-
fn send(&mut self, byte: u8) -> nb::Result<(), Error<E>> {
216+
fn write(&mut self, byte: u8) -> nb::Result<(), Error<E>> {
159217
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
160218
.clock_data_out(self.clk_out, &[byte])
161219
.send_immediate();

0 commit comments

Comments
 (0)