lora: Use a trait for RF frontend switching

The Seeed Studio Lora-E5 module only has two control pins.
With the `RadioSwitch` trait the user can implement any method required
by the module/board to control the TX/RX direction of the radio frontend.
This commit is contained in:
Timo Kröger 2022-06-25 10:10:59 +02:00
parent 6ee29ff0bd
commit 69d80c086d
2 changed files with 34 additions and 36 deletions

View File

@ -4,7 +4,6 @@ use core::task::Poll;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{AnyPin, Output};
use embassy_stm32::interrupt::{Interrupt, InterruptExt, SUBGHZ_RADIO}; use embassy_stm32::interrupt::{Interrupt, InterruptExt, SUBGHZ_RADIO};
use embassy_stm32::subghz::{ use embassy_stm32::subghz::{
CalibrateImage, CfgIrq, CodingRate, Error, HeaderType, Irq, LoRaBandwidth, LoRaModParams, LoRaPacketParams, CalibrateImage, CfgIrq, CodingRate, Error, HeaderType, Irq, LoRaBandwidth, LoRaModParams, LoRaPacketParams,
@ -100,7 +99,7 @@ impl<'d, RS: RadioSwitch> SubGhzRadio<'d, RS> {
async fn do_tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, RadioError> { async fn do_tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, RadioError> {
//trace!("TX Request: {}", config); //trace!("TX Request: {}", config);
trace!("TX START"); trace!("TX START");
self.switch.set_tx_lp(); self.switch.set_tx();
self.configure()?; self.configure()?;
self.radio self.radio
@ -236,15 +235,15 @@ impl<'d, RS: RadioSwitch> SubGhzRadio<'d, RS> {
} }
} }
impl PhyRxTx for SubGhzRadio<'static> { impl<RS: RadioSwitch> PhyRxTx for SubGhzRadio<'static, RS> {
type PhyError = RadioError; type PhyError = RadioError;
type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm; type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm where RS: 'm;
fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> { fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> {
async move { self.do_tx(config, buf).await } async move { self.do_tx(config, buf).await }
} }
type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm; type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm where RS: 'm;
fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> { fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> {
async move { self.do_rx(config, buf).await } async move { self.do_rx(config, buf).await }
} }
@ -265,36 +264,9 @@ impl<'d, RS> Timings for SubGhzRadio<'d, RS> {
} }
} }
/// Represents the radio switch found on STM32WL based boards, used to control the radio for transmission or reception. pub trait RadioSwitch {
pub struct RadioSwitch<'d> { fn set_rx(&mut self);
ctrl1: Output<'d, AnyPin>, fn set_tx(&mut self);
ctrl2: Output<'d, AnyPin>,
ctrl3: Output<'d, AnyPin>,
}
impl<'d> RadioSwitch<'d> {
pub fn new(ctrl1: Output<'d, AnyPin>, ctrl2: Output<'d, AnyPin>, ctrl3: Output<'d, AnyPin>) -> Self {
Self { ctrl1, ctrl2, ctrl3 }
}
pub(crate) fn set_rx(&mut self) {
self.ctrl1.set_high();
self.ctrl2.set_low();
self.ctrl3.set_high();
}
pub(crate) fn set_tx_lp(&mut self) {
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();
self.ctrl1.set_low();
self.ctrl3.set_high();
}
} }
fn convert_spreading_factor(sf: SpreadingFactor) -> SF { fn convert_spreading_factor(sf: SpreadingFactor) -> SF {

View File

@ -9,7 +9,7 @@ use embassy_executor::Spawner;
use embassy_lora::stm32wl::*; use embassy_lora::stm32wl::*;
use embassy_lora::LoraTimer; use embassy_lora::LoraTimer;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{Level, Output, Pin, Speed}; use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed};
use embassy_stm32::rng::Rng; use embassy_stm32::rng::Rng;
use embassy_stm32::subghz::*; use embassy_stm32::subghz::*;
use embassy_stm32::{interrupt, pac}; use embassy_stm32::{interrupt, pac};
@ -17,6 +17,32 @@ use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::{region, Device, JoinMode}; use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
struct RadioSwitch<'a> {
ctrl1: Output<'a, AnyPin>,
ctrl2: Output<'a, AnyPin>,
ctrl3: Output<'a, AnyPin>,
}
impl<'a> RadioSwitch<'a> {
fn new(ctrl1: Output<'a, AnyPin>, ctrl2: Output<'a, AnyPin>, ctrl3: Output<'a, AnyPin>) -> Self {
Self { ctrl1, ctrl2, ctrl3 }
}
}
impl<'a> embassy_lora::stm32wl::RadioSwitch for RadioSwitch<'a> {
fn set_rx(&mut self) {
self.ctrl1.set_high();
self.ctrl2.set_low();
self.ctrl3.set_high();
}
fn set_tx(&mut self) {
self.ctrl1.set_low();
self.ctrl2.set_high();
self.ctrl3.set_high();
}
}
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default(); let mut config = embassy_stm32::Config::default();