From fd5430fa7024484457a9ac7a649937d988113011 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:00:02 +0200 Subject: [PATCH 1/9] Remove `crate::hal` It was only used in one place, wasn't public, and overall just seems very unnecessary. --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 118b46e9f..786e67119 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,7 +144,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 +171,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 From 535e079fcfc2bc2832af0bf2f087ffa8cc64c166 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:01:56 +0200 Subject: [PATCH 2/9] Add dependency on alpha version of embedded-hal --- Cargo.toml | 4 ++++ src/lib.rs | 1 + 2 files changed, 5 insertions(+) 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/lib.rs b/src/lib.rs index 786e67119..6edddb938 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; From e97b7d50cb61740bb1aad93019aa2eeb1ce6c06b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:08:57 +0200 Subject: [PATCH 3/9] Add VSCode config to make rust-analyzer work --- .vscode/settings.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..867ac11e6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "rust-analyzer.cargo.features": [ + "845-rt" + ], + "rust-analyzer.checkOnSave.allTargets": false +} \ No newline at end of file From 66be0461fff92d5a6b01edf376d16c5f40c187f5 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:10:04 +0200 Subject: [PATCH 4/9] Implement GPIO traits of alpha embedded-hal --- src/gpio.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) 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 { From 86c2f68a622e4f328192a80bb48e6f64464c971e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:20:22 +0200 Subject: [PATCH 5/9] Configure VS Code to format code on save This makes it easier to conform with the formatting requirements. --- .vscode/settings.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 867ac11e6..92af313ff 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,6 @@ "rust-analyzer.cargo.features": [ "845-rt" ], - "rust-analyzer.checkOnSave.allTargets": false + "rust-analyzer.checkOnSave.allTargets": false, + "editor.formatOnSave": true } \ No newline at end of file From 25dae6c910ccf7c69eb5983fc0617df366dc741b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:31:51 +0200 Subject: [PATCH 6/9] Fix comments --- src/delay.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/delay.rs b/src/delay.rs index 5fd9246ea..2d3e18a0e 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -90,7 +90,7 @@ impl DelayMs for Delay { // 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. @@ -119,14 +119,14 @@ impl DelayUs for Delay { } 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 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) } From 59b7887b58056aa12dc6edc1cd09634b3721ce2c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:34:00 +0200 Subject: [PATCH 7/9] Implement `Delay` trait of alpha embedded-hal --- src/delay.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/delay.rs b/src/delay.rs index 2d3e18a0e..b51535e7c 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,17 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) + // So we implement a separate, higher level, delay loop + 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 +96,17 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) + // So we implement a separate, higher level, delay loop + 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,6 +114,17 @@ impl DelayMs for Delay { } } +impl DelayMsAlpha for Delay { + type Error = Void; + + /// Pauses execution for `ms` milliseconds + // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) + // So we implement a separate, higher level, delay loop + 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` microseconds @@ -118,6 +155,15 @@ 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` microseconds fn delay_us(&mut self, us: u16) { @@ -125,9 +171,27 @@ impl DelayUs for Delay { } } +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` 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)) + } +} From 23a5ef585c266317e5ce22109eed46fc9e826d32 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Oct 2020 16:34:28 +0200 Subject: [PATCH 8/9] Fix typos --- src/delay.rs | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/delay.rs b/src/delay.rs index b51535e7c..9a6c46e16 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -145,7 +145,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) diff --git a/src/lib.rs b/src/lib.rs index 6edddb938..12e6e29a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -489,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`]. From fe6a3051c9803d012f867b7bed2b587974d6d0bf Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 14 Oct 2020 12:54:08 +0200 Subject: [PATCH 9/9] Update `Delay` documentation --- src/delay.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/delay.rs b/src/delay.rs index 9a6c46e16..8d4812c2c 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -80,8 +80,6 @@ impl DelayMsAlpha for Delay { type Error = Void; /// Pauses execution for `ms` milliseconds - // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) - // So we implement a separate, higher level, delay loop fn try_delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { Ok(self.delay_ms(ms)) } @@ -100,8 +98,6 @@ impl DelayMsAlpha for Delay { type Error = Void; /// Pauses execution for `ms` milliseconds - // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) - // So we implement a separate, higher level, delay loop fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> { Ok(self.delay_ms(ms)) } @@ -118,8 +114,6 @@ impl DelayMsAlpha for Delay { type Error = Void; /// Pauses execution for `ms` milliseconds - // At 30 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x418937 (over the max u16 value) - // So we implement a separate, higher level, delay loop fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> { Ok(self.delay_ms(ms)) }