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