From b6ba1ea53ada2f503ae89de66490957723a21866 Mon Sep 17 00:00:00 2001 From: xoviat Date: Wed, 24 May 2023 18:09:04 -0500 Subject: [PATCH] stm32: move lora to bind_interrupts --- ci.sh | 2 +- embassy-lora/src/iv.rs | 74 +++++++++++--------- examples/stm32wl/src/bin/lora_lorawan.rs | 12 ++-- examples/stm32wl/src/bin/lora_p2p_receive.rs | 12 ++-- examples/stm32wl/src/bin/lora_p2p_send.rs | 12 ++-- examples/stm32wl/src/bin/uart_async.rs | 15 ++-- 6 files changed, 71 insertions(+), 56 deletions(-) diff --git a/ci.sh b/ci.sh index 106689d0..2c46dcc6 100755 --- a/ci.sh +++ b/ci.sh @@ -112,6 +112,7 @@ cargo batch \ --- build --release --manifest-path examples/stm32l5/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/stm32l5 \ --- build --release --manifest-path examples/stm32u5/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/stm32u5 \ --- build --release --manifest-path examples/stm32wb/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wb \ + --- build --release --manifest-path examples/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wl \ --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv7em-none-eabi --features embassy-nrf/nrf52840 --out-dir out/examples/boot/nrf --bin b \ --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns --out-dir out/examples/boot/nrf --bin b \ --- build --release --manifest-path examples/boot/application/rp/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/boot/rp --bin b \ @@ -142,7 +143,6 @@ cargo batch \ $BUILD_EXTRA -# --- build --release --manifest-path examples/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wl \ function run_elf { echo Running target=$1 elf=$2 diff --git a/embassy-lora/src/iv.rs b/embassy-lora/src/iv.rs index f8113440..d515bc36 100644 --- a/embassy-lora/src/iv.rs +++ b/embassy-lora/src/iv.rs @@ -1,7 +1,9 @@ #[cfg(feature = "stm32wl")] +use embassy_stm32::interrupt; +#[cfg(feature = "stm32wl")] use embassy_stm32::interrupt::*; #[cfg(feature = "stm32wl")] -use embassy_stm32::{pac, PeripheralRef}; +use embassy_stm32::pac; #[cfg(feature = "stm32wl")] use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; #[cfg(feature = "stm32wl")] @@ -13,47 +15,51 @@ use lora_phy::mod_params::RadioError::*; use lora_phy::mod_params::{BoardType, RadioError}; use lora_phy::mod_traits::InterfaceVariant; +/// Interrupt handler. #[cfg(feature = "stm32wl")] -static IRQ_SIGNAL: Signal = Signal::new(); +pub struct InterruptHandler {} #[cfg(feature = "stm32wl")] -/// Base for the InterfaceVariant implementation for an stm32wl/sx1262 combination -pub struct Stm32wlInterfaceVariant<'a, CTRL> { - board_type: BoardType, - irq: PeripheralRef<'a, SUBGHZ_RADIO>, - rf_switch_rx: Option, - rf_switch_tx: Option, -} - -#[cfg(feature = "stm32wl")] -impl<'a, CTRL> Stm32wlInterfaceVariant<'a, CTRL> -where - CTRL: OutputPin, -{ - /// Create an InterfaceVariant instance for an stm32wl/sx1262 combination - pub fn new( - irq: PeripheralRef<'a, SUBGHZ_RADIO>, - rf_switch_rx: Option, - rf_switch_tx: Option, - ) -> Result { - irq.disable(); - irq.set_handler(Self::on_interrupt); - Ok(Self { - board_type: BoardType::Stm32wlSx1262, // updated when associated with a specific LoRa board - irq, - rf_switch_rx, - rf_switch_tx, - }) - } - - fn on_interrupt(_: *mut ()) { +impl interrupt::Handler for InterruptHandler { + unsafe fn on_interrupt() { unsafe { SUBGHZ_RADIO::steal() }.disable(); IRQ_SIGNAL.signal(()); } } #[cfg(feature = "stm32wl")] -impl InterfaceVariant for Stm32wlInterfaceVariant<'_, CTRL> +static IRQ_SIGNAL: Signal = Signal::new(); + +#[cfg(feature = "stm32wl")] +/// Base for the InterfaceVariant implementation for an stm32wl/sx1262 combination +pub struct Stm32wlInterfaceVariant { + board_type: BoardType, + rf_switch_rx: Option, + rf_switch_tx: Option, +} + +#[cfg(feature = "stm32wl")] +impl<'a, CTRL> Stm32wlInterfaceVariant +where + CTRL: OutputPin, +{ + /// Create an InterfaceVariant instance for an stm32wl/sx1262 combination + pub fn new( + _irq: impl interrupt::Binding, + rf_switch_rx: Option, + rf_switch_tx: Option, + ) -> Result { + unsafe { interrupt::SUBGHZ_RADIO::steal() }.disable(); + Ok(Self { + board_type: BoardType::Stm32wlSx1262, // updated when associated with a specific LoRa board + rf_switch_rx, + rf_switch_tx, + }) + } +} + +#[cfg(feature = "stm32wl")] +impl InterfaceVariant for Stm32wlInterfaceVariant where CTRL: OutputPin, { @@ -89,7 +95,7 @@ where } async fn await_irq(&mut self) -> Result<(), RadioError> { - self.irq.enable(); + unsafe { interrupt::SUBGHZ_RADIO::steal() }.enable(); IRQ_SIGNAL.wait().await; Ok(()) } diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs index 1a271b2f..e179c5ca 100644 --- a/examples/stm32wl/src/bin/lora_lorawan.rs +++ b/examples/stm32wl/src/bin/lora_lorawan.rs @@ -7,12 +7,12 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_lora::iv::Stm32wlInterfaceVariant; +use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant}; use embassy_lora::LoraTimer; use embassy_stm32::gpio::{Level, Output, Pin, Speed}; use embassy_stm32::rng::Rng; use embassy_stm32::spi::Spi; -use embassy_stm32::{interrupt, into_ref, pac, Peripheral}; +use embassy_stm32::{bind_interrupts, pac}; use embassy_time::Delay; use lora_phy::mod_params::*; use lora_phy::sx1261_2::SX1261_2; @@ -24,6 +24,10 @@ use {defmt_rtt as _, panic_probe as _}; const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region +bind_interrupts!(struct Irqs{ + SUBGHZ_RADIO => InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -35,13 +39,11 @@ async fn main(_spawner: Spawner) { let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2); - let irq = interrupt::take!(SUBGHZ_RADIO); - into_ref!(irq); // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); - let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); + let iv = Stm32wlInterfaceVariant::new(Irqs, None, Some(ctrl2)).unwrap(); let mut delay = Delay; diff --git a/examples/stm32wl/src/bin/lora_p2p_receive.rs b/examples/stm32wl/src/bin/lora_p2p_receive.rs index 5e80e8f6..d3f051b1 100644 --- a/examples/stm32wl/src/bin/lora_p2p_receive.rs +++ b/examples/stm32wl/src/bin/lora_p2p_receive.rs @@ -7,10 +7,10 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_lora::iv::Stm32wlInterfaceVariant; +use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant}; +use embassy_stm32::bind_interrupts; use embassy_stm32::gpio::{Level, Output, Pin, Speed}; use embassy_stm32::spi::Spi; -use embassy_stm32::{interrupt, into_ref, Peripheral}; use embassy_time::{Delay, Duration, Timer}; use lora_phy::mod_params::*; use lora_phy::sx1261_2::SX1261_2; @@ -19,6 +19,10 @@ use {defmt_rtt as _, panic_probe as _}; const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region +bind_interrupts!(struct Irqs{ + SUBGHZ_RADIO => InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -27,13 +31,11 @@ async fn main(_spawner: Spawner) { let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2); - let irq = interrupt::take!(SUBGHZ_RADIO); - into_ref!(irq); // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); - let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); + let iv = Stm32wlInterfaceVariant::new(Irqs, None, Some(ctrl2)).unwrap(); let mut delay = Delay; diff --git a/examples/stm32wl/src/bin/lora_p2p_send.rs b/examples/stm32wl/src/bin/lora_p2p_send.rs index e22c714b..fc5205c8 100644 --- a/examples/stm32wl/src/bin/lora_p2p_send.rs +++ b/examples/stm32wl/src/bin/lora_p2p_send.rs @@ -7,10 +7,10 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_lora::iv::Stm32wlInterfaceVariant; +use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant}; +use embassy_stm32::bind_interrupts; use embassy_stm32::gpio::{Level, Output, Pin, Speed}; use embassy_stm32::spi::Spi; -use embassy_stm32::{interrupt, into_ref, Peripheral}; use embassy_time::Delay; use lora_phy::mod_params::*; use lora_phy::sx1261_2::SX1261_2; @@ -19,6 +19,10 @@ use {defmt_rtt as _, panic_probe as _}; const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region +bind_interrupts!(struct Irqs{ + SUBGHZ_RADIO => InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -27,13 +31,11 @@ async fn main(_spawner: Spawner) { let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2); - let irq = interrupt::take!(SUBGHZ_RADIO); - into_ref!(irq); // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); - let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); + let iv = Stm32wlInterfaceVariant::new(Irqs, None, Some(ctrl2)).unwrap(); let mut delay = Delay; diff --git a/examples/stm32wl/src/bin/uart_async.rs b/examples/stm32wl/src/bin/uart_async.rs index ac8766af..07b0f9d2 100644 --- a/examples/stm32wl/src/bin/uart_async.rs +++ b/examples/stm32wl/src/bin/uart_async.rs @@ -4,10 +4,15 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_stm32::interrupt; -use embassy_stm32::usart::{Config, Uart}; +use embassy_stm32::usart::{Config, InterruptHandler, Uart}; +use embassy_stm32::{bind_interrupts, peripherals}; use {defmt_rtt as _, panic_probe as _}; +bind_interrupts!(struct Irqs{ + USART1 => InterruptHandler; + LPUART1 => InterruptHandler; +}); + /* Pass Incoming data from LPUART1 to USART1 Example is written for the LoRa-E5 mini v1.0, @@ -28,12 +33,10 @@ async fn main(_spawner: Spawner) { config2.baudrate = 9600; //RX/TX connected to USB/UART Bridge on LoRa-E5 mini v1.0 - let irq = interrupt::take!(USART1); - let mut usart1 = Uart::new(p.USART1, p.PB7, p.PB6, irq, p.DMA1_CH3, p.DMA1_CH4, config1); + let mut usart1 = Uart::new(p.USART1, p.PB7, p.PB6, Irqs, p.DMA1_CH3, p.DMA1_CH4, config1); //RX1/TX1 (LPUART) on LoRa-E5 mini v1.0 - let irq = interrupt::take!(LPUART1); - let mut usart2 = Uart::new(p.LPUART1, p.PC0, p.PC1, irq, p.DMA1_CH5, p.DMA1_CH6, config2); + let mut usart2 = Uart::new(p.LPUART1, p.PC0, p.PC1, Irqs, p.DMA1_CH5, p.DMA1_CH6, config2); unwrap!(usart1.write(b"Hello Embassy World!\r\n").await); unwrap!(usart2.write(b"Hello Embassy World!\r\n").await);