aboutsummaryrefslogtreecommitdiff
path: root/embassy-lora
diff options
context:
space:
mode:
authorTimo Kröger <[email protected]>2022-06-25 10:10:59 +0200
committerTimo Kröger <[email protected]>2022-08-26 15:44:58 +0200
commit69d80c086d2f22f66aa25fafb55918cb1c5e3bdd (patch)
tree0bf973222a790689263c5483582110f76e008ff9 /embassy-lora
parent6ee29ff0bd4d8cbd2da547dfdf631332cda551d4 (diff)
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.
Diffstat (limited to 'embassy-lora')
-rw-r--r--embassy-lora/src/stm32wl/mod.rs42
1 files changed, 7 insertions, 35 deletions
diff --git a/embassy-lora/src/stm32wl/mod.rs b/embassy-lora/src/stm32wl/mod.rs
index 5e1773d55..7502dc379 100644
--- a/embassy-lora/src/stm32wl/mod.rs
+++ b/embassy-lora/src/stm32wl/mod.rs
@@ -4,7 +4,6 @@ use core::task::Poll;
4 4
5use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; 5use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
6use embassy_stm32::dma::NoDma; 6use embassy_stm32::dma::NoDma;
7use embassy_stm32::gpio::{AnyPin, Output};
8use embassy_stm32::interrupt::{Interrupt, InterruptExt, SUBGHZ_RADIO}; 7use embassy_stm32::interrupt::{Interrupt, InterruptExt, SUBGHZ_RADIO};
9use embassy_stm32::subghz::{ 8use embassy_stm32::subghz::{
10 CalibrateImage, CfgIrq, CodingRate, Error, HeaderType, Irq, LoRaBandwidth, LoRaModParams, LoRaPacketParams, 9 CalibrateImage, CfgIrq, CodingRate, Error, HeaderType, Irq, LoRaBandwidth, LoRaModParams, LoRaPacketParams,
@@ -100,7 +99,7 @@ impl<'d, RS: RadioSwitch> SubGhzRadio<'d, RS> {
100 async fn do_tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, RadioError> { 99 async fn do_tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, RadioError> {
101 //trace!("TX Request: {}", config); 100 //trace!("TX Request: {}", config);
102 trace!("TX START"); 101 trace!("TX START");
103 self.switch.set_tx_lp(); 102 self.switch.set_tx();
104 self.configure()?; 103 self.configure()?;
105 104
106 self.radio 105 self.radio
@@ -236,15 +235,15 @@ impl<'d, RS: RadioSwitch> SubGhzRadio<'d, RS> {
236 } 235 }
237} 236}
238 237
239impl PhyRxTx for SubGhzRadio<'static> { 238impl<RS: RadioSwitch> PhyRxTx for SubGhzRadio<'static, RS> {
240 type PhyError = RadioError; 239 type PhyError = RadioError;
241 240
242 type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm; 241 type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm where RS: 'm;
243 fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> { 242 fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> {
244 async move { self.do_tx(config, buf).await } 243 async move { self.do_tx(config, buf).await }
245 } 244 }
246 245
247 type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm; 246 type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm where RS: 'm;
248 fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> { 247 fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> {
249 async move { self.do_rx(config, buf).await } 248 async move { self.do_rx(config, buf).await }
250 } 249 }
@@ -265,36 +264,9 @@ impl<'d, RS> Timings for SubGhzRadio<'d, RS> {
265 } 264 }
266} 265}
267 266
268/// Represents the radio switch found on STM32WL based boards, used to control the radio for transmission or reception. 267pub trait RadioSwitch {
269pub struct RadioSwitch<'d> { 268 fn set_rx(&mut self);
270 ctrl1: Output<'d, AnyPin>, 269 fn set_tx(&mut self);
271 ctrl2: Output<'d, AnyPin>,
272 ctrl3: Output<'d, AnyPin>,
273}
274
275impl<'d> RadioSwitch<'d> {
276 pub fn new(ctrl1: Output<'d, AnyPin>, ctrl2: Output<'d, AnyPin>, ctrl3: Output<'d, AnyPin>) -> Self {
277 Self { ctrl1, ctrl2, ctrl3 }
278 }
279
280 pub(crate) fn set_rx(&mut self) {
281 self.ctrl1.set_high();
282 self.ctrl2.set_low();
283 self.ctrl3.set_high();
284 }
285
286 pub(crate) fn set_tx_lp(&mut self) {
287 self.ctrl1.set_high();
288 self.ctrl2.set_high();
289 self.ctrl3.set_high();
290 }
291
292 #[allow(dead_code)]
293 pub(crate) fn set_tx_hp(&mut self) {
294 self.ctrl2.set_high();
295 self.ctrl1.set_low();
296 self.ctrl3.set_high();
297 }
298} 270}
299 271
300fn convert_spreading_factor(sf: SpreadingFactor) -> SF { 272fn convert_spreading_factor(sf: SpreadingFactor) -> SF {