Skip to main content

nucleo_wl55jc_bsp/
lib.rs

1//! NUCLEO-WL55JC 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    fe_ctrl1: Output<pins::C4>,
21    fe_ctrl2: Output<pins::C5>,
22    fe_ctrl3: Output<pins::C3>,
23}
24
25impl RfSwitch {
26    /// Create a new `RfSwitch` struct from GPIOs.
27    ///
28    /// # Example
29    ///
30    /// ```no_run
31    /// use nucleo_wl55jc_bsp::{
32    ///     hal::{cortex_m, gpio::PortC, pac},
33    ///     RfSwitch,
34    /// };
35    ///
36    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
37    ///
38    /// let gpioc: PortC = PortC::split(dp.GPIOC, &mut dp.RCC);
39    /// let rfs: RfSwitch =
40    ///     cortex_m::interrupt::free(|cs| RfSwitch::new(gpioc.c3, gpioc.c4, gpioc.c5, cs));
41    /// ```
42    pub fn new(c3: pins::C3, c4: pins::C4, c5: pins::C5, cs: &CriticalSection) -> RfSwitch {
43        const ARGS: OutputArgs = OutputArgs {
44            speed: gpio::Speed::Fast,
45            level: gpio::PinState::High,
46            ot: gpio::OutputType::PushPull,
47            pull: gpio::Pull::None,
48        };
49        RfSwitch {
50            fe_ctrl1: Output::new(c4, &ARGS, cs),
51            fe_ctrl2: Output::new(c5, &ARGS, cs),
52            fe_ctrl3: Output::new(c3, &ARGS, cs),
53        }
54    }
55
56    /// Steal the RF switch from whatever is currently using it.
57    ///
58    /// # Safety
59    ///
60    /// 1. Ensure that the code stealing the RF switch has exclusive access.
61    ///    Singleton checks are bypassed with this method.
62    /// 2. You must set up the RF switch pins.
63    ///    No setup will occur when using this method.
64    ///
65    /// # Example
66    ///
67    /// ```no_run
68    /// use nucleo_wl55jc_bsp::{
69    ///     hal::{
70    ///         cortex_m,
71    ///         gpio::{pins, Output, PortC},
72    ///         pac,
73    ///     },
74    ///     RfSwitch,
75    /// };
76    ///
77    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
78    ///
79    /// let pc: PortC = PortC::split(dp.GPIOC, &mut dp.RCC);
80    /// cortex_m::interrupt::free(|cs| {
81    ///     let _: Output<pins::C3> = Output::default(pc.c3, cs);
82    ///     let _: Output<pins::C4> = Output::default(pc.c4, cs);
83    ///     let _: Output<pins::C5> = Output::default(pc.c5, cs);
84    /// });
85    ///
86    /// // safety:
87    /// // 1. we have exclusive access to the underlying pins
88    /// // 2. the pins have been setup
89    /// let rfs: RfSwitch = unsafe { RfSwitch::steal() };
90    /// ```
91    #[inline]
92    pub unsafe fn steal() -> Self {
93        RfSwitch {
94            fe_ctrl1: Output::steal(),
95            fe_ctrl2: Output::steal(),
96            fe_ctrl3: Output::steal(),
97        }
98    }
99
100    /// Set the RF switch to receive.
101    ///
102    /// # Example
103    ///
104    /// ```no_run
105    /// use nucleo_wl55jc_bsp::{
106    ///     hal::{cortex_m, gpio::PortC, pac},
107    ///     RfSwitch,
108    /// };
109    ///
110    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
111    ///
112    /// let gpioc: PortC = PortC::split(dp.GPIOC, &mut dp.RCC);
113    /// let mut rfs: RfSwitch =
114    ///     cortex_m::interrupt::free(|cs| RfSwitch::new(gpioc.c3, gpioc.c4, gpioc.c5, cs));
115    /// rfs.set_rx()
116    /// ```
117    #[inline]
118    pub fn set_rx(&mut self) {
119        self.fe_ctrl1.set_level(PinState::High);
120        self.fe_ctrl2.set_level(PinState::Low);
121        self.fe_ctrl3.set_level(PinState::High);
122    }
123
124    /// Set the RF switch to low power transmit.
125    ///
126    /// # Example
127    ///
128    /// ```no_run
129    /// use nucleo_wl55jc_bsp::{
130    ///     hal::{cortex_m, gpio::PortC, pac},
131    ///     RfSwitch,
132    /// };
133    ///
134    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
135    ///
136    /// let gpioc: PortC = PortC::split(dp.GPIOC, &mut dp.RCC);
137    /// let mut rfs: RfSwitch =
138    ///     cortex_m::interrupt::free(|cs| RfSwitch::new(gpioc.c3, gpioc.c4, gpioc.c5, cs));
139    /// rfs.set_tx_lp()
140    /// ```
141    #[inline]
142    pub fn set_tx_lp(&mut self) {
143        self.fe_ctrl1.set_level(PinState::High);
144        self.fe_ctrl2.set_level(PinState::High);
145        self.fe_ctrl3.set_level(PinState::High);
146    }
147
148    /// Set the RF switch to high power transmit.
149    ///
150    /// # Example
151    ///
152    /// ```no_run
153    /// use nucleo_wl55jc_bsp::{
154    ///     hal::{cortex_m, gpio::PortC, pac},
155    ///     RfSwitch,
156    /// };
157    ///
158    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
159    ///
160    /// let gpioc: PortC = PortC::split(dp.GPIOC, &mut dp.RCC);
161    /// let mut rfs: RfSwitch =
162    ///     cortex_m::interrupt::free(|cs| RfSwitch::new(gpioc.c3, gpioc.c4, gpioc.c5, cs));
163    /// rfs.set_tx_hp()
164    /// ```
165    #[inline]
166    pub fn set_tx_hp(&mut self) {
167        self.fe_ctrl2.set_level(PinState::High);
168        self.fe_ctrl1.set_level(PinState::Low);
169        self.fe_ctrl3.set_level(PinState::High);
170    }
171}