From 1b9925e3da0ec42b770f736cd22325203c97cb47 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 29 Nov 2023 16:26:31 +0100 Subject: Move embassy-lora, lora examples to lora-phy repo. --- embassy-lora/src/fmt.rs | 258 --------------------------------------- embassy-lora/src/iv.rs | 317 ------------------------------------------------ embassy-lora/src/lib.rs | 40 ------ 3 files changed, 615 deletions(-) delete mode 100644 embassy-lora/src/fmt.rs delete mode 100644 embassy-lora/src/iv.rs delete mode 100644 embassy-lora/src/lib.rs (limited to 'embassy-lora/src') diff --git a/embassy-lora/src/fmt.rs b/embassy-lora/src/fmt.rs deleted file mode 100644 index 78e583c1c..000000000 --- a/embassy-lora/src/fmt.rs +++ /dev/null @@ -1,258 +0,0 @@ -#![macro_use] -#![allow(unused_macros)] - -use core::fmt::{Debug, Display, LowerHex}; - -#[cfg(all(feature = "defmt", feature = "log"))] -compile_error!("You may not enable both `defmt` and `log` features."); - -macro_rules! assert { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::assert!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::assert!($($x)*); - } - }; -} - -macro_rules! assert_eq { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::assert_eq!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::assert_eq!($($x)*); - } - }; -} - -macro_rules! assert_ne { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::assert_ne!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::assert_ne!($($x)*); - } - }; -} - -macro_rules! debug_assert { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::debug_assert!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::debug_assert!($($x)*); - } - }; -} - -macro_rules! debug_assert_eq { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::debug_assert_eq!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::debug_assert_eq!($($x)*); - } - }; -} - -macro_rules! debug_assert_ne { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::debug_assert_ne!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::debug_assert_ne!($($x)*); - } - }; -} - -macro_rules! todo { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::todo!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::todo!($($x)*); - } - }; -} - -#[cfg(not(feature = "defmt"))] -macro_rules! unreachable { - ($($x:tt)*) => { - ::core::unreachable!($($x)*) - }; -} - -#[cfg(feature = "defmt")] -macro_rules! unreachable { - ($($x:tt)*) => { - ::defmt::unreachable!($($x)*) - }; -} - -macro_rules! panic { - ($($x:tt)*) => { - { - #[cfg(not(feature = "defmt"))] - ::core::panic!($($x)*); - #[cfg(feature = "defmt")] - ::defmt::panic!($($x)*); - } - }; -} - -macro_rules! trace { - ($s:literal $(, $x:expr)* $(,)?) => { - { - #[cfg(feature = "log")] - ::log::trace!($s $(, $x)*); - #[cfg(feature = "defmt")] - ::defmt::trace!($s $(, $x)*); - #[cfg(not(any(feature = "log", feature="defmt")))] - let _ = ($( & $x ),*); - } - }; -} - -macro_rules! debug { - ($s:literal $(, $x:expr)* $(,)?) => { - { - #[cfg(feature = "log")] - ::log::debug!($s $(, $x)*); - #[cfg(feature = "defmt")] - ::defmt::debug!($s $(, $x)*); - #[cfg(not(any(feature = "log", feature="defmt")))] - let _ = ($( & $x ),*); - } - }; -} - -macro_rules! info { - ($s:literal $(, $x:expr)* $(,)?) => { - { - #[cfg(feature = "log")] - ::log::info!($s $(, $x)*); - #[cfg(feature = "defmt")] - ::defmt::info!($s $(, $x)*); - #[cfg(not(any(feature = "log", feature="defmt")))] - let _ = ($( & $x ),*); - } - }; -} - -macro_rules! warn { - ($s:literal $(, $x:expr)* $(,)?) => { - { - #[cfg(feature = "log")] - ::log::warn!($s $(, $x)*); - #[cfg(feature = "defmt")] - ::defmt::warn!($s $(, $x)*); - #[cfg(not(any(feature = "log", feature="defmt")))] - let _ = ($( & $x ),*); - } - }; -} - -macro_rules! error { - ($s:literal $(, $x:expr)* $(,)?) => { - { - #[cfg(feature = "log")] - ::log::error!($s $(, $x)*); - #[cfg(feature = "defmt")] - ::defmt::error!($s $(, $x)*); - #[cfg(not(any(feature = "log", feature="defmt")))] - let _ = ($( & $x ),*); - } - }; -} - -#[cfg(feature = "defmt")] -macro_rules! unwrap { - ($($x:tt)*) => { - ::defmt::unwrap!($($x)*) - }; -} - -#[cfg(not(feature = "defmt"))] -macro_rules! unwrap { - ($arg:expr) => { - match $crate::fmt::Try::into_result($arg) { - ::core::result::Result::Ok(t) => t, - ::core::result::Result::Err(e) => { - ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); - } - } - }; - ($arg:expr, $($msg:expr),+ $(,)? ) => { - match $crate::fmt::Try::into_result($arg) { - ::core::result::Result::Ok(t) => t, - ::core::result::Result::Err(e) => { - ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); - } - } - } -} - -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct NoneError; - -pub trait Try { - type Ok; - type Error; - fn into_result(self) -> Result; -} - -impl Try for Option { - type Ok = T; - type Error = NoneError; - - #[inline] - fn into_result(self) -> Result { - self.ok_or(NoneError) - } -} - -impl Try for Result { - type Ok = T; - type Error = E; - - #[inline] - fn into_result(self) -> Self { - self - } -} - -#[allow(unused)] -pub(crate) struct Bytes<'a>(pub &'a [u8]); - -impl<'a> Debug for Bytes<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{:#02x?}", self.0) - } -} - -impl<'a> Display for Bytes<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{:#02x?}", self.0) - } -} - -impl<'a> LowerHex for Bytes<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{:#02x?}", self.0) - } -} - -#[cfg(feature = "defmt")] -impl<'a> defmt::Format for Bytes<'a> { - fn format(&self, fmt: defmt::Formatter) { - defmt::write!(fmt, "{:02x}", self.0) - } -} diff --git a/embassy-lora/src/iv.rs b/embassy-lora/src/iv.rs deleted file mode 100644 index d22beb337..000000000 --- a/embassy-lora/src/iv.rs +++ /dev/null @@ -1,317 +0,0 @@ -#[cfg(feature = "stm32wl")] -use embassy_stm32::interrupt; -#[cfg(feature = "stm32wl")] -use embassy_stm32::interrupt::InterruptExt; -#[cfg(feature = "stm32wl")] -use embassy_stm32::pac; -#[cfg(feature = "stm32wl")] -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -#[cfg(feature = "stm32wl")] -use embassy_sync::signal::Signal; -use embedded_hal::digital::v2::OutputPin; -use embedded_hal_async::delay::DelayUs; -use embedded_hal_async::digital::Wait; -use lora_phy::mod_params::RadioError::*; -use lora_phy::mod_params::{BoardType, RadioError}; -use lora_phy::mod_traits::InterfaceVariant; - -/// Interrupt handler. -#[cfg(feature = "stm32wl")] -pub struct InterruptHandler {} - -#[cfg(feature = "stm32wl")] -impl interrupt::typelevel::Handler for InterruptHandler { - unsafe fn on_interrupt() { - interrupt::SUBGHZ_RADIO.disable(); - IRQ_SIGNAL.signal(()); - } -} - -#[cfg(feature = "stm32wl")] -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::typelevel::Binding, - rf_switch_rx: Option, - rf_switch_tx: Option, - ) -> Result { - interrupt::SUBGHZ_RADIO.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, -{ - fn set_board_type(&mut self, board_type: BoardType) { - self.board_type = board_type; - } - async fn set_nss_low(&mut self) -> Result<(), RadioError> { - pac::PWR.subghzspicr().modify(|w| w.set_nss(false)); - Ok(()) - } - async fn set_nss_high(&mut self) -> Result<(), RadioError> { - pac::PWR.subghzspicr().modify(|w| w.set_nss(true)); - Ok(()) - } - async fn reset(&mut self, _delay: &mut impl DelayUs) -> Result<(), RadioError> { - pac::RCC.csr().modify(|w| w.set_rfrst(true)); - pac::RCC.csr().modify(|w| w.set_rfrst(false)); - Ok(()) - } - async fn wait_on_busy(&mut self) -> Result<(), RadioError> { - while pac::PWR.sr2().read().rfbusys() {} - Ok(()) - } - - async fn await_irq(&mut self) -> Result<(), RadioError> { - unsafe { interrupt::SUBGHZ_RADIO.enable() }; - IRQ_SIGNAL.wait().await; - Ok(()) - } - - async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?, - None => (), - }; - match &mut self.rf_switch_rx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchRx), - None => Ok(()), - } - } - async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } - async fn disable_rf_switch(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } -} - -/// Base for the InterfaceVariant implementation for an stm32l0/sx1276 combination -pub struct Stm32l0InterfaceVariant { - board_type: BoardType, - nss: CTRL, - reset: CTRL, - irq: WAIT, - rf_switch_rx: Option, - rf_switch_tx: Option, -} - -impl Stm32l0InterfaceVariant -where - CTRL: OutputPin, - WAIT: Wait, -{ - /// Create an InterfaceVariant instance for an stm32l0/sx1276 combination - pub fn new( - nss: CTRL, - reset: CTRL, - irq: WAIT, - rf_switch_rx: Option, - rf_switch_tx: Option, - ) -> Result { - Ok(Self { - board_type: BoardType::Stm32l0Sx1276, // updated when associated with a specific LoRa board - nss, - reset, - irq, - rf_switch_rx, - rf_switch_tx, - }) - } -} - -impl InterfaceVariant for Stm32l0InterfaceVariant -where - CTRL: OutputPin, - WAIT: Wait, -{ - fn set_board_type(&mut self, board_type: BoardType) { - self.board_type = board_type; - } - async fn set_nss_low(&mut self) -> Result<(), RadioError> { - self.nss.set_low().map_err(|_| NSS) - } - async fn set_nss_high(&mut self) -> Result<(), RadioError> { - self.nss.set_high().map_err(|_| NSS) - } - async fn reset(&mut self, delay: &mut impl DelayUs) -> Result<(), RadioError> { - delay.delay_ms(10).await; - self.reset.set_low().map_err(|_| Reset)?; - delay.delay_ms(10).await; - self.reset.set_high().map_err(|_| Reset)?; - delay.delay_ms(10).await; - Ok(()) - } - async fn wait_on_busy(&mut self) -> Result<(), RadioError> { - Ok(()) - } - async fn await_irq(&mut self) -> Result<(), RadioError> { - self.irq.wait_for_high().await.map_err(|_| Irq) - } - - async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?, - None => (), - }; - match &mut self.rf_switch_rx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchRx), - None => Ok(()), - } - } - async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } - async fn disable_rf_switch(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } -} - -/// Base for the InterfaceVariant implementation for a generic Sx126x LoRa board -pub struct GenericSx126xInterfaceVariant { - board_type: BoardType, - nss: CTRL, - reset: CTRL, - dio1: WAIT, - busy: WAIT, - rf_switch_rx: Option, - rf_switch_tx: Option, -} - -impl GenericSx126xInterfaceVariant -where - CTRL: OutputPin, - WAIT: Wait, -{ - /// Create an InterfaceVariant instance for an nrf52840/sx1262 combination - pub fn new( - nss: CTRL, - reset: CTRL, - dio1: WAIT, - busy: WAIT, - rf_switch_rx: Option, - rf_switch_tx: Option, - ) -> Result { - Ok(Self { - board_type: BoardType::Rak4631Sx1262, // updated when associated with a specific LoRa board - nss, - reset, - dio1, - busy, - rf_switch_rx, - rf_switch_tx, - }) - } -} - -impl InterfaceVariant for GenericSx126xInterfaceVariant -where - CTRL: OutputPin, - WAIT: Wait, -{ - fn set_board_type(&mut self, board_type: BoardType) { - self.board_type = board_type; - } - async fn set_nss_low(&mut self) -> Result<(), RadioError> { - self.nss.set_low().map_err(|_| NSS) - } - async fn set_nss_high(&mut self) -> Result<(), RadioError> { - self.nss.set_high().map_err(|_| NSS) - } - async fn reset(&mut self, delay: &mut impl DelayUs) -> Result<(), RadioError> { - delay.delay_ms(10).await; - self.reset.set_low().map_err(|_| Reset)?; - delay.delay_ms(20).await; - self.reset.set_high().map_err(|_| Reset)?; - delay.delay_ms(10).await; - Ok(()) - } - async fn wait_on_busy(&mut self) -> Result<(), RadioError> { - self.busy.wait_for_low().await.map_err(|_| Busy) - } - async fn await_irq(&mut self) -> Result<(), RadioError> { - self.dio1.wait_for_high().await.map_err(|_| DIO1)?; - Ok(()) - } - - async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?, - None => (), - }; - match &mut self.rf_switch_rx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchRx), - None => Ok(()), - } - } - async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_high().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } - async fn disable_rf_switch(&mut self) -> Result<(), RadioError> { - match &mut self.rf_switch_rx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?, - None => (), - }; - match &mut self.rf_switch_tx { - Some(pin) => pin.set_low().map_err(|_| RfSwitchTx), - None => Ok(()), - } - } -} diff --git a/embassy-lora/src/lib.rs b/embassy-lora/src/lib.rs deleted file mode 100644 index 653c98253..000000000 --- a/embassy-lora/src/lib.rs +++ /dev/null @@ -1,40 +0,0 @@ -#![no_std] -#![feature(async_fn_in_trait, impl_trait_projections)] -#![allow(stable_features, unknown_lints, async_fn_in_trait)] -//! embassy-lora holds LoRa-specific functionality. - -pub(crate) mod fmt; - -/// interface variants required by the external lora physical layer crate (lora-phy) -pub mod iv; - -#[cfg(feature = "time")] -use embassy_time::{Duration, Instant, Timer}; - -/// A convenience timer to use with the LoRaWAN crate -#[cfg(feature = "time")] -pub struct LoraTimer { - start: Instant, -} - -#[cfg(feature = "time")] -impl LoraTimer { - pub fn new() -> Self { - Self { start: Instant::now() } - } -} - -#[cfg(feature = "time")] -impl lorawan_device::async_device::radio::Timer for LoraTimer { - fn reset(&mut self) { - self.start = Instant::now(); - } - - async fn at(&mut self, millis: u64) { - Timer::at(self.start + Duration::from_millis(millis)).await - } - - async fn delay_ms(&mut self, millis: u64) { - Timer::after_millis(millis).await - } -} -- cgit