lora_phy/
iv.rs

1use embedded_hal::digital::OutputPin;
2use embedded_hal_async::delay::DelayNs;
3use embedded_hal_async::digital::Wait;
4
5use crate::mod_params::RadioError;
6use crate::mod_params::RadioError::*;
7use crate::mod_traits::InterfaceVariant;
8
9/// Base for the InterfaceVariant implementation for the Sx127x combination
10pub struct GenericSx127xInterfaceVariant<CTRL, WAIT> {
11    reset: CTRL,
12    irq: WAIT,
13    rf_switch_rx: Option<CTRL>,
14    rf_switch_tx: Option<CTRL>,
15}
16
17impl<CTRL, WAIT> GenericSx127xInterfaceVariant<CTRL, WAIT>
18where
19    CTRL: OutputPin,
20    WAIT: Wait,
21{
22    /// Create an InterfaceVariant instance for an stm32l0/sx1276 combination
23    pub fn new(
24        reset: CTRL,
25        irq: WAIT,
26        rf_switch_rx: Option<CTRL>,
27        rf_switch_tx: Option<CTRL>,
28    ) -> Result<Self, RadioError> {
29        Ok(Self {
30            reset,
31            irq,
32            rf_switch_rx,
33            rf_switch_tx,
34        })
35    }
36}
37
38impl<CTRL, WAIT> InterfaceVariant for GenericSx127xInterfaceVariant<CTRL, WAIT>
39where
40    CTRL: OutputPin,
41    WAIT: Wait,
42{
43    async fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), RadioError> {
44        delay.delay_ms(10).await;
45        self.reset.set_low().map_err(|_| Reset)?;
46        delay.delay_ms(10).await;
47        self.reset.set_high().map_err(|_| Reset)?;
48        delay.delay_ms(10).await;
49        Ok(())
50    }
51    async fn wait_on_busy(&mut self) -> Result<(), RadioError> {
52        Ok(())
53    }
54    async fn await_irq(&mut self) -> Result<(), RadioError> {
55        self.irq.wait_for_high().await.map_err(|_| Irq)
56    }
57
58    async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> {
59        match &mut self.rf_switch_tx {
60            Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?,
61            None => (),
62        };
63        match &mut self.rf_switch_rx {
64            Some(pin) => pin.set_high().map_err(|_| RfSwitchRx),
65            None => Ok(()),
66        }
67    }
68    async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> {
69        match &mut self.rf_switch_rx {
70            Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
71            None => (),
72        };
73        match &mut self.rf_switch_tx {
74            Some(pin) => pin.set_high().map_err(|_| RfSwitchTx),
75            None => Ok(()),
76        }
77    }
78    async fn disable_rf_switch(&mut self) -> Result<(), RadioError> {
79        match &mut self.rf_switch_rx {
80            Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
81            None => (),
82        };
83        match &mut self.rf_switch_tx {
84            Some(pin) => pin.set_low().map_err(|_| RfSwitchTx),
85            None => Ok(()),
86        }
87    }
88}
89
90/// Base for the InterfaceVariant implementation for a generic Sx126x LoRa board
91pub struct GenericSx126xInterfaceVariant<CTRL, WAIT> {
92    reset: CTRL,
93    dio1: WAIT,
94    busy: WAIT,
95    rf_switch_rx: Option<CTRL>,
96    rf_switch_tx: Option<CTRL>,
97}
98
99impl<CTRL, WAIT> GenericSx126xInterfaceVariant<CTRL, WAIT>
100where
101    CTRL: OutputPin,
102    WAIT: Wait,
103{
104    /// Create an InterfaceVariant instance for an nrf52840/sx1262 combination
105    pub fn new(
106        reset: CTRL,
107        dio1: WAIT,
108        busy: WAIT,
109        rf_switch_rx: Option<CTRL>,
110        rf_switch_tx: Option<CTRL>,
111    ) -> Result<Self, RadioError> {
112        Ok(Self {
113            reset,
114            dio1,
115            busy,
116            rf_switch_rx,
117            rf_switch_tx,
118        })
119    }
120}
121
122impl<CTRL, WAIT> InterfaceVariant for GenericSx126xInterfaceVariant<CTRL, WAIT>
123where
124    CTRL: OutputPin,
125    WAIT: Wait,
126{
127    async fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), RadioError> {
128        delay.delay_ms(10).await;
129        self.reset.set_low().map_err(|_| Reset)?;
130        delay.delay_ms(20).await;
131        self.reset.set_high().map_err(|_| Reset)?;
132        delay.delay_ms(10).await;
133        Ok(())
134    }
135    async fn wait_on_busy(&mut self) -> Result<(), RadioError> {
136        self.busy.wait_for_low().await.map_err(|_| Busy)
137    }
138    async fn await_irq(&mut self) -> Result<(), RadioError> {
139        self.dio1.wait_for_high().await.map_err(|_| DIO1)?;
140        Ok(())
141    }
142
143    async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> {
144        match &mut self.rf_switch_tx {
145            Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?,
146            None => (),
147        };
148        match &mut self.rf_switch_rx {
149            Some(pin) => pin.set_high().map_err(|_| RfSwitchRx),
150            None => Ok(()),
151        }
152    }
153    async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> {
154        match &mut self.rf_switch_rx {
155            Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
156            None => (),
157        };
158        match &mut self.rf_switch_tx {
159            Some(pin) => pin.set_high().map_err(|_| RfSwitchTx),
160            None => Ok(()),
161        }
162    }
163    async fn disable_rf_switch(&mut self) -> Result<(), RadioError> {
164        match &mut self.rf_switch_rx {
165            Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
166            None => (),
167        };
168        match &mut self.rf_switch_tx {
169            Some(pin) => pin.set_low().map_err(|_| RfSwitchTx),
170            None => Ok(()),
171        }
172    }
173}