From ecda57dff17dcbcfb2b4000e8cd3efdfe0bd4cf2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 21:05:28 +0100 Subject: [PATCH 01/10] stm32: remove unused .pep8 file --- embassy-stm32/.pep8 | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 embassy-stm32/.pep8 diff --git a/embassy-stm32/.pep8 b/embassy-stm32/.pep8 deleted file mode 100644 index c9a137c8..00000000 --- a/embassy-stm32/.pep8 +++ /dev/null @@ -1,2 +0,0 @@ -[pep8] -max_line_length = 255 \ No newline at end of file From 52e156b429417bde59d0ea67d11256866f1dcec9 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 21:05:48 +0100 Subject: [PATCH 02/10] stm32: use critical_section instead of cortex_m::interrupt --- embassy-stm32/src/exti.rs | 4 ++-- embassy-stm32/src/gpio.rs | 12 ++++++------ embassy-stm32/src/sdmmc/v2.rs | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 5af51cd1..552433b8 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -148,7 +148,7 @@ pub struct ExtiInputFuture<'a> { impl<'a> ExtiInputFuture<'a> { fn new(pin: u8, port: u8, rising: bool, falling: bool) -> Self { - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let pin = pin as usize; exticr_regs() .exticr(pin / 4) @@ -177,7 +177,7 @@ impl<'a> ExtiInputFuture<'a> { impl<'a> Drop for ExtiInputFuture<'a> { fn drop(&mut self) { - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let pin = self.pin as _; cpu_regs().imr(0).modify(|w| w.set_line(pin, false)); }); diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 1b257c38..6b4a9285 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -80,7 +80,7 @@ impl<'d, T: Pin> Input<'d, T> { pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { unborrow!(pin); - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = pin.block(); let n = pin.pin() as usize; #[cfg(gpio_v1)] @@ -117,7 +117,7 @@ impl<'d, T: Pin> Input<'d, T> { impl<'d, T: Pin> Drop for Input<'d, T> { fn drop(&mut self) { - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = self.pin.block(); let n = self.pin.pin() as usize; #[cfg(gpio_v1)] @@ -168,7 +168,7 @@ impl<'d, T: Pin> Output<'d, T> { Level::Low => pin.set_low(), } - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = pin.block(); let n = pin.pin() as usize; #[cfg(gpio_v1)] @@ -195,7 +195,7 @@ impl<'d, T: Pin> Output<'d, T> { impl<'d, T: Pin> Drop for Output<'d, T> { fn drop(&mut self) { - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = self.pin.block(); let n = self.pin.pin() as usize; #[cfg(gpio_v1)] @@ -265,7 +265,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { Level::Low => pin.set_low(), } - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = pin.block(); let n = pin.pin() as usize; #[cfg(gpio_v1)] @@ -298,7 +298,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { impl<'d, T: Pin> Drop for OutputOpenDrain<'d, T> { fn drop(&mut self) { - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { let r = self.pin.block(); let n = self.pin.pin() as usize; #[cfg(gpio_v1)] diff --git a/embassy-stm32/src/sdmmc/v2.rs b/embassy-stm32/src/sdmmc/v2.rs index 5914f92f..74382ce6 100644 --- a/embassy-stm32/src/sdmmc/v2.rs +++ b/embassy-stm32/src/sdmmc/v2.rs @@ -1254,7 +1254,7 @@ where fn configure(&mut self) { let (clk_pin, cmd_pin, d0_pin, d1_pin, d2_pin, d3_pin) = self; - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { // clk let block = clk_pin.block(); let n = clk_pin.pin() as usize; @@ -1298,7 +1298,7 @@ where let (clk_pin, cmd_pin, d0_pin, d1_pin, d2_pin, d3_pin) = self; - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { // clk let n = clk_pin.pin().into(); clk_pin @@ -1400,7 +1400,7 @@ where fn configure(&mut self) { let (clk_pin, cmd_pin, d0_pin) = self; - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { // clk let block = clk_pin.block(); let n = clk_pin.pin() as usize; @@ -1426,7 +1426,7 @@ where let (clk_pin, cmd_pin, d0_pin) = self; - cortex_m::interrupt::free(|_| unsafe { + critical_section::with(|_| unsafe { // clk let n = clk_pin.pin().into(); clk_pin From 58fc64722c65bbdc209ae0fd1700f03702bbcd08 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 22:02:00 +0100 Subject: [PATCH 03/10] stm32/gpio: expose all functionality as inherent methods. --- embassy-lora/src/stm32wl/mod.rs | 19 +- embassy-stm32/src/exti.rs | 12 +- embassy-stm32/src/gpio.rs | 230 ++++++++++++------ examples/stm32f1/src/bin/blinky.rs | 5 +- examples/stm32f3/src/bin/blinky.rs | 5 +- examples/stm32f3/src/bin/button.rs | 11 +- examples/stm32f4/src/bin/blinky.rs | 5 +- examples/stm32f4/src/bin/button.rs | 11 +- examples/stm32f4/src/bin/spi.rs | 5 +- examples/stm32f7/src/bin/blinky.rs | 5 +- examples/stm32f7/src/bin/button.rs | 11 +- examples/stm32g0/src/bin/blinky.rs | 5 +- examples/stm32g0/src/bin/button.rs | 3 +- examples/stm32g4/src/bin/blinky.rs | 5 +- examples/stm32g4/src/bin/button.rs | 3 +- examples/stm32h7/src/bin/blinky.rs | 5 +- examples/stm32h7/src/bin/camera.rs | 5 +- examples/stm32h7/src/bin/mco.rs | 5 +- examples/stm32h7/src/bin/rng.rs | 5 +- examples/stm32l0/src/bin/blinky.rs | 5 +- examples/stm32l0/src/bin/button.rs | 11 +- examples/stm32l0/src/bin/spi.rs | 5 +- examples/stm32l1/src/bin/blinky.rs | 5 +- examples/stm32l1/src/bin/spi.rs | 5 +- examples/stm32l4/src/bin/blinky.rs | 5 +- examples/stm32l4/src/bin/button.rs | 3 +- examples/stm32l4/src/bin/spi.rs | 5 +- .../stm32l4/src/bin/spi_blocking_async.rs | 9 +- examples/stm32l4/src/bin/spi_dma.rs | 9 +- examples/stm32wb55/src/bin/blinky.rs | 5 +- examples/stm32wl55/src/bin/blinky.rs | 5 +- examples/stm32wl55/src/bin/button.rs | 11 +- examples/stm32wl55/src/bin/subghz.rs | 11 +- tests/stm32/src/bin/gpio.rs | 27 +- 34 files changed, 266 insertions(+), 210 deletions(-) diff --git a/embassy-lora/src/stm32wl/mod.rs b/embassy-lora/src/stm32wl/mod.rs index 8cac46f7..783140cb 100644 --- a/embassy-lora/src/stm32wl/mod.rs +++ b/embassy-lora/src/stm32wl/mod.rs @@ -16,7 +16,6 @@ use embassy_stm32::{ TxParams, }, }; -use embedded_hal::digital::v2::OutputPin; use lorawan_device::async_device::{ radio::{Bandwidth, PhyRxTx, RfConfig, RxQuality, SpreadingFactor, TxConfig}, Timings, @@ -329,22 +328,22 @@ impl<'a> RadioSwitch<'a> { } pub(crate) fn set_rx(&mut self) { - self.ctrl1.set_high().unwrap(); - self.ctrl2.set_low().unwrap(); - self.ctrl3.set_high().unwrap(); + self.ctrl1.set_high(); + self.ctrl2.set_low(); + self.ctrl3.set_high(); } pub(crate) fn set_tx_lp(&mut self) { - self.ctrl1.set_high().unwrap(); - self.ctrl2.set_high().unwrap(); - self.ctrl3.set_high().unwrap(); + self.ctrl1.set_high(); + self.ctrl2.set_high(); + self.ctrl3.set_high(); } #[allow(dead_code)] pub(crate) fn set_tx_hp(&mut self) { - self.ctrl2.set_high().unwrap(); - self.ctrl1.set_low().unwrap(); - self.ctrl3.set_high().unwrap(); + self.ctrl2.set_high(); + self.ctrl1.set_low(); + self.ctrl3.set_high(); } } diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 552433b8..eded6ba7 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -94,17 +94,25 @@ impl<'d, T: GpioPin> ExtiInput<'d, T> { pub fn new(pin: Input<'d, T>, _ch: impl Unborrow + 'd) -> Self { Self { pin } } + + pub fn is_high(&self) -> bool { + self.pin.is_high() + } + + pub fn is_low(&self) -> bool { + self.pin.is_low() + } } impl<'d, T: GpioPin> InputPin for ExtiInput<'d, T> { type Error = Infallible; fn is_high(&self) -> Result { - self.pin.is_high() + Ok(self.is_high()) } fn is_low(&self) -> Result { - self.pin.is_low() + Ok(self.is_low()) } } diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 6b4a9285..57b9ba6b 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -3,7 +3,7 @@ use core::convert::Infallible; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::{unborrow, unsafe_impl_unborrow}; -use embedded_hal::digital::v2::{toggleable, InputPin, OutputPin, StatefulOutputPin}; +use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}; use crate::pac; use crate::pac::gpio::{self, vals}; @@ -113,6 +113,15 @@ impl<'d, T: Pin> Input<'d, T> { phantom: PhantomData, } } + + pub fn is_high(&self) -> bool { + !self.is_low() + } + + pub fn is_low(&self) -> bool { + let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as _) }; + state == vals::Idr::LOW + } } impl<'d, T: Pin> Drop for Input<'d, T> { @@ -132,19 +141,6 @@ impl<'d, T: Pin> Drop for Input<'d, T> { } } -impl<'d, T: Pin> InputPin for Input<'d, T> { - type Error = Infallible; - - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) - } - - fn is_low(&self) -> Result { - let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as _) }; - Ok(state == vals::Idr::LOW) - } -} - /// Digital input or output level. #[derive(Debug, Eq, PartialEq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -191,6 +187,36 @@ impl<'d, T: Pin> Output<'d, T> { phantom: PhantomData, } } + + /// Set the output as high. + pub fn set_high(&mut self) { + self.pin.set_high(); + } + + /// Set the output as low. + pub fn set_low(&mut self) { + self.pin.set_low(); + } + + /// Is the output pin set as high? + pub fn is_set_high(&self) -> bool { + !self.is_set_low() + } + + /// Is the output pin set as low? + pub fn is_set_low(&self) -> bool { + let state = unsafe { self.pin.block().odr().read().odr(self.pin.pin() as _) }; + state == vals::Odr::LOW + } + + /// Toggle pin output + pub fn toggle(&mut self) { + if self.is_set_low() { + self.set_high() + } else { + self.set_low() + } + } } impl<'d, T: Pin> Drop for Output<'d, T> { @@ -214,37 +240,6 @@ impl<'d, T: Pin> Drop for Output<'d, T> { } } -impl<'d, T: Pin> OutputPin for Output<'d, T> { - type Error = Infallible; - - /// Set the output as high. - fn set_high(&mut self) -> Result<(), Self::Error> { - self.pin.set_high(); - Ok(()) - } - - /// Set the output as low. - fn set_low(&mut self) -> Result<(), Self::Error> { - self.pin.set_low(); - Ok(()) - } -} - -impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> { - /// Is the output pin set as high? - fn is_set_high(&self) -> Result { - self.is_set_low().map(|v| !v) - } - - /// Is the output pin set as low? - fn is_set_low(&self) -> Result { - let state = unsafe { self.pin.block().odr().read().odr(self.pin.pin() as _) }; - Ok(state == vals::Odr::LOW) - } -} - -impl<'d, T: Pin> toggleable::Default for Output<'d, T> {} - /// GPIO output open-drain driver. pub struct OutputOpenDrain<'d, T: Pin> { pub(crate) pin: T, @@ -294,6 +289,45 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { phantom: PhantomData, } } + + pub fn is_high(&self) -> bool { + !self.is_low() + } + + pub fn is_low(&self) -> bool { + let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as _) }; + state == vals::Idr::LOW + } + + /// Set the output as high. + pub fn set_high(&mut self) { + self.pin.set_high(); + } + + /// Set the output as low. + pub fn set_low(&mut self) { + self.pin.set_low(); + } + + /// Is the output pin set as high? + pub fn is_set_high(&self) -> bool { + !self.is_set_low() + } + + /// Is the output pin set as low? + pub fn is_set_low(&self) -> bool { + let state = unsafe { self.pin.block().odr().read().odr(self.pin.pin() as _) }; + state == vals::Odr::LOW + } + + /// Toggle pin output + pub fn toggle(&mut self) { + if self.is_set_low() { + self.set_high() + } else { + self.set_low() + } + } } impl<'d, T: Pin> Drop for OutputOpenDrain<'d, T> { @@ -317,36 +351,6 @@ impl<'d, T: Pin> Drop for OutputOpenDrain<'d, T> { } } -impl<'d, T: Pin> OutputPin for OutputOpenDrain<'d, T> { - type Error = Infallible; - - /// Set the output as high. - fn set_high(&mut self) -> Result<(), Self::Error> { - self.pin.set_high(); - Ok(()) - } - - /// Set the output as low. - fn set_low(&mut self) -> Result<(), Self::Error> { - self.pin.set_low(); - Ok(()) - } -} - -impl<'d, T: Pin> InputPin for OutputOpenDrain<'d, T> { - type Error = Infallible; - - fn is_high(&self) -> Result { - self.is_low().map(|v| !v) - } - - fn is_low(&self) -> Result { - // NOTE(safety) Atomic read - let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as usize) }; - Ok(state == vals::Idr::LOW) - } -} - pub(crate) mod sealed { use super::*; @@ -612,3 +616,79 @@ pub(crate) unsafe fn init() { }; } } + +mod eh02 { + use super::*; + + impl<'d, T: Pin> InputPin for Input<'d, T> { + type Error = Infallible; + + fn is_high(&self) -> Result { + Ok(self.is_high()) + } + + fn is_low(&self) -> Result { + Ok(self.is_low()) + } + } + + impl<'d, T: Pin> OutputPin for Output<'d, T> { + type Error = Infallible; + + fn set_high(&mut self) -> Result<(), Self::Error> { + Ok(self.set_high()) + } + + fn set_low(&mut self) -> Result<(), Self::Error> { + Ok(self.set_low()) + } + } + + impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> { + fn is_set_high(&self) -> Result { + Ok(self.is_set_high()) + } + + /// Is the output pin set as low? + fn is_set_low(&self) -> Result { + Ok(self.is_set_low()) + } + } + + impl<'d, T: Pin> ToggleableOutputPin for Output<'d, T> { + type Error = Infallible; + fn toggle(&mut self) -> Result<(), Self::Error> { + Ok(self.toggle()) + } + } + + impl<'d, T: Pin> OutputPin for OutputOpenDrain<'d, T> { + type Error = Infallible; + + fn set_high(&mut self) -> Result<(), Self::Error> { + Ok(self.set_high()) + } + + fn set_low(&mut self) -> Result<(), Self::Error> { + Ok(self.set_low()) + } + } + + impl<'d, T: Pin> StatefulOutputPin for OutputOpenDrain<'d, T> { + fn is_set_high(&self) -> Result { + Ok(self.is_set_high()) + } + + /// Is the output pin set as low? + fn is_set_low(&self) -> Result { + Ok(self.is_set_low()) + } + } + + impl<'d, T: Pin> ToggleableOutputPin for OutputOpenDrain<'d, T> { + type Error = Infallible; + fn toggle(&mut self) -> Result<(), Self::Error> { + Ok(self.toggle()) + } + } +} diff --git a/examples/stm32f1/src/bin/blinky.rs b/examples/stm32f1/src/bin/blinky.rs index 1e4f2dee..0d953745 100644 --- a/examples/stm32f1/src/bin/blinky.rs +++ b/examples/stm32f1/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32f3/src/bin/blinky.rs b/examples/stm32f3/src/bin/blinky.rs index 32164355..e8b8dc23 100644 --- a/examples/stm32f3/src/bin/blinky.rs +++ b/examples/stm32f3/src/bin/blinky.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -20,11 +19,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(1000)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(1000)).await; } } diff --git a/examples/stm32f3/src/bin/button.rs b/examples/stm32f3/src/bin/button.rs index c5fab138..131d4af4 100644 --- a/examples/stm32f3/src/bin/button.rs +++ b/examples/stm32f3/src/bin/button.rs @@ -6,7 +6,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[entry] @@ -20,14 +19,14 @@ fn main() -> ! { let mut led2 = Output::new(p.PE15, Level::High, Speed::Low); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); - unwrap!(led1.set_high()); - unwrap!(led2.set_low()); + led1.set_high(); + led2.set_low(); } else { info!("low"); - unwrap!(led1.set_low()); - unwrap!(led2.set_high()); + led1.set_low(); + led2.set_high(); } } } diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs index c4857195..00d67dac 100644 --- a/examples/stm32f4/src/bin/blinky.rs +++ b/examples/stm32f4/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32f4/src/bin/button.rs b/examples/stm32f4/src/bin/button.rs index 95dee7c7..24eef75b 100644 --- a/examples/stm32f4/src/bin/button.rs +++ b/examples/stm32f4/src/bin/button.rs @@ -6,7 +6,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[entry] @@ -21,14 +20,14 @@ fn main() -> ! { let mut led3 = Output::new(p.PB14, Level::High, Speed::Low); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); - unwrap!(led1.set_high()); - unwrap!(led3.set_low()); + led1.set_high(); + led3.set_low(); } else { info!("low"); - unwrap!(led1.set_low()); - unwrap!(led3.set_high()); + led1.set_low(); + led3.set_high(); } } } diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs index 0192e186..b66eb958 100644 --- a/examples/stm32f4/src/bin/spi.rs +++ b/examples/stm32f4/src/bin/spi.rs @@ -11,7 +11,6 @@ use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embedded_hal::blocking::spi::Transfer; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[entry] @@ -35,9 +34,9 @@ fn main() -> ! { loop { let mut buf = [0x0Au8; 4]; - unwrap!(cs.set_low()); + cs.set_low(); unwrap!(spi.transfer(&mut buf)); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", buf); } } diff --git a/examples/stm32f7/src/bin/blinky.rs b/examples/stm32f7/src/bin/blinky.rs index c4857195..00d67dac 100644 --- a/examples/stm32f7/src/bin/blinky.rs +++ b/examples/stm32f7/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32f7/src/bin/button.rs b/examples/stm32f7/src/bin/button.rs index 95dee7c7..24eef75b 100644 --- a/examples/stm32f7/src/bin/button.rs +++ b/examples/stm32f7/src/bin/button.rs @@ -6,7 +6,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[entry] @@ -21,14 +20,14 @@ fn main() -> ! { let mut led3 = Output::new(p.PB14, Level::High, Speed::Low); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); - unwrap!(led1.set_high()); - unwrap!(led3.set_low()); + led1.set_high(); + led3.set_low(); } else { info!("low"); - unwrap!(led1.set_low()); - unwrap!(led3.set_high()); + led1.set_low(); + led3.set_high(); } } } diff --git a/examples/stm32g0/src/bin/blinky.rs b/examples/stm32g0/src/bin/blinky.rs index c4857195..00d67dac 100644 --- a/examples/stm32g0/src/bin/blinky.rs +++ b/examples/stm32g0/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32g0/src/bin/button.rs b/examples/stm32g0/src/bin/button.rs index 4ca2a43b..e901c575 100644 --- a/examples/stm32g0/src/bin/button.rs +++ b/examples/stm32g0/src/bin/button.rs @@ -6,7 +6,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::gpio::{Input, Pull}; -use embedded_hal::digital::v2::InputPin; use example_common::*; #[entry] @@ -18,7 +17,7 @@ fn main() -> ! { let button = Input::new(p.PC13, Pull::Up); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); } else { info!("low"); diff --git a/examples/stm32g4/src/bin/blinky.rs b/examples/stm32g4/src/bin/blinky.rs index a43922a6..1dc67f99 100644 --- a/examples/stm32g4/src/bin/blinky.rs +++ b/examples/stm32g4/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32g4/src/bin/button.rs b/examples/stm32g4/src/bin/button.rs index f0a4c874..8c0d7d4f 100644 --- a/examples/stm32g4/src/bin/button.rs +++ b/examples/stm32g4/src/bin/button.rs @@ -6,7 +6,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::gpio::{Input, Pull}; -use embedded_hal::digital::v2::InputPin; use example_common::*; #[entry] @@ -18,7 +17,7 @@ fn main() -> ! { let button = Input::new(p.PC13, Pull::Down); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); } else { info!("low"); diff --git a/examples/stm32h7/src/bin/blinky.rs b/examples/stm32h7/src/bin/blinky.rs index 78edb5e2..7e593423 100644 --- a/examples/stm32h7/src/bin/blinky.rs +++ b/examples/stm32h7/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32h7/src/bin/camera.rs b/examples/stm32h7/src/bin/camera.rs index d9459207..9e8071bb 100644 --- a/examples/stm32h7/src/bin/camera.rs +++ b/examples/stm32h7/src/bin/camera.rs @@ -11,7 +11,6 @@ use embassy_stm32::interrupt; use embassy_stm32::rcc::{Mco, Mco1Source, McoClock}; use embassy_stm32::time::U32Ext; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use defmt_rtt as _; // global logger use panic_probe as _; @@ -114,11 +113,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { defmt::info!("main loop running"); loop { defmt::info!("high"); - defmt::unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; defmt::info!("low"); - defmt::unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32h7/src/bin/mco.rs b/examples/stm32h7/src/bin/mco.rs index 4cecd9b0..f27bd8ef 100644 --- a/examples/stm32h7/src/bin/mco.rs +++ b/examples/stm32h7/src/bin/mco.rs @@ -9,7 +9,6 @@ use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::rcc::{Mco, Mco1Source, McoClock}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -22,11 +21,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs index d64ad9bc..8e03861d 100644 --- a/examples/stm32h7/src/bin/rng.rs +++ b/examples/stm32h7/src/bin/rng.rs @@ -10,7 +10,6 @@ use embassy::traits::rng::Random; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::rng::Rng; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -23,11 +22,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high {}", unwrap!(rng.next_u8(16).await)); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; info!("low {}", unwrap!(rng.next_u8(16).await)); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32l0/src/bin/blinky.rs b/examples/stm32l0/src/bin/blinky.rs index 1198b29d..46e29a89 100644 --- a/examples/stm32l0/src/bin/blinky.rs +++ b/examples/stm32l0/src/bin/blinky.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -20,11 +19,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32l0/src/bin/button.rs b/examples/stm32l0/src/bin/button.rs index c2915530..046c43ca 100644 --- a/examples/stm32l0/src/bin/button.rs +++ b/examples/stm32l0/src/bin/button.rs @@ -7,7 +7,6 @@ mod example_common; use embassy::executor::Spawner; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[embassy::main] @@ -19,14 +18,14 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led2 = Output::new(p.PB5, Level::High, Speed::Low); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); - unwrap!(led1.set_high()); - unwrap!(led2.set_low()); + led1.set_high(); + led2.set_low(); } else { info!("low"); - unwrap!(led1.set_low()); - unwrap!(led2.set_high()); + led1.set_low(); + led2.set_high(); } } } diff --git a/examples/stm32l0/src/bin/spi.rs b/examples/stm32l0/src/bin/spi.rs index f768a522..d30bb8d7 100644 --- a/examples/stm32l0/src/bin/spi.rs +++ b/examples/stm32l0/src/bin/spi.rs @@ -7,7 +7,6 @@ mod example_common; use embassy::executor::Spawner; use embassy_stm32::gpio::{Level, Output, Speed}; -use embedded_hal::digital::v2::OutputPin; use example_common::*; use embassy_stm32::dma::NoDma; @@ -35,9 +34,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { let mut buf = [0x0Au8; 4]; - unwrap!(cs.set_low()); + cs.set_low(); unwrap!(spi.transfer(&mut buf)); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", buf); } } diff --git a/examples/stm32l1/src/bin/blinky.rs b/examples/stm32l1/src/bin/blinky.rs index deabdddb..07c804e9 100644 --- a/examples/stm32l1/src/bin/blinky.rs +++ b/examples/stm32l1/src/bin/blinky.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -20,11 +19,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(1000)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(1000)).await; } } diff --git a/examples/stm32l1/src/bin/spi.rs b/examples/stm32l1/src/bin/spi.rs index 3cfbe3fc..9d1a2fc8 100644 --- a/examples/stm32l1/src/bin/spi.rs +++ b/examples/stm32l1/src/bin/spi.rs @@ -7,7 +7,6 @@ mod example_common; use embassy::executor::Spawner; use embassy_stm32::gpio::{Level, Output, Speed}; -use embedded_hal::digital::v2::OutputPin; use example_common::*; use embassy_stm32::dma::NoDma; @@ -35,9 +34,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { let mut buf = [0x0Au8; 4]; - unwrap!(cs.set_low()); + cs.set_low(); unwrap!(spi.transfer(&mut buf)); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", buf); } } diff --git a/examples/stm32l4/src/bin/blinky.rs b/examples/stm32l4/src/bin/blinky.rs index 8a65858f..03028375 100644 --- a/examples/stm32l4/src/bin/blinky.rs +++ b/examples/stm32l4/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -18,9 +17,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led = Output::new(p.PB14, Level::High, Speed::Low); loop { - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(300)).await; - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32l4/src/bin/button.rs b/examples/stm32l4/src/bin/button.rs index fd867454..6073c137 100644 --- a/examples/stm32l4/src/bin/button.rs +++ b/examples/stm32l4/src/bin/button.rs @@ -5,7 +5,6 @@ #[path = "../example_common.rs"] mod example_common; use embassy_stm32::gpio::{Input, Pull}; -use embedded_hal::digital::v2::InputPin; use example_common::*; #[cortex_m_rt::entry] @@ -17,7 +16,7 @@ fn main() -> ! { let button = Input::new(p.PC13, Pull::Up); loop { - if unwrap!(button.is_high()) { + if button.is_high() { info!("high"); } else { info!("low"); diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs index 5b9ae1ce..1b6e3946 100644 --- a/examples/stm32l4/src/bin/spi.rs +++ b/examples/stm32l4/src/bin/spi.rs @@ -10,7 +10,6 @@ use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embedded_hal::blocking::spi::Transfer; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[cortex_m_rt::entry] @@ -34,9 +33,9 @@ fn main() -> ! { loop { let mut buf = [0x0Au8; 4]; - unwrap!(cs.set_low()); + cs.set_low(); unwrap!(spi.transfer(&mut buf)); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", buf); } } diff --git a/examples/stm32l4/src/bin/spi_blocking_async.rs b/examples/stm32l4/src/bin/spi_blocking_async.rs index f092706d..3be3f21c 100644 --- a/examples/stm32l4/src/bin/spi_blocking_async.rs +++ b/examples/stm32l4/src/bin/spi_blocking_async.rs @@ -12,7 +12,6 @@ use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; use embassy_traits::{adapter::BlockingAsync, spi::FullDuplex}; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[embassy::main] @@ -41,17 +40,17 @@ async fn main(_spawner: Spawner, p: Peripherals) { let ready = Input::new(p.PE1, Pull::Up); cortex_m::asm::delay(100_000); - unwrap!(reset.set_high()); + reset.set_high(); cortex_m::asm::delay(100_000); - while unwrap!(ready.is_low()) { + while ready.is_low() { info!("waiting for ready"); } let write = [0x0A; 10]; let mut read = [0; 10]; - unwrap!(cs.set_low()); + cs.set_low(); spi.read_write(&mut read, &write).await.ok(); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", read); } diff --git a/examples/stm32l4/src/bin/spi_dma.rs b/examples/stm32l4/src/bin/spi_dma.rs index 4b74c7d7..d6464bbf 100644 --- a/examples/stm32l4/src/bin/spi_dma.rs +++ b/examples/stm32l4/src/bin/spi_dma.rs @@ -11,7 +11,6 @@ use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; use embassy_traits::spi::FullDuplex; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[embassy::main] @@ -38,17 +37,17 @@ async fn main(_spawner: Spawner, p: Peripherals) { let ready = Input::new(p.PE1, Pull::Up); cortex_m::asm::delay(100_000); - unwrap!(reset.set_high()); + reset.set_high(); cortex_m::asm::delay(100_000); - while unwrap!(ready.is_low()) { + while ready.is_low() { info!("waiting for ready"); } let write = [0x0A; 10]; let mut read = [0; 10]; - unwrap!(cs.set_low()); + cs.set_low(); spi.read_write(&mut read, &write).await.ok(); - unwrap!(cs.set_high()); + cs.set_high(); info!("xfer {=[u8]:x}", read); } diff --git a/examples/stm32wb55/src/bin/blinky.rs b/examples/stm32wb55/src/bin/blinky.rs index 42522fe9..e1dbb30d 100644 --- a/examples/stm32wb55/src/bin/blinky.rs +++ b/examples/stm32wb55/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32wl55/src/bin/blinky.rs b/examples/stm32wl55/src/bin/blinky.rs index 3c12a79d..9ec208c3 100644 --- a/examples/stm32wl55/src/bin/blinky.rs +++ b/examples/stm32wl55/src/bin/blinky.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::*; #[embassy::main] @@ -19,11 +18,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - unwrap!(led.set_high()); + led.set_high(); Timer::after(Duration::from_millis(500)).await; info!("low"); - unwrap!(led.set_low()); + led.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32wl55/src/bin/button.rs b/examples/stm32wl55/src/bin/button.rs index 55b68866..be8f60e2 100644 --- a/examples/stm32wl55/src/bin/button.rs +++ b/examples/stm32wl55/src/bin/button.rs @@ -5,7 +5,6 @@ #[path = "../example_common.rs"] mod example_common; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; use cortex_m_rt::entry; @@ -21,12 +20,12 @@ fn main() -> ! { let mut led2 = Output::new(p.PB9, Level::High, Speed::Low); loop { - if button.is_high().unwrap() { - led1.set_high().unwrap(); - led2.set_low().unwrap(); + if button.is_high() { + led1.set_high(); + led2.set_low(); } else { - led1.set_low().unwrap(); - led2.set_high().unwrap(); + led1.set_low(); + led2.set_high(); } } } diff --git a/examples/stm32wl55/src/bin/subghz.rs b/examples/stm32wl55/src/bin/subghz.rs index 52fe6e9f..570bd980 100644 --- a/examples/stm32wl55/src/bin/subghz.rs +++ b/examples/stm32wl55/src/bin/subghz.rs @@ -17,7 +17,6 @@ use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::interrupt; use embassy_stm32::subghz::*; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::OutputPin; use example_common::unwrap; const PING_DATA: &str = "PING"; @@ -89,9 +88,9 @@ async fn main(_spawner: embassy::executor::Spawner, p: Peripherals) { defmt::info!("Radio ready for use"); - unwrap!(led1.set_low()); + led1.set_low(); - unwrap!(led2.set_high()); + led2.set_high(); unwrap!(radio.set_standby(StandbyClk::Rc)); unwrap!(radio.set_tcxo_mode(&TCXO_MODE)); @@ -110,11 +109,11 @@ async fn main(_spawner: embassy::executor::Spawner, p: Peripherals) { defmt::info!("Status: {:?}", unwrap!(radio.status())); - unwrap!(led2.set_low()); + led2.set_low(); loop { pin.wait_for_rising_edge().await; - unwrap!(led3.set_high()); + led3.set_high(); unwrap!(radio.set_irq_cfg(&CfgIrq::new().irq_enable_all(Irq::TxDone))); unwrap!(radio.write_buffer(TX_BUF_OFFSET, PING_DATA_BYTES)); unwrap!(radio.set_tx(Timeout::DISABLED)); @@ -127,6 +126,6 @@ async fn main(_spawner: embassy::executor::Spawner, p: Peripherals) { defmt::info!("TX done"); } unwrap!(radio.clear_irq_status(irq_status)); - unwrap!(led3.set_low()); + led3.set_low(); } } diff --git a/tests/stm32/src/bin/gpio.rs b/tests/stm32/src/bin/gpio.rs index 51ede6ce..305da8d1 100644 --- a/tests/stm32/src/bin/gpio.rs +++ b/tests/stm32/src/bin/gpio.rs @@ -8,7 +8,6 @@ use defmt::assert; use embassy::executor::Spawner; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embedded_hal::digital::v2::{InputPin, OutputPin}; use example_common::*; #[embassy::main(config = "config()")] @@ -35,12 +34,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { { let _a = Output::new(&mut a, Level::Low, Speed::Low); delay(); - assert!(b.is_low().unwrap()); + assert!(b.is_low()); } { let _a = Output::new(&mut a, Level::High, Speed::Low); delay(); - assert!(b.is_high().unwrap()); + assert!(b.is_high()); } } @@ -51,38 +50,38 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut a = Output::new(&mut a, Level::Low, Speed::Low); delay(); - assert!(b.is_low().unwrap()); - a.set_high().unwrap(); + assert!(b.is_low()); + a.set_high(); delay(); - assert!(b.is_high().unwrap()); + assert!(b.is_high()); } // Test input pulldown { let b = Input::new(&mut b, Pull::Down); delay(); - assert!(b.is_low().unwrap()); + assert!(b.is_low()); let mut a = Output::new(&mut a, Level::Low, Speed::Low); delay(); - assert!(b.is_low().unwrap()); - a.set_high().unwrap(); + assert!(b.is_low()); + a.set_high(); delay(); - assert!(b.is_high().unwrap()); + assert!(b.is_high()); } // Test input pullup { let b = Input::new(&mut b, Pull::Up); delay(); - assert!(b.is_high().unwrap()); + assert!(b.is_high()); let mut a = Output::new(&mut a, Level::Low, Speed::Low); delay(); - assert!(b.is_low().unwrap()); - a.set_high().unwrap(); + assert!(b.is_low()); + a.set_high(); delay(); - assert!(b.is_high().unwrap()); + assert!(b.is_high()); } info!("Test OK"); From b526addf7b98a9c3612f2299184592dad3cd74f7 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 22:10:24 +0100 Subject: [PATCH 04/10] stm32/exti: expose all functionality as inherent methods. --- embassy-stm32/src/exti.rs | 26 +++++++++++++++++------ examples/stm32f3/src/bin/button_exti.rs | 1 - examples/stm32f4/src/bin/button_exti.rs | 1 - examples/stm32f7/src/bin/button_exti.rs | 1 - examples/stm32g0/src/bin/button_exti.rs | 1 - examples/stm32g4/src/bin/button_exti.rs | 1 - examples/stm32h7/src/bin/button_exti.rs | 1 - examples/stm32l0/src/bin/button_exti.rs | 1 - examples/stm32l4/src/bin/button_exti.rs | 1 - examples/stm32wb55/src/bin/button_exti.rs | 1 - examples/stm32wl55/src/bin/button_exti.rs | 1 - examples/stm32wl55/src/bin/subghz.rs | 1 - 12 files changed, 19 insertions(+), 18 deletions(-) diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index eded6ba7..b2f16888 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -102,6 +102,18 @@ impl<'d, T: GpioPin> ExtiInput<'d, T> { pub fn is_low(&self) -> bool { self.pin.is_low() } + + pub async fn wait_for_rising_edge<'a>(&'a mut self) { + ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, false).await + } + + pub async fn wait_for_falling_edge<'a>(&'a mut self) { + ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), false, true).await + } + + pub async fn wait_for_any_edge<'a>(&'a mut self) { + ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, true).await + } } impl<'d, T: GpioPin> InputPin for ExtiInput<'d, T> { @@ -120,10 +132,10 @@ impl<'d, T: GpioPin> WaitForRisingEdge for ExtiInput<'d, T> { type Future<'a> where Self: 'a, - = ExtiInputFuture<'a>; + = impl Future + 'a; fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a> { - ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, false) + self.wait_for_rising_edge() } } @@ -131,10 +143,10 @@ impl<'d, T: GpioPin> WaitForFallingEdge for ExtiInput<'d, T> { type Future<'a> where Self: 'a, - = ExtiInputFuture<'a>; + = impl Future + 'a; fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a> { - ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), false, true) + self.wait_for_falling_edge() } } @@ -142,14 +154,14 @@ impl<'d, T: GpioPin> WaitForAnyEdge for ExtiInput<'d, T> { type Future<'a> where Self: 'a, - = ExtiInputFuture<'a>; + = impl Future + 'a; fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { - ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, true) + self.wait_for_any_edge() } } -pub struct ExtiInputFuture<'a> { +struct ExtiInputFuture<'a> { pin: u8, phantom: PhantomData<&'a mut AnyPin>, } diff --git a/examples/stm32f3/src/bin/button_exti.rs b/examples/stm32f3/src/bin/button_exti.rs index d45e4365..b11f38ea 100644 --- a/examples/stm32f3/src/bin/button_exti.rs +++ b/examples/stm32f3/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32f4/src/bin/button_exti.rs b/examples/stm32f4/src/bin/button_exti.rs index 2c4318d6..852fbe3c 100644 --- a/examples/stm32f4/src/bin/button_exti.rs +++ b/examples/stm32f4/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32f7/src/bin/button_exti.rs b/examples/stm32f7/src/bin/button_exti.rs index 2c4318d6..852fbe3c 100644 --- a/examples/stm32f7/src/bin/button_exti.rs +++ b/examples/stm32f7/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32g0/src/bin/button_exti.rs b/examples/stm32g0/src/bin/button_exti.rs index 0c2483ec..848818bf 100644 --- a/examples/stm32g0/src/bin/button_exti.rs +++ b/examples/stm32g0/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32g4/src/bin/button_exti.rs b/examples/stm32g4/src/bin/button_exti.rs index 2c4318d6..852fbe3c 100644 --- a/examples/stm32g4/src/bin/button_exti.rs +++ b/examples/stm32g4/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32h7/src/bin/button_exti.rs b/examples/stm32h7/src/bin/button_exti.rs index 2c4318d6..852fbe3c 100644 --- a/examples/stm32h7/src/bin/button_exti.rs +++ b/examples/stm32h7/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32l0/src/bin/button_exti.rs b/examples/stm32l0/src/bin/button_exti.rs index 88c75ce6..3edea397 100644 --- a/examples/stm32l0/src/bin/button_exti.rs +++ b/examples/stm32l0/src/bin/button_exti.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; fn config() -> embassy_stm32::Config { diff --git a/examples/stm32l4/src/bin/button_exti.rs b/examples/stm32l4/src/bin/button_exti.rs index 0c2483ec..848818bf 100644 --- a/examples/stm32l4/src/bin/button_exti.rs +++ b/examples/stm32l4/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32wb55/src/bin/button_exti.rs b/examples/stm32wb55/src/bin/button_exti.rs index aeb7bd8a..4592fa30 100644 --- a/examples/stm32wb55/src/bin/button_exti.rs +++ b/examples/stm32wb55/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32wl55/src/bin/button_exti.rs b/examples/stm32wl55/src/bin/button_exti.rs index 31adfb5d..8d66c725 100644 --- a/examples/stm32wl55/src/bin/button_exti.rs +++ b/examples/stm32wl55/src/bin/button_exti.rs @@ -8,7 +8,6 @@ use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::Peripherals; -use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; #[embassy::main] diff --git a/examples/stm32wl55/src/bin/subghz.rs b/examples/stm32wl55/src/bin/subghz.rs index 570bd980..42d4eb64 100644 --- a/examples/stm32wl55/src/bin/subghz.rs +++ b/examples/stm32wl55/src/bin/subghz.rs @@ -10,7 +10,6 @@ mod example_common; use embassy::channel::signal::Signal; use embassy::interrupt::{Interrupt, InterruptExt}; -use embassy::traits::gpio::WaitForRisingEdge; use embassy_stm32::dma::NoDma; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; From ade44e91c47364b40d015f2f0d438866d8af0fd4 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 22:22:38 +0100 Subject: [PATCH 05/10] stm32/exti: add wait_for_high, wait_for_low. --- embassy-stm32/src/exti.rs | 42 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index b2f16888..af401796 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -3,7 +3,9 @@ use core::future::Future; use core::marker::PhantomData; use core::pin::Pin; use core::task::{Context, Poll}; -use embassy::traits::gpio::{WaitForAnyEdge, WaitForFallingEdge, WaitForRisingEdge}; +use embassy::traits::gpio::{ + WaitForAnyEdge, WaitForFallingEdge, WaitForHigh, WaitForLow, WaitForRisingEdge, +}; use embassy::util::Unborrow; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::unsafe_impl_unborrow; @@ -103,6 +105,22 @@ impl<'d, T: GpioPin> ExtiInput<'d, T> { self.pin.is_low() } + pub async fn wait_for_high<'a>(&'a mut self) { + let fut = ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, false); + if self.is_high() { + return; + } + fut.await + } + + pub async fn wait_for_low<'a>(&'a mut self) { + let fut = ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), false, true); + if self.is_low() { + return; + } + fut.await + } + pub async fn wait_for_rising_edge<'a>(&'a mut self) { ExtiInputFuture::new(self.pin.pin.pin(), self.pin.pin.port(), true, false).await } @@ -128,6 +146,28 @@ impl<'d, T: GpioPin> InputPin for ExtiInput<'d, T> { } } +impl<'d, T: GpioPin> WaitForHigh for ExtiInput<'d, T> { + type Future<'a> + where + Self: 'a, + = impl Future + 'a; + + fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { + self.wait_for_high() + } +} + +impl<'d, T: GpioPin> WaitForLow for ExtiInput<'d, T> { + type Future<'a> + where + Self: 'a, + = impl Future + 'a; + + fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { + self.wait_for_low() + } +} + impl<'d, T: GpioPin> WaitForRisingEdge for ExtiInput<'d, T> { type Future<'a> where From 98f24bf819a527997ed983d686c4ab933468439d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 22:24:17 +0100 Subject: [PATCH 06/10] examples/stm32l0: cleanup --- examples/stm32l0/.cargo/config.toml | 2 +- examples/stm32l0/Cargo.toml | 2 +- examples/stm32l0/build.rs | 30 ----------------------------- examples/stm32l0/memory.x | 5 ----- 4 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 examples/stm32l0/memory.x diff --git a/examples/stm32l0/.cargo/config.toml b/examples/stm32l0/.cargo/config.toml index 4ea04748..840faa62 100644 --- a/examples/stm32l0/.cargo/config.toml +++ b/examples/stm32l0/.cargo/config.toml @@ -1,6 +1,6 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] # replace your chip as listed in `probe-run --list-chips` -runner = "probe-run --chip STM32L072CZ" +runner = "probe-run --chip STM32L072CZTx" [build] target = "thumbv6m-none-eabi" diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index 7056d580..20a2ec8d 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-tim3", "exti"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-tim3", "exti", "memory-x"] } embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt"] } diff --git a/examples/stm32l0/build.rs b/examples/stm32l0/build.rs index 30691aa9..8cd32d7e 100644 --- a/examples/stm32l0/build.rs +++ b/examples/stm32l0/build.rs @@ -1,34 +1,4 @@ -//! This build script copies the `memory.x` file from the crate root into -//! a directory where the linker can always find it at build time. -//! For many projects this is optional, as the linker always searches the -//! project root directory -- wherever `Cargo.toml` is. However, if you -//! are using a workspace or have a more complicated build setup, this -//! build script becomes required. Additionally, by requesting that -//! Cargo re-run the build script whenever `memory.x` is changed, -//! updating `memory.x` ensures a rebuild of the application with the -//! new memory settings. - -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::PathBuf; - fn main() { - // Put `memory.x` in our output directory and ensure it's - // on the linker search path. - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); - File::create(out.join("memory.x")) - .unwrap() - .write_all(include_bytes!("memory.x")) - .unwrap(); - println!("cargo:rustc-link-search={}", out.display()); - - // By default, Cargo will re-run a build script whenever - // any file in the project changes. By specifying `memory.x` - // here, we ensure the build script is only re-run when - // `memory.x` is changed. - println!("cargo:rerun-if-changed=memory.x"); - println!("cargo:rustc-link-arg-bins=--nmagic"); println!("cargo:rustc-link-arg-bins=-Tlink.x"); println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); diff --git a/examples/stm32l0/memory.x b/examples/stm32l0/memory.x deleted file mode 100644 index 67a50a5f..00000000 --- a/examples/stm32l0/memory.x +++ /dev/null @@ -1,5 +0,0 @@ -MEMORY -{ - FLASH : ORIGIN = 0x08000000, LENGTH = 192K - RAM : ORIGIN = 0x20000000, LENGTH = 20K -} From c949519714268afaf9b26d0ff4a7bc3c207b27d2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 22:43:40 +0100 Subject: [PATCH 07/10] stm32/usart: expose all functionality as inherent methods. --- embassy-stm32/src/usart/mod.rs | 44 ++++++++++++++++----------- examples/stm32f3/src/bin/usart_dma.rs | 1 - examples/stm32f4/src/bin/usart.rs | 7 ++--- examples/stm32f4/src/bin/usart_dma.rs | 1 - examples/stm32f7/src/bin/usart_dma.rs | 1 - examples/stm32h7/src/bin/usart.rs | 7 ++--- examples/stm32h7/src/bin/usart_dma.rs | 1 - examples/stm32l0/src/bin/usart_dma.rs | 1 - examples/stm32l4/src/bin/usart.rs | 7 ++--- examples/stm32l4/src/bin/usart_dma.rs | 1 - tests/stm32/src/bin/usart.rs | 5 ++- tests/stm32/src/bin/usart_dma.rs | 1 - tests/stm32/teleprobe.sh | 12 ++++++++ 13 files changed, 49 insertions(+), 40 deletions(-) create mode 100755 tests/stm32/teleprobe.sh diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index e46e14ee..5309c629 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -129,7 +129,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { } } - async fn write_dma(&mut self, buffer: &[u8]) -> Result<(), Error> + pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error> where TxDma: crate::usart::TxDma, { @@ -146,7 +146,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { Ok(()) } - async fn read_dma(&mut self, buffer: &mut [u8]) -> Result<(), Error> + pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> where RxDma: crate::usart::RxDma, { @@ -163,7 +163,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { Ok(()) } - pub fn read_blocking(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + pub fn blocking_read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { unsafe { let r = self.inner.regs(); for b in buffer { @@ -190,6 +190,25 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { } Ok(()) } + + pub fn blocking_write(&mut self, buffer: &[u8]) -> Result<(), Error> { + unsafe { + let r = self.inner.regs(); + for &b in buffer { + while !sr(r).read().txe() {} + tdr(r).write_volatile(b); + } + } + Ok(()) + } + + pub fn blocking_flush(&mut self) -> Result<(), Error> { + unsafe { + let r = self.inner.regs(); + while !sr(r).read().tc() {} + } + Ok(()) + } } impl<'d, T: Instance, TxDma, RxDma> embedded_hal::serial::Read for Uart<'d, T, TxDma, RxDma> { @@ -224,21 +243,10 @@ impl<'d, T: Instance, TxDma, RxDma> embedded_hal::blocking::serial::Write { type Error = Error; fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - unsafe { - let r = self.inner.regs(); - for &b in buffer { - while !sr(r).read().txe() {} - tdr(r).write_volatile(b); - } - } - Ok(()) + self.blocking_write(buffer) } fn bflush(&mut self) -> Result<(), Self::Error> { - unsafe { - let r = self.inner.regs(); - while !sr(r).read().tc() {} - } - Ok(()) + self.blocking_flush() } } @@ -252,7 +260,7 @@ where = impl Future> + 'a; fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma(buf) + self.write(buf) .map_err(|_| embassy_traits::uart::Error::Other) } } @@ -267,7 +275,7 @@ where = impl Future> + 'a; fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma(buf) + self.read(buf) .map_err(|_| embassy_traits::uart::Error::Other) } } diff --git a/examples/stm32f3/src/bin/usart_dma.rs b/examples/stm32f3/src/bin/usart_dma.rs index 99530b5c..0e67bb1f 100644 --- a/examples/stm32f3/src/bin/usart_dma.rs +++ b/examples/stm32f3/src/bin/usart_dma.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::Write as _; use example_common::*; use heapless::String; diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs index 391a8b9b..b5ea98cc 100644 --- a/examples/stm32f4/src/bin/usart.rs +++ b/examples/stm32f4/src/bin/usart.rs @@ -7,7 +7,6 @@ mod example_common; use cortex_m_rt::entry; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; -use embedded_hal::blocking::serial::Write; use example_common::*; #[entry] @@ -19,12 +18,12 @@ fn main() -> ! { let config = Config::default(); let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); - unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); + unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - unwrap!(usart.read_blocking(&mut buf)); - unwrap!(usart.bwrite_all(&buf)); + unwrap!(usart.blocking_read(&mut buf)); + unwrap!(usart.blocking_write(&buf)); } } diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs index 0dbdd7c0..862a91ea 100644 --- a/examples/stm32f4/src/bin/usart_dma.rs +++ b/examples/stm32f4/src/bin/usart_dma.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::Write as _; use example_common::*; use heapless::String; diff --git a/examples/stm32f7/src/bin/usart_dma.rs b/examples/stm32f7/src/bin/usart_dma.rs index 82af0bc2..00deae8b 100644 --- a/examples/stm32f7/src/bin/usart_dma.rs +++ b/examples/stm32f7/src/bin/usart_dma.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::Write as _Write; use example_common::*; use heapless::String; diff --git a/examples/stm32h7/src/bin/usart.rs b/examples/stm32h7/src/bin/usart.rs index 95f0a860..211e57cd 100644 --- a/examples/stm32h7/src/bin/usart.rs +++ b/examples/stm32h7/src/bin/usart.rs @@ -4,7 +4,6 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m::prelude::_embedded_hal_blocking_serial_Write; use embassy::executor::Executor; use embassy::util::Forever; use embassy_stm32::dma::NoDma; @@ -20,13 +19,13 @@ async fn main_task() { let config = Config::default(); let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, NoDma, NoDma, config); - unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); + unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - unwrap!(usart.read_blocking(&mut buf)); - unwrap!(usart.bwrite_all(&buf)); + unwrap!(usart.blocking_read(&mut buf)); + unwrap!(usart.blocking_write(&buf)); } } diff --git a/examples/stm32h7/src/bin/usart_dma.rs b/examples/stm32h7/src/bin/usart_dma.rs index d603347a..a9221e1b 100644 --- a/examples/stm32h7/src/bin/usart_dma.rs +++ b/examples/stm32h7/src/bin/usart_dma.rs @@ -9,7 +9,6 @@ use embassy::executor::Executor; use embassy::util::Forever; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; -use embassy_traits::uart::Write as _Write; use example_common::*; use cortex_m_rt::entry; diff --git a/examples/stm32l0/src/bin/usart_dma.rs b/examples/stm32l0/src/bin/usart_dma.rs index 3fe61c13..543e66f6 100644 --- a/examples/stm32l0/src/bin/usart_dma.rs +++ b/examples/stm32l0/src/bin/usart_dma.rs @@ -10,7 +10,6 @@ use example_common::*; use embassy::executor::Spawner; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::{Read, Write}; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { diff --git a/examples/stm32l4/src/bin/usart.rs b/examples/stm32l4/src/bin/usart.rs index b6decbc9..00875c89 100644 --- a/examples/stm32l4/src/bin/usart.rs +++ b/examples/stm32l4/src/bin/usart.rs @@ -7,7 +7,6 @@ mod example_common; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; -use embedded_hal::blocking::serial::Write; use example_common::*; #[cortex_m_rt::entry] @@ -19,12 +18,12 @@ fn main() -> ! { let config = Config::default(); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config); - unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); + unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - unwrap!(usart.read_blocking(&mut buf)); - unwrap!(usart.bwrite_all(&buf)); + unwrap!(usart.blocking_read(&mut buf)); + unwrap!(usart.blocking_write(&buf)); } } diff --git a/examples/stm32l4/src/bin/usart_dma.rs b/examples/stm32l4/src/bin/usart_dma.rs index b49d3d88..b3a1e389 100644 --- a/examples/stm32l4/src/bin/usart_dma.rs +++ b/examples/stm32l4/src/bin/usart_dma.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::Write as _; use example_common::*; use heapless::String; diff --git a/tests/stm32/src/bin/usart.rs b/tests/stm32/src/bin/usart.rs index f887b084..44ee730e 100644 --- a/tests/stm32/src/bin/usart.rs +++ b/tests/stm32/src/bin/usart.rs @@ -9,7 +9,6 @@ use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embedded_hal::blocking::serial::Write; use example_common::*; #[embassy::main(config = "config()")] @@ -42,10 +41,10 @@ async fn main(_spawner: Spawner, p: Peripherals) { // This is because we aren't sending+receiving at the same time. let data = [0xC0, 0xDE]; - usart.bwrite_all(&data).unwrap(); + usart.blocking_write(&data).unwrap(); let mut buf = [0; 2]; - usart.read_blocking(&mut buf).unwrap(); + usart.blocking_read(&mut buf).unwrap(); assert_eq!(buf, data); info!("Test OK"); diff --git a/tests/stm32/src/bin/usart_dma.rs b/tests/stm32/src/bin/usart_dma.rs index 96c6a664..37faaf37 100644 --- a/tests/stm32/src/bin/usart_dma.rs +++ b/tests/stm32/src/bin/usart_dma.rs @@ -8,7 +8,6 @@ use defmt::assert_eq; use embassy::executor::Spawner; use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::Peripherals; -use embassy_traits::uart::{Read, Write}; use example_common::*; #[embassy::main(config = "config()")] diff --git a/tests/stm32/teleprobe.sh b/tests/stm32/teleprobe.sh new file mode 100755 index 00000000..6eec6ca9 --- /dev/null +++ b/tests/stm32/teleprobe.sh @@ -0,0 +1,12 @@ +echo Running target=$1 elf=$2 +STATUSCODE=$( + curl \ + -sS \ + --output /dev/stderr \ + --write-out "%{http_code}" \ + -H "Authorization: Bearer $TELEPROBE_TOKEN" \ + https://teleprobe.embassy.dev/targets/$1/run --data-binary @$2 +) +echo +echo HTTP Status code: $STATUSCODE +test "$STATUSCODE" -eq 200 From 97ab859f0025ee6ffad19733dc24f17b6621fff8 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 14 Jan 2022 23:31:10 +0100 Subject: [PATCH 08/10] stm32/i2c: expose all functionality as inherent methods. --- embassy-stm32/src/i2c/v1.rs | 50 ++++++++----- embassy-stm32/src/i2c/v2.rs | 109 +++++++++++++++++----------- examples/stm32l4/src/bin/i2c.rs | 3 +- examples/stm32l4/src/bin/i2c_dma.rs | 1 - 4 files changed, 99 insertions(+), 64 deletions(-) diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 6fa269fc..6b2c8a35 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs @@ -3,9 +3,6 @@ use crate::time::Hertz; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::unborrow; -use embedded_hal::blocking::i2c::Read; -use embedded_hal::blocking::i2c::Write; -use embedded_hal::blocking::i2c::WriteRead; use crate::pac::i2c; @@ -179,12 +176,8 @@ impl<'d, T: Instance> I2c<'d, T> { let value = T::regs().dr().read().dr(); Ok(value) } -} -impl<'d, T: Instance> Read for I2c<'d, T> { - type Error = Error; - - fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + pub fn blocking_read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { if let Some((last, buffer)) = buffer.split_last_mut() { // Send a START condition and set ACK bit unsafe { @@ -248,12 +241,8 @@ impl<'d, T: Instance> Read for I2c<'d, T> { Err(Error::Overrun) } } -} -impl<'d, T: Instance> Write for I2c<'d, T> { - type Error = Error; - - fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { + pub fn blocking_write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { unsafe { self.write_bytes(addr, bytes)?; // Send a STOP condition @@ -267,16 +256,41 @@ impl<'d, T: Instance> Write for I2c<'d, T> { // Fallthrough is success Ok(()) } + + pub fn blocking_write_read( + &mut self, + addr: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Error> { + unsafe { self.write_bytes(addr, bytes)? }; + self.blocking_read(addr, buffer)?; + + Ok(()) + } } -impl<'d, T: Instance> WriteRead for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { + type Error = Error; + + fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + self.blocking_read(addr, buffer) + } +} + +impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { + type Error = Error; + + fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { + self.blocking_write(addr, bytes) + } +} + +impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { type Error = Error; fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { - unsafe { self.write_bytes(addr, bytes)? }; - self.read(addr, buffer)?; - - Ok(()) + self.blocking_write_read(addr, bytes, buffer) } } diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 73b6f551..af04dc06 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -10,9 +10,6 @@ use embassy::util::Unborrow; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; use embassy_hal_common::unborrow; -use embedded_hal::blocking::i2c::Read; -use embedded_hal::blocking::i2c::Write; -use embedded_hal::blocking::i2c::WriteRead; use futures::future::poll_fn; use crate::dma::NoDma; @@ -300,7 +297,12 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { } } - fn read(&mut self, address: u8, buffer: &mut [u8], restart: bool) -> Result<(), Error> { + fn read_internal( + &mut self, + address: u8, + buffer: &mut [u8], + restart: bool, + ) -> Result<(), Error> { let completed_chunks = buffer.len() / 255; let total_chunks = if completed_chunks * 255 == buffer.len() { completed_chunks @@ -339,7 +341,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { Ok(()) } - fn write(&mut self, address: u8, bytes: &[u8], send_stop: bool) -> Result<(), Error> { + fn write_internal(&mut self, address: u8, bytes: &[u8], send_stop: bool) -> Result<(), Error> { let completed_chunks = bytes.len() / 255; let total_chunks = if completed_chunks * 255 == bytes.len() { completed_chunks @@ -568,14 +570,17 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { Ok(()) } - pub async fn write_dma(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error> + // ========================= + // Async public API + + pub async fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error> where TXDMA: crate::i2c::TxDma, { self.write_dma_internal(address, bytes, true, true).await } - pub async fn write_dma_vectored(&mut self, address: u8, bytes: &[&[u8]]) -> Result<(), Error> + pub async fn write_vectored(&mut self, address: u8, bytes: &[&[u8]]) -> Result<(), Error> where TXDMA: crate::i2c::TxDma, { @@ -597,19 +602,52 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { Ok(()) } - pub async fn read_dma( - &mut self, - address: u8, - buffer: &mut [u8], - restart: bool, - ) -> Result<(), Error> + pub async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error> where RXDMA: crate::i2c::RxDma, { - self.read_dma_internal(address, buffer, restart).await + self.read_dma_internal(address, buffer, false).await } - pub fn write_vectored(&mut self, address: u8, bytes: &[&[u8]]) -> Result<(), Error> { + pub async fn write_read( + &mut self, + address: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Error> + where + TXDMA: super::TxDma, + RXDMA: super::RxDma, + { + self.write_dma_internal(address, bytes, true, true).await?; + self.read_dma_internal(address, buffer, true).await?; + Ok(()) + } + + // ========================= + // Blocking public API + + pub fn blocking_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error> { + self.read_internal(address, buffer, false) + // Automatic Stop + } + + pub fn blocking_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error> { + self.write_internal(address, bytes, true) + } + + pub fn blocking_write_read( + &mut self, + address: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Error> { + self.write_internal(address, bytes, false)?; + self.read_internal(address, buffer, true) + // Automatic Stop + } + + pub fn blocking_write_vectored(&mut self, address: u8, bytes: &[&[u8]]) -> Result<(), Error> { if bytes.is_empty() { return Err(Error::ZeroLengthTransfer); } @@ -679,24 +717,23 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { } } -impl<'d, T: Instance> Read for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { type Error = Error; fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - self.read(address, buffer, false) - // Automatic Stop + self.blocking_read(address, buffer) } } -impl<'d, T: Instance> Write for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { type Error = Error; fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - self.write(address, bytes, true) + self.blocking_write(address, bytes) } } -impl<'d, T: Instance> WriteRead for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { type Error = Error; fn write_read( @@ -705,9 +742,7 @@ impl<'d, T: Instance> WriteRead for I2c<'d, T> { bytes: &[u8], buffer: &mut [u8], ) -> Result<(), Self::Error> { - self.write(address, bytes, false)?; - self.read(address, buffer, true) - // Automatic Stop + self.blocking_write_read(address, bytes, buffer) } } @@ -715,7 +750,7 @@ impl<'d, T: Instance> WriteRead for I2c<'d, T> { /// /// Peripheral options for generating the STOP condition #[derive(Copy, Clone, PartialEq)] -pub enum Stop { +enum Stop { /// Software end mode: Must write register to generate STOP condition Software, /// Automatic end mode: A STOP condition is automatically generated once the @@ -860,32 +895,23 @@ impl<'d, T: Instance, TXDMA: super::TxDma, RXDMA: super::RxDma> I2cTrait where - 'd: 'a, - T: 'a, - TXDMA: 'a, - RXDMA: 'a, + Self: 'a, = impl Future> + 'a; type ReadFuture<'a> where - 'd: 'a, - T: 'a, - TXDMA: 'a, - RXDMA: 'a, + Self: 'a, = impl Future> + 'a; type WriteReadFuture<'a> where - 'd: 'a, - T: 'a, - TXDMA: 'a, - RXDMA: 'a, + Self: 'a, = impl Future> + 'a; fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma(address, buffer, false) + self.read(address, buffer) } fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma(address, bytes) + self.write(address, bytes) } fn write_read<'a>( @@ -894,9 +920,6 @@ impl<'d, T: Instance, TXDMA: super::TxDma, RXDMA: super::RxDma> I2cTrait Self::WriteReadFuture<'a> { - async move { - self.write_dma(address, bytes).await?; - self.read_dma(address, buffer, true).await - } + self.write_read(address, bytes, buffer) } } diff --git a/examples/stm32l4/src/bin/i2c.rs b/examples/stm32l4/src/bin/i2c.rs index 86215697..615012a0 100644 --- a/examples/stm32l4/src/bin/i2c.rs +++ b/examples/stm32l4/src/bin/i2c.rs @@ -11,7 +11,6 @@ use embassy_stm32::i2c::I2c; use embassy_stm32::interrupt; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embedded_hal::blocking::i2c::WriteRead; use example_common::{info, unwrap}; const ADDRESS: u8 = 0x5F; @@ -23,6 +22,6 @@ async fn main(_spawner: Spawner, p: Peripherals) -> ! { let mut i2c = I2c::new(p.I2C2, p.PB10, p.PB11, irq, NoDma, NoDma, Hertz(100_000)); let mut data = [0u8; 1]; - unwrap!(i2c.write_read(ADDRESS, &[WHOAMI], &mut data)); + unwrap!(i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data)); info!("Whoami: {}", data[0]); } diff --git a/examples/stm32l4/src/bin/i2c_dma.rs b/examples/stm32l4/src/bin/i2c_dma.rs index b0596aab..d77bee8c 100644 --- a/examples/stm32l4/src/bin/i2c_dma.rs +++ b/examples/stm32l4/src/bin/i2c_dma.rs @@ -6,7 +6,6 @@ mod example_common; use embassy::executor::Spawner; -use embassy::traits::i2c::I2c as I2cTrait; use embassy_stm32::i2c::I2c; use embassy_stm32::interrupt; use embassy_stm32::time::Hertz; From 3d27a0e7cbf226d2a9df3e2efa058589ebfca858 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 19 Jan 2022 15:59:25 +0100 Subject: [PATCH 09/10] stm32/dma: make lowlevel api take ptrs instead of slices. --- embassy-stm32/src/dma/bdma.rs | 14 +++++----- embassy-stm32/src/dma/dma.rs | 14 +++++----- embassy-stm32/src/dma/mod.rs | 49 +++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs index b4c77757..e06ce8c3 100644 --- a/embassy-stm32/src/dma/bdma.rs +++ b/embassy-stm32/src/dma/bdma.rs @@ -89,7 +89,8 @@ pac::dma_channels! { ($channel_peri:ident, $dma_peri:ident, bdma, $channel_num:expr, $dmamux:tt) => { impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri { - unsafe fn start_write(&mut self, request: Request, buf: &[W], reg_addr: *mut W) { + unsafe fn start_write(&mut self, request: Request, buf: *const[W], reg_addr: *mut W) { + let (ptr, len) = super::slice_ptr_parts(buf); low_level_api::start_transfer( pac::$dma_peri, $channel_num, @@ -97,8 +98,8 @@ pac::dma_channels! { request, vals::Dir::FROMMEMORY, reg_addr as *const u32, - buf.as_ptr() as *mut u32, - buf.len(), + ptr as *mut u32, + len, true, vals::Size::from(W::bits()), #[cfg(dmamux)] @@ -129,7 +130,8 @@ pac::dma_channels! { ) } - unsafe fn start_read(&mut self, request: Request, reg_addr: *mut W, buf: &mut [W]) { + unsafe fn start_read(&mut self, request: Request, reg_addr: *const W, buf: *mut [W]) { + let (ptr, len) = super::slice_ptr_parts_mut(buf); low_level_api::start_transfer( pac::$dma_peri, $channel_num, @@ -137,8 +139,8 @@ pac::dma_channels! { request, vals::Dir::FROMPERIPHERAL, reg_addr as *const u32, - buf.as_ptr() as *mut u32, - buf.len(), + ptr as *mut u32, + len, true, vals::Size::from(W::bits()), #[cfg(dmamux)] diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs index efe4d1d9..8e48bb26 100644 --- a/embassy-stm32/src/dma/dma.rs +++ b/embassy-stm32/src/dma/dma.rs @@ -84,15 +84,16 @@ pub(crate) unsafe fn init() { pac::dma_channels! { ($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $dmamux:tt) => { impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri { - unsafe fn start_write(&mut self, request: Request, buf: &[W], reg_addr: *mut W) { + unsafe fn start_write(&mut self, request: Request, buf: *const [W], reg_addr: *mut W) { + let (ptr, len) = super::slice_ptr_parts(buf); low_level_api::start_transfer( pac::$dma_peri, $channel_num, request, vals::Dir::MEMORYTOPERIPHERAL, reg_addr as *const u32, - buf.as_ptr() as *mut u32, - buf.len(), + ptr as *mut u32, + len, true, vals::Size::from(W::bits()), #[cfg(dmamux)] @@ -121,15 +122,16 @@ pac::dma_channels! { ) } - unsafe fn start_read(&mut self, request: Request, reg_addr: *mut W, buf: &mut [W]) { + unsafe fn start_read(&mut self, request: Request, reg_addr: *const W, buf: *mut [W]) { + let (ptr, len) = super::slice_ptr_parts_mut(buf); low_level_api::start_transfer( pac::$dma_peri, $channel_num, request, vals::Dir::PERIPHERALTOMEMORY, reg_addr as *const u32, - buf.as_ptr() as *mut u32, - buf.len(), + ptr as *mut u32, + len, true, vals::Size::from(W::bits()), #[cfg(dmamux)] diff --git a/embassy-stm32/src/dma/mod.rs b/embassy-stm32/src/dma/mod.rs index accc5565..b7067a9c 100644 --- a/embassy-stm32/src/dma/mod.rs +++ b/embassy-stm32/src/dma/mod.rs @@ -10,6 +10,7 @@ pub use dmamux::*; use core::future::Future; use core::marker::PhantomData; +use core::mem; use core::pin::Pin; use core::task::Waker; use core::task::{Context, Poll}; @@ -36,12 +37,13 @@ pub(crate) mod sealed { /// Starts this channel for writing a stream of words. /// /// Safety: + /// - `buf` must point to a valid buffer for DMA reading. /// - `buf` must be alive for the entire duration of the DMA transfer. /// - `reg_addr` must be a valid peripheral register address to write to. unsafe fn start_write( &mut self, request: Request, - buf: &[W], + buf: *const [W], reg_addr: *mut W, ); @@ -60,13 +62,14 @@ pub(crate) mod sealed { /// Starts this channel for reading a stream of words. /// /// Safety: + /// - `buf` must point to a valid buffer for DMA writing. /// - `buf` must be alive for the entire duration of the DMA transfer. - /// - `reg_addr` must be a valid peripheral register address to write to. + /// - `reg_addr` must be a valid peripheral register address to read from. unsafe fn start_read( &mut self, request: Request, - reg_addr: *mut W, - buf: &mut [W], + reg_addr: *const W, + buf: *mut [W], ); /// Requests the channel to stop. @@ -132,10 +135,7 @@ mod transfers { unsafe { channel.start_read::(request, reg_addr, buf) }; - Transfer { - channel, - _phantom: PhantomData, - } + Transfer::new(channel) } #[allow(unused)] @@ -150,10 +150,7 @@ mod transfers { unsafe { channel.start_write::(request, buf, reg_addr) }; - Transfer { - channel, - _phantom: PhantomData, - } + Transfer::new(channel) } #[allow(unused)] @@ -168,17 +165,24 @@ mod transfers { unsafe { channel.start_write_repeated::(request, repeated, count, reg_addr) }; - Transfer { - channel, - _phantom: PhantomData, - } + Transfer::new(channel) } - struct Transfer<'a, C: Channel> { + pub(crate) struct Transfer<'a, C: Channel> { channel: C, _phantom: PhantomData<&'a mut C>, } + impl<'a, C: Channel> Transfer<'a, C> { + pub(crate) fn new(channel: impl Unborrow + 'a) -> Self { + unborrow!(channel); + Self { + channel, + _phantom: PhantomData, + } + } + } + impl<'a, C: Channel> Drop for Transfer<'a, C> { fn drop(&mut self) { self.channel.request_stop(); @@ -221,3 +225,14 @@ pub(crate) unsafe fn init() { #[cfg(dmamux)] dmamux::init(); } + +// TODO: replace transmutes with core::ptr::metadata once it's stable +#[allow(unused)] +pub(crate) fn slice_ptr_parts(slice: *const [T]) -> (usize, usize) { + unsafe { mem::transmute(slice) } +} + +#[allow(unused)] +pub(crate) fn slice_ptr_parts_mut(slice: *mut [T]) -> (usize, usize) { + unsafe { mem::transmute(slice) } +} From 889d757ab8bcfc10caf0a7d75ffb7733a7e71ed1 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 19 Jan 2022 17:29:47 +0100 Subject: [PATCH 10/10] stm32/spi: expose all functionality as inherent methods. --- embassy-stm32/src/spi/mod.rs | 204 ++++++++++++++++------------ embassy-stm32/src/spi/v1.rs | 42 +++--- embassy-stm32/src/spi/v2.rs | 41 +++--- embassy-stm32/src/spi/v3.rs | 41 +++--- embassy-stm32/src/subghz/mod.rs | 28 ++-- examples/stm32f4/src/bin/spi.rs | 3 +- examples/stm32f4/src/bin/spi_dma.rs | 3 +- examples/stm32h7/src/bin/spi.rs | 3 +- examples/stm32l0/src/bin/spi.rs | 3 +- examples/stm32l1/src/bin/spi.rs | 3 +- examples/stm32l4/src/bin/spi.rs | 3 +- tests/stm32/src/bin/spi.rs | 3 +- 12 files changed, 203 insertions(+), 174 deletions(-) diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index f83ef785..f1ea8592 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -1,5 +1,13 @@ #![macro_use] +use core::future::Future; +use core::marker::PhantomData; +use core::ptr; +use embassy::util::Unborrow; +use embassy_hal_common::unborrow; +use embassy_traits::spi as traits; + +use self::sealed::WordSize; use crate::dma; use crate::dma::NoDma; use crate::gpio::sealed::{AFType, Pin}; @@ -8,19 +16,14 @@ use crate::pac::spi::{regs, vals}; use crate::peripherals; use crate::rcc::RccPeripheral; use crate::time::Hertz; -use core::future::Future; -use core::marker::PhantomData; -use core::ptr; -use embassy::util::Unborrow; -use embassy_hal_common::unborrow; -use embassy_traits::spi as traits; + +pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; #[cfg_attr(spi_v1, path = "v1.rs")] #[cfg_attr(spi_f1, path = "v1.rs")] #[cfg_attr(spi_v2, path = "v2.rs")] #[cfg_attr(spi_v3, path = "v3.rs")] mod _version; -pub use _version::*; type Regs = &'static crate::pac::spi::Spi; @@ -40,54 +43,6 @@ pub enum BitOrder { MsbFirst, } -#[derive(Copy, Clone, PartialOrd, PartialEq)] -enum WordSize { - EightBit, - SixteenBit, -} - -impl WordSize { - #[cfg(any(spi_v1, spi_f1))] - fn dff(&self) -> vals::Dff { - match self { - WordSize::EightBit => vals::Dff::EIGHTBIT, - WordSize::SixteenBit => vals::Dff::SIXTEENBIT, - } - } - - #[cfg(spi_v2)] - fn ds(&self) -> vals::Ds { - match self { - WordSize::EightBit => vals::Ds::EIGHTBIT, - WordSize::SixteenBit => vals::Ds::SIXTEENBIT, - } - } - - #[cfg(spi_v2)] - fn frxth(&self) -> vals::Frxth { - match self { - WordSize::EightBit => vals::Frxth::QUARTER, - WordSize::SixteenBit => vals::Frxth::HALF, - } - } - - #[cfg(spi_v3)] - fn dsize(&self) -> u8 { - match self { - WordSize::EightBit => 0b0111, - WordSize::SixteenBit => 0b1111, - } - } - - #[cfg(spi_v3)] - fn _frxth(&self) -> vals::Fthlv { - match self { - WordSize::EightBit => vals::Fthlv::ONEFRAME, - WordSize::SixteenBit => vals::Fthlv::ONEFRAME, - } - } -} - #[non_exhaustive] #[derive(Copy, Clone)] pub struct Config { @@ -379,6 +334,47 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { self.current_word_size = word_size; } + + pub async fn write(&mut self, data: &[u8]) -> Result<(), Error> + where + Tx: TxDmaChannel, + { + self.write_dma_u8(data).await + } + + pub async fn read(&mut self, data: &mut [u8]) -> Result<(), Error> + where + Tx: TxDmaChannel, + Rx: RxDmaChannel, + { + self.read_dma_u8(data).await + } + + pub async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> + where + Tx: TxDmaChannel, + Rx: RxDmaChannel, + { + self.transfer_dma_u8(read, write).await + } + + pub fn blocking_write(&mut self, words: &[W]) -> Result<(), Error> { + self.set_word_size(W::WORDSIZE); + let regs = T::regs(); + for word in words.iter() { + let _ = transfer_word(regs, *word)?; + } + Ok(()) + } + + pub fn blocking_transfer_in_place(&mut self, words: &mut [W]) -> Result<(), Error> { + self.set_word_size(W::WORDSIZE); + let regs = T::regs(); + for word in words.iter_mut() { + *word = transfer_word(regs, *word)?; + } + Ok(()) + } } impl<'d, T: Instance, Tx, Rx> Drop for Spi<'d, T, Tx, Rx> { @@ -537,17 +533,6 @@ fn finish_dma(regs: Regs) { } } -trait Word { - const WORDSIZE: WordSize; -} - -impl Word for u8 { - const WORDSIZE: WordSize = WordSize::EightBit; -} -impl Word for u16 { - const WORDSIZE: WordSize = WordSize::SixteenBit; -} - fn transfer_word(regs: Regs, tx_word: W) -> Result { spin_until_tx_ready(regs)?; @@ -572,14 +557,7 @@ macro_rules! impl_blocking { type Error = Error; fn write(&mut self, words: &[$w]) -> Result<(), Self::Error> { - self.set_word_size($w::WORDSIZE); - let regs = T::regs(); - - for word in words.iter() { - let _ = transfer_word(regs, *word)?; - } - - Ok(()) + self.blocking_write(words) } } @@ -589,13 +567,7 @@ macro_rules! impl_blocking { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [$w]) -> Result<&'w [$w], Self::Error> { - self.set_word_size($w::WORDSIZE); - let regs = T::regs(); - - for word in words.iter_mut() { - *word = transfer_word(regs, *word)?; - } - + self.blocking_transfer_in_place(words)?; Ok(words) } } @@ -616,7 +588,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, = impl Future> + 'a; fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma_u8(data) + self.write(data) } } @@ -629,7 +601,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read = impl Future> + 'a; fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma_u8(data) + self.read(data) } } @@ -646,7 +618,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDupl read: &'a mut [u8], write: &'a [u8], ) -> Self::WriteReadFuture<'a> { - self.read_write_dma_u8(read, write) + self.transfer(read, write) } } @@ -676,8 +648,72 @@ pub(crate) mod sealed { pub trait RxDmaChannel { fn request(&self) -> dma::Request; } + + pub trait Word: Copy + 'static { + const WORDSIZE: WordSize; + } + + impl Word for u8 { + const WORDSIZE: WordSize = WordSize::EightBit; + } + impl Word for u16 { + const WORDSIZE: WordSize = WordSize::SixteenBit; + } + + #[derive(Copy, Clone, PartialOrd, PartialEq)] + pub enum WordSize { + EightBit, + SixteenBit, + } + + impl WordSize { + #[cfg(any(spi_v1, spi_f1))] + pub fn dff(&self) -> vals::Dff { + match self { + WordSize::EightBit => vals::Dff::EIGHTBIT, + WordSize::SixteenBit => vals::Dff::SIXTEENBIT, + } + } + + #[cfg(spi_v2)] + pub fn ds(&self) -> vals::Ds { + match self { + WordSize::EightBit => vals::Ds::EIGHTBIT, + WordSize::SixteenBit => vals::Ds::SIXTEENBIT, + } + } + + #[cfg(spi_v2)] + pub fn frxth(&self) -> vals::Frxth { + match self { + WordSize::EightBit => vals::Frxth::QUARTER, + WordSize::SixteenBit => vals::Frxth::HALF, + } + } + + #[cfg(spi_v3)] + pub fn dsize(&self) -> u8 { + match self { + WordSize::EightBit => 0b0111, + WordSize::SixteenBit => 0b1111, + } + } + + #[cfg(spi_v3)] + pub fn _frxth(&self) -> vals::Fthlv { + match self { + WordSize::EightBit => vals::Fthlv::ONEFRAME, + WordSize::SixteenBit => vals::Fthlv::ONEFRAME, + } + } + } } +pub trait Word: Copy + 'static + sealed::Word {} + +impl Word for u8 {} +impl Word for u16 {} + pub trait Instance: sealed::Instance + RccPeripheral {} pub trait SckPin: sealed::SckPin {} pub trait MosiPin: sealed::MosiPin {} diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index ef7e0f59..68e5a734 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -1,13 +1,12 @@ #![macro_use] -pub use embedded_hal::blocking; -pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join; use super::*; +use crate::dma::{slice_ptr_parts, slice_ptr_parts_mut, Transfer}; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: *const [u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -18,9 +17,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } self.set_word_size(WordSize::EightBit); - let request = self.txdma.request(); - let dst = T::regs().tx_ptr(); - let f = crate::dma::write(&mut self.txdma, request, write, dst); + let tx_request = self.txdma.request(); + let tx_dst = T::regs().tx_ptr(); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cr2().modify(|reg| { @@ -31,14 +31,14 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { }); } - f.await; + tx_f.await; finish_dma(T::regs()); Ok(()) } - pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: *mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -53,11 +53,12 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } self.set_word_size(WordSize::EightBit); - let clock_byte_count = read.len(); + let (_, clock_byte_count) = slice_ptr_parts_mut(read); let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read(&mut self.rxdma, rx_request, rx_src, read); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); @@ -86,16 +87,18 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - pub(super) async fn read_write_dma_u8( + pub(super) async fn transfer_dma_u8( &mut self, - read: &mut [u8], - write: &[u8], + read: *mut [u8], + write: *const [u8], ) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, { - assert!(read.len() >= write.len()); + let (_, rx_len) = slice_ptr_parts(read); + let (_, tx_len) = slice_ptr_parts(write); + assert_eq!(rx_len, tx_len); unsafe { T::regs().cr1().modify(|w| { @@ -109,16 +112,13 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read( - &mut self.rxdma, - rx_request, - rx_src, - &mut read[0..write.len()], - ); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); - let tx_f = crate::dma::write(&mut self.txdma, tx_request, write, tx_dst); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cr2().modify(|reg| { diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs index f2ba3ac6..78bb1192 100644 --- a/embassy-stm32/src/spi/v2.rs +++ b/embassy-stm32/src/spi/v2.rs @@ -1,12 +1,12 @@ #![macro_use] -pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join; use super::*; +use crate::dma::{slice_ptr_parts, slice_ptr_parts_mut, Transfer}; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: *const [u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -22,9 +22,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } self.set_word_size(WordSize::EightBit); - let request = self.txdma.request(); - let dst = T::regs().tx_ptr(); - let f = crate::dma::write(&mut self.txdma, request, write, dst); + let tx_request = self.txdma.request(); + let tx_dst = T::regs().tx_ptr(); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cr2().modify(|reg| { @@ -35,14 +36,14 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { }); } - f.await; + tx_f.await; finish_dma(T::regs()); Ok(()) } - pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: *mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -57,11 +58,12 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } self.set_word_size(WordSize::EightBit); - let clock_byte_count = read.len(); + let (_, clock_byte_count) = slice_ptr_parts_mut(read); let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read(&mut self.rxdma, rx_request, rx_src, read); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); @@ -90,16 +92,18 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - pub(super) async fn read_write_dma_u8( + pub(super) async fn transfer_dma_u8( &mut self, - read: &mut [u8], - write: &[u8], + read: *mut [u8], + write: *const [u8], ) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, { - assert!(read.len() >= write.len()); + let (_, rx_len) = slice_ptr_parts(read); + let (_, tx_len) = slice_ptr_parts(write); + assert_eq!(rx_len, tx_len); unsafe { T::regs().cr1().modify(|w| { @@ -118,16 +122,13 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read( - &mut self.rxdma, - rx_request, - rx_src, - &mut read[0..write.len()], - ); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); - let tx_f = crate::dma::write(&mut self.txdma, tx_request, write, tx_dst); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cr2().modify(|reg| { diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index b42eeed8..50650da1 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -1,12 +1,12 @@ #![macro_use] -pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join; use super::*; +use crate::dma::{slice_ptr_parts, slice_ptr_parts_mut, Transfer}; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: *const [u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -22,9 +22,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } } - let request = self.txdma.request(); - let dst = T::regs().tx_ptr(); - let f = crate::dma::write(&mut self.txdma, request, write, dst); + let tx_request = self.txdma.request(); + let tx_dst = T::regs().tx_ptr(); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cfg1().modify(|reg| { @@ -38,14 +39,14 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { }); } - f.await; + tx_f.await; finish_dma(T::regs()); Ok(()) } - pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: *mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -60,11 +61,12 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { }); } - let clock_byte_count = read.len(); + let (_, clock_byte_count) = slice_ptr_parts_mut(read); let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read(&mut self.rxdma, rx_request, rx_src, read); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); @@ -96,16 +98,18 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - pub(super) async fn read_write_dma_u8( + pub(super) async fn transfer_dma_u8( &mut self, - read: &mut [u8], - write: &[u8], + read: *mut [u8], + write: *const [u8], ) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, { - assert!(read.len() >= write.len()); + let (_, rx_len) = slice_ptr_parts(read); + let (_, tx_len) = slice_ptr_parts(write); + assert_eq!(rx_len, tx_len); self.set_word_size(WordSize::EightBit); unsafe { @@ -124,16 +128,13 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { let rx_request = self.rxdma.request(); let rx_src = T::regs().rx_ptr(); - let rx_f = crate::dma::read( - &mut self.rxdma, - rx_request, - rx_src, - &mut read[0..write.len()], - ); + unsafe { self.rxdma.start_read(rx_request, rx_src, read) }; + let rx_f = Transfer::new(&mut self.rxdma); let tx_request = self.txdma.request(); let tx_dst = T::regs().tx_ptr(); - let tx_f = crate::dma::write(&mut self.txdma, tx_request, write, tx_dst); + unsafe { self.txdma.start_write(tx_request, write, tx_dst) } + let tx_f = Transfer::new(&mut self.txdma); unsafe { T::regs().cfg1().modify(|reg| { diff --git a/embassy-stm32/src/subghz/mod.rs b/embassy-stm32/src/subghz/mod.rs index 7d74569f..87f376c4 100644 --- a/embassy-stm32/src/subghz/mod.rs +++ b/embassy-stm32/src/subghz/mod.rs @@ -82,14 +82,10 @@ use crate::{ pac, peripherals::SUBGHZSPI, rcc::sealed::RccPeripheral, - spi::{BitOrder, Config as SpiConfig, MisoPin, MosiPin, SckPin, Spi}, + spi::{BitOrder, Config as SpiConfig, MisoPin, MosiPin, SckPin, Spi, MODE_0}, time::Hertz, }; use embassy::util::Unborrow; -use embedded_hal::{ - blocking::spi::{Transfer, Write}, - spi::MODE_0, -}; /// Passthrough for SPI errors (for now) pub type Error = crate::spi::Error; @@ -255,8 +251,8 @@ impl<'d> SubGhz<'d, NoDma, NoDma> { self.poll_not_busy(); { let _nss: Nss = Nss::new(); - self.spi.write(&[opcode as u8])?; - self.spi.transfer(data)?; + self.spi.blocking_write(&[opcode as u8])?; + self.spi.blocking_transfer_in_place(data)?; } self.poll_not_busy(); Ok(()) @@ -280,7 +276,7 @@ impl<'d> SubGhz<'d, NoDma, NoDma> { self.poll_not_busy(); { let _nss: Nss = Nss::new(); - self.spi.write(data)?; + self.spi.blocking_write(data)?; } self.poll_not_busy(); Ok(()) @@ -290,8 +286,9 @@ impl<'d> SubGhz<'d, NoDma, NoDma> { self.poll_not_busy(); { let _nss: Nss = Nss::new(); - self.spi.write(&[OpCode::WriteBuffer as u8, offset])?; - self.spi.write(data)?; + self.spi + .blocking_write(&[OpCode::WriteBuffer as u8, offset])?; + self.spi.blocking_write(data)?; } self.poll_not_busy(); @@ -308,9 +305,10 @@ impl<'d> SubGhz<'d, NoDma, NoDma> { self.poll_not_busy(); { let _nss: Nss = Nss::new(); - self.spi.write(&[OpCode::ReadBuffer as u8, offset])?; - self.spi.transfer(&mut status_buf)?; - self.spi.transfer(buf)?; + self.spi + .blocking_write(&[OpCode::ReadBuffer as u8, offset])?; + self.spi.blocking_transfer_in_place(&mut status_buf)?; + self.spi.blocking_transfer_in_place(buf)?; } self.poll_not_busy(); @@ -342,8 +340,8 @@ impl<'d> SubGhz<'d, NoDma, NoDma> { { let _nss: Nss = Nss::new(); self.spi - .write(&[OpCode::WriteRegister as u8, addr[0], addr[1]])?; - self.spi.write(data)?; + .blocking_write(&[OpCode::WriteRegister as u8, addr[0], addr[1]])?; + self.spi.blocking_write(data)?; } self.poll_not_busy(); diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs index b66eb958..6b04f1fe 100644 --- a/examples/stm32f4/src/bin/spi.rs +++ b/examples/stm32f4/src/bin/spi.rs @@ -10,7 +10,6 @@ use embassy_stm32::dma::NoDma; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; -use embedded_hal::blocking::spi::Transfer; use example_common::*; #[entry] @@ -35,7 +34,7 @@ fn main() -> ! { loop { let mut buf = [0x0Au8; 4]; cs.set_low(); - unwrap!(spi.transfer(&mut buf)); + unwrap!(spi.blocking_transfer_in_place(&mut buf)); cs.set_high(); info!("xfer {=[u8]:x}", buf); } diff --git a/examples/stm32f4/src/bin/spi_dma.rs b/examples/stm32f4/src/bin/spi_dma.rs index b3bf6fc2..9171f751 100644 --- a/examples/stm32f4/src/bin/spi_dma.rs +++ b/examples/stm32f4/src/bin/spi_dma.rs @@ -10,7 +10,6 @@ use embassy::executor::Spawner; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embassy_traits::spi::FullDuplex; use example_common::*; use heapless::String; @@ -33,7 +32,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut write: String<128> = String::new(); let mut read = [0; 128]; core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); - spi.read_write(&mut read[0..write.len()], write.as_bytes()) + spi.transfer(&mut read[0..write.len()], write.as_bytes()) .await .ok(); info!("read via spi+dma: {}", from_utf8(&read).unwrap()); diff --git a/examples/stm32h7/src/bin/spi.rs b/examples/stm32h7/src/bin/spi.rs index 0b375b0d..17e64da7 100644 --- a/examples/stm32h7/src/bin/spi.rs +++ b/examples/stm32h7/src/bin/spi.rs @@ -10,7 +10,6 @@ use embassy::executor::Executor; use embassy::util::Forever; use embassy_stm32::dma::NoDma; use embassy_stm32::spi; -use embedded_hal::blocking::spi::Transfer; use example_common::*; use core::str::from_utf8; @@ -25,7 +24,7 @@ async fn main_task(mut spi: spi::Spi<'static, SPI3, NoDma, NoDma>) { let mut write: String<128> = String::new(); core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); unsafe { - let result = spi.transfer(write.as_bytes_mut()); + let result = spi.blocking_transfer_in_place(write.as_bytes_mut()); if let Err(_) = result { defmt::panic!("crap"); } diff --git a/examples/stm32l0/src/bin/spi.rs b/examples/stm32l0/src/bin/spi.rs index d30bb8d7..8d6e89d9 100644 --- a/examples/stm32l0/src/bin/spi.rs +++ b/examples/stm32l0/src/bin/spi.rs @@ -13,7 +13,6 @@ use embassy_stm32::dma::NoDma; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embedded_hal::blocking::spi::Transfer; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { @@ -35,7 +34,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { let mut buf = [0x0Au8; 4]; cs.set_low(); - unwrap!(spi.transfer(&mut buf)); + unwrap!(spi.blocking_transfer_in_place(&mut buf)); cs.set_high(); info!("xfer {=[u8]:x}", buf); } diff --git a/examples/stm32l1/src/bin/spi.rs b/examples/stm32l1/src/bin/spi.rs index 9d1a2fc8..e97e3ebb 100644 --- a/examples/stm32l1/src/bin/spi.rs +++ b/examples/stm32l1/src/bin/spi.rs @@ -13,7 +13,6 @@ use embassy_stm32::dma::NoDma; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embedded_hal::blocking::spi::Transfer; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { @@ -35,7 +34,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { let mut buf = [0x0Au8; 4]; cs.set_low(); - unwrap!(spi.transfer(&mut buf)); + unwrap!(spi.blocking_transfer_in_place(&mut buf)); cs.set_high(); info!("xfer {=[u8]:x}", buf); } diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs index 1b6e3946..8567d306 100644 --- a/examples/stm32l4/src/bin/spi.rs +++ b/examples/stm32l4/src/bin/spi.rs @@ -9,7 +9,6 @@ use embassy_stm32::dma::NoDma; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; -use embedded_hal::blocking::spi::Transfer; use example_common::*; #[cortex_m_rt::entry] @@ -34,7 +33,7 @@ fn main() -> ! { loop { let mut buf = [0x0Au8; 4]; cs.set_low(); - unwrap!(spi.transfer(&mut buf)); + unwrap!(spi.blocking_transfer_in_place(&mut buf)); cs.set_high(); info!("xfer {=[u8]:x}", buf); } diff --git a/tests/stm32/src/bin/spi.rs b/tests/stm32/src/bin/spi.rs index 043505c7..47d0017a 100644 --- a/tests/stm32/src/bin/spi.rs +++ b/tests/stm32/src/bin/spi.rs @@ -10,7 +10,6 @@ use embassy_stm32::dma::NoDma; use embassy_stm32::spi::{self, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embedded_hal::blocking::spi::Transfer; use example_common::*; #[embassy::main(config = "config()")] @@ -38,7 +37,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { // Arduino pins D11 and D12 (MOSI-MISO) are connected together with a 1K resistor. // so we should get the data we sent back. let mut buf = data; - spi.transfer(&mut buf).unwrap(); + spi.blocking_transfer_in_place(&mut buf).unwrap(); assert_eq!(buf, data); info!("Test OK");