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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.