Mistake on this page?
Report an issue in GitHub or email us
SX1272_LoRaRadio.h
1 /**
2  / _____) _ | |
3 ( (____ _____ ____ _| |_ _____ ____| |__
4  \____ \| ___ | (_ _) ___ |/ ___) _ \
5  _____) ) ____| | | || |_| ____( (___| | | |
6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
7  (C)2013 Semtech
8  ___ _____ _ ___ _ _____ ___ ___ ___ ___
9 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
10 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
11 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
12 embedded.connectivity.solutions===============
13 
14 Description: Radio driver for Semtech SX1272 radio. Implements LoRaRadio class.
15 
16 License: Revised BSD License, see LICENSE.TXT file include in the project
17 
18 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
19 
20 
21 Copyright (c) 2017, Arm Limited and affiliates.
22 
23 SPDX-License-Identifier: BSD-3-Clause
24 */
25 
26 #ifndef SX1272_LORARADIO_H_
27 #define SX1272_LORARADIO_H_
28 
29 #if DEVICE_SPI
30 
31 #include "PinNames.h"
32 #include "InterruptIn.h"
33 #include "DigitalOut.h"
34 #include "DigitalInOut.h"
35 #include "SPI.h"
36 #include "platform/PlatformMutex.h"
37 #ifdef MBED_CONF_RTOS_PRESENT
38 #include "rtos/Thread.h"
39 #endif
40 
41 #include "lorawan/LoRaRadio.h"
42 
43 #ifdef MBED_CONF_SX1272_LORA_DRIVER_BUFFER_SIZE
44 #define MAX_DATA_BUFFER_SIZE_SX172 MBED_CONF_SX1272_LORA_DRIVER_BUFFER_SIZE
45 #else
46 #define MAX_DATA_BUFFER_SIZE_SX172 255
47 #endif
48 
49 #if DEVICE_LPTICKER
50 #include "LowPowerTimeout.h"
51 #define ALIAS_LORAWAN_TIMER mbed::LowPowerTimeout
52 #else
53 #include "Timeout.h"
54 #define ALIAS_LORAWAN_TIMER mbed::Timeout
55 #endif
56 
57 /**
58  * Radio driver implementation for Semtech SX1272 plus variants.
59  * Supports only SPI at the moment. Implements pure virtual LoRaRadio class.
60  */
61 class SX1272_LoRaRadio: public LoRaRadio {
62 public:
63  /**
64  * Use this constructor if pin definitions are provided manually.
65  * The pins that are marked NC are optional. It is assumed that these
66  * pins are not connected until/unless configured otherwise.
67  */
68  SX1272_LoRaRadio(PinName mosi,
69  PinName miso,
70  PinName sclk,
71  PinName nss,
72  PinName reset,
73  PinName dio0,
74  PinName dio1,
75  PinName dio2,
76  PinName dio3,
77  PinName dio4,
78  PinName dio5,
79  PinName rf_switch_ctl1 = NC,
80  PinName rf_switch_ctl2 = NC,
81  PinName txctl = NC,
82  PinName rxctl = NC,
83  PinName ant_switch = NC,
84  PinName pwr_amp_ctl = NC,
85  PinName tcxo = NC);
86 
87  /**
88  * Destructor
89  */
90  virtual ~SX1272_LoRaRadio();
91 
92  /**
93  * Registers radio events with the Mbed LoRaWAN stack and
94  * undergoes initialization steps if any
95  *
96  * @param events Structure containing the driver callback functions
97  */
98  virtual void init_radio(radio_events_t *events);
99 
100  /**
101  * Resets the radio module
102  */
103  virtual void radio_reset();
104 
105  /**
106  * Put the RF module in sleep mode
107  */
108  virtual void sleep(void);
109 
110  /**
111  * Sets the radio in standby mode
112  */
113  virtual void standby(void);
114 
115  /**
116  * Sets the reception parameters
117  *
118  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
119  * @param bandwidth Sets the bandwidth
120  * FSK : >= 2600 and <= 250000 Hz
121  * LoRa: [0: 125 kHz, 1: 250 kHz,
122  * 2: 500 kHz, 3: Reserved]
123  * @param datarate Sets the Datarate
124  * FSK : 600..300000 bits/s
125  * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
126  * 10: 1024, 11: 2048, 12: 4096 chips]
127  * @param coderate Sets the coding rate ( LoRa only )
128  * FSK : N/A ( set to 0 )
129  * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
130  * @param bandwidth_afc Sets the AFC Bandwidth ( FSK only )
131  * FSK : >= 2600 and <= 250000 Hz
132  * LoRa: N/A ( set to 0 )
133  * @param preamble_len Sets the Preamble length ( LoRa only )
134  * FSK : N/A ( set to 0 )
135  * LoRa: Length in symbols ( the hardware adds 4 more symbols )
136  * @param symb_timeout Sets the RxSingle timeout value
137  * FSK : timeout number of bytes
138  * LoRa: timeout in symbols
139  * @param fixLen Fixed length packets [0: variable, 1: fixed]
140  * @param payload_len Sets payload length when fixed lenght is used
141  * @param crc_on Enables/Disables the CRC [0: OFF, 1: ON]
142  * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
143  * @param hop_period Number of symbols bewteen each hop (LoRa only)
144  * @param iq_inverted Inverts IQ signals ( LoRa only )
145  * FSK : N/A ( set to 0 )
146  * LoRa: [0: not inverted, 1: inverted]
147  * @param rx_continuous Sets the reception in continuous mode
148  * [false: single mode, true: continuous mode]
149  */
150  virtual void set_rx_config(radio_modems_t modem, uint32_t bandwidth,
151  uint32_t datarate, uint8_t coderate,
152  uint32_t bandwidth_afc, uint16_t preamble_len,
153  uint16_t symb_timeout, bool fix_len,
154  uint8_t payload_len,
155  bool crc_on, bool freq_hop_on, uint8_t hop_period,
156  bool iq_inverted, bool rx_continuous);
157 
158  /**
159  * Sets the transmission parameters
160  *
161  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
162  * @param power Sets the output power [dBm]
163  * @param fdev Sets the frequency deviation ( FSK only )
164  * FSK : [Hz]
165  * LoRa: 0
166  * @param bandwidth Sets the bandwidth ( LoRa only )
167  * FSK : 0
168  * LoRa: [0: 125 kHz, 1: 250 kHz,
169  * 2: 500 kHz, 3: Reserved]
170  * @param datarate Sets the Datarate
171  * FSK : 600..300000 bits/s
172  * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
173  * 10: 1024, 11: 2048, 12: 4096 chips]
174  * @param coderate Sets the coding rate ( LoRa only )
175  * FSK : N/A ( set to 0 )
176  * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
177  * @param preamble_len Sets the preamble length
178  * @param fix_len Fixed length packets [0: variable, 1: fixed]
179  * @param crc_on Enables disables the CRC [0: OFF, 1: ON]
180  * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
181  * @param hop_period Number of symbols bewteen each hop (LoRa only)
182  * @param iq_inverted Inverts IQ signals ( LoRa only )
183  * FSK : N/A ( set to 0 )
184  * LoRa: [0: not inverted, 1: inverted]
185  * @param timeout Transmission timeout [ms]
186  */
187  virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
188  uint32_t bandwidth, uint32_t datarate,
189  uint8_t coderate, uint16_t preamble_len,
190  bool fix_len, bool crc_on, bool freq_hop_on,
191  uint8_t hop_period, bool iq_inverted, uint32_t timeout);
192 
193  /**
194  * Sends the buffer of size
195  *
196  * Prepares the packet to be sent and sets the radio in transmission
197  *
198  * @param buffer Buffer pointer
199  * @param size Buffer size
200  */
201  virtual void send(uint8_t *buffer, uint8_t size);
202 
203  /**
204  * For backwards compatibility
205  */
206  virtual void receive(uint32_t timeout)
207  {
208  (void) timeout;
209  receive();
210  }
211 
212  /**
213  * Sets the radio to receive
214  *
215  * All necessary configuration options for receptions are set in
216  * 'set_rx_config(parameters)' API.
217  */
218  virtual void receive(void);
219 
220  /**
221  * Sets the carrier frequency
222  *
223  * @param freq Channel RF frequency
224  */
225  virtual void set_channel(uint32_t freq);
226 
227  /**
228  * Generates a 32 bits random value based on the RSSI readings
229  *
230  * Remark this function sets the radio in LoRa modem mode and disables
231  * all interrupts.
232  * After calling this function either Radio.SetRxConfig or
233  * Radio.SetTxConfig functions must be called.
234  *
235  * @return 32 bits random value
236  */
237  virtual uint32_t random(void);
238 
239  /**
240  * Get radio status
241  *
242  * @param status Radio status [RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
243  * @return Return current radio status
244  */
245  virtual uint8_t get_status(void);
246 
247  /**
248  * Sets the maximum payload length
249  *
250  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
251  * @param max Maximum payload length in bytes
252  */
253  virtual void set_max_payload_length(radio_modems_t modem, uint8_t max);
254 
255  /**
256  * Sets the network to public or private
257  *
258  * Updates the sync byte. Applies to LoRa modem only
259  *
260  * @param enable if true, it enables a public network
261  */
262  virtual void set_public_network(bool enable);
263 
264  /**
265  * Computes the packet time on air for the given payload
266  *
267  * Remark can only be called once SetRxConfig or SetTxConfig have been called
268  *
269  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
270  * @param pkt_len Packet payload length
271  * @return Computed airTime for the given packet payload length
272  */
273  virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len);
274 
275  /**
276  * Perform carrier sensing
277  *
278  * Checks for a certain time if the RSSI is above a given threshold.
279  * This threshold determines if there is already a transmission going on
280  * in the channel or not.
281  *
282  * @param modem Type of the radio modem
283  * @param freq Carrier frequency
284  * @param rssi_threshold Threshold value of RSSI
285  * @param max_carrier_sense_time time to sense the channel
286  *
287  * @return true if there is no active transmission
288  * in the channel, false otherwise
289  */
290  virtual bool perform_carrier_sense(radio_modems_t modem,
291  uint32_t freq,
292  int16_t rssi_threshold,
293  uint32_t max_carrier_sense_time);
294 
295  /**
296  * Sets the radio in CAD mode
297  *
298  */
299  virtual void start_cad(void);
300 
301  /**
302  * Check if the given RF is in range
303  *
304  * @param frequency frequency needed to be checked
305  */
306  virtual bool check_rf_frequency(uint32_t frequency);
307 
308  /** Sets the radio in continuous wave transmission mode
309  *
310  * @param freq Channel RF frequency
311  * @param power Sets the output power [dBm]
312  * @param time Transmission mode timeout [s]
313  */
314  virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time);
315 
316  /**
317  * Acquire exclusive access
318  */
319  virtual void lock(void);
320 
321  /**
322  * Release exclusive access
323  */
324  virtual void unlock(void);
325 
326 private:
327 
328  // SPI and chip select control
329  mbed::SPI _spi;
330  mbed::DigitalOut _chip_select;
331 
332  // module rest control
333  mbed::DigitalInOut _reset_ctl;
334 
335  // Interrupt controls
336  mbed::InterruptIn _dio0_ctl;
337  mbed::InterruptIn _dio1_ctl;
338  mbed::InterruptIn _dio2_ctl;
339  mbed::InterruptIn _dio3_ctl;
340  mbed::InterruptIn _dio4_ctl;
341  mbed::InterruptIn _dio5_ctl;
342 
343  // Radio specific controls
344  mbed::DigitalOut _rf_switch_ctl1;
345  mbed::DigitalOut _rf_switch_ctl2;
346  mbed::DigitalOut _txctl;
347  mbed::DigitalOut _rxctl;
348  mbed::DigitalInOut _ant_switch;
349  mbed::DigitalOut _pwr_amp_ctl;
350  mbed::DigitalOut _tcxo;
351 
352  // Contains all RF control pin names
353  // This storage is needed even after assigning the
354  // pins to corresponding object, as the driver needs to know
355  // which control pins are connected and which are not. This
356  // variation is inherent to driver because of target configuration.
357  rf_ctrls _rf_ctrls;
358 
359  // We need these PinNames as not all modules have those connected
360  PinName _dio4_pin;
361  PinName _dio5_pin;
362 
363  // Structure containing all user and network specified settings
364  // for radio module
365  radio_settings_t _rf_settings;
366 
367  // Structure containing function pointers to the stack callbacks
368  radio_events_t *_radio_events;
369 
370  // Data buffer used for both TX and RX
371  // Size of this buffer is configurable via Mbed config system
372  // Default is 256 bytes
373  uint8_t _data_buffer[MAX_DATA_BUFFER_SIZE_SX172];
374 
375  // TX timer in ms. This timer is used as a fail safe for TX.
376  // If the chip fails to transmit, its a fatal error, reflecting
377  // some catastrophic bus failure etc. We wish to have the control
378  // back from the driver in such a case.
379  ALIAS_LORAWAN_TIMER tx_timeout_timer;
380 
381 #ifdef MBED_CONF_RTOS_PRESENT
382  // Thread to handle interrupts
383  rtos::Thread irq_thread;
384 #endif
385 
386  // Access protection
387  PlatformMutex mutex;
388 
389  uint8_t radio_variant;
390 
391  /**
392  * Flag used to set the RF switch control pins in low power mode when the radio is not active.
393  */
394  bool radio_is_active;
395 
396  // helper functions
397  void setup_registers();
398  void default_antenna_switch_ctrls();
399  void set_antenna_switch(uint8_t operation_mode);
400  void setup_spi();
401  void gpio_init();
402  void gpio_deinit();
403  void setup_interrupts();
404  void set_modem(uint8_t modem);
405  void set_operation_mode(uint8_t mode);
406  void set_low_power_mode(bool status);
407  void set_sx1272_variant_type();
408  uint8_t get_pa_conf_reg();
409  void set_rf_tx_power(int8_t power);
410  int16_t get_rssi(radio_modems_t modem);
411  uint8_t get_fsk_bw_reg_val(uint32_t bandwidth);
412  void write_to_register(uint8_t addr, uint8_t data);
413  void write_to_register(uint8_t addr, uint8_t *data, uint8_t size);
414  uint8_t read_register(uint8_t addr);
415  void read_register(uint8_t addr, uint8_t *buffer, uint8_t size);
416  void write_fifo(uint8_t *buffer, uint8_t size);
417  void read_fifo(uint8_t *buffer, uint8_t size);
418  void transmit(uint32_t timeout);
419  void rf_irq_task(void);
420 
421  // ISRs
422  void dio0_irq_isr();
423  void dio1_irq_isr();
424  void dio2_irq_isr();
425  void dio3_irq_isr();
426  void dio4_irq_isr();
427  void dio5_irq_isr();
428  void timeout_irq_isr();
429 
430  // Handlers called by thread in response to signal
431  void handle_dio0_irq();
432  void handle_dio1_irq();
433  void handle_dio2_irq();
434  void handle_dio3_irq();
435  void handle_dio4_irq();
436  void handle_dio5_irq();
437  void handle_timeout_irq();
438 };
439 
440 #endif // DEVICE_SPI
441 
442 #endif /* SX1272_LORARADIO_H_ */
The Thread class allow defining, creating, and controlling thread functions in the system...
Definition: Thread.h:92
virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len)
Computes the packet time on air for the given payload.
virtual void receive(void)
Sets the radio to receive.
virtual void standby(void)
Sets the radio in standby mode.
enum modem_type radio_modems_t
Type of modem.
virtual void set_channel(uint32_t freq)
Sets the carrier frequency.
Structure to hold RF controls for LoRa Radio.
Definition: LoRaRadio.h:35
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 start_cad(void)
Sets the radio in CAD mode.
virtual ~SX1272_LoRaRadio()
Destructor.
virtual void radio_reset()
Resets the radio module.
A digital input/output, used for setting or reading a bi-directional pin.
Definition: DigitalInOut.h:35
SX1272_LoRaRadio(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5, PinName rf_switch_ctl1=NC, PinName rf_switch_ctl2=NC, PinName txctl=NC, PinName rxctl=NC, PinName ant_switch=NC, PinName pwr_amp_ctl=NC, PinName tcxo=NC)
Use this constructor if pin definitions are provided manually.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time)
Sets the radio in continuous wave transmission mode.
Reporting functions for upper layers.
Definition: LoRaRadio.h:389
virtual void unlock(void)
Release exclusive access.
virtual void set_max_payload_length(radio_modems_t modem, uint8_t max)
Sets the maximum payload length.
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.
virtual void init_radio(radio_events_t *events)
Registers radio events with the Mbed LoRaWAN stack and undergoes initialization steps if any...
virtual void lock(void)
Acquire exclusive access.
virtual void send(uint8_t *buffer, uint8_t size)
Sends the buffer of size.
A digital interrupt input, used to call a function on a rising or falling edge.
Definition: InterruptIn.h:65
virtual void receive(uint32_t timeout)
For backwards compatibility.
virtual bool check_rf_frequency(uint32_t frequency)
Check if the given RF is in range.
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
Global radio settings.
Definition: LoRaRadio.h:347
virtual uint32_t random(void)
Generates a 32 bits random value based on the RSSI readings.
Radio driver implementation for Semtech SX1272 plus variants.
virtual uint8_t get_status(void)
Get radio status.
Interface for the radios, containing the main functions that a radio needs, and five callback functio...
Definition: LoRaRadio.h:440
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 set_public_network(bool enable)
Sets the network to public or private.
virtual void sleep(void)
Put the RF module in sleep mode.
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.