diff --git a/rp2040-hal/Cargo.toml b/rp2040-hal/Cargo.toml index 8322ec1a1..d089f87da 100644 --- a/rp2040-hal/Cargo.toml +++ b/rp2040-hal/Cargo.toml @@ -17,9 +17,9 @@ targets = ["thumbv6m-none-eabi"] [dependencies] cortex-m = "0.7.2" -embedded-hal = { version = "0.2.5", features = ["unproven"] } -eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.3", optional = true } -eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.3", optional = true } +embedded_hal_0_2 = { package = "embedded-hal", version = "0.2.5", features = ["unproven"] } +embedded-hal = "1.0.0" +embedded-hal-nb = "1.0.0" embedded-dma = "0.2.0" embedded-io = "0.6.1" fugit = "0.3.6" @@ -79,9 +79,6 @@ rp2040-e5 = [] # critical section that is safe for multicore use critical-section-impl = ["critical-section/restore-state-u8"] -# Support alpha release of embedded-hal -eh1_0_alpha = ["dep:eh1_0_alpha", "dep:eh_nb_1_0_alpha"] - # Add conversion functions between chrono types and the rp2040-hal specific DateTime type chrono = ["dep:chrono"] @@ -192,7 +189,7 @@ required-features = ["critical-section-impl"] [[example]] name = "pwm_blink_embedded_hal_1" -required-features = ["critical-section-impl", "eh1_0_alpha"] +required-features = ["critical-section-impl"] [[example]] name = "rom_funcs" diff --git a/rp2040-hal/README.md b/rp2040-hal/README.md index a15eadd25..8dae33f5b 100644 --- a/rp2040-hal/README.md +++ b/rp2040-hal/README.md @@ -92,18 +92,11 @@ volatile until a 1.0.0 release. See the [open issues](https://github.com/rp-rs/rp-hal/issues) for a list of proposed features (and known issues). -### Support for embedded-hal 1.0 +### Implemented traits -We plan to support embedded-hal 1.0 soon after it is released. - -For now, there is preliminary support for alpha/rc versions of embedded-hal, which can -be enabled with the feature `eh1_0_alpha`. Please note that this support does not -provide any semver compatibility guarantees: With that feature activated, there -will be breaking changes even in minor versions of rp2040-hal. - -Support for embedded-hal 1.0(-alpha/rc) exists in parallel to support for -embedded-hal 0.2: Traits of both versions are implemented and can be used -at the same time. +This crate aims to implement all traits from embedded-hal, both version +0.2 and 1.0. They can be used at the same time, so you can upgrade drivers +incrementally. ## Contributing diff --git a/rp2040-hal/examples/adc.rs b/rp2040-hal/examples/adc.rs index b66f189ac..a6a049450 100644 --- a/rp2040-hal/examples/adc.rs +++ b/rp2040-hal/examples/adc.rs @@ -19,7 +19,8 @@ use rp2040_hal as hal; // Some traits we need use core::fmt::Write; -use embedded_hal::adc::OneShot; +// Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2 +use embedded_hal_0_2::adc::OneShot; use hal::fugit::RateExtU32; use rp2040_hal::Clock; diff --git a/rp2040-hal/examples/blinky.rs b/rp2040-hal/examples/blinky.rs index 86fead6af..32825634e 100644 --- a/rp2040-hal/examples/blinky.rs +++ b/rp2040-hal/examples/blinky.rs @@ -21,8 +21,8 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::blocking::delay::DelayMs; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::delay::DelayNs; +use embedded_hal::digital::OutputPin; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. diff --git a/rp2040-hal/examples/dht11.rs b/rp2040-hal/examples/dht11.rs index f8af8b131..b6bc64c53 100644 --- a/rp2040-hal/examples/dht11.rs +++ b/rp2040-hal/examples/dht11.rs @@ -24,7 +24,7 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use hal::Clock; /// The linker will place this boot block at the start of our program image. We diff --git a/rp2040-hal/examples/gpio_dyn_pin_array.rs b/rp2040-hal/examples/gpio_dyn_pin_array.rs index 70ee44cab..09462adf4 100644 --- a/rp2040-hal/examples/gpio_dyn_pin_array.rs +++ b/rp2040-hal/examples/gpio_dyn_pin_array.rs @@ -28,8 +28,8 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::blocking::delay::DelayMs; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::delay::DelayNs; +use embedded_hal::digital::OutputPin; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. diff --git a/rp2040-hal/examples/gpio_in_out.rs b/rp2040-hal/examples/gpio_in_out.rs index fd7356660..9dcd49a35 100644 --- a/rp2040-hal/examples/gpio_in_out.rs +++ b/rp2040-hal/examples/gpio_in_out.rs @@ -21,8 +21,8 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::InputPin; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::InputPin; +use embedded_hal::digital::OutputPin; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. @@ -78,7 +78,7 @@ fn main() -> ! { let mut out_pin = pins.gpio25.into_push_pull_output(); // Configure GPIO 23 as an input - let in_pin = pins.gpio23.into_pull_down_input(); + let mut in_pin = pins.gpio23.into_pull_down_input(); // Output is the opposite of the input loop { diff --git a/rp2040-hal/examples/gpio_irq_example.rs b/rp2040-hal/examples/gpio_irq_example.rs index 9c20716f4..2cabf3610 100644 --- a/rp2040-hal/examples/gpio_irq_example.rs +++ b/rp2040-hal/examples/gpio_irq_example.rs @@ -34,7 +34,7 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; // Our interrupt macro use hal::pac::interrupt; diff --git a/rp2040-hal/examples/i2c.rs b/rp2040-hal/examples/i2c.rs index 889748ff4..ef6480912 100644 --- a/rp2040-hal/examples/i2c.rs +++ b/rp2040-hal/examples/i2c.rs @@ -17,7 +17,7 @@ use panic_halt as _; use rp2040_hal as hal; // Some traits we need -use embedded_hal::blocking::i2c::Write; +use embedded_hal_0_2::blocking::i2c::Write; use hal::fugit::RateExtU32; // A shorter alias for the Peripheral Access Crate, which provides low-level diff --git a/rp2040-hal/examples/mem_to_mem_dma.rs b/rp2040-hal/examples/mem_to_mem_dma.rs index 6dc0cf5a6..a72940dcc 100644 --- a/rp2040-hal/examples/mem_to_mem_dma.rs +++ b/rp2040-hal/examples/mem_to_mem_dma.rs @@ -8,7 +8,7 @@ use cortex_m::singleton; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use hal::dma::{single_buffer, DMAExt}; use hal::pac; use panic_halt as _; diff --git a/rp2040-hal/examples/multicore_fifo_blink.rs b/rp2040-hal/examples/multicore_fifo_blink.rs index ae7d0851b..0168619e4 100644 --- a/rp2040-hal/examples/multicore_fifo_blink.rs +++ b/rp2040-hal/examples/multicore_fifo_blink.rs @@ -27,7 +27,7 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. diff --git a/rp2040-hal/examples/multicore_polyblink.rs b/rp2040-hal/examples/multicore_polyblink.rs index f61301f9e..d30f41b96 100644 --- a/rp2040-hal/examples/multicore_polyblink.rs +++ b/rp2040-hal/examples/multicore_polyblink.rs @@ -26,7 +26,7 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. diff --git a/rp2040-hal/examples/pwm_blink.rs b/rp2040-hal/examples/pwm_blink.rs index 048fd4f81..2e5d5579b 100644 --- a/rp2040-hal/examples/pwm_blink.rs +++ b/rp2040-hal/examples/pwm_blink.rs @@ -18,7 +18,7 @@ use panic_halt as _; use rp2040_hal as hal; // Some traits we need -use embedded_hal::PwmPin; +use embedded_hal::pwm::SetDutyCycle; use rp2040_hal::clocks::Clock; // A shorter alias for the Peripheral Access Crate, which provides low-level @@ -105,13 +105,13 @@ fn main() -> ! { // Ramp brightness up for i in LOW..=HIGH { delay.delay_us(8); - channel.set_duty(i); + let _ = channel.set_duty_cycle(i); } // Ramp brightness down for i in (LOW..=HIGH).rev() { delay.delay_us(8); - channel.set_duty(i); + let _ = channel.set_duty_cycle(i); } delay.delay_ms(500); diff --git a/rp2040-hal/examples/pwm_blink_embedded_hal_1.rs b/rp2040-hal/examples/pwm_blink_embedded_hal_1.rs index aec3697ed..32bb24223 100644 --- a/rp2040-hal/examples/pwm_blink_embedded_hal_1.rs +++ b/rp2040-hal/examples/pwm_blink_embedded_hal_1.rs @@ -18,7 +18,7 @@ use panic_halt as _; use rp2040_hal as hal; // Some traits we need -use eh1_0_alpha::pwm::SetDutyCycle; +use embedded_hal::pwm::SetDutyCycle; use rp2040_hal::clocks::Clock; // A shorter alias for the Peripheral Access Crate, which provides low-level diff --git a/rp2040-hal/examples/pwm_irq_input.rs b/rp2040-hal/examples/pwm_irq_input.rs index 2de79a4ff..f28abf40b 100644 --- a/rp2040-hal/examples/pwm_irq_input.rs +++ b/rp2040-hal/examples/pwm_irq_input.rs @@ -20,8 +20,7 @@ use panic_halt as _; use rp2040_hal as hal; // Some traits we need -use embedded_hal::digital::v2::OutputPin; -use embedded_hal::PwmPin; +use embedded_hal::digital::OutputPin; // Our interrupt macro use hal::pac::interrupt; @@ -135,7 +134,7 @@ fn main() -> ! { // Connect to GPI O1 as the input to channel B on PWM0 let input_pin = pins.gpio1.reconfigure(); let channel = &mut pwm.channel_b; - channel.enable(); + channel.set_enabled(true); // Enable an interrupt whenever GPI O1 goes from high to low (the end of a pulse) input_pin.set_interrupt_enabled(gpio::Interrupt::EdgeLow, true); diff --git a/rp2040-hal/examples/rosc_as_system_clock.rs b/rp2040-hal/examples/rosc_as_system_clock.rs index 31238d539..d55d2e661 100644 --- a/rp2040-hal/examples/rosc_as_system_clock.rs +++ b/rp2040-hal/examples/rosc_as_system_clock.rs @@ -21,7 +21,7 @@ use rp2040_hal as hal; // register access use hal::pac; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use fugit::{HertzU32, RateExtU32}; use hal::clocks::{Clock, ClockSource, ClocksManager, StoppableClock}; use hal::pac::rosc::ctrl::FREQ_RANGE_A; diff --git a/rp2040-hal/examples/rtc_irq_example.rs b/rp2040-hal/examples/rtc_irq_example.rs index e7d89b745..26fe72d03 100644 --- a/rp2040-hal/examples/rtc_irq_example.rs +++ b/rp2040-hal/examples/rtc_irq_example.rs @@ -21,7 +21,7 @@ use rp2040_hal as hal; use hal::{gpio, pac, rtc}; // Some traits we need -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; // Our interrupt macro use hal::pac::interrupt; diff --git a/rp2040-hal/examples/rtc_sleep_example.rs b/rp2040-hal/examples/rtc_sleep_example.rs index 18b8f07c1..559508a24 100644 --- a/rp2040-hal/examples/rtc_sleep_example.rs +++ b/rp2040-hal/examples/rtc_sleep_example.rs @@ -20,7 +20,7 @@ use rp2040_hal as hal; use hal::{clocks::ClockGate, gpio, pac, rtc}; // Some traits we need -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; // Our interrupt macro use hal::pac::interrupt; diff --git a/rp2040-hal/examples/spi_dma.rs b/rp2040-hal/examples/spi_dma.rs index 5710de0b7..a18fcfc2f 100644 --- a/rp2040-hal/examples/spi_dma.rs +++ b/rp2040-hal/examples/spi_dma.rs @@ -11,7 +11,7 @@ use cortex_m::singleton; use cortex_m_rt::entry; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::OutputPin; use hal::dma::{bidirectional, DMAExt}; use hal::fugit::RateExtU32; use hal::pac; diff --git a/rp2040-hal/examples/vector_table.rs b/rp2040-hal/examples/vector_table.rs index 27d2eeccc..1669ab00f 100644 --- a/rp2040-hal/examples/vector_table.rs +++ b/rp2040-hal/examples/vector_table.rs @@ -21,7 +21,7 @@ use hal::pac; // Some traits we need use core::cell::RefCell; use critical_section::Mutex; -use embedded_hal::digital::v2::ToggleableOutputPin; +use embedded_hal::digital::StatefulOutputPin; use hal::fugit::MicrosDurationU32; use pac::interrupt; use rp2040_hal::clocks::Clock; diff --git a/rp2040-hal/examples/watchdog.rs b/rp2040-hal/examples/watchdog.rs index de9fd0bd6..4a9385862 100644 --- a/rp2040-hal/examples/watchdog.rs +++ b/rp2040-hal/examples/watchdog.rs @@ -21,8 +21,7 @@ use rp2040_hal as hal; use hal::pac; // Some traits we need -use embedded_hal::digital::v2::OutputPin; -use embedded_hal::watchdog::{Watchdog, WatchdogEnable}; +use embedded_hal::digital::OutputPin; use hal::fugit::ExtU32; use rp2040_hal::clocks::Clock; diff --git a/rp2040-hal/src/adc.rs b/rp2040-hal/src/adc.rs index f47c4fefc..a875ae881 100644 --- a/rp2040-hal/src/adc.rs +++ b/rp2040-hal/src/adc.rs @@ -4,9 +4,11 @@ //! //! ## Usage //! -//! Capture ADC reading from a pin +//! Capture ADC reading from a pin: + //! ```no_run -//! use embedded_hal::adc::OneShot; +//! // Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2 +//! use embedded_hal_0_2::adc::OneShot; //! use rp2040_hal::{adc::Adc, adc::AdcPin, gpio::Pins, pac, Sio}; //! let mut peripherals = pac::Peripherals::take().unwrap(); //! let sio = Sio::new(peripherals.SIO); @@ -20,8 +22,10 @@ //! ``` //! //! Capture ADC reading from temperature sensor. Note that this needs conversion to be a real-world temperature. +//! //! ```no_run -//! use embedded_hal::adc::OneShot; +//! // Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2 +//! use embedded_hal_0_2::adc::OneShot; //! use rp2040_hal::{adc::Adc, gpio::Pins, pac, Sio}; //! let mut peripherals = pac::Peripherals::take().unwrap(); //! let sio = Sio::new(peripherals.SIO); @@ -118,7 +122,8 @@ use core::convert::Infallible; use core::marker::PhantomData; -use embedded_hal::adc::{Channel, OneShot}; +// Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2 +use embedded_hal_0_2::adc::{Channel, OneShot}; use crate::{ dma, @@ -223,7 +228,7 @@ impl Channel for TempSense { /// by calling [`Adc::take_temp_sensor()`]. Either way, the resulting objects can be /// passed to the [`OneShot::read()`][a] trait method to actually do the read. /// -/// [a]: embedded_hal::adc::OneShot::read +/// [a]: embedded_hal_0_2::adc::OneShot::read pub struct Adc { device: ADC, } diff --git a/rp2040-hal/src/gpio/mod.rs b/rp2040-hal/src/gpio/mod.rs index b54f71f67..e5b017578 100644 --- a/rp2040-hal/src/gpio/mod.rs +++ b/rp2040-hal/src/gpio/mod.rs @@ -2,7 +2,7 @@ //! //! ## Basic usage //! ```no_run -//! use embedded_hal::digital::v2::{InputPin, OutputPin}; +//! use embedded_hal::digital::{InputPin, OutputPin}; //! use rp2040_hal::{clocks::init_clocks_and_plls, gpio::Pins, watchdog::Watchdog, pac, Sio}; //! let mut peripherals = pac::Peripherals::take().unwrap(); //! let mut watchdog = Watchdog::new(peripherals.WATCHDOG); @@ -19,7 +19,7 @@ //! // Drive output to 0V //! output_pin.set_low().unwrap(); //! // Set a pin to input -//! let input_pin = pins.gpio24.into_floating_input(); +//! let mut input_pin = pins.gpio24.into_floating_input(); //! // pinstate will be true if the pin is above 2V //! let pinstate = input_pin.is_high().unwrap(); //! // pinstate_low will be true if the pin is below 1.15V @@ -39,7 +39,7 @@ // advanced usage of the pin (relative to reading/writing a gpio) and it is the responsibility of // the user to make sure these are in a correct state when converting and passing the pin around. -pub use embedded_hal::digital::v2::PinState; +pub use embedded_hal::digital::PinState; use crate::{ atomic_register_access::{write_bitmask_clear, write_bitmask_set}, @@ -863,7 +863,7 @@ pub struct AsInputPin<'a, I: PinId, F: func::Function, P: PullType>(&'a Pin embedded_hal::digital::v2::OutputPin for Pin, P> +impl embedded_hal_0_2::digital::v2::OutputPin for Pin, P> where I: PinId, P: PullType, @@ -883,7 +883,7 @@ where /// Deprecated: Instead of implicitly implementing InputPin for function SioOutput, /// use `pin.as_input()` to get access to input values indepentent of the selected function. -impl embedded_hal::digital::v2::InputPin for Pin, P> +impl embedded_hal_0_2::digital::v2::InputPin for Pin, P> where I: PinId, P: PullType, @@ -899,7 +899,7 @@ where } } -impl<'a, I: PinId, F: func::Function, P: PullType> embedded_hal::digital::v2::InputPin +impl<'a, I: PinId, F: func::Function, P: PullType> embedded_hal_0_2::digital::v2::InputPin for AsInputPin<'a, I, F, P> { type Error = core::convert::Infallible; @@ -913,7 +913,7 @@ impl<'a, I: PinId, F: func::Function, P: PullType> embedded_hal::digital::v2::In } } -impl embedded_hal::digital::v2::StatefulOutputPin for Pin, P> +impl embedded_hal_0_2::digital::v2::StatefulOutputPin for Pin, P> where I: PinId, P: PullType, @@ -927,7 +927,7 @@ where } } -impl embedded_hal::digital::v2::ToggleableOutputPin for Pin, P> +impl embedded_hal_0_2::digital::v2::ToggleableOutputPin for Pin, P> where I: PinId, P: PullType, @@ -939,7 +939,7 @@ where Ok(()) } } -impl embedded_hal::digital::v2::InputPin for Pin, P> +impl embedded_hal_0_2::digital::v2::InputPin for Pin, P> where I: PinId, P: PullType, @@ -1367,7 +1367,7 @@ where } } -impl embedded_hal::digital::v2::InputPin for InOutPin { +impl embedded_hal_0_2::digital::v2::InputPin for InOutPin { type Error = Error; fn is_high(&self) -> Result { self.inner.is_high() @@ -1378,7 +1378,7 @@ impl embedded_hal::digital::v2::InputPin for InOutPin { } } -impl embedded_hal::digital::v2::OutputPin for InOutPin { +impl embedded_hal_0_2::digital::v2::OutputPin for InOutPin { type Error = Error; fn set_low(&mut self) -> Result<(), Error> { // The pin is already set to output low but this is inhibited by the override. @@ -1396,13 +1396,13 @@ impl embedded_hal::digital::v2::OutputPin for InOutPin { } } -#[cfg(feature = "eh1_0_alpha")] mod eh1 { - use eh1_0_alpha::digital::{ - ErrorType, InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin, - }; + use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin}; - use super::{Error, FunctionSio, Pin, PinId, PullType, SioConfig, SioInput, SioOutput}; + use super::{ + func, AnyPin, AsInputPin, Error, FunctionSio, InOutPin, Pin, PinId, PullType, SioConfig, + SioInput, SioOutput, + }; impl ErrorType for Pin, P> where @@ -1441,13 +1441,7 @@ mod eh1 { fn is_set_low(&mut self) -> Result { Ok(self._is_set_low()) } - } - impl ToggleableOutputPin for Pin, P> - where - I: PinId, - P: PullType, - { fn toggle(&mut self) -> Result<(), Self::Error> { self._toggle(); Ok(()) @@ -1468,18 +1462,16 @@ mod eh1 { } } - impl<'a, I, F, P> ErrorType for super::AsInputPin<'a, I, F, P> + impl<'a, I, F, P> ErrorType for AsInputPin<'a, I, F, P> where I: PinId, - F: super::func::Function, + F: func::Function, P: PullType, { type Error = Error; } - impl<'a, I: PinId, F: super::func::Function, P: PullType> InputPin - for super::AsInputPin<'a, I, F, P> - { + impl<'a, I: PinId, F: func::Function, P: PullType> InputPin for AsInputPin<'a, I, F, P> { fn is_high(&mut self) -> Result { Ok(self.0._is_high()) } @@ -1488,4 +1480,39 @@ mod eh1 { Ok(self.0._is_low()) } } + + impl ErrorType for InOutPin + where + I: AnyPin, + { + type Error = Error; + } + + impl OutputPin for InOutPin + where + I: AnyPin, + { + fn set_low(&mut self) -> Result<(), Self::Error> { + self.inner._set_low(); + Ok(()) + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + self.inner._set_high(); + Ok(()) + } + } + + impl InputPin for InOutPin + where + I: AnyPin, + { + fn is_high(&mut self) -> Result { + Ok(self.inner._is_high()) + } + + fn is_low(&mut self) -> Result { + Ok(self.inner._is_low()) + } + } } diff --git a/rp2040-hal/src/gpio/pin_group.rs b/rp2040-hal/src/gpio/pin_group.rs index 61657818a..99a63bb91 100644 --- a/rp2040-hal/src/gpio/pin_group.rs +++ b/rp2040-hal/src/gpio/pin_group.rs @@ -1,4 +1,8 @@ -use embedded_hal::digital::v2::PinState; +//! Pin Groups +//! +//! Lets you set multiple GPIOs simultaneously. + +use embedded_hal::digital::PinState; use frunk::{hlist::Plucker, HCons, HNil}; use crate::typelevel::Sealed; diff --git a/rp2040-hal/src/i2c.rs b/rp2040-hal/src/i2c.rs index 01b90444a..a01fccdbb 100644 --- a/rp2040-hal/src/i2c.rs +++ b/rp2040-hal/src/i2c.rs @@ -20,7 +20,7 @@ //! ); //! //! // Scan for devices on the bus by attempting to read from them -//! use embedded_hal::prelude::_embedded_hal_blocking_i2c_Read; +//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Read; //! for i in 0..=127 { //! let mut readbuf: [u8; 1] = [0; 1]; //! let result = i2c.read(i, &mut readbuf); @@ -31,11 +31,11 @@ //! } //! //! // Write some data to a device at 0x2c -//! use embedded_hal::prelude::_embedded_hal_blocking_i2c_Write; +//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Write; //! i2c.write(0x2c, &[1, 2, 3]).unwrap(); //! //! // Write and then read from a device at 0x3a -//! use embedded_hal::prelude::_embedded_hal_blocking_i2c_WriteRead; +//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_WriteRead; //! let mut readbuf: [u8; 1] = [0; 1]; //! i2c.write_read(0x2c, &[1, 2, 3], &mut readbuf).unwrap(); //! ``` @@ -72,13 +72,6 @@ impl I2cDevice for pac::I2C1 { /// I2C error #[non_exhaustive] -// when eh1_0_alpha is set, Debug & defmt::Format are manually implemented -// to rely on eh1.0's ErrorKind. -#[cfg_attr(not(feature = "eh1_0_alpha"), derive(Debug))] -#[cfg_attr( - all(feature = "defmt", not(feature = "eh1_0_alpha")), - derive(defmt::Format) -)] pub enum Error { /// I2C abort with error Abort(u32), @@ -100,10 +93,9 @@ pub enum Error { AddressReserved(u16), } -#[cfg(feature = "eh1_0_alpha")] impl core::fmt::Debug for Error { fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - use eh1_0_alpha::i2c::Error as _; + use embedded_hal::i2c::Error as _; match self { Error::InvalidReadBufferLength => write!(fmt, "InvalidReadBufferLength"), Error::InvalidWriteBufferLength => write!(fmt, "InvalidWriteBufferLength"), @@ -116,10 +108,10 @@ impl core::fmt::Debug for Error { } } -#[cfg(all(feature = "defmt", feature = "eh1_0_alpha"))] +#[cfg(feature = "defmt")] impl defmt::Format for Error { fn format(&self, fmt: defmt::Formatter) { - use eh1_0_alpha::i2c::Error as _; + use embedded_hal::i2c::Error as _; match self { Error::InvalidReadBufferLength => defmt::write!(fmt, "InvalidReadBufferLength"), Error::InvalidWriteBufferLength => defmt::write!(fmt, "InvalidWriteBufferLength"), @@ -132,27 +124,26 @@ impl defmt::Format for Error { } } -#[cfg(feature = "eh1_0_alpha")] -impl eh1_0_alpha::i2c::Error for Error { - fn kind(&self) -> eh1_0_alpha::i2c::ErrorKind { +impl embedded_hal::i2c::Error for Error { + fn kind(&self) -> embedded_hal::i2c::ErrorKind { match &self { Error::Abort(v) if v & 1<<12 != 0 // ARB_LOST - => eh1_0_alpha::i2c::ErrorKind::ArbitrationLoss, + => embedded_hal::i2c::ErrorKind::ArbitrationLoss, Error::Abort(v) if v & 1<<7 != 0 // ABRT_SBYTE_ACKDET - => eh1_0_alpha::i2c::ErrorKind::Bus, + => embedded_hal::i2c::ErrorKind::Bus, Error::Abort(v) if v & 1<<6 != 0 // ABRT_HS_ACKDET - => eh1_0_alpha::i2c::ErrorKind::Bus, + => embedded_hal::i2c::ErrorKind::Bus, Error::Abort(v) if v & 1<<4 != 0 // ABRT_GCALL_NOACK - => eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address), + => embedded_hal::i2c::ErrorKind::NoAcknowledge(embedded_hal::i2c::NoAcknowledgeSource::Address), Error::Abort(v) if v & 1<<3 != 0 // ABRT_TXDATA_NOACK - => eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Data), + => embedded_hal::i2c::ErrorKind::NoAcknowledge(embedded_hal::i2c::NoAcknowledgeSource::Data), Error::Abort(v) if v & 1<<2 != 0 // ABRT_10ADDR2_NOACK - => eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address), + => embedded_hal::i2c::ErrorKind::NoAcknowledge(embedded_hal::i2c::NoAcknowledgeSource::Address), Error::Abort(v) if v & 1<<1 != 0 // ABRT_10ADDR1_NOACK - => eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address), + => embedded_hal::i2c::ErrorKind::NoAcknowledge(embedded_hal::i2c::NoAcknowledgeSource::Address), Error::Abort(v) if v & 1<<0 != 0 // ABRT_7B_ADDR_NOACK - => eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address), - _ => eh1_0_alpha::i2c::ErrorKind::Other, + => embedded_hal::i2c::ErrorKind::NoAcknowledge(embedded_hal::i2c::NoAcknowledgeSource::Address), + _ => embedded_hal::i2c::ErrorKind::Other, } } } diff --git a/rp2040-hal/src/i2c/controller.rs b/rp2040-hal/src/i2c/controller.rs index cd084929c..3fef2f609 100644 --- a/rp2040-hal/src/i2c/controller.rs +++ b/rp2040-hal/src/i2c/controller.rs @@ -1,9 +1,8 @@ use core::{marker::PhantomData, ops::Deref}; -use embedded_hal::blocking::i2c::{Read, Write, WriteIter, WriteIterRead, WriteRead}; +use embedded_hal_0_2::blocking::i2c::{Read, Write, WriteIter, WriteIterRead, WriteRead}; use fugit::HertzU32; -#[cfg(feature = "eh1_0_alpha")] -use eh1_0_alpha::i2c as eh1; +use embedded_hal::i2c as eh1; use super::{i2c_reserved_addr, Controller, Error, ValidPinScl, ValidPinSda, I2C}; use crate::{ @@ -291,7 +290,6 @@ impl, PINS> I2C { /// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing /// - `SR` = repeated start condition /// - `SP` = stop condition - #[cfg(feature = "eh1_0_alpha")] pub fn transaction_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Error> where O: IntoIterator>, @@ -375,12 +373,10 @@ impl, PINS> WriteIterRead for I2C } } -#[cfg(feature = "eh1_0_alpha")] impl, PINS> eh1::ErrorType for I2C { type Error = Error; } -#[cfg(feature = "eh1_0_alpha")] impl, PINS> eh1::I2c for I2C { fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { Write::write(self, addr, bytes) diff --git a/rp2040-hal/src/lib.rs b/rp2040-hal/src/lib.rs index 35ba8e924..0deafdaed 100644 --- a/rp2040-hal/src/lib.rs +++ b/rp2040-hal/src/lib.rs @@ -15,7 +15,7 @@ //! Implement `defmt::Format` for several types. //! * **disable-intrinsics** - //! Disable automatic mapping of language features (like floating point math) to ROM functions -//! * **eh1_0_alpha** - +//! * **embedded_hal_1** - //! Support alpha release of embedded-hal //! * **rom-func-cache** - //! Memoize(cache) ROM function pointers on first use to improve performance diff --git a/rp2040-hal/src/pwm/mod.rs b/rp2040-hal/src/pwm/mod.rs index a1b2d51c1..9675809a9 100644 --- a/rp2040-hal/src/pwm/mod.rs +++ b/rp2040-hal/src/pwm/mod.rs @@ -37,7 +37,7 @@ //! # &mut pac.RESETS, //! # ); //! # -//! use embedded_hal::PwmPin; +//! use embedded_hal::pwm::SetDutyCycle; //! //! // Use B channel (which inputs from GPIO 25) //! let mut channel_b = pwm.channel_b; @@ -48,8 +48,8 @@ //! let channel_pin_a = channel_a.output_to(pins.gpio24); //! //! // Set duty cycle -//! channel_a.set_duty(0x00ff); -//! channel_a.get_duty(); +//! channel_a.set_duty_cycle(0x00ff); +//! let max_duty_cycle = channel_a.max_duty_cycle(); //! channel_a.set_inverted(); // Invert the output //! channel_a.clr_inverted(); // Don't invert the output //! ``` @@ -76,14 +76,11 @@ //! min_config() leaves those registers in the state they were before it was called (Careful, this can lead to unexpected behavior) //! It's recommended to only call min_config() after calling default_config() on a pin that shares a PWM block. -#[cfg(feature = "eh1_0_alpha")] use core::convert::Infallible; use core::marker::PhantomData; -#[cfg(feature = "eh1_0_alpha")] -use eh1_0_alpha::pwm::{ErrorType, SetDutyCycle}; use embedded_dma::Word; -use embedded_hal::PwmPin; +use embedded_hal::pwm::{ErrorType, SetDutyCycle}; use crate::{ atomic_register_access::{write_bitmask_clear, write_bitmask_set}, @@ -628,24 +625,15 @@ impl Channel { impl Sealed for Channel {} -impl PwmPin for Channel { +impl embedded_hal_0_2::PwmPin for Channel { type Duty = u16; - /// We cant disable the channel without disturbing the other channel. - /// So this just sets the duty cycle to zero fn disable(&mut self) { - if self.enabled { - self.duty_cycle = self.regs.read_cc_a(); - } - self.enabled = false; - self.regs.write_cc_a(0) + self.set_enabled(false); } fn enable(&mut self) { - if !self.enabled { - self.enabled = true; - self.regs.write_cc_a(self.duty_cycle) - } + self.set_enabled(true); } fn get_duty(&self) -> Self::Duty { @@ -657,52 +645,23 @@ impl PwmPin for Channel { } fn get_max_duty(&self) -> Self::Duty { - self.regs.read_top() + SetDutyCycle::max_duty_cycle(self) } fn set_duty(&mut self, duty: Self::Duty) { - self.duty_cycle = duty; - if self.enabled { - self.regs.write_cc_a(duty) - } + let _ = SetDutyCycle::set_duty_cycle(self, duty); } } -#[cfg(feature = "eh1_0_alpha")] -impl ErrorType for Channel { - type Error = Infallible; -} - -#[cfg(feature = "eh1_0_alpha")] -impl SetDutyCycle for Channel { - fn max_duty_cycle(&self) -> u16 { - self.get_max_duty() - } - - fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { - self.set_duty(duty); - Ok(()) - } -} - -impl PwmPin for Channel { +impl embedded_hal_0_2::PwmPin for Channel { type Duty = u16; - /// We cant disable the channel without disturbing the other channel. - /// So this just sets the duty cycle to zero fn disable(&mut self) { - if self.enabled { - self.duty_cycle = self.regs.read_cc_b(); - } - self.enabled = false; - self.regs.write_cc_b(0) + self.set_enabled(false); } fn enable(&mut self) { - if !self.enabled { - self.enabled = true; - self.regs.write_cc_b(self.duty_cycle) - } + self.set_enabled(true); } fn get_duty(&self) -> Self::Duty { @@ -714,18 +673,66 @@ impl PwmPin for Channel { } fn get_max_duty(&self) -> Self::Duty { - self.regs.read_top().saturating_add(1) + SetDutyCycle::max_duty_cycle(self) } fn set_duty(&mut self, duty: Self::Duty) { + let _ = SetDutyCycle::set_duty_cycle(self, duty); + } +} + +impl ErrorType for Channel { + type Error = Infallible; +} + +impl SetDutyCycle for Channel { + fn max_duty_cycle(&self) -> u16 { + self.regs.read_top().saturating_add(1) + } + + fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { + self.duty_cycle = duty; + if self.enabled { + self.regs.write_cc_a(duty) + } + Ok(()) + } +} + +impl ErrorType for Channel { + type Error = Infallible; +} + +impl SetDutyCycle for Channel { + fn max_duty_cycle(&self) -> u16 { + self.regs.read_top().saturating_add(1) + } + + fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { self.duty_cycle = duty; if self.enabled { self.regs.write_cc_b(duty) } + Ok(()) } } impl Channel { + /// Enable or disable the PWM channel + pub fn set_enabled(&mut self, enable: bool) { + if enable && !self.enabled { + // Restore the duty cycle. + self.regs.write_cc_a(self.duty_cycle); + self.enabled = true; + } else if !enable && self.enabled { + // We can't disable it without disturbing the other channel so this + // just sets the duty cycle to zero. + self.duty_cycle = self.regs.read_cc_a(); + self.regs.write_cc_a(0); + self.enabled = false; + } + } + /// Capture a gpio pin and use it as pwm output for channel A pub fn output_to(&mut self, pin: P) -> Pin where @@ -748,6 +755,21 @@ impl Channel { } impl Channel { + /// Enable or disable the PWM channel + pub fn set_enabled(&mut self, enable: bool) { + if enable && !self.enabled { + // Restore the duty cycle. + self.regs.write_cc_b(self.duty_cycle); + self.enabled = true; + } else if !enable && self.enabled { + // We can't disable it without disturbing the other channel so this + // just sets the duty cycle to zero. + self.duty_cycle = self.regs.read_cc_b(); + self.regs.write_cc_b(0); + self.enabled = false; + } + } + /// Capture a gpio pin and use it as pwm output for channel B pub fn output_to(&mut self, pin: P) -> Pin where diff --git a/rp2040-hal/src/spi.rs b/rp2040-hal/src/spi.rs index f53173285..73103a233 100644 --- a/rp2040-hal/src/spi.rs +++ b/rp2040-hal/src/spi.rs @@ -28,14 +28,10 @@ use core::{convert::Infallible, marker::PhantomData, ops::Deref}; -#[cfg(feature = "eh1_0_alpha")] -use eh1_0_alpha::spi as eh1; -#[cfg(feature = "eh1_0_alpha")] -use eh_nb_1_0_alpha::spi as eh1nb; -use embedded_hal::{ - blocking::spi, - spi::{FullDuplex, Phase, Polarity}, -}; +use embedded_hal::spi::{self, Phase, Polarity}; +// Support Embedded HAL 0.2 for backwards-compatibility +use embedded_hal_0_2::{blocking::spi as blocking_spi02, spi as spi02}; +use embedded_hal_nb::spi::FullDuplex; use fugit::{HertzU32, RateExtU32}; use crate::{ @@ -48,14 +44,14 @@ use crate::{ mod pins; pub use pins::*; -impl From for FrameFormat { - fn from(f: embedded_hal::spi::Mode) -> Self { +impl From for FrameFormat { + fn from(f: spi::Mode) -> Self { Self::MotorolaSpi(f) } } -impl From<&embedded_hal::spi::Mode> for FrameFormat { - fn from(f: &embedded_hal::spi::Mode) -> Self { +impl From<&spi::Mode> for FrameFormat { + fn from(f: &spi::Mode) -> Self { Self::MotorolaSpi(*f) } } @@ -64,38 +60,39 @@ impl From<&embedded_hal::spi::Mode> for FrameFormat { #[derive(Clone, Copy, PartialEq, Eq)] pub enum FrameFormat { /// Motorola SPI format. See section 4.4.3.9 of RP2040 datasheet. - MotorolaSpi(embedded_hal::spi::Mode), + MotorolaSpi(spi::Mode), /// Texas Instruments synchronous serial frame format. See section 4.4.3.8 of RP2040 datasheet. TexasInstrumentsSynchronousSerial, /// National Semiconductor Microwire frame format. See section 4.4.3.14 of RP2040 datasheet. NationalSemiconductorMicrowire, } -#[cfg(feature = "eh1_0_alpha")] -impl From for FrameFormat { - fn from(f: eh1_0_alpha::spi::Mode) -> Self { - let eh1_0_alpha::spi::Mode { polarity, phase } = f; +impl From<&embedded_hal_0_2::spi::Mode> for FrameFormat { + fn from(f: &embedded_hal_0_2::spi::Mode) -> Self { + let embedded_hal_0_2::spi::Mode { polarity, phase } = f; match (polarity, phase) { - ( - eh1_0_alpha::spi::Polarity::IdleLow, - eh1_0_alpha::spi::Phase::CaptureOnFirstTransition, - ) => FrameFormat::MotorolaSpi(embedded_hal::spi::MODE_0), - ( - eh1_0_alpha::spi::Polarity::IdleLow, - eh1_0_alpha::spi::Phase::CaptureOnSecondTransition, - ) => FrameFormat::MotorolaSpi(embedded_hal::spi::MODE_1), - ( - eh1_0_alpha::spi::Polarity::IdleHigh, - eh1_0_alpha::spi::Phase::CaptureOnFirstTransition, - ) => FrameFormat::MotorolaSpi(embedded_hal::spi::MODE_2), - ( - eh1_0_alpha::spi::Polarity::IdleHigh, - eh1_0_alpha::spi::Phase::CaptureOnSecondTransition, - ) => FrameFormat::MotorolaSpi(embedded_hal::spi::MODE_3), + (spi02::Polarity::IdleLow, spi02::Phase::CaptureOnFirstTransition) => { + FrameFormat::MotorolaSpi(spi::MODE_0) + } + (spi02::Polarity::IdleLow, spi02::Phase::CaptureOnSecondTransition) => { + FrameFormat::MotorolaSpi(spi::MODE_1) + } + (spi02::Polarity::IdleHigh, spi02::Phase::CaptureOnFirstTransition) => { + FrameFormat::MotorolaSpi(spi::MODE_2) + } + (spi02::Polarity::IdleHigh, spi02::Phase::CaptureOnSecondTransition) => { + FrameFormat::MotorolaSpi(spi::MODE_3) + } } } } +impl From for FrameFormat { + fn from(f: embedded_hal_0_2::spi::Mode) -> Self { + From::from(&f) + } +} + /// State of the SPI pub trait State: Sealed {} @@ -380,7 +377,7 @@ macro_rules! impl_write { ($type:ident, [$($nr:expr),+]) => { $( - impl> FullDuplex<$type> for Spi { + impl> spi02::FullDuplex<$type> for Spi { type Error = Infallible; fn read(&mut self) -> Result<$type, nb::Error> { @@ -405,17 +402,15 @@ macro_rules! impl_write { } } - impl> spi::write::Default<$type> for Spi {} - impl> spi::transfer::Default<$type> for Spi {} - impl> spi::write_iter::Default<$type> for Spi {} + impl> blocking_spi02::write::Default<$type> for Spi {} + impl> blocking_spi02::transfer::Default<$type> for Spi {} + impl> blocking_spi02::write_iter::Default<$type> for Spi {} - #[cfg(feature = "eh1_0_alpha")] - impl> eh1::ErrorType for Spi { + impl> spi::ErrorType for Spi { type Error = Infallible; } - #[cfg(feature = "eh1_0_alpha")] - impl> eh1::SpiBus<$type> for Spi { + impl> spi::SpiBus<$type> for Spi { fn read(&mut self, words: &mut [$type]) -> Result<(), Self::Error> { for word in words.iter_mut() { // write empty word @@ -489,8 +484,7 @@ macro_rules! impl_write { } } - #[cfg(feature = "eh1_0_alpha")] - impl> eh1nb::FullDuplex<$type> for Spi { + impl> FullDuplex<$type> for Spi { fn read(&mut self) -> Result<$type, nb::Error> { if !self.is_readable() { return Err(nb::Error::WouldBlock); diff --git a/rp2040-hal/src/timer.rs b/rp2040-hal/src/timer.rs index 4b897c6c9..444ce985a 100644 --- a/rp2040-hal/src/timer.rs +++ b/rp2040-hal/src/timer.rs @@ -143,14 +143,14 @@ impl Timer { macro_rules! impl_delay_traits { ($($t:ty),+) => { $( - impl embedded_hal::blocking::delay::DelayUs<$t> for Timer { + impl embedded_hal_0_2::blocking::delay::DelayUs<$t> for Timer { fn delay_us(&mut self, us: $t) { #![allow(unused_comparisons)] assert!(us >= 0); // Only meaningful for i32 self.delay_us_internal(us as u32) } } - impl embedded_hal::blocking::delay::DelayMs<$t> for Timer { + impl embedded_hal_0_2::blocking::delay::DelayMs<$t> for Timer { fn delay_ms(&mut self, ms: $t) { #![allow(unused_comparisons)] assert!(ms >= 0); // Only meaningful for i32 @@ -166,8 +166,7 @@ macro_rules! impl_delay_traits { // The implementation for i32 is a workaround to allow `delay_ms(42)` construction without specifying a type. impl_delay_traits!(u8, u16, u32, i32); -#[cfg(feature = "eh1_0_alpha")] -impl eh1_0_alpha::delay::DelayNs for Timer { +impl embedded_hal::delay::DelayNs for Timer { fn delay_ns(&mut self, ns: u32) { // For now, just use microsecond delay, internally. Of course, this // might cause a much longer delay than necessary. So a more advanced @@ -189,11 +188,15 @@ impl eh1_0_alpha::delay::DelayNs for Timer { } } -/// Implementation of the embedded_hal::Timer traits using rp2040_hal::timer counter +/// Implementation of the [`embedded_hal_0_2::timer`] traits using [`rp2040_hal::timer`] counter. +/// +/// There is no Embedded HAL 1.0 equivalent at this time. +/// +/// If all you need is a delay, [`Timer`] does implement [`embedded_hal::delay::DelayNs`]. /// /// ## Usage /// ```no_run -/// use embedded_hal::timer::{CountDown, Cancel}; +/// use embedded_hal_0_2::timer::{CountDown, Cancel}; /// use fugit::ExtU32; /// use rp2040_hal; /// let mut pac = rp2040_hal::pac::Peripherals::take().unwrap(); @@ -218,7 +221,7 @@ pub struct CountDown<'timer> { next_end: Option, } -impl embedded_hal::timer::CountDown for CountDown<'_> { +impl embedded_hal_0_2::timer::CountDown for CountDown<'_> { type Time = MicrosDurationU64; fn start(&mut self, count: T) @@ -249,9 +252,9 @@ impl embedded_hal::timer::CountDown for CountDown<'_> { } } -impl embedded_hal::timer::Periodic for CountDown<'_> {} +impl embedded_hal_0_2::timer::Periodic for CountDown<'_> {} -impl embedded_hal::timer::Cancel for CountDown<'_> { +impl embedded_hal_0_2::timer::Cancel for CountDown<'_> { type Error = &'static str; fn cancel(&mut self) -> Result<(), Self::Error> { diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index 61193347a..1d5f65291 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -4,7 +4,7 @@ //! UartPeripheral object that can both read and write. use core::{convert::Infallible, fmt}; -use embedded_hal::serial as eh0; +use embedded_hal_0_2::serial as eh0; use fugit::HertzU32; use nb::Error::{Other, WouldBlock}; @@ -14,8 +14,7 @@ use crate::{ uart::*, }; -#[cfg(feature = "eh1_0_alpha")] -use eh_nb_1_0_alpha::serial as eh1nb; +use embedded_hal_nb::serial::{ErrorType, Read, Write}; /// An UART Peripheral based on an underlying UART device. pub struct UartPeripheral> { @@ -360,13 +359,11 @@ impl> eh0::Read for UartPeripheral> eh1nb::ErrorType for UartPeripheral { +impl> ErrorType for UartPeripheral { type Error = ReadErrorType; } -#[cfg(feature = "eh1_0_alpha")] -impl> eh1nb::Read for UartPeripheral { +impl> Read for UartPeripheral { fn read(&mut self) -> nb::Result { let byte: &mut [u8] = &mut [0; 1]; @@ -396,8 +393,7 @@ impl> eh0::Write for UartPeripheral> eh1nb::Write for UartPeripheral { +impl> Write for UartPeripheral { fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { if self.write_raw(&[word]).is_err() { Err(WouldBlock) @@ -416,7 +412,6 @@ impl> eh1nb::Write for UartPeripheral> fmt::Write for UartPeripheral { fn write_str(&mut self, s: &str) -> fmt::Result { - use eh0::Write; s.bytes() .try_for_each(|c| nb::block!(self.write(c))) .map_err(|_| fmt::Error) diff --git a/rp2040-hal/src/uart/reader.rs b/rp2040-hal/src/uart/reader.rs index 6e53a2fec..18fbb36f6 100644 --- a/rp2040-hal/src/uart/reader.rs +++ b/rp2040-hal/src/uart/reader.rs @@ -5,11 +5,10 @@ use super::{FifoWatermark, UartDevice, ValidUartPinout}; use crate::dma::{EndlessReadTarget, ReadTarget}; use crate::pac::uart0::RegisterBlock; -use embedded_hal::serial::Read; +use embedded_hal_0_2::serial::Read as Read02; use nb::Error::*; -#[cfg(feature = "eh1_0_alpha")] -use eh_nb_1_0_alpha::serial as eh1nb; +use embedded_hal_nb::serial::{Error, ErrorKind, ErrorType, Read}; /// When there's a read error. #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -39,14 +38,13 @@ pub enum ReadErrorType { Framing, } -#[cfg(feature = "eh1_0_alpha")] -impl eh1nb::Error for ReadErrorType { - fn kind(&self) -> eh1nb::ErrorKind { +impl Error for ReadErrorType { + fn kind(&self) -> ErrorKind { match self { - ReadErrorType::Overrun => eh1nb::ErrorKind::Overrun, - ReadErrorType::Break => eh1nb::ErrorKind::Other, - ReadErrorType::Parity => eh1nb::ErrorKind::Parity, - ReadErrorType::Framing => eh1nb::ErrorKind::FrameFormat, + ReadErrorType::Overrun => ErrorKind::Overrun, + ReadErrorType::Break => ErrorKind::Other, + ReadErrorType::Parity => ErrorKind::Parity, + ReadErrorType::Framing => ErrorKind::FrameFormat, } } } @@ -219,7 +217,7 @@ impl> Reader { } } -impl> Read for Reader { +impl> Read02 for Reader { type Error = ReadErrorType; fn read(&mut self) -> nb::Result { @@ -255,13 +253,11 @@ unsafe impl> ReadTarget for Reader { impl> EndlessReadTarget for Reader {} -#[cfg(feature = "eh1_0_alpha")] -impl> eh1nb::ErrorType for Reader { +impl> ErrorType for Reader { type Error = ReadErrorType; } -#[cfg(feature = "eh1_0_alpha")] -impl> eh1nb::Read for Reader { +impl> Read for Reader { fn read(&mut self) -> nb::Result { let byte: &mut [u8] = &mut [0; 1]; diff --git a/rp2040-hal/src/uart/writer.rs b/rp2040-hal/src/uart/writer.rs index f474e9f7b..326c2f610 100644 --- a/rp2040-hal/src/uart/writer.rs +++ b/rp2040-hal/src/uart/writer.rs @@ -7,9 +7,8 @@ use crate::dma::{EndlessWriteTarget, WriteTarget}; use crate::pac::uart0::RegisterBlock; use core::fmt; use core::{convert::Infallible, marker::PhantomData}; -#[cfg(feature = "eh1_0_alpha")] -use eh_nb_1_0_alpha::serial as eh1nb; -use embedded_hal::serial::Write; +use embedded_hal_0_2::serial::Write as Write02; +use embedded_hal_nb::serial::{ErrorType, Write}; use nb::Error::*; /// Set tx FIFO watermark @@ -173,7 +172,7 @@ impl> Writer { } } -impl> Write for Writer { +impl> Write02 for Writer { type Error = Infallible; fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { @@ -209,13 +208,11 @@ unsafe impl> WriteTarget for Writer { impl> EndlessWriteTarget for Writer {} -#[cfg(feature = "eh1_0_alpha")] -impl> eh1nb::ErrorType for Writer { - type Error = core::convert::Infallible; +impl> ErrorType for Writer { + type Error = Infallible; } -#[cfg(feature = "eh1_0_alpha")] -impl> eh1nb::Write for Writer { +impl> Write for Writer { fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { if self.write_raw(&[word]).is_err() { Err(WouldBlock) @@ -235,7 +232,7 @@ impl> eh1nb::Write for Writer { impl> fmt::Write for Writer { fn write_str(&mut self, s: &str) -> fmt::Result { s.bytes() - .try_for_each(|c| nb::block!(self.write(c))) + .try_for_each(|c| nb::block!(Write::write(self, c))) .map_err(|_| fmt::Error) } } diff --git a/rp2040-hal/src/watchdog.rs b/rp2040-hal/src/watchdog.rs index 9133752aa..6969c3551 100644 --- a/rp2040-hal/src/watchdog.rs +++ b/rp2040-hal/src/watchdog.rs @@ -34,7 +34,8 @@ //! ``` //! See [examples/watchdog.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/watchdog.rs) for a more complete example -use embedded_hal::watchdog; +// Embedded HAL 1.0.0 doesn't have an ADC trait, so use the one from 0.2 +use embedded_hal_0_2::watchdog; use fugit::MicrosDurationU32; use crate::pac::{self, WATCHDOG}; @@ -149,18 +150,17 @@ impl Watchdog { w }); } -} -impl watchdog::Watchdog for Watchdog { - fn feed(&mut self) { + /// Set the watchdog counter back to its load value, making sure + /// that the watchdog reboot will not be triggered for the configured + /// period. + pub fn feed(&self) { self.load_counter(self.load_value) } -} - -impl watchdog::WatchdogEnable for Watchdog { - type Time = MicrosDurationU32; - fn start>(&mut self, period: T) { + /// Start the watchdog. This enables a timer which will reboot the + /// rp2040 if [`feed()`] doesnot get called for the configured period. + pub fn start>(&mut self, period: T) { const MAX_PERIOD: u32 = 0xFFFFFF; let delay_us = period.into().to_micros(); @@ -182,10 +182,29 @@ impl watchdog::WatchdogEnable for Watchdog { self.load_counter(self.load_value); self.enable(true); } + + /// Disable the watchdog timer. + pub fn disable(&self) { + self.enable(false) + } +} + +impl watchdog::Watchdog for Watchdog { + fn feed(&mut self) { + (*self).feed() + } +} + +impl watchdog::WatchdogEnable for Watchdog { + type Time = MicrosDurationU32; + + fn start>(&mut self, period: T) { + self.start(period) + } } impl watchdog::WatchdogDisable for Watchdog { fn disable(&mut self) { - self.enable(false) + (*self).disable() } }