sx128x/config/
lora.rs

1use super::*;
2
3/// LoRa spreading factor
4#[derive(Copy, Clone, Default, PartialEq, Debug)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub enum LoRaSpreadingFactor {
7    Sf5 = 0x50,
8    Sf6 = 0x60,
9    Sf7 = 0x70,
10    #[default]
11    Sf8 = 0x80,
12    Sf9 = 0x90,
13    Sf10 = 0xA0,
14    Sf11 = 0xB0,
15    Sf12 = 0xC0,
16}
17
18/// LoRa bandwidth
19#[derive(Copy, Clone, Default, PartialEq, Debug)]
20#[cfg_attr(feature = "defmt", derive(defmt::Format))]
21pub enum LoRaBandwidth {
22    #[default]
23    Bw200kHz = 0x34,
24    Bw400kHz = 0x26,
25    Bw800kHz = 0x18,
26    Bw1600kHz = 0x0A,
27}
28
29impl LoRaBandwidth {
30    /// Bandwidth in MHz
31    pub fn mhz(&self) -> f32 {
32        match self {
33            LoRaBandwidth::Bw200kHz => 0.203125,
34            LoRaBandwidth::Bw400kHz => 0.40625,
35            LoRaBandwidth::Bw800kHz => 0.8125,
36            LoRaBandwidth::Bw1600kHz => 1.625,
37        }
38    }
39}
40
41/// LoRa coding rate
42#[derive(Copy, Clone, Default, PartialEq, Debug)]
43#[cfg_attr(feature = "defmt", derive(defmt::Format))]
44pub enum LoRaCodingRate {
45    #[default]
46    Cr4_5 = 0x01,
47    Cr4_6 = 0x02,
48    Cr4_7 = 0x03,
49    Cr4_8 = 0x04,
50    CrLi4_5 = 0x05,
51    CrLi4_6 = 0x06,
52    CrLi4_7 = 0x07,
53}
54
55/// LoRa modulation params
56#[derive(Copy, Clone, PartialEq, Default, Debug)]
57#[cfg_attr(feature = "defmt", derive(defmt::Format))]
58pub struct LoRaModulationParams {
59    pub spreading_factor: LoRaSpreadingFactor,
60    pub bandwidth: LoRaBandwidth,
61    pub coding_rate: LoRaCodingRate,
62}
63
64impl LoRaModulationParams {
65    pub(crate) fn as_bytes(&self) -> [u8; 3] {
66        [
67            self.spreading_factor as u8,
68            self.bandwidth as u8,
69            self.coding_rate as u8,
70        ]
71    }
72}
73
74/// LoRa CRC mode
75#[derive(Copy, Clone, Default, PartialEq, Debug)]
76#[cfg_attr(feature = "defmt", derive(defmt::Format))]
77pub enum LoRaCrc {
78    #[default]
79    Enabled = 0x20,
80    Disabled = 0x00,
81}
82
83/// LoRa IQ mod
84#[derive(Copy, Clone, Default, PartialEq, Debug)]
85#[cfg_attr(feature = "defmt", derive(defmt::Format))]
86pub enum LoRaIq {
87    #[default]
88    Normal = 0x40,
89    Inverted = 0x00,
90}
91
92/// LoRa header type
93#[derive(Copy, Clone, Default, PartialEq, Debug)]
94#[cfg_attr(feature = "defmt", derive(defmt::Format))]
95pub enum LoRaHeader {
96    #[default]
97    Explicit = 0x00,
98    Implicit = 0x80,
99}
100
101/// LoRa packet params
102#[derive(Copy, Clone, Default, PartialEq, Debug)]
103#[cfg_attr(feature = "defmt", derive(defmt::Format))]
104pub struct LoRaPacketParams {
105    pub preamble_length: LoRaPreambleLength,
106    pub header_type: LoRaHeader,
107    pub payload_length: u8,
108    pub crc_mode: LoRaCrc,
109    pub invert_iq: LoRaIq,
110    pub sync_word: u8,
111}
112
113/// LoRa preamble length
114#[derive(Copy, Clone, PartialEq, Debug)]
115#[cfg_attr(feature = "defmt", derive(defmt::Format))]
116pub struct LoRaPreambleLength {
117    pub mantissa: u8,
118    pub exponenta: u8,
119}
120
121impl LoRaPreambleLength {
122    pub fn value(&self) -> u8 {
123        self.mantissa & 0x0f | (self.exponenta << 4)
124    }
125}
126
127impl Default for LoRaPreambleLength {
128    fn default() -> Self {
129        Self {
130            mantissa: 1,
131            exponenta: 3,
132        }
133    }
134}
135
136/// Ranging result type
137#[derive(Copy, Clone, Default, PartialEq, Debug)]
138#[cfg_attr(feature = "defmt", derive(defmt::Format))]
139pub enum RangingResultType {
140    #[default]
141    Raw = 0x00,
142    AverageRSSI = 0x10,
143}
144
145/// Ranging address bits
146#[derive(Copy, Clone, Default, PartialEq, Debug)]
147#[cfg_attr(feature = "defmt", derive(defmt::Format))]
148pub enum RangingAddressBits {
149    #[default]
150    Bits8 = 0x00,
151    Bits16 = 0x01,
152    Bits24 = 0x02,
153    Bits32 = 0x03,
154}
155
156impl LoRaPacketParams {
157    pub(crate) fn as_bytes(&self) -> [u8; 7] {
158        [
159            self.preamble_length.value(),
160            self.header_type as u8,
161            self.payload_length,
162            self.crc_mode as u8,
163            self.invert_iq as u8,
164            0x00,
165            0x00,
166        ]
167    }
168}
169
170/// LoRa modem params
171#[derive(Copy, Clone, Default, PartialEq, Debug)]
172#[cfg_attr(feature = "defmt", derive(defmt::Format))]
173pub struct LoRaModem {
174    pub frequency: Frequency,
175    pub tx_params: TxParams,
176    pub modulation_params: LoRaModulationParams,
177    pub packet_params: LoRaPacketParams,
178}
179
180/// Ranging modem params
181#[derive(Copy, Clone, Default, PartialEq, Debug)]
182#[cfg_attr(feature = "defmt", derive(defmt::Format))]
183pub struct RangingModem {
184    pub lora: LoRaModem,
185    pub role: RangingRole,
186    pub calibration: u32,
187    pub address: u32,
188    pub address_bits: RangingAddressBits,
189}