diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..92af313ff --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "rust-analyzer.cargo.features": [ + "845-rt" + ], + "rust-analyzer.checkOnSave.allTargets": false, + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 140bad541..a8c7e82ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,10 @@ optional = true version = "0.2.4" features = ["unproven"] +[dependencies.embedded-hal-alpha] +version = "1.0.0-alpha.1" +package = "embedded-hal" + [dependencies.lpc82x-pac] optional = true version = "0.7.0" diff --git a/src/delay.rs b/src/delay.rs index 5fd9246ea..8d4812c2c 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -24,6 +24,10 @@ use cortex_m::peripheral::syst::SystClkSource; use crate::pac::SYST; use embedded_hal::blocking::delay::{DelayMs, DelayUs}; +use embedded_hal_alpha::blocking::delay::{ + DelayMs as DelayMsAlpha, DelayUs as DelayUsAlpha, +}; +use void::Void; const SYSTICK_RANGE: u32 = 0x0100_0000; const SYSTEM_CLOCK: u32 = 12_000_000; @@ -72,6 +76,15 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + fn try_delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { + Ok(self.delay_ms(ms)) + } +} + impl DelayMs for Delay { /// Pauses execution for `ms` milliseconds fn delay_ms(&mut self, ms: u16) { @@ -81,6 +94,15 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> { + Ok(self.delay_ms(ms)) + } +} + impl DelayMs for Delay { /// Pauses execution for `ms` milliseconds fn delay_ms(&mut self, ms: u8) { @@ -88,9 +110,18 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> { + Ok(self.delay_ms(ms)) + } +} + // At 30MHz (the maximum frequency), this overflows at approx. 2^32 / 30 = 146 seconds impl DelayUs for Delay { - /// Pauses execution for `us` milliseconds + /// Pauses execution for `us` microseconds fn delay_us(&mut self, us: u32) { // The SysTick Reload Value register supports values between 1 and 0x00FFFFFF. // Here half the maximum is used so we have some play if there's a long running interrupt. @@ -108,7 +139,7 @@ impl DelayUs for Delay { let start_count = SYST::get_current(); total_ticks -= current_ticks; - // Use the wrapping substraction and the modulo to deal with the systick wrapping around + // Use the wrapping subtraction and the modulo to deal with the systick wrapping around // from 0 to 0xFFFF while (start_count.wrapping_sub(SYST::get_current()) % SYSTICK_RANGE) @@ -118,16 +149,43 @@ impl DelayUs for Delay { } } +impl DelayUsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `us` microseconds + fn try_delay_us(&mut self, us: u32) -> Result<(), Self::Error> { + Ok(self.delay_us(us)) + } +} + impl DelayUs for Delay { - /// Pauses execution for `us` milliseconds + /// Pauses execution for `us` microseconds fn delay_us(&mut self, us: u16) { self.delay_us(us as u32) } } +impl DelayUsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `us` microseconds + fn try_delay_us(&mut self, us: u16) -> Result<(), Self::Error> { + Ok(self.delay_us(us)) + } +} + impl DelayUs for Delay { - /// Pauses execution for `us` milliseconds + /// Pauses execution for `us` microseconds fn delay_us(&mut self, us: u8) { self.delay_us(us as u32) } } + +impl DelayUsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `us` microseconds + fn try_delay_us(&mut self, us: u8) -> Result<(), Self::Error> { + Ok(self.delay_us(us)) + } +} diff --git a/src/gpio.rs b/src/gpio.rs index a09151d90..7a2ec7312 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -46,6 +46,11 @@ use core::marker::PhantomData; use embedded_hal::digital::v2::{ InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin, }; +use embedded_hal_alpha::digital::{ + InputPin as InputPinAlpha, OutputPin as OutputPinAlpha, + StatefulOutputPin as StatefulOutputPinAlpha, + ToggleableOutputPin as ToggleableOutputPinAlpha, +}; use void::Void; use crate::{ @@ -427,6 +432,23 @@ where } } +impl InputPinAlpha for GpioPin +where + T: pins::Trait, +{ + type Error = Void; + + fn try_is_high(&self) -> Result { + // Call the inherent method defined above. + Ok(self.is_high()) + } + + fn try_is_low(&self) -> Result { + // Call the inherent method defined above. + Ok(self.is_low()) + } +} + impl OutputPin for GpioPin where T: pins::Trait, @@ -444,6 +466,23 @@ where } } +impl OutputPinAlpha for GpioPin +where + T: pins::Trait, +{ + type Error = Void; + + fn try_set_high(&mut self) -> Result<(), Self::Error> { + // Call the inherent method defined above. + Ok(self.set_high()) + } + + fn try_set_low(&mut self) -> Result<(), Self::Error> { + // Call the inherent method defined above. + Ok(self.set_low()) + } +} + impl StatefulOutputPin for GpioPin where T: pins::Trait, @@ -459,6 +498,21 @@ where } } +impl StatefulOutputPinAlpha for GpioPin +where + T: pins::Trait, +{ + fn try_is_set_high(&self) -> Result { + // Call the inherent method defined above. + Ok(self.is_set_high()) + } + + fn try_is_set_low(&self) -> Result { + // Call the inherent method defined above. + Ok(self.is_set_low()) + } +} + impl ToggleableOutputPin for GpioPin where T: pins::Trait, @@ -471,6 +525,18 @@ where } } +impl ToggleableOutputPinAlpha for GpioPin +where + T: pins::Trait, +{ + type Error = Void; + + fn try_toggle(&mut self) -> Result<(), Self::Error> { + // Call the inherent method defined above. + Ok(self.toggle()) + } +} + /// The voltage level of a pin #[derive(Debug)] pub enum Level { diff --git a/src/lib.rs b/src/lib.rs index 118b46e9f..12e6e29a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,7 @@ pub extern crate cortex_m; #[cfg(feature = "rt-selected")] pub extern crate cortex_m_rt; pub extern crate embedded_hal; +pub extern crate embedded_hal_alpha; pub extern crate nb; pub extern crate void; @@ -144,7 +145,7 @@ pub mod prelude { pub use core::fmt::Write as _; pub use crate::clock::{Enabled as _, Frequency as _}; - pub use crate::hal::{digital::v2::*, prelude::*}; + pub use crate::embedded_hal::{digital::v2::*, prelude::*}; pub use crate::sleep::Sleep as _; } @@ -171,8 +172,6 @@ pub use self::wkt::WKT; pub use pac::CorePeripherals; -use embedded_hal as hal; - /// Provides access to all peripherals /// /// This is the entry point to the HAL API. Before you can do anything else, you @@ -490,7 +489,7 @@ impl Peripherals { /// into a type state that allows you to execute operations that are known /// to put the hardware in a safe state. Like forcing the type state for a /// peripheral API to the "disabled" state, then enabling it, to make sure - /// it is enabled, regardless of wheter it was enabled before. + /// it is enabled, regardless of whether it was enabled before. /// /// Since there are no means within this API to forcibly change type state, /// you will need to resort to something like [`core::mem::transmute`].