Mbed OS Reference
Loading...
Searching...
No Matches
SX126X_LoRaRadio.h
1/**
2 / _____) _ | |
3( (____ _____ ____ _| |_ _____ ____| |__
4 \____ \| ___ | (_ _) ___ |/ ___) _ \
5 _____) ) ____| | | || |_| ____( (___| | | |
6(______/|_____)_|_|_| \__)_____)\____)_| |_|
7 (C)2015 Semtech
8 ___ _____ _ ___ _ _____ ___ ___ ___ ___
9/ __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
10\__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
11|___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
12embedded.connectivity.solutions===============
13
14Description: LoRaWAN stack layer that controls both MAC and PHY underneath
15
16License: Revised BSD License, see LICENSE.TXT file include in the project
17
18Maintainer: Miguel Luis, Gregory Cristian & Gilbert Menth
19
20Copyright (c) 2019, Arm Limited and affiliates.
21
22SPDX-License-Identifier: BSD-3-Clause
23*/
24
25#ifndef MBED_LORA_RADIO_DRV_SX126X_LORARADIO_H_
26#define MBED_LORA_RADIO_DRV_SX126X_LORARADIO_H_
27
28#if DEVICE_SPI
29
30#include "mbed_critical.h"
31#include "PinNames.h"
32#include "InterruptIn.h"
33#include "DigitalOut.h"
34#include "DigitalInOut.h"
35#include "DigitalIn.h"
36#include "AnalogIn.h"
37#include "SPI.h"
38#include "rtos/Mutex.h"
39#ifdef MBED_CONF_RTOS_PRESENT
40#include "rtos/Thread.h"
41#include "rtos/ThisThread.h"
42#endif
43#include "sx126x_ds.h"
44#include "lorawan/LoRaRadio.h"
45
46#ifdef MBED_CONF_SX126X_LORA_DRIVER_BUFFER_SIZE
47#define MAX_DATA_BUFFER_SIZE_SX126X MBED_CONF_SX126X_LORA_DRIVER_BUFFER_SIZE
48#else
49#define MAX_DATA_BUFFER_SIZE_SX126X 255
50#endif
51
53
54public:
55 SX126X_LoRaRadio(PinName mosi,
56 PinName miso,
57 PinName sclk,
58 PinName nss,
59 PinName reset,
60 PinName dio1,
61 PinName busy,
62 PinName freq_select,
63 PinName device_select,
64 PinName crystal_select,
65 PinName ant_switch);
66
67 virtual ~SX126X_LoRaRadio();
68
69 /**
70 * Registers radio events with the Mbed LoRaWAN stack and
71 * undergoes initialization steps if any
72 *
73 * @param events Structure containing the driver callback functions
74 */
75 virtual void init_radio(radio_events_t *events);
76
77 /**
78 * Resets the radio module
79 */
80 virtual void radio_reset();
81
82 /**
83 * Put the RF module in sleep mode
84 */
85 virtual void sleep(void);
86
87 /**
88 * Sets the radio in standby mode
89 */
90 virtual void standby(void);
91
92 void set_rx_config(radio_modems_t modem, uint32_t bandwidth,
93 uint32_t datarate, uint8_t coderate,
94 uint32_t bandwidth_afc, uint16_t preamble_len,
95 uint16_t symb_timeout, bool fix_len,
96 uint8_t payload_len,
97 bool crc_on, bool freq_hop_on, uint8_t hop_period,
98 bool iq_inverted, bool rx_continuous) override;
99
100 /**
101 * Sets the transmission parameters
102 *
103 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
104 * @param power Sets the output power [dBm]
105 * @param fdev Sets the frequency deviation ( FSK only )
106 * FSK : [Hz]
107 * LoRa: 0
108 * @param bandwidth Sets the bandwidth ( LoRa only )
109 * FSK : 0
110 * LoRa: [0: 125 kHz, 1: 250 kHz,
111 * 2: 500 kHz, 3: Reserved]
112 * @param datarate Sets the Datarate
113 * FSK : 600..300000 bits/s
114 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
115 * 10: 1024, 11: 2048, 12: 4096 chips]
116 * @param coderate Sets the coding rate ( LoRa only )
117 * FSK : N/A ( set to 0 )
118 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
119 * @param preamble_len Sets the preamble length
120 * @param fix_len Fixed length packets [0: variable, 1: fixed]
121 * @param crc_on Enables disables the CRC [0: OFF, 1: ON]
122 * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
123 * @param hop_period Number of symbols bewteen each hop (LoRa only)
124 * @param iq_inverted Inverts IQ signals ( LoRa only )
125 * FSK : N/A ( set to 0 )
126 * LoRa: [0: not inverted, 1: inverted]
127 * @param timeout Transmission timeout [ms]
128 */
129 virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
130 uint32_t bandwidth, uint32_t datarate,
131 uint8_t coderate, uint16_t preamble_len,
132 bool fix_len, bool crc_on, bool freq_hop_on,
133 uint8_t hop_period, bool iq_inverted, uint32_t timeout);
134
135 /**
136 * Sends the buffer of size
137 *
138 * Prepares the packet to be sent and sets the radio in transmission
139 *
140 * @param buffer Buffer pointer
141 * @param size Buffer size
142 */
143 virtual void send(uint8_t *buffer, uint8_t size);
144
145 /**
146 * Sets the radio to receive
147 *
148 * All necessary configuration options for reception are set in
149 * 'set_rx_config(parameters)' API.
150 */
151 virtual void receive(void);
152
153 /**
154 * Sets the carrier frequency
155 *
156 * @param freq Channel RF frequency
157 */
158 virtual void set_channel(uint32_t freq);
159
160 /**
161 * Generates a 32 bits random value based on the RSSI readings
162 *
163 * Remark this function sets the radio in LoRa modem mode and disables
164 * all interrupts.
165 * After calling this function either Radio.SetRxConfig or
166 * Radio.SetTxConfig functions must be called.
167 *
168 * @return 32 bits random value
169 */
170 virtual uint32_t random(void);
171
172 radio_state_t get_status(void) override;
173
174 /**
175 * Sets the maximum payload length
176 *
177 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
178 * @param max Maximum payload length in bytes
179 */
180 virtual void set_max_payload_length(radio_modems_t modem, uint8_t max);
181
182 /**
183 * Sets the network to public or private
184 *
185 * Updates the sync byte. Applies to LoRa modem only
186 *
187 * @param enable if true, it enables a public network
188 */
189 virtual void set_public_network(bool enable);
190
191 /**
192 * Computes the packet time on air for the given payload
193 *
194 * Remark can only be called once SetRxConfig or SetTxConfig have been called
195 *
196 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
197 * @param pkt_len Packet payload length
198 * @return Computed airTime for the given packet payload length
199 */
200 virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len);
201
202 /**
203 * Perform carrier sensing
204 *
205 * Checks for a certain time if the RSSI is above a given threshold.
206 * This threshold determines if there is already a transmission going on
207 * in the channel or not.
208 *
209 * @param modem Type of the radio modem
210 * @param freq Carrier frequency
211 * @param rssi_threshold Threshold value of RSSI
212 * @param max_carrier_sense_time time to sense the channel
213 *
214 * @return true if there is no active transmission
215 * in the channel, false otherwise
216 */
218 uint32_t freq,
219 int16_t rssi_threshold,
220 uint32_t max_carrier_sense_time);
221
222 /**
223 * Sets the radio in CAD mode
224 *
225 */
226 virtual void start_cad(void);
227
228 /**
229 * Check if the given RF is in range
230 *
231 * @param frequency frequency needed to be checked
232 */
233 virtual bool check_rf_frequency(uint32_t frequency);
234
235 /** Sets the radio in continuous wave transmission mode
236 *
237 * @param freq Channel RF frequency
238 * @param power Sets the output power [dBm]
239 * @param time Transmission mode timeout [s]
240 */
241 virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time);
242
243 /**
244 * Acquire exclusive access
245 */
246 virtual void lock(void);
247
248 /**
249 * Release exclusive access
250 */
251 virtual void unlock(void);
252
253private:
254
255 // SPI and chip select control
256 mbed::SPI _spi;
257 mbed::DigitalOut _chip_select;
258
259 // module rest control
260 mbed::DigitalInOut _reset_ctl;
261
262 // Interrupt controls
263 mbed::InterruptIn _dio1_ctl;;
264
265 // module busy control
266 mbed::DigitalIn _busy;
267
268// module frequency selection
269#if MBED_CONF_SX126X_LORA_DRIVER_FREQ_SUPPORT == -1
270 mbed::AnalogIn _freq_select;
271#else
272 mbed::DigitalIn _freq_select; // AnalogIn errors if NC
273#endif
274
275// module device variant selection
276#if MBED_CONF_SX126X_LORA_DRIVER_DEVICE_VARIANT == -1
277 mbed::AnalogIn _dev_select;
278#else
279 mbed::DigitalIn _dev_select; // AnalogIn errors if NC
280#endif
281
282 // module TCXO/XTAL control
283 mbed::DigitalIn _crystal_select;
284
285 // Radio specific controls (TX/RX duplexer switch control)
286 mbed::DigitalInOut _ant_switch;
287
288 // Structure containing function pointers to the stack callbacks
289 radio_events_t *_radio_events;
290
291 // Data buffer used for both TX and RX
292 // Size of this buffer is configurable via Mbed config system
293 // Default is 255 bytes
294 uint8_t _data_buffer[MAX_DATA_BUFFER_SIZE_SX126X];
295
296#ifdef MBED_CONF_RTOS_PRESENT
297 // Thread to handle interrupts
298 rtos::Thread irq_thread;
299#endif
300
301 // Access protection
302 rtos::Mutex mutex;
303
304 // helper functions
305 void wakeup();
306 void read_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
307 void write_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
308 void set_dio2_as_rfswitch_ctrl(uint8_t enable);
309 void set_dio3_as_tcxo_ctrl(radio_TCXO_ctrl_voltage_t voltage, uint32_t timeout);
310 uint8_t get_device_variant(void);
311 void set_device_ready(void);
312 int8_t get_rssi();
313 uint8_t get_fsk_bw_reg_val(uint32_t bandwidth);
314 void write_to_register(uint16_t addr, uint8_t data);
315 void write_to_register(uint16_t addr, uint8_t *data, uint8_t size);
316 uint8_t read_register(uint16_t addr);
317 void read_register(uint16_t addr, uint8_t *buffer, uint8_t size);
318 void write_fifo(uint8_t *buffer, uint8_t size);
319 void read_fifo(uint8_t *buffer, uint8_t size, uint8_t offset);
320 void rf_irq_task(void);
321 void set_modem(uint8_t modem);
322 uint8_t get_modem();
323 uint16_t get_irq_status(void);
324 uint8_t get_frequency_support(void);
325
326 // ISR
327 void dio1_irq_isr();
328
329 // Handler called by thread in response to signal
330 void handle_dio1_irq();
331
332 void set_modulation_params(modulation_params_t *modulationParams);
333 void set_packet_params(packet_params_t *packet_params);
334 void set_cad_params(lora_cad_symbols_t nb_symbols, uint8_t det_peak,
335 uint8_t det_min, cad_exit_modes_t exit_mode,
336 uint32_t timeout);
337 void set_buffer_base_addr(uint8_t tx_base_addr, uint8_t rx_base_addr);
338 void get_rx_buffer_status(uint8_t *payload_len, uint8_t *rx_buffer_ptr);
339 void get_packet_status(packet_status_t *pkt_status);
340 radio_error_t get_device_errors(void);
341 void clear_device_errors(void);
342 void clear_irq_status(uint16_t irq);
343 void set_crc_seed(uint16_t seed);
344 void set_crc_polynomial(uint16_t polynomial);
345 void set_whitening_seed(uint16_t seed);
346 void set_pa_config(uint8_t pa_DC, uint8_t hp_max, uint8_t device_type,
347 uint8_t pa_LUT);
348 void set_tx_power(int8_t power);
349 void calibrate_image(uint32_t freq);
350 void configure_dio_irq(uint16_t irq_mask, uint16_t dio1_mask,
351 uint16_t dio2_mask, uint16_t dio3_mask);
352 void cold_start_wakeup();
353
354private:
355 uint8_t _active_modem;
356 uint8_t _standby_mode;
357 uint8_t _operation_mode;
358 uint8_t _reception_mode;
359 uint32_t _tx_timeout;
360 uint32_t _rx_timeout;
361 uint8_t _rx_timeout_in_symbols;
362 int8_t _tx_power;
363 bool _image_calibrated;
364 bool _force_image_calibration;
365 bool _network_mode_public;
366
367 // Structure containing all user and network specified settings
368 // for radio module
369 modulation_params_t _mod_params;
370 packet_params_t _packet_params;
371};
372
373#endif // DEVICE_SPI
374
375#endif /* MBED_LORA_RADIO_DRV_SX126X_LORARADIO_H_ */
Interface for the radios, containing the main functions that a radio needs, and five callback functio...
Definition: LoRaRadio.h:441
virtual bool check_rf_frequency(uint32_t frequency)
Check if the given RF is in range.
virtual void set_public_network(bool enable)
Sets the network to public or private.
void set_rx_config(radio_modems_t modem, uint32_t bandwidth, uint32_t datarate, uint8_t coderate, uint32_t bandwidth_afc, uint16_t preamble_len, uint16_t symb_timeout, bool fix_len, uint8_t payload_len, bool crc_on, bool freq_hop_on, uint8_t hop_period, bool iq_inverted, bool rx_continuous) override
Sets reception parameters.
virtual void set_channel(uint32_t freq)
Sets the carrier frequency.
virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len)
Computes the packet time on air for the given payload.
radio_state_t get_status(void) override
Gets the radio status.
virtual void unlock(void)
Release exclusive access.
virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, uint32_t bandwidth, uint32_t datarate, uint8_t coderate, uint16_t preamble_len, bool fix_len, bool crc_on, bool freq_hop_on, uint8_t hop_period, bool iq_inverted, uint32_t timeout)
Sets the transmission parameters.
virtual void receive(void)
Sets the radio to receive.
virtual void send(uint8_t *buffer, uint8_t size)
Sends the buffer of size.
virtual bool perform_carrier_sense(radio_modems_t modem, uint32_t freq, int16_t rssi_threshold, uint32_t max_carrier_sense_time)
Perform carrier sensing.
virtual void init_radio(radio_events_t *events)
Registers radio events with the Mbed LoRaWAN stack and undergoes initialization steps if any.
virtual void sleep(void)
Put the RF module in sleep mode.
virtual void standby(void)
Sets the radio in standby mode.
virtual void radio_reset()
Resets the radio module.
virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time)
Sets the radio in continuous wave transmission mode.
virtual uint32_t random(void)
Generates a 32 bits random value based on the RSSI readings.
virtual void lock(void)
Acquire exclusive access.
virtual void set_max_payload_length(radio_modems_t modem, uint8_t max)
Sets the maximum payload length.
virtual void start_cad(void)
Sets the radio in CAD mode.
An analog input, used for reading the voltage on a pin.
Definition: AnalogIn.h:68
A digital input, used for reading the state of a pin.
Definition: DigitalIn.h:60
A digital input/output, used for setting or reading a bi-directional pin.
Definition: DigitalInOut.h:40
A digital output, used for setting the state of a pin.
Definition: DigitalOut.h:55
A digital interrupt input, used to call a function on a rising or falling edge.
Definition: InterruptIn.h:65
An SPI Master, used for communicating with SPI slave devices.
Definition: SPI.h:263
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
The Thread class allow defining, creating, and controlling thread functions in the system.
Definition: Thread.h:92
enum radio_state radio_state_t
Radio driver internal state.
enum modem_type radio_modems_t
Type of modem.
The type describing the modulation parameters for every packet types.
Definition: sx126x_ds.h:557
The type describing the packet parameters for every packet types.
Definition: sx126x_ds.h:581
Represents the packet status for every packet type.
Definition: sx126x_ds.h:613
Reporting functions for upper layers.
Definition: LoRaRadio.h:390
SX126x driver implementation.
radio_TCXO_ctrl_voltage_t
Represents the volatge used to control the TCXO on/off from DIO3.
Definition: sx126x_ds.h:438
lora_cad_symbols_t
Represents the number of symbols to be used for channel activity detection operation.
Definition: sx126x_ds.h:263
cad_exit_modes_t
Represents the Channel Activity Detection actions after the CAD operation is finished.
Definition: sx126x_ds.h:274
Represents the possible radio system error states.
Definition: sx126x_ds.h:191