Skip to content

Commit

Permalink
embedded-hal-1
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jan 14, 2024
1 parent 55fb10f commit c04b088
Show file tree
Hide file tree
Showing 27 changed files with 152 additions and 199 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Move from bors/manual merge to GH merge queue
- Add tools/check.py python script for local check
- Add changelog check on PRs
- support `embedded-hal-1.0`

## [v0.10.0] - 2022-12-12

Expand All @@ -20,7 +21,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
fix errata.
- `PwmHz::get_period`: fix computation of return value, prevent division by zero
- return `i2c::Error::Timeout` instead of `nb::WouldBlock` when time is out
- support `embedded-hal-1.0-alpha`

### Breaking changes

Expand Down
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ default-target = "x86_64-unknown-linux-gnu"
[dependencies]
cortex-m = "0.7.6"
cortex-m-rt = "0.7.1"
nb = "1"
nb = "1.1"
stm32f1 = "0.15.1"
embedded-dma = "0.2.0"
bxcan = "0.7"
void = { default-features = false, version = "1.0.2" }
embedded-hal = { features = ["unproven"], version = "0.2.7" }
fugit = "0.3.6"
fugit-timer = "0.1.3"
rtic-monotonic = { version = "1.0", optional = true }
bitflags = "1.3.2"
vcell = "0.1.3"

[dependencies.embedded-hal-one]
version = "=1.0.0-alpha.8"
[dependencies.embedded-hal-02]
package = "embedded-hal"
version = "0.2.7"
features = ["unproven"]

[dependencies.embedded-hal]
version = "1.0"

[dependencies.embedded-hal-nb]
version = "1.0"

[dependencies.stm32-usbd]
version = "0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/delay-timer-blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> ! {
loop {
// On for 1s, off for 3s.
led.set_high();
// Use `embedded_hal::DelayMs` trait
// Use `embedded_hal_02::DelayMs` trait
delay.delay_ms(1000_u32);
led.set_low();
// or use `fugit` duration units
Expand Down
2 changes: 1 addition & 1 deletion examples/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() -> ! {

loop {
led.set_high();
// Use `embedded_hal::DelayMs` trait
// Use `embedded_hal_02::DelayMs` trait
delay.delay_ms(1_000_u16);
led.set_low();
// or use `fugit` duration units
Expand Down
2 changes: 1 addition & 1 deletion examples/dynamic_gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal_02::digital::v2::{InputPin, OutputPin};
use stm32f1xx_hal::{pac, prelude::*};

#[entry]
Expand Down
4 changes: 2 additions & 2 deletions examples/mfrc522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use panic_itm as _;
use cortex_m::iprintln;

use cortex_m_rt::entry;
use embedded_hal::digital::v1_compat::OldOutputPin;
use embedded_hal::spi::{Mode, Phase, Polarity};
use embedded_hal_02::digital::v1_compat::OldOutputPin;
use embedded_hal_02::spi::{Mode, Phase, Polarity};
use mfrc522::Mfrc522;
use stm32f1xx_hal::{pac, prelude::*, spi::Spi};
pub const MODE: Mode = Mode {
Expand Down
6 changes: 3 additions & 3 deletions examples/serial_9bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MSG_MAX_LEN: usize = u8::MAX as usize;
// Receives a message addressed to the slave device. Returns the size of the received message.
fn receive_msg<RX>(serial_rx: &mut RX, buf: &mut [u8; MSG_MAX_LEN]) -> usize
where
RX: embedded_hal::serial::Read<u16, Error = serial::Error>,
RX: embedded_hal_02::serial::Read<u16, Error = serial::Error>,
{
enum RxPhase {
Start,
Expand Down Expand Up @@ -79,8 +79,8 @@ where
// Send message.
fn send_msg<TX>(serial_tx: &mut TX, msg: &[u8])
where
TX: embedded_hal::serial::Write<u8, Error = Infallible>
+ embedded_hal::serial::Write<u16, Error = Infallible>,
TX: embedded_hal_02::serial::Write<u8, Error = Infallible>
+ embedded_hal_02::serial::Write<u16, Error = Infallible>,
{
// Send address.
block!(serial_tx.write(SLAVE_ADDR as u16 | 0x100)).unwrap_infallible();
Expand Down
2 changes: 1 addition & 1 deletion examples/spi-slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cortex_m_rt::entry;
use panic_halt as _;

use cortex_m::{asm, singleton};
use embedded_hal::spi::{Mode, Phase, Polarity};
use embedded_hal_02::spi::{Mode, Phase, Polarity};
pub const MODE: Mode = Mode {
phase: Phase::CaptureOnSecondTransition,
polarity: Polarity::IdleHigh,
Expand Down
2 changes: 1 addition & 1 deletion examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use cortex_m_rt::entry;
use panic_halt as _;

use embedded_hal::spi::{Mode, Phase, Polarity};
use embedded_hal_02::spi::{Mode, Phase, Polarity};
pub const MODE: Mode = Mode {
phase: Phase::CaptureOnSecondTransition,
polarity: Polarity::IdleHigh,
Expand Down
2 changes: 1 addition & 1 deletion src/adc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # API for the Analog to Digital converter

use core::marker::PhantomData;
use embedded_hal::adc::{Channel, OneShot};
use embedded_hal_02::adc::{Channel, OneShot};

#[cfg(all(feature = "stm32f103", any(feature = "high", feature = "xl",),))]
use crate::dma::dma2;
Expand Down
2 changes: 1 addition & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub struct Alternate<MODE = PushPull> {
impl<MODE> Active for Alternate<MODE> {}

/// Digital output pin state
pub use embedded_hal::digital::v2::PinState;
pub use embedded_hal_02::digital::v2::PinState;

// Using SCREAMING_SNAKE_CASE to be consistent with other HALs
// see 59b2740 and #125 for motivation
Expand Down
2 changes: 1 addition & 1 deletion src/gpio/hal_02.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
use embedded_hal_02::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};

// Pin

Expand Down
107 changes: 48 additions & 59 deletions src/gpio/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ use core::convert::Infallible;

use super::{Dynamic, ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin, Pin, PinModeError};

pub use embedded_hal_one::digital::PinState;
use embedded_hal_one::digital::{
blocking::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin},
ErrorType,
};
pub use embedded_hal::digital::PinState;
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};

fn into_state(state: PinState) -> super::PinState {
match state {
Expand All @@ -23,6 +20,14 @@ impl<const P: char, const N: u8, MODE> ErrorType for Pin<P, N, Input<MODE>> {
type Error = Infallible;
}

impl embedded_hal::digital::Error for PinModeError {
fn kind(&self) -> embedded_hal::digital::ErrorKind {
match self {
PinModeError::IncorrectMode => embedded_hal::digital::ErrorKind::Other,
}
}
}

impl<const P: char, const N: u8> ErrorType for Pin<P, N, Dynamic> {
type Error = PinModeError;
}
Expand All @@ -47,10 +52,10 @@ impl<const P: char, const N: u8> OutputPin for Pin<P, N, Dynamic> {
}

impl<const P: char, const N: u8> InputPin for Pin<P, N, Dynamic> {
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
self.is_low().map(|b| !b)
}
fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
if self.mode.is_input() {
Ok(self._is_low())
} else {
Expand All @@ -74,44 +79,36 @@ impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {

impl<const P: char, const N: u8, MODE> StatefulOutputPin for Pin<P, N, Output<MODE>> {
#[inline]
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
#[inline]
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}

impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<MODE>> {
#[inline(always)]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Ok(())
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}

impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, Input<MODE>> {
#[inline]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

#[inline]
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}

impl<const P: char, const N: u8> InputPin for Pin<P, N, Output<OpenDrain>> {
#[inline]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

#[inline]
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}

Expand All @@ -137,45 +134,37 @@ impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {

impl<const P: char, MODE> StatefulOutputPin for PartiallyErasedPin<P, Output<MODE>> {
#[inline(always)]
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}

#[inline(always)]
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}

impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<MODE>> {
#[inline(always)]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Ok(())
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}

impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

#[inline(always)]
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}

impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, Input<MODE>> {
#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

#[inline(always)]
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}

Expand All @@ -198,31 +187,31 @@ impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
}

impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}

impl<MODE> InputPin for ErasedPin<Input<MODE>> {
fn is_high(&self) -> Result<bool, Infallible> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok((*self).is_high())
}

fn is_low(&self) -> Result<bool, Infallible> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Infallible> {
Ok((*self).is_low())
}
}

impl InputPin for ErasedPin<Output<OpenDrain>> {
fn is_high(&self) -> Result<bool, Infallible> {
Ok(self.is_high())
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok((*self).is_high())
}

fn is_low(&self) -> Result<bool, Infallible> {
Ok(self.is_low())
fn is_low(&mut self) -> Result<bool, Infallible> {
Ok((*self).is_low())
}
}
2 changes: 1 addition & 1 deletion src/i2c/hal_02.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use embedded_hal::blocking::i2c::{Read, Write, WriteRead};
use embedded_hal_02::blocking::i2c::{Read, Write, WriteRead};

impl<I2C: Instance, PINS> Write for BlockingI2c<I2C, PINS> {
type Error = Error;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ compile_error!(
);

#[cfg(feature = "device-selected")]
use embedded_hal as hal;
pub use embedded_hal as hal;
#[cfg(feature = "device-selected")]
pub use embedded_hal_02 as hal_02;

#[cfg(feature = "stm32f100")]
pub use stm32f1::stm32f100 as pac;
Expand Down
6 changes: 2 additions & 4 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ pub use crate::dma::ReadWriteDma as _stm32_hal_dma_ReadWriteDma;
pub use crate::dma::WriteDma as _stm32_hal_dma_WriteDma;
pub use crate::flash::FlashExt as _stm32_hal_flash_FlashExt;
pub use crate::gpio::GpioExt as _stm32_hal_gpio_GpioExt;
pub use crate::hal::adc::OneShot as _embedded_hal_adc_OneShot;
pub use crate::hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_StatefulOutputPin;
pub use crate::hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin;
pub use crate::hal::prelude::*;
pub use crate::hal_02::adc::OneShot as _embedded_hal_adc_OneShot;
pub use crate::hal_02::prelude::*;
pub use crate::rcc::RccExt as _stm32_hal_rcc_RccExt;
pub use crate::time::U32Ext as _stm32_hal_time_U32Ext;
#[cfg(feature = "rtic")]
Expand Down
Loading

0 comments on commit c04b088

Please sign in to comment.