aboutsummaryrefslogtreecommitdiff
path: root/embassy-lora/src/iv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-lora/src/iv.rs')
-rw-r--r--embassy-lora/src/iv.rs317
1 files changed, 0 insertions, 317 deletions
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 @@
1#[cfg(feature = "stm32wl")]
2use embassy_stm32::interrupt;
3#[cfg(feature = "stm32wl")]
4use embassy_stm32::interrupt::InterruptExt;
5#[cfg(feature = "stm32wl")]
6use embassy_stm32::pac;
7#[cfg(feature = "stm32wl")]
8use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
9#[cfg(feature = "stm32wl")]
10use embassy_sync::signal::Signal;
11use embedded_hal::digital::v2::OutputPin;
12use embedded_hal_async::delay::DelayUs;
13use embedded_hal_async::digital::Wait;
14use lora_phy::mod_params::RadioError::*;
15use lora_phy::mod_params::{BoardType, RadioError};
16use lora_phy::mod_traits::InterfaceVariant;
17
18/// Interrupt handler.
19#[cfg(feature = "stm32wl")]
20pub struct InterruptHandler {}
21
22#[cfg(feature = "stm32wl")]
23impl interrupt::typelevel::Handler<interrupt::typelevel::SUBGHZ_RADIO> for InterruptHandler {
24 unsafe fn on_interrupt() {
25 interrupt::SUBGHZ_RADIO.disable();
26 IRQ_SIGNAL.signal(());
27 }
28}
29
30#[cfg(feature = "stm32wl")]
31static IRQ_SIGNAL: Signal<CriticalSectionRawMutex, ()> = Signal::new();
32
33#[cfg(feature = "stm32wl")]
34/// Base for the InterfaceVariant implementation for an stm32wl/sx1262 combination
35pub struct Stm32wlInterfaceVariant<CTRL> {
36 board_type: BoardType,
37 rf_switch_rx: Option<CTRL>,
38 rf_switch_tx: Option<CTRL>,
39}
40
41#[cfg(feature = "stm32wl")]
42impl<'a, CTRL> Stm32wlInterfaceVariant<CTRL>
43where
44 CTRL: OutputPin,
45{
46 /// Create an InterfaceVariant instance for an stm32wl/sx1262 combination
47 pub fn new(
48 _irq: impl interrupt::typelevel::Binding<interrupt::typelevel::SUBGHZ_RADIO, InterruptHandler>,
49 rf_switch_rx: Option<CTRL>,
50 rf_switch_tx: Option<CTRL>,
51 ) -> Result<Self, RadioError> {
52 interrupt::SUBGHZ_RADIO.disable();
53 Ok(Self {
54 board_type: BoardType::Stm32wlSx1262, // updated when associated with a specific LoRa board
55 rf_switch_rx,
56 rf_switch_tx,
57 })
58 }
59}
60
61#[cfg(feature = "stm32wl")]
62impl<CTRL> InterfaceVariant for Stm32wlInterfaceVariant<CTRL>
63where
64 CTRL: OutputPin,
65{
66 fn set_board_type(&mut self, board_type: BoardType) {
67 self.board_type = board_type;
68 }
69 async fn set_nss_low(&mut self) -> Result<(), RadioError> {
70 pac::PWR.subghzspicr().modify(|w| w.set_nss(false));
71 Ok(())
72 }
73 async fn set_nss_high(&mut self) -> Result<(), RadioError> {
74 pac::PWR.subghzspicr().modify(|w| w.set_nss(true));
75 Ok(())
76 }
77 async fn reset(&mut self, _delay: &mut impl DelayUs) -> Result<(), RadioError> {
78 pac::RCC.csr().modify(|w| w.set_rfrst(true));
79 pac::RCC.csr().modify(|w| w.set_rfrst(false));
80 Ok(())
81 }
82 async fn wait_on_busy(&mut self) -> Result<(), RadioError> {
83 while pac::PWR.sr2().read().rfbusys() {}
84 Ok(())
85 }
86
87 async fn await_irq(&mut self) -> Result<(), RadioError> {
88 unsafe { interrupt::SUBGHZ_RADIO.enable() };
89 IRQ_SIGNAL.wait().await;
90 Ok(())
91 }
92
93 async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> {
94 match &mut self.rf_switch_tx {
95 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?,
96 None => (),
97 };
98 match &mut self.rf_switch_rx {
99 Some(pin) => pin.set_high().map_err(|_| RfSwitchRx),
100 None => Ok(()),
101 }
102 }
103 async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> {
104 match &mut self.rf_switch_rx {
105 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
106 None => (),
107 };
108 match &mut self.rf_switch_tx {
109 Some(pin) => pin.set_high().map_err(|_| RfSwitchTx),
110 None => Ok(()),
111 }
112 }
113 async fn disable_rf_switch(&mut self) -> Result<(), RadioError> {
114 match &mut self.rf_switch_rx {
115 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
116 None => (),
117 };
118 match &mut self.rf_switch_tx {
119 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx),
120 None => Ok(()),
121 }
122 }
123}
124
125/// Base for the InterfaceVariant implementation for an stm32l0/sx1276 combination
126pub struct Stm32l0InterfaceVariant<CTRL, WAIT> {
127 board_type: BoardType,
128 nss: CTRL,
129 reset: CTRL,
130 irq: WAIT,
131 rf_switch_rx: Option<CTRL>,
132 rf_switch_tx: Option<CTRL>,
133}
134
135impl<CTRL, WAIT> Stm32l0InterfaceVariant<CTRL, WAIT>
136where
137 CTRL: OutputPin,
138 WAIT: Wait,
139{
140 /// Create an InterfaceVariant instance for an stm32l0/sx1276 combination
141 pub fn new(
142 nss: CTRL,
143 reset: CTRL,
144 irq: WAIT,
145 rf_switch_rx: Option<CTRL>,
146 rf_switch_tx: Option<CTRL>,
147 ) -> Result<Self, RadioError> {
148 Ok(Self {
149 board_type: BoardType::Stm32l0Sx1276, // updated when associated with a specific LoRa board
150 nss,
151 reset,
152 irq,
153 rf_switch_rx,
154 rf_switch_tx,
155 })
156 }
157}
158
159impl<CTRL, WAIT> InterfaceVariant for Stm32l0InterfaceVariant<CTRL, WAIT>
160where
161 CTRL: OutputPin,
162 WAIT: Wait,
163{
164 fn set_board_type(&mut self, board_type: BoardType) {
165 self.board_type = board_type;
166 }
167 async fn set_nss_low(&mut self) -> Result<(), RadioError> {
168 self.nss.set_low().map_err(|_| NSS)
169 }
170 async fn set_nss_high(&mut self) -> Result<(), RadioError> {
171 self.nss.set_high().map_err(|_| NSS)
172 }
173 async fn reset(&mut self, delay: &mut impl DelayUs) -> Result<(), RadioError> {
174 delay.delay_ms(10).await;
175 self.reset.set_low().map_err(|_| Reset)?;
176 delay.delay_ms(10).await;
177 self.reset.set_high().map_err(|_| Reset)?;
178 delay.delay_ms(10).await;
179 Ok(())
180 }
181 async fn wait_on_busy(&mut self) -> Result<(), RadioError> {
182 Ok(())
183 }
184 async fn await_irq(&mut self) -> Result<(), RadioError> {
185 self.irq.wait_for_high().await.map_err(|_| Irq)
186 }
187
188 async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> {
189 match &mut self.rf_switch_tx {
190 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?,
191 None => (),
192 };
193 match &mut self.rf_switch_rx {
194 Some(pin) => pin.set_high().map_err(|_| RfSwitchRx),
195 None => Ok(()),
196 }
197 }
198 async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> {
199 match &mut self.rf_switch_rx {
200 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
201 None => (),
202 };
203 match &mut self.rf_switch_tx {
204 Some(pin) => pin.set_high().map_err(|_| RfSwitchTx),
205 None => Ok(()),
206 }
207 }
208 async fn disable_rf_switch(&mut self) -> Result<(), RadioError> {
209 match &mut self.rf_switch_rx {
210 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
211 None => (),
212 };
213 match &mut self.rf_switch_tx {
214 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx),
215 None => Ok(()),
216 }
217 }
218}
219
220/// Base for the InterfaceVariant implementation for a generic Sx126x LoRa board
221pub struct GenericSx126xInterfaceVariant<CTRL, WAIT> {
222 board_type: BoardType,
223 nss: CTRL,
224 reset: CTRL,
225 dio1: WAIT,
226 busy: WAIT,
227 rf_switch_rx: Option<CTRL>,
228 rf_switch_tx: Option<CTRL>,
229}
230
231impl<CTRL, WAIT> GenericSx126xInterfaceVariant<CTRL, WAIT>
232where
233 CTRL: OutputPin,
234 WAIT: Wait,
235{
236 /// Create an InterfaceVariant instance for an nrf52840/sx1262 combination
237 pub fn new(
238 nss: CTRL,
239 reset: CTRL,
240 dio1: WAIT,
241 busy: WAIT,
242 rf_switch_rx: Option<CTRL>,
243 rf_switch_tx: Option<CTRL>,
244 ) -> Result<Self, RadioError> {
245 Ok(Self {
246 board_type: BoardType::Rak4631Sx1262, // updated when associated with a specific LoRa board
247 nss,
248 reset,
249 dio1,
250 busy,
251 rf_switch_rx,
252 rf_switch_tx,
253 })
254 }
255}
256
257impl<CTRL, WAIT> InterfaceVariant for GenericSx126xInterfaceVariant<CTRL, WAIT>
258where
259 CTRL: OutputPin,
260 WAIT: Wait,
261{
262 fn set_board_type(&mut self, board_type: BoardType) {
263 self.board_type = board_type;
264 }
265 async fn set_nss_low(&mut self) -> Result<(), RadioError> {
266 self.nss.set_low().map_err(|_| NSS)
267 }
268 async fn set_nss_high(&mut self) -> Result<(), RadioError> {
269 self.nss.set_high().map_err(|_| NSS)
270 }
271 async fn reset(&mut self, delay: &mut impl DelayUs) -> Result<(), RadioError> {
272 delay.delay_ms(10).await;
273 self.reset.set_low().map_err(|_| Reset)?;
274 delay.delay_ms(20).await;
275 self.reset.set_high().map_err(|_| Reset)?;
276 delay.delay_ms(10).await;
277 Ok(())
278 }
279 async fn wait_on_busy(&mut self) -> Result<(), RadioError> {
280 self.busy.wait_for_low().await.map_err(|_| Busy)
281 }
282 async fn await_irq(&mut self) -> Result<(), RadioError> {
283 self.dio1.wait_for_high().await.map_err(|_| DIO1)?;
284 Ok(())
285 }
286
287 async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError> {
288 match &mut self.rf_switch_tx {
289 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx)?,
290 None => (),
291 };
292 match &mut self.rf_switch_rx {
293 Some(pin) => pin.set_high().map_err(|_| RfSwitchRx),
294 None => Ok(()),
295 }
296 }
297 async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError> {
298 match &mut self.rf_switch_rx {
299 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
300 None => (),
301 };
302 match &mut self.rf_switch_tx {
303 Some(pin) => pin.set_high().map_err(|_| RfSwitchTx),
304 None => Ok(()),
305 }
306 }
307 async fn disable_rf_switch(&mut self) -> Result<(), RadioError> {
308 match &mut self.rf_switch_rx {
309 Some(pin) => pin.set_low().map_err(|_| RfSwitchRx)?,
310 None => (),
311 };
312 match &mut self.rf_switch_tx {
313 Some(pin) => pin.set_low().map_err(|_| RfSwitchTx),
314 None => Ok(()),
315 }
316 }
317}