Skip to content
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

use embedded-hal 1.0 with ehal1 feature #338

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Use both `0.2.x` and `1.0` `embedded-hal` crates.
With `ehal1` option enabled traits of `1.0` version are used.
- Add `Spi::new` and deprecate `Spi:spix`, deprecate `Serial::usartx`, remove deprecated `I2c::i2cx`
- Deprecate `free` in favour of `release`
- Clean features in `serial`, `spi`, `i2c`, `timer`
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ display-interface = { version = "0.4.0", optional = true }
version = "0.2.0"
optional = true

[dependencies.ehal1]
package = "embedded-hal"
git = "https://github.com/rust-embedded/embedded-hal"
optional = true

[dev-dependencies]
panic-semihosting = "0.5.3"
cortex-m-semihosting = "0.3.3"
Expand Down
8 changes: 7 additions & 1 deletion src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*/

use crate::dma::traits::PeriAddress;
use crate::hal_nb::adc::{Channel, OneShot};
use crate::rcc::{Enable, Reset};
use crate::{gpio::*, pac, signature::VrefCal, signature::VDDA_CALIB};
use core::fmt;
use embedded_hal::adc::{Channel, OneShot};

/// Vref internal signal, used for calibration
pub struct Vref;
Expand All @@ -28,7 +28,10 @@ macro_rules! adc_pins {
$(
impl Channel<pac::$adc> for $pin {
type ID = u8;
#[cfg(not(feature = "ehal1"))]
fn channel() -> u8 { $chan }
#[cfg(feature = "ehal1")]
fn channel(&self) -> u8 { $chan }
}
)+
};
Expand Down Expand Up @@ -909,7 +912,10 @@ macro_rules! adc {
}
});

#[cfg(not(feature = "ehal1"))]
let channel = CHANNEL::channel();
#[cfg(feature = "ehal1")]
let channel = _channel.channel();

//Set the channel in the right sequence field
match sequence {
Expand Down
73 changes: 71 additions & 2 deletions src/delay/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,84 @@
//! Delays

use crate::hal::blocking::delay::{DelayMs, DelayUs};

mod syst;

use cortex_m::peripheral::SYST;

use crate::rcc::Clocks;

/// Timer as a delay provider (SysTick by default)
pub struct Delay<T = SYST> {
tim: T,
pub struct Delay<TIM = SYST> {
tim: TIM,
clocks: Clocks,
}

mod timer;

mod infallible {
pub trait DelayMs<UXX> {
fn delay_ms(&mut self, ms: UXX);
}

pub trait DelayUs<UXX> {
fn delay_us(&mut self, us: UXX);
}
}

impl<TIM> Delay<TIM> {
pub fn delay_ms<T>(&mut self, ms: T)
where
Self: infallible::DelayMs<T>,
{
<Self as infallible::DelayMs<T>>::delay_ms(self, ms)
}
pub fn delay_us<T>(&mut self, us: T)
where
Self: infallible::DelayUs<T>,
{
<Self as infallible::DelayUs<T>>::delay_us(self, us)
}
}

#[cfg(not(feature = "ehal1"))]
impl<TIM, UXX> DelayMs<UXX> for Delay<TIM>
where
Self: infallible::DelayMs<UXX>,
{
fn delay_ms(&mut self, ms: UXX) {
<Self as infallible::DelayMs<UXX>>::delay_ms(self, ms);
}
}
#[cfg(feature = "ehal1")]
impl<TIM, UXX> DelayMs<UXX> for Delay<TIM>
where
Self: infallible::DelayMs<UXX>,
{
type Error = core::convert::Infallible;
fn delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error> {
<Self as infallible::DelayMs<UXX>>::delay_ms(self, ms);
Ok(())
}
}

#[cfg(not(feature = "ehal1"))]
impl<TIM, UXX> DelayUs<UXX> for Delay<TIM>
where
Self: infallible::DelayUs<UXX>,
{
fn delay_us(&mut self, us: UXX) {
<Self as infallible::DelayUs<UXX>>::delay_us(self, us);
}
}
#[cfg(feature = "ehal1")]
impl<TIM, UXX> DelayUs<UXX> for Delay<TIM>
where
Self: infallible::DelayUs<UXX>,
{
type Error = core::convert::Infallible;
fn delay_us(&mut self, us: UXX) -> Result<(), Self::Error> {
<Self as infallible::DelayUs<UXX>>::delay_us(self, us);
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/delay/syst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use cast::u32;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;

use super::infallible::{DelayMs, DelayUs};
use crate::rcc::Clocks;
use embedded_hal::blocking::delay::{DelayMs, DelayUs};

use super::Delay;

Expand Down
2 changes: 1 addition & 1 deletion src/delay/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use core::cmp::max;

use super::infallible::{DelayMs, DelayUs};
use cast::{u16, u32};
use embedded_hal::blocking::delay::{DelayMs, DelayUs};

use crate::{
pac::{self, RCC},
Expand Down
26 changes: 25 additions & 1 deletion src/dwt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Debug and trace and stuff

use crate::hal::blocking::delay::{DelayMs, DelayUs};
use crate::rcc::Clocks;
use crate::time::Hertz;
use cortex_m::peripheral::{DCB, DWT};
use embedded_hal::blocking::delay::{DelayMs, DelayUs};

pub trait DwtExt {
fn constrain(self, dcb: DCB, clocks: Clocks) -> Dwt;
Expand Down Expand Up @@ -100,6 +100,7 @@ impl Delay {
}

// Implement DelayUs/DelayMs for various integer types
#[cfg(not(feature = "ehal1"))]
impl<T: Into<u64>> DelayUs<T> for Delay {
fn delay_us(&mut self, us: T) {
// Convert us to ticks
Expand All @@ -108,6 +109,18 @@ impl<T: Into<u64>> DelayUs<T> for Delay {
Delay::delay_ticks(start, ticks);
}
}
#[cfg(feature = "ehal1")]
impl<T: Into<u64>> DelayUs<T> for Delay {
type Error = core::convert::Infallible;
fn delay_us(&mut self, us: T) -> Result<(), Self::Error> {
// Convert us to ticks
let start = DWT::get_cycle_count();
let ticks = (us.into() * self.clock.0 as u64) / 1_000_000;
Delay::delay_ticks(start, ticks);
Ok(())
}
}
#[cfg(not(feature = "ehal1"))]
impl<T: Into<u64>> DelayMs<T> for Delay {
fn delay_ms(&mut self, ms: T) {
// Convert ms to ticks
Expand All @@ -116,6 +129,17 @@ impl<T: Into<u64>> DelayMs<T> for Delay {
Delay::delay_ticks(start, ticks);
}
}
#[cfg(feature = "ehal1")]
impl<T: Into<u64>> DelayMs<T> for Delay {
type Error = core::convert::Infallible;
fn delay_ms(&mut self, ms: T) -> Result<(), Self::Error> {
// Convert ms to ticks
let start = DWT::get_cycle_count();
let ticks = (ms.into() * self.clock.0 as u64) / 1_000;
Delay::delay_ticks(start, ticks);
Ok(())
}
}

/// Very simple stopwatch which reads from DWT Cycle Counter to record timing.
///
Expand Down
5 changes: 4 additions & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use core::convert::Infallible;
use core::marker::PhantomData;

use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
#[cfg(feature = "ehal1")]
use crate::hal::blocking::digital::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
#[cfg(not(feature = "ehal1"))]
use crate::hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};

use crate::pac::EXTI;
use crate::syscfg::SysCfg;
Expand Down
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use core::ops::Deref;
use embedded_hal::blocking::i2c::{Read, Write, WriteRead};

use crate::pac::i2c1;
use crate::rcc::{Enable, Reset};
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ compile_error!(
stm32f479"
);

#[cfg(feature = "ehal1")]
pub use ehal1 as hal;
#[cfg(not(feature = "ehal1"))]
pub use embedded_hal as hal;

#[cfg(not(feature = "ehal1"))]
use crate::hal as hal_blocking;
#[cfg(not(feature = "ehal1"))]
use crate::hal as hal_nb;
#[cfg(feature = "ehal1")]
use crate::hal::blocking as hal_blocking;
#[cfg(feature = "ehal1")]
use crate::hal::nb as hal_nb;

pub use nb;
pub use nb::block;

Expand Down
47 changes: 31 additions & 16 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,42 @@
//! ```
//! use stm32f4xx_hal::prelude::*;
//! ```
pub use embedded_hal::adc::OneShot as _embedded_hal_adc_OneShot;
pub use embedded_hal::blocking::delay::DelayMs as _embedded_hal_blocking_delay_DelayMs;
pub use embedded_hal::blocking::delay::DelayUs as _embedded_hal_blocking_delay_DelayUs;
pub use embedded_hal::blocking::i2c::{
pub use crate::hal::blocking::delay::DelayMs as _embedded_hal_blocking_delay_DelayMs;
pub use crate::hal::blocking::delay::DelayUs as _embedded_hal_blocking_delay_DelayUs;
pub use crate::hal::blocking::i2c::{
Read as _embedded_hal_blocking_i2c_Read, Write as _embedded_hal_blocking_i2c_Write,
WriteRead as _embedded_hal_blocking_i2c_WriteRead,
};
pub use embedded_hal::blocking::serial::Write as _embedded_hal_blocking_serial_Write;
pub use embedded_hal::blocking::spi::{
#[cfg(feature = "ehal1")]
pub use crate::hal::blocking::pwm::Pwm as _embedded_hal_blocking_pwm_Pwm;
#[cfg(feature = "ehal1")]
pub use crate::hal::blocking::qei::Qei as _embedded_hal_blocking_qei_Qei;
pub use crate::hal::blocking::serial::Write as _embedded_hal_blocking_serial_Write;
pub use crate::hal::blocking::spi::{
Transfer as _embedded_hal_blocking_spi_Transfer, Write as _embedded_hal_blocking_spi_Write,
};
pub use embedded_hal::serial::Read as _embedded_hal_serial_Read;
pub use embedded_hal::serial::Write as _embedded_hal_serial_Write;
pub use embedded_hal::spi::FullDuplex as _embedded_hal_spi_FullDuplex;
pub use embedded_hal::timer::CountDown as _embedded_hal_timer_CountDown;
pub use embedded_hal::watchdog::Watchdog as _embedded_hal_watchdog_Watchdog;
pub use embedded_hal::watchdog::WatchdogDisable as _embedded_hal_watchdog_WatchdogDisable;
pub use embedded_hal::watchdog::WatchdogEnable as _embedded_hal_watchdog_WatchdogEnable;
pub use embedded_hal::Capture as _embedded_hal_Capture;
pub use embedded_hal::Pwm as _embedded_hal_Pwm;
pub use embedded_hal::Qei as _embedded_hal_Qei;
#[cfg(feature = "ehal1")]
pub use crate::hal::blocking::watchdog::Disable as _embedded_hal_blocking_watchdog_Disable;
#[cfg(feature = "ehal1")]
pub use crate::hal::blocking::watchdog::Enable as _embedded_hal_blocking_watchdog_Enable;
#[cfg(feature = "ehal1")]
pub use crate::hal::nb::capture::Capture as _embedded_hal_nb_capture_Capture;
#[cfg(not(feature = "ehal1"))]
pub use crate::hal::watchdog::WatchdogDisable as _embedded_hal_blocking_watchdog_Disable;
#[cfg(not(feature = "ehal1"))]
pub use crate::hal::watchdog::WatchdogEnable as _embedded_hal_blocking_watchdog_Enable;
#[cfg(not(feature = "ehal1"))]
pub use crate::hal::Capture as _embedded_hal_nb_capture_Capture;
#[cfg(not(feature = "ehal1"))]
pub use crate::hal::Pwm as _embedded_hal_blocking_pwm_Pwm;
#[cfg(not(feature = "ehal1"))]
pub use crate::hal::Qei as _embedded_hal_blocking_qei_Qei;
pub use crate::hal_blocking::watchdog::Watchdog as _embedded_hal_blocking_watchdog_Watchdog;
pub use crate::hal_nb::adc::OneShot as _embedded_hal_nb_adc_OneShot;
pub use crate::hal_nb::serial::Read as _embedded_hal_nb_serial_Read;
pub use crate::hal_nb::serial::Write as _embedded_hal_nb_serial_Write;
pub use crate::hal_nb::spi::FullDuplex as _embedded_hal_nb_spi_FullDuplex;
pub use crate::hal_nb::timer::CountDown as _embedded_hal_nb_timer_CountDown;

#[cfg(all(feature = "device-selected", feature = "dac"))]
pub use crate::dac::DacExt as _stm32f4xx_hal_dac_DacExt;
Expand Down
Loading