diff --git a/embassy-stm32/src/exti/mod.rs b/embassy-stm32/src/exti/mod.rs index 164cdba33..3f43e0bc7 100644 --- a/embassy-stm32/src/exti/mod.rs +++ b/embassy-stm32/src/exti/mod.rs @@ -37,6 +37,7 @@ macro_rules! foreach_exti_irq { #[cfg_attr(exti_v1, path = "v1.rs")] #[cfg_attr(exti_h7, path = "v1.rs")] #[cfg_attr(exti_wb55, path = "v2.rs")] +#[cfg_attr(exti_wl5x, path = "v2.rs")] mod _version; #[allow(unused)] @@ -110,6 +111,6 @@ pub(crate) unsafe fn init() { foreach_exti_irq!(enable_irq); - #[cfg(not(rcc_wb55))] + #[cfg(not(any(rcc_wb55, rcc_wl5x)))] ::enable(); } diff --git a/embassy-stm32/src/exti/v2.rs b/embassy-stm32/src/exti/v2.rs index 8b1378917..2e62331fe 100644 --- a/embassy-stm32/src/exti/v2.rs +++ b/embassy-stm32/src/exti/v2.rs @@ -1 +1,184 @@ +use core::convert::Infallible; +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::util::{AtomicWaker, Unborrow}; +use embedded_hal::digital::v2::InputPin; +use pac::exti::{regs, vals}; +use crate::gpio::{AnyPin, Input, Pin as GpioPin}; +use crate::pac; +use crate::pac::CORE_INDEX; +use crate::pac::{EXTI, SYSCFG}; + +const EXTI_COUNT: usize = 16; +const NEW_AW: AtomicWaker = AtomicWaker::new(); +static EXTI_WAKERS: [AtomicWaker; EXTI_COUNT] = [NEW_AW; EXTI_COUNT]; + +pub unsafe fn on_irq() { + let bits = EXTI.pr(0).read().0; + + // Mask all the channels that fired. + EXTI.cpu(CORE_INDEX) + .imr(CORE_INDEX) + .modify(|w| w.0 &= !bits); + + // Wake the tasks + for pin in BitIter(bits) { + EXTI_WAKERS[pin as usize].wake(); + } + + // Clear pending + EXTI.pr(0).write_value(regs::Pr(bits)); +} + +struct BitIter(u32); + +impl Iterator for BitIter { + type Item = u32; + + fn next(&mut self) -> Option { + match self.0.trailing_zeros() { + 32 => None, + b => { + self.0 &= !(1 << b); + Some(b) + } + } + } +} + +/// EXTI input driver +pub struct ExtiInput<'d, T: GpioPin> { + pin: Input<'d, T>, +} + +impl<'d, T: GpioPin> Unpin for ExtiInput<'d, T> {} + +impl<'d, T: GpioPin> ExtiInput<'d, T> { + pub fn new(pin: Input<'d, T>, _ch: impl Unborrow + 'd) -> Self { + Self { pin } + } +} + +impl<'d, T: GpioPin> InputPin for ExtiInput<'d, T> { + type Error = Infallible; + + fn is_high(&self) -> Result { + self.pin.is_high() + } + + fn is_low(&self) -> Result { + self.pin.is_low() + } +} + +impl<'d, T: GpioPin> WaitForRisingEdge for ExtiInput<'d, T> { + type Future<'a> = ExtiInputFuture<'a>; + + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a> { + ExtiInputFuture::new( + self.pin.pin.pin(), + self.pin.pin.port(), + vals::Rt::ENABLED, + vals::Ft::DISABLED, + ) + } +} + +impl<'d, T: GpioPin> WaitForFallingEdge for ExtiInput<'d, T> { + type Future<'a> = ExtiInputFuture<'a>; + + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a> { + ExtiInputFuture::new( + self.pin.pin.pin(), + self.pin.pin.port(), + vals::Rt::DISABLED, + vals::Ft::ENABLED, + ) + } +} + +impl<'d, T: GpioPin> WaitForAnyEdge for ExtiInput<'d, T> { + type Future<'a> = ExtiInputFuture<'a>; + + fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { + ExtiInputFuture::new( + self.pin.pin.pin(), + self.pin.pin.port(), + vals::Rt::ENABLED, + vals::Ft::ENABLED, + ) + } +} + +pub struct ExtiInputFuture<'a> { + pin: u8, + phantom: PhantomData<&'a mut AnyPin>, +} + +impl<'a> ExtiInputFuture<'a> { + fn new(pin: u8, port: u8, rising: vals::Rt, falling: vals::Ft) -> Self { + cortex_m::interrupt::free(|_| unsafe { + let pin = pin as usize; + SYSCFG.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port)); + EXTI.rtsr(CORE_INDEX).modify(|w| w.set_rt(pin, rising)); + EXTI.ftsr(CORE_INDEX).modify(|w| w.set_ft(pin, falling)); + EXTI.pr(CORE_INDEX).write(|w| w.set_pif(pin, true)); // clear pending bit + EXTI.cpu(CORE_INDEX) + .imr(CORE_INDEX) + .modify(|w| w.set_im(pin, vals::Mr::UNMASKED)); + }); + + Self { + pin, + phantom: PhantomData, + } + } +} + +impl<'a> Drop for ExtiInputFuture<'a> { + fn drop(&mut self) { + cortex_m::interrupt::free(|_| unsafe { + let pin = self.pin as _; + EXTI.cpu(CORE_INDEX) + .imr(CORE_INDEX) + .modify(|w| w.set_im(pin, vals::Mr::MASKED)); + }); + } +} + +impl<'a> Future for ExtiInputFuture<'a> { + type Output = (); + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + EXTI_WAKERS[self.pin as usize].register(cx.waker()); + + if unsafe { + EXTI.cpu(CORE_INDEX) + .imr(CORE_INDEX) + .read() + .im(self.pin as _) + == vals::Mr::MASKED + } { + Poll::Ready(()) + } else { + Poll::Pending + } + } +} + +use crate::interrupt; + +macro_rules! impl_irq { + ($e:ident) => { + #[interrupt] + unsafe fn $e() { + on_irq() + } + }; +} + +foreach_exti_irq!(impl_irq); diff --git a/embassy-stm32/src/rcc/wl5x/mod.rs b/embassy-stm32/src/rcc/wl5x/mod.rs index b91adb203..72caad2ec 100644 --- a/embassy-stm32/src/rcc/wl5x/mod.rs +++ b/embassy-stm32/src/rcc/wl5x/mod.rs @@ -116,15 +116,6 @@ impl<'d> Rcc<'d> { pub fn clocks(&self) -> &'static Clocks { unsafe { get_freqs() } } - - pub fn enable_debug_wfe(&mut self, _dbg: &mut peripherals::DBGMCU, enable_dma: bool) { - // NOTE(unsafe) We have exclusive access to the RCC and DBGMCU - unsafe { - pac::RCC.ahb1enr().modify(|w| w.set_dma1en(enable_dma)); - - Dbgmcu::enable_all(); - } - } } /// Extension trait that freezes the `RCC` peripheral with provided clocks configuration diff --git a/examples/stm32wl55/Cargo.toml b/examples/stm32wl55/Cargo.toml new file mode 100644 index 000000000..ebbe8b847 --- /dev/null +++ b/examples/stm32wl55/Cargo.toml @@ -0,0 +1,34 @@ +[package] +authors = ["Dario Nieuwenhuis ", "Ulf Lilleengen "] +edition = "2018" +name = "embassy-stm32wl55-examples" +version = "0.1.0" +resolver = "2" + +[features] +default = [ + "defmt-default", +] +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] + +[dependencies] +embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } +embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x"] } +embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } + +defmt = "0.2.0" +defmt-rtt = "0.2.0" + +cortex-m = "0.7.1" +cortex-m-rt = "0.6.14" +embedded-hal = { version = "0.2.4" } +panic-probe = { version = "0.2.0", features= ["print-defmt"] } +futures = { version = "0.3.8", default-features = false, features = ["async-await"] } +rtt-target = { version = "0.3", features = ["cortex-m"] } +heapless = { version = "0.7.1", default-features = false } diff --git a/examples/stm32wl55/memory.x b/examples/stm32wl55/memory.x new file mode 100644 index 000000000..e8b2737fe --- /dev/null +++ b/examples/stm32wl55/memory.x @@ -0,0 +1,5 @@ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 256K + RAM : ORIGIN = 0x20000000, LENGTH = 64K +} diff --git a/examples/stm32wl55/src/bin/blinky.rs b/examples/stm32wl55/src/bin/blinky.rs new file mode 100644 index 000000000..b5e5ffcf9 --- /dev/null +++ b/examples/stm32wl55/src/bin/blinky.rs @@ -0,0 +1,34 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::dbgmcu::Dbgmcu; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + unsafe { Dbgmcu::enable_all() }; + + let mut led = Output::new(p.PB15, Level::High, Speed::Low); + + loop { + info!("high"); + unwrap!(led.set_high()); + Timer::after(Duration::from_millis(500)).await; + + info!("low"); + unwrap!(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 new file mode 100644 index 000000000..90212d3d7 --- /dev/null +++ b/examples/stm32wl55/src/bin/button.rs @@ -0,0 +1,40 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy_stm32::{ + dbgmcu::Dbgmcu, + gpio::{Input, Level, Output, Pull, Speed}, + rcc::*, +}; +use embedded_hal::digital::v2::{InputPin, OutputPin}; +use example_common::*; + +use cortex_m_rt::entry; + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let mut p = embassy_stm32::init(Default::default()); + + unsafe { Dbgmcu::enable_all() }; + + let button = Input::new(p.PA0, Pull::Up); + let mut led1 = Output::new(p.PB15, Level::High, Speed::Low); + 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(); + } else { + led1.set_low().unwrap(); + led2.set_high().unwrap(); + } + } +} diff --git a/examples/stm32wl55/src/bin/button_exti.rs b/examples/stm32wl55/src/bin/button_exti.rs new file mode 100644 index 000000000..2f6e55115 --- /dev/null +++ b/examples/stm32wl55/src/bin/button_exti.rs @@ -0,0 +1,34 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy_stm32::dbgmcu::Dbgmcu; +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] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + unsafe { Dbgmcu::enable_all() }; + + let button = Input::new(p.PA0, Pull::Up); + let mut button = ExtiInput::new(button, p.EXTI0); + + info!("Press the USER button..."); + + loop { + button.wait_for_falling_edge().await; + info!("Pressed!"); + button.wait_for_rising_edge().await; + info!("Released!"); + } +} diff --git a/examples/stm32wl55/src/example_common.rs b/examples/stm32wl55/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32wl55/src/example_common.rs @@ -0,0 +1,17 @@ +#![macro_use] + +use defmt_rtt as _; // global logger +use panic_probe as _; + +pub use defmt::*; + +use core::sync::atomic::{AtomicUsize, Ordering}; + +defmt::timestamp! {"{=u64}", { + static COUNT: AtomicUsize = AtomicUsize::new(0); + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n as u64 + } +} diff --git a/stm32-data b/stm32-data index 75a76596c..bfd4797d1 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit 75a76596c37f07cbbdaa3a689c1776297063b651 +Subproject commit bfd4797d1278c3e0b4611bc79e12346dedbde7c9 diff --git a/stm32-metapac-gen/src/lib.rs b/stm32-metapac-gen/src/lib.rs index ae401bb67..b1980d84e 100644 --- a/stm32-metapac-gen/src/lib.rs +++ b/stm32-metapac-gen/src/lib.rs @@ -231,21 +231,23 @@ pub fn gen(options: Options) { let chip: Chip = serde_yaml::from_slice(&chip).unwrap(); println!("looking for core {:?}", core_name); - let core: Option<&Core> = if let Some(core_name) = core_name { + let core: Option<(&Core, usize)> = if let Some(core_name) = core_name { let core_name = core_name.to_ascii_lowercase(); let mut c = None; - for core in chip.cores.iter() { + let mut idx = 0; + for (i, core) in chip.cores.iter().enumerate() { if core.name == core_name { c = Some(core); + idx = i; break; } } - c + c.map(|c| (c, idx)) } else { - Some(&chip.cores[0]) + Some((&chip.cores[0], 0)) }; - let core = core.unwrap(); + let (core, core_index) = core.unwrap(); let core_name = &core.name; let mut ir = ir::IR::new(); @@ -584,6 +586,12 @@ pub fn gen(options: Options) { ) .unwrap(); } + write!( + &mut extra, + "pub const CORE_INDEX: usize = {};\n", + core_index + ) + .unwrap(); // Cleanups! transform::sort::Sort {}.run(&mut ir).unwrap();