lora_phy/
mod_traits.rs

1use embedded_hal_async::delay::DelayNs;
2
3use crate::mod_params::*;
4
5/// Functions implemented for an embedded framework for an MCU/LoRa chip combination
6/// to allow this crate to control the LoRa chip.
7pub trait InterfaceVariant {
8    /// Reset the LoRa chip
9    async fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), RadioError>;
10    /// Wait for the LoRa chip to become available for an operation
11    async fn wait_on_busy(&mut self) -> Result<(), RadioError>;
12    /// Wait for the LoRa chip to indicate an event has occurred
13    async fn await_irq(&mut self) -> Result<(), RadioError>;
14    /// Enable an antenna used for receive operations, disabling other antennas
15    async fn enable_rf_switch_rx(&mut self) -> Result<(), RadioError>;
16    /// Enable an antenna used for send operations, disabling other antennas
17    async fn enable_rf_switch_tx(&mut self) -> Result<(), RadioError>;
18    /// Disable all antennas
19    async fn disable_rf_switch(&mut self) -> Result<(), RadioError>;
20}
21
22/// Specifies an IRQ processing state to run the loop to
23#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
24pub enum IrqState {
25    /// Runs the loop until after the preamble has been received
26    PreambleReceived,
27    /// Runs the loop until the operation is fully complete
28    Done,
29}
30
31/// Functions implemented for a specific kind of LoRa chip, called internally by the outward facing
32/// LoRa physical layer API
33pub trait RadioKind {
34    /// Initialize lora radio
35    async fn init_lora(&mut self, is_public_network: bool) -> Result<(), RadioError>;
36    /// Create modulation parameters specific to the LoRa chip kind and type
37    fn create_modulation_params(
38        &self,
39        spreading_factor: SpreadingFactor,
40        bandwidth: Bandwidth,
41        coding_rate: CodingRate,
42        frequency_in_hz: u32,
43    ) -> Result<ModulationParams, RadioError>;
44    /// Create packet parameters specific to the LoRa chip kind and type
45    fn create_packet_params(
46        &self,
47        preamble_length: u16,
48        implicit_header: bool,
49        payload_length: u8,
50        crc_on: bool,
51        iq_inverted: bool,
52        modulation_params: &ModulationParams,
53    ) -> Result<PacketParams, RadioError>;
54    /// Reset the loRa chip
55    async fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), RadioError>;
56    /// Ensure the LoRa chip is in the appropriate state to allow operation requests
57    async fn ensure_ready(&mut self, mode: RadioMode) -> Result<(), RadioError>;
58    /// Place the LoRa chip in standby mode
59    async fn set_standby(&mut self) -> Result<(), RadioError>;
60    /// Place the LoRa chip in power-saving mode
61    async fn set_sleep(&mut self, warm_start_if_possible: bool, delay: &mut impl DelayNs) -> Result<(), RadioError>;
62    /// Set the LoRa chip send and receive buffer base addresses
63    async fn set_tx_rx_buffer_base_address(
64        &mut self,
65        tx_base_addr: usize,
66        rx_base_addr: usize,
67    ) -> Result<(), RadioError>;
68    /// Perform any necessary LoRa chip power setup prior to a send operation
69    async fn set_tx_power_and_ramp_time(
70        &mut self,
71        output_power: i32,
72        mdltn_params: Option<&ModulationParams>,
73        is_tx_prep: bool,
74    ) -> Result<(), RadioError>;
75    /// Set the LoRa chip modulation parameters prior to using a communication channel
76    async fn set_modulation_params(&mut self, mdltn_params: &ModulationParams) -> Result<(), RadioError>;
77    /// Set the LoRa chip packet parameters prior to sending or receiving packets
78    async fn set_packet_params(&mut self, pkt_params: &PacketParams) -> Result<(), RadioError>;
79    /// Set the LoRa chip to support a given communication channel frequency
80    async fn calibrate_image(&mut self, frequency_in_hz: u32) -> Result<(), RadioError>;
81    /// Set the frequency for a communication channel
82    async fn set_channel(&mut self, frequency_in_hz: u32) -> Result<(), RadioError>;
83    /// Set a payload for a subsequent send operation
84    async fn set_payload(&mut self, payload: &[u8]) -> Result<(), RadioError>;
85    /// Perform a transmit operation
86    async fn do_tx(&mut self) -> Result<(), RadioError>;
87    /// Set up to perform a receive operation (single-shot, continuous, or duty cycle)
88    async fn do_rx(&mut self, rx_mode: RxMode) -> Result<(), RadioError>;
89    /// Get an available packet made available as the result of a receive operation
90    async fn get_rx_payload(
91        &mut self,
92        rx_pkt_params: &PacketParams,
93        receiving_buffer: &mut [u8],
94    ) -> Result<u8, RadioError>;
95    /// Get the RSSI and SNR for the packet made available as the result of a receive operation
96    async fn get_rx_packet_status(&mut self) -> Result<PacketStatus, RadioError>;
97    /// Perform a channel activity detection operation
98    async fn do_cad(&mut self, mdltn_params: &ModulationParams) -> Result<(), RadioError>;
99    /// Set the LoRa chip to provide notification of specific events based on radio state
100    async fn set_irq_params(&mut self, radio_mode: Option<RadioMode>) -> Result<(), RadioError>;
101    /// Set the LoRa chip into the TxContinuousWave mode
102    async fn set_tx_continuous_wave_mode(&mut self) -> Result<(), RadioError>;
103
104    /// Await for an IRQ event. This is droppable and thus safe to use in a select branch.
105    async fn await_irq(&mut self) -> Result<(), RadioError>;
106    /// Process LoRa radio IRQs
107    async fn process_irq_event(
108        &mut self,
109        radio_mode: RadioMode,
110        cad_activity_detected: Option<&mut bool>,
111        clear_interrupts: bool,
112    ) -> Result<Option<IrqState>, RadioError>;
113}