Skip to content

Add support for embedded-hal version 1.0.0-alpha.8 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Added re-exports for `libftd2xx` and `ftdi` when the respective feature is used.
- Added `embedded-hal` version `1.0.0-alpha.8` trait implementations for:
- GPIOs
- Delay
- SPI

### Changed
- Changed the `embedded-hal` version `0.2` re-export name from `embedded-hal` to
`eh0` to differentiate from `embedded-hal` version `1.0.0-alpha.8`.

## [0.11.0] - 2022-01-18
### Added
Expand Down
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ libftd2xx-static = ["libftd2xx/static"]
default = []

[dependencies]
nb = "^1"
embedded-hal = { version = "~0.2.4", features = ["unproven"] }
ftdi-mpsse = "^0.1"
libftd2xx = { version = "0.32.0", optional = true }
eh0 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
eh1 = { package = "embedded-hal", version = "1.0.0-alpha.8" }
ftdi = { version = "0.1.3", optional = true }
ftdi-mpsse = "0.1"
libftd2xx = { version = "0.32", optional = true }
nb = "1"

[dev-dependencies]
version-sync = "~0.9.2"
spi-memory = "0.2.0"
cfg-if = "1"
eeprom24x = "0.5.0"
lm75 = "0.2.0"
spi-memory = "0.2.0"
version-sync = "0.9.2"

[badges]
maintenance = { status = "experimental" }
26 changes: 12 additions & 14 deletions examples/at24c04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ use ftdi_embedded_hal as hal;
use std::thread::sleep;
use std::time::Duration;

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

fn main() {
#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();

#[cfg(feature = "libftd2xx")]
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_freq(device, 400_000).unwrap();
let i2c = hal.i2c().unwrap();
Expand Down
28 changes: 13 additions & 15 deletions examples/blink.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
use embedded_hal::digital::v2::OutputPin;
use eh0::digital::v2::OutputPin;
use ftdi_embedded_hal as hal;
use std::{thread::sleep, time::Duration};

const NUM_BLINK: usize = 10;
const SLEEP_DURATION: Duration = Duration::from_millis(500);

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

fn main() {
#[cfg(feature = "libftd2xx")]
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();

#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_default(device).unwrap();
let mut output_pin = hal.ad3().unwrap();
Expand Down
28 changes: 13 additions & 15 deletions examples/bme280.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
//! * https://www.adafruit.com/product/4399
//! * https://www.adafruit.com/product/4472

use embedded_hal::prelude::*;
use eh0::prelude::*;
use ftdi_embedded_hal as hal;

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

fn main() {
#[cfg(feature = "libftd2xx")]
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();

#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_default(device).unwrap();
let mut i2c = hal.i2c().unwrap();
Expand Down
28 changes: 13 additions & 15 deletions examples/input.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use embedded_hal::digital::v2::InputPin;
use eh0::digital::v2::InputPin;
use ftdi_embedded_hal as hal;
use std::{thread::sleep, time::Duration};

const SLEEP_DURATION: Duration = Duration::from_millis(500);

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

fn main() {
#[cfg(feature = "libftd2xx")]
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();

#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device: libftd2xx::Ft232h = libftd2xx::Ftdi::new().unwrap().try_into().unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_default(device).unwrap();

Expand Down
26 changes: 12 additions & 14 deletions examples/lm75.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ use lm75::{Address, Lm75};
use std::thread::sleep;
use std::time::Duration;

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

fn main() {
#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();

#[cfg(feature = "libftd2xx")]
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_freq(device, 400_000).unwrap();
let i2c = hal.i2c().unwrap();
Expand Down
33 changes: 14 additions & 19 deletions examples/spi-flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ use spi_memory::series25::Flash;
use std::thread::sleep;
use std::time::Duration;

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");

const LINE: u32 = 0x10;

fn main() {
#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();

#[cfg(feature = "libftd2xx")]
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
let data: [u8; 8] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
} else if #[cfg(feature = "libftd2xx")] {
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
let data: [u8; 8] = [0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70];
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_freq(device, 1_000_000).unwrap();
let spi = hal.spi().unwrap();
Expand All @@ -31,11 +31,6 @@ fn main() {
let id = flash.read_jedec_id().unwrap();
println!("JEDEC ID: {:?}", id);

#[cfg(feature = "libftd2xx")]
let data: [u8; 8] = [0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70];
#[cfg(feature = "ftdi")]
let data: [u8; 8] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];

let addrs: [u32; 5] = [0, LINE, 2 * LINE, 3 * LINE, 4 * LINE];
let zero: [u8; 8] = [0; 8];
let mut bytes_w: [u8; 8] = [0; 8];
Expand Down
28 changes: 13 additions & 15 deletions examples/ws2812.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use embedded_hal::blocking::spi::Write;
use eh0::blocking::spi::Write;
use ftdi_embedded_hal as hal;
use std::thread::sleep;
use std::time::Duration;

#[cfg(all(feature = "ftdi", feature = "libftd2xx"))]
compile_error!("features 'ftdi' and 'libftd2xx' cannot be enabled at the same time");

#[cfg(not(any(feature = "ftdi", feature = "libftd2xx")))]
compile_error!("one of features 'ftdi' and 'libftdi2xx' shall be enabled");

fn main() {
#[cfg(feature = "libftd2xx")]
let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A").unwrap();

#[cfg(feature = "ftdi")]
let device = ftdi::find_by_vid_pid(0x0403, 0x6010)
.interface(ftdi::Interface::A)
.open()
.unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6010)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A").unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}

let hal = hal::FtHal::init_freq(device, 3_000_000).unwrap();
let mut spi = hal.spi().unwrap();
Expand Down
31 changes: 23 additions & 8 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Implementation of the [`embedded_hal::blocking::delay`] traits.
//! Implementation of the [`eh0::blocking::delay`] and [`eh1::delay::blocking`]
//! traits.

/// Delay structure.
///
Expand Down Expand Up @@ -31,23 +32,37 @@ impl Default for Delay {
}
}

macro_rules! impl_delay_for {
impl eh1::delay::blocking::DelayUs for Delay {
type Error = std::convert::Infallible;

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
std::thread::sleep(std::time::Duration::from_micros(us.into()));
Ok(())
}

fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
std::thread::sleep(std::time::Duration::from_millis(ms.into()));
Ok(())
}
}

macro_rules! impl_eh0_delay_for {
($UXX:ty) => {
impl embedded_hal::blocking::delay::DelayMs<$UXX> for Delay {
impl eh0::blocking::delay::DelayMs<$UXX> for Delay {
fn delay_ms(&mut self, ms: $UXX) {
std::thread::sleep(std::time::Duration::from_millis(ms.into()))
}
}

impl embedded_hal::blocking::delay::DelayUs<$UXX> for Delay {
impl eh0::blocking::delay::DelayUs<$UXX> for Delay {
fn delay_us(&mut self, us: $UXX) {
std::thread::sleep(std::time::Duration::from_micros(us.into()))
}
}
};
}

impl_delay_for!(u8);
impl_delay_for!(u16);
impl_delay_for!(u32);
impl_delay_for!(u64);
impl_eh0_delay_for!(u8);
impl_eh0_delay_for!(u16);
impl_eh0_delay_for!(u32);
impl_eh0_delay_for!(u64);
Loading