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

Upgrade to embedded-hal 1.0.0-alpha.6 #26

Closed
wants to merge 3 commits 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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ulp = []
[dependencies]
nb = "0.1.2"
mutex-trait = "0.2"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal = "=1.0.0-alpha.6"
esp-idf-sys = { version = "0.28.1", optional = true, default-features = false, features = ["pio"] }

[build-dependencies]
Expand Down
12 changes: 6 additions & 6 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,23 @@ impl<ADC: Adc> PoweredAdc<ADC> {
}

#[cfg(not(feature = "ulp"))]
impl<ADC, AN, PIN> embedded_hal::adc::OneShot<AN, u16, PIN> for PoweredAdc<ADC>
impl<ADC, AN, PIN> embedded_hal::adc::nb::OneShot<AN, u16, PIN> for PoweredAdc<ADC>
where
ADC: Adc,
AN: Analog<ADC>,
PIN: embedded_hal::adc::Channel<AN, ID = u8>,
PIN: embedded_hal::adc::nb::Channel<AN, ID = u8>,
{
type Error = EspError;

fn read(&mut self, _pin: &mut PIN) -> nb::Result<u16, Self::Error> {
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
let mut measurement = 0_i32;

if ADC::unit() == adc_unit_t_ADC_UNIT_1 {
measurement = unsafe { adc1_get_raw(PIN::channel() as adc_channel_t) };
measurement = unsafe { adc1_get_raw(pin.channel() as adc_channel_t) };
} else {
let res = unsafe {
adc2_get_raw(
PIN::channel() as adc_channel_t,
pin.channel() as adc_channel_t,
self.resolution.into(),
&mut measurement as *mut _,
)
Expand All @@ -296,7 +296,7 @@ where
}

#[cfg(all(esp32, not(feature = "ulp")))]
impl embedded_hal::adc::OneShot<ADC1, u16, hall::HallSensor> for PoweredAdc<ADC1> {
impl embedded_hal::adc::nb::OneShot<ADC1, u16, hall::HallSensor> for PoweredAdc<ADC1> {
type Error = EspError;

fn read(&mut self, _hall_sensor: &mut hall::HallSensor) -> nb::Result<u16, Self::Error> {
Expand Down
57 changes: 18 additions & 39 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::time::Duration;

use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::delay::blocking::DelayUs;

use esp_idf_sys::*;

Expand Down Expand Up @@ -50,61 +49,41 @@ impl From<TickType> for Option<Duration> {
/// Espressif Task Scheduler-based delay provider
pub struct Ets;

impl DelayUs<u32> for Ets {
fn delay_us(&mut self, us: u32) {
impl DelayUs for Ets {
type Error = core::convert::Infallible;

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
unsafe {
ets_delay_us(us);
}
Ok(())
}
}

impl DelayUs<u16> for Ets {
fn delay_us(&mut self, us: u16) {
DelayUs::<u32>::delay_us(self, us as u32);
}
}

impl DelayUs<u8> for Ets {
fn delay_us(&mut self, us: u8) {
DelayUs::<u32>::delay_us(self, us as u32);
}
}

impl DelayMs<u32> for Ets {
fn delay_ms(&mut self, ms: u32) {
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
unsafe {
ets_delay_us(ms * 1000);
}
}
}

impl DelayMs<u16> for Ets {
fn delay_ms(&mut self, ms: u16) {
DelayMs::<u32>::delay_ms(self, ms as u32);
}
}

impl DelayMs<u8> for Ets {
fn delay_ms(&mut self, ms: u8) {
DelayMs::<u32>::delay_ms(self, ms as u32);
Ok(())
}
}

/// FreeRTOS-based delay provider
pub struct FreeRtos;

impl DelayMs<u32> for FreeRtos {
fn delay_ms(&mut self, ms: u32) {
impl DelayUs for FreeRtos {
type Error = core::convert::Infallible;

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
let ms = us / 1000;
Self::delay_ms(self, ms)
}

fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
// divide by tick length, rounding up
let ticks = (ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
unsafe {
vTaskDelay(ticks);
}
}
}

impl DelayMs<u16> for FreeRtos {
fn delay_ms(&mut self, ms: u16) {
DelayMs::<u32>::delay_ms(self, ms as u32);
Ok(())
}
}
52 changes: 23 additions & 29 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where

impl InputPin for GpioPin<Input> {}

impl embedded_hal::digital::v2::InputPin for GpioPin<Input> {
impl embedded_hal::digital::blocking::InputPin for GpioPin<Input> {
type Error = EspError;

fn is_high(&self) -> Result<bool, Self::Error> {
Expand All @@ -196,7 +196,7 @@ impl embedded_hal::digital::v2::InputPin for GpioPin<Input> {

impl OutputPin for GpioPin<Output> {}

impl embedded_hal::digital::v2::OutputPin for GpioPin<Output> {
impl embedded_hal::digital::blocking::OutputPin for GpioPin<Output> {
type Error = EspError;

fn set_high(&mut self) -> Result<(), Self::Error> {
Expand All @@ -208,7 +208,7 @@ impl embedded_hal::digital::v2::OutputPin for GpioPin<Output> {
}
}

impl embedded_hal::digital::v2::StatefulOutputPin for GpioPin<Output> {
impl embedded_hal::digital::blocking::StatefulOutputPin for GpioPin<Output> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.get_output_level())
}
Expand All @@ -218,11 +218,9 @@ impl embedded_hal::digital::v2::StatefulOutputPin for GpioPin<Output> {
}
}

impl embedded_hal::digital::v2::toggleable::Default for GpioPin<Output> {}

impl InputPin for GpioPin<InputOutput> {}

impl embedded_hal::digital::v2::InputPin for GpioPin<InputOutput> {
impl embedded_hal::digital::blocking::InputPin for GpioPin<InputOutput> {
type Error = EspError;

fn is_high(&self) -> Result<bool, Self::Error> {
Expand All @@ -236,7 +234,7 @@ impl embedded_hal::digital::v2::InputPin for GpioPin<InputOutput> {

impl OutputPin for GpioPin<InputOutput> {}

impl embedded_hal::digital::v2::OutputPin for GpioPin<InputOutput> {
impl embedded_hal::digital::blocking::OutputPin for GpioPin<InputOutput> {
type Error = EspError;

fn set_high(&mut self) -> Result<(), Self::Error> {
Expand All @@ -248,7 +246,7 @@ impl embedded_hal::digital::v2::OutputPin for GpioPin<InputOutput> {
}
}

impl embedded_hal::digital::v2::StatefulOutputPin for GpioPin<InputOutput> {
impl embedded_hal::digital::blocking::StatefulOutputPin for GpioPin<InputOutput> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.get_output_level())
}
Expand All @@ -258,17 +256,13 @@ impl embedded_hal::digital::v2::StatefulOutputPin for GpioPin<InputOutput> {
}
}

impl embedded_hal::digital::v2::toggleable::Default for GpioPin<InputOutput> {}

// Not possible with embedded-hal V0.2 (possible with V1.0)
// because in V0.2, channel() does not take &self
// impl<AN: Analog<ADC>, ADC: Adc> embedded_hal::adc::Channel for GpioPin<AN> {
// type ID = u8;
impl embedded_hal::digital::blocking::ToggleableOutputPin for GpioPin<InputOutput> {
type Error = EspError;

// fn channel() -> Self::ID {
// todo!()
// }
// }
fn toggle(&mut self) -> Result<(), Self::Error> {
self.set_output_level(!self.get_output_level())
}
}

/// Interrupt events
///
Expand Down Expand Up @@ -345,7 +339,7 @@ impl From<gpio_drive_cap_t> for DriveStrength {

macro_rules! impl_hal_input_pin {
($pxi:ident: $mode:ident) => {
impl embedded_hal::digital::v2::InputPin for $pxi<$mode> {
impl embedded_hal::digital::blocking::InputPin for $pxi<$mode> {
type Error = EspError;

fn is_high(&self) -> Result<bool, Self::Error> {
Expand Down Expand Up @@ -379,7 +373,7 @@ macro_rules! impl_hal_output_pin {
}
}

impl embedded_hal::digital::v2::OutputPin for $pxi<$mode> {
impl embedded_hal::digital::blocking::OutputPin for $pxi<$mode> {
type Error = EspError;

fn set_high(&mut self) -> Result<(), Self::Error> {
Expand All @@ -397,7 +391,7 @@ macro_rules! impl_hal_output_pin {
}
}

impl embedded_hal::digital::v2::StatefulOutputPin for $pxi<$mode> {
impl embedded_hal::digital::blocking::StatefulOutputPin for $pxi<$mode> {
#[cfg(not(feature = "ulp"))]
fn is_set_high(&self) -> Result<bool, Self::Error> {
let pin = $pxi::<$mode>::runtime_pin() as u32;
Expand Down Expand Up @@ -426,7 +420,7 @@ macro_rules! impl_hal_output_pin {
}
}

impl embedded_hal::digital::v2::ToggleableOutputPin for $pxi<$mode> {
impl embedded_hal::digital::blocking::ToggleableOutputPin for $pxi<$mode> {
type Error = EspError;

fn toggle(&mut self) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -784,13 +778,13 @@ macro_rules! impl_adc {
}
}

impl<AN> embedded_hal::adc::Channel<AN> for $pxi<AN>
impl<AN> embedded_hal::adc::nb::Channel<AN> for $pxi<AN>
where
AN: adc::Analog<adc::ADC1> + Send,
{
type ID = u8;

fn channel() -> Self::ID {
fn channel(&self) -> Self::ID {
$adc as u8
}
}
Expand Down Expand Up @@ -852,13 +846,13 @@ macro_rules! impl_adc {
}
}

impl<AN> embedded_hal::adc::Channel<AN> for $pxi<AN>
impl<AN> embedded_hal::adc::nb::Channel<AN> for $pxi<AN>
where
AN: adc::Analog<adc::ADC2> + Send,
{
type ID = u8;

fn channel() -> Self::ID {
fn channel(&self) -> Self::ID {
$adc as u8
}
}
Expand Down Expand Up @@ -921,7 +915,7 @@ macro_rules! pin {
mod chip {
use core::marker::PhantomData;

use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin as _};
use embedded_hal::digital::blocking::{OutputPin as _, StatefulOutputPin as _};
#[cfg(not(feature = "ulp"))]
use esp_idf_sys::*;

Expand Down Expand Up @@ -1101,7 +1095,7 @@ mod chip {
mod chip {
use core::marker::PhantomData;

use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin as _};
use embedded_hal::digital::blocking::{OutputPin as _, StatefulOutputPin as _};
#[cfg(not(feature = "ulp"))]
use esp_idf_sys::*;

Expand Down Expand Up @@ -1345,7 +1339,7 @@ mod chip {
mod chip {
use core::marker::PhantomData;

use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin as _};
use embedded_hal::digital::blocking::{OutputPin as _, StatefulOutputPin as _};
use esp_idf_sys::*;

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions src/hall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ impl HallSensor {

unsafe impl Send for HallSensor {}

impl embedded_hal::adc::Channel<adc::ADC1> for HallSensor {
impl embedded_hal::adc::nb::Channel<adc::ADC1> for HallSensor {
type ID = ();

fn channel() -> Self::ID {
fn channel(&self) -> Self::ID {
()
}
}
Loading