lora_e5_bsp/
lib.rs

1//! seeed LoRa-E5 development kit board support package.
2
3#![cfg_attr(not(test), no_std)]
4#![warn(missing_docs)]
5
6pub mod led;
7pub mod pb;
8
9pub use stm32wlxx_hal as hal;
10
11use hal::{
12    cortex_m::interrupt::CriticalSection,
13    gpio::{self, pins, Output, OutputArgs, PinState},
14};
15
16/// RF switch
17#[derive(Debug)]
18#[cfg_attr(feature = "defmt", derive(defmt::Format))]
19pub struct RfSwitch {
20    a4: Output<pins::A4>,
21    a5: Output<pins::A5>,
22}
23
24impl RfSwitch {
25    /// Create a new `RfSwitch` struct from GPIOs.
26    ///
27    /// # Example
28    ///
29    /// ```no_run
30    /// use lora_e5_bsp::{
31    ///     hal::{cortex_m, gpio::PortA, pac},
32    ///     RfSwitch,
33    /// };
34    ///
35    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
36    ///
37    /// let gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);
38    /// let rfs: RfSwitch = cortex_m::interrupt::free(|cs| RfSwitch::new(gpioa.a4, gpioa.a5, cs));
39    /// ```
40    pub fn new(a4: pins::A4, a5: pins::A5, cs: &CriticalSection) -> RfSwitch {
41        const ARGS: OutputArgs = OutputArgs {
42            speed: gpio::Speed::Fast,
43            level: gpio::PinState::High,
44            ot: gpio::OutputType::PushPull,
45            pull: gpio::Pull::None,
46        };
47        RfSwitch {
48            a4: Output::new(a4, &ARGS, cs),
49            a5: Output::new(a5, &ARGS, cs),
50        }
51    }
52
53    /// Steal the RF switch from whatever is currently using it.
54    ///
55    /// # Safety
56    ///
57    /// 1. Ensure that the code stealing the RF switch has exclusive access.
58    ///    Singleton checks are bypassed with this method.
59    /// 2. You must set up the RF switch pins.
60    ///    No setup will occur when using this method.
61    ///
62    /// # Example
63    ///
64    /// ```no_run
65    /// use lora_e5_bsp::{
66    ///     hal::{
67    ///         cortex_m,
68    ///         gpio::{pins, Output, PortA},
69    ///         pac,
70    ///     },
71    ///     RfSwitch,
72    /// };
73    ///
74    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
75    ///
76    /// let pa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);
77    /// cortex_m::interrupt::free(|cs| {
78    ///     let _: Output<pins::A4> = Output::default(pa.a4, cs);
79    ///     let _: Output<pins::A5> = Output::default(pa.a5, cs);
80    /// });
81    ///
82    /// // safety:
83    /// // 1. we have exclusive access to the underlying pins
84    /// // 2. the pins have been setup
85    /// let rfs: RfSwitch = unsafe { RfSwitch::steal() };
86    /// ```
87    #[inline]
88    pub unsafe fn steal() -> Self {
89        RfSwitch {
90            a4: Output::steal(),
91            a5: Output::steal(),
92        }
93    }
94
95    /// Set the RF switch to receive.
96    ///
97    /// # Example
98    ///
99    /// ```no_run
100    /// use lora_e5_bsp::{
101    ///     hal::{cortex_m, gpio::PortA, pac},
102    ///     RfSwitch,
103    /// };
104    ///
105    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
106    ///
107    /// let gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);
108    /// let mut rfs: RfSwitch = cortex_m::interrupt::free(|cs| RfSwitch::new(gpioa.a4, gpioa.a5, cs));
109    /// rfs.set_rx();
110    /// ```
111    #[inline]
112    pub fn set_rx(&mut self) {
113        self.a5.set_level(PinState::Low);
114        self.a4.set_level(PinState::High);
115    }
116
117    /// Set the RF switch to high power transmit.
118    ///
119    /// # Example
120    ///
121    /// ```no_run
122    /// use lora_e5_bsp::{
123    ///     hal::{cortex_m, gpio::PortA, pac},
124    ///     RfSwitch,
125    /// };
126    ///
127    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
128    ///
129    /// let gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);
130    /// let mut rfs: RfSwitch = cortex_m::interrupt::free(|cs| RfSwitch::new(gpioa.a4, gpioa.a5, cs));
131    /// rfs.set_tx_hp();
132    /// ```
133    #[inline]
134    pub fn set_tx_hp(&mut self) {
135        self.a4.set_level(PinState::Low);
136        self.a5.set_level(PinState::High);
137    }
138}