Mbed OS Reference
Loading...
Searching...
No Matches
STM32WL_LoRaRadio.h
Go to the documentation of this file.
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 ******************************************************************************
26 *
27 * Portions COPYRIGHT 2020 STMicroelectronics
28 *
29 * @file STM32WL_loRaRadio.h
30 * @author MCD Application Team
31 * @brief header of radio driver
32 ******************************************************************************
33 */
34
35#ifndef MBED_LORA_RADIO_DRV_STM32WL_LORARADIO_H_
36#define MBED_LORA_RADIO_DRV_STM32WL_LORARADIO_H_
37
38#include "platform/mbed_critical.h"
39#include "drivers/DigitalOut.h"
40#include "rtos/Mutex.h"
41#include "lorawan/LoRaRadio.h"
42
44
45// Data buffer used for both TX and RX
46// Size of this buffer is configurable via Mbed config system
47// Default is 255 bytes
48#ifdef MBED_CONF_STM32WL_LORA_DRIVER_BUFFER_SIZE
49#define MAX_DATA_BUFFER_SIZE_STM32WL MBED_CONF_STM32WL_LORA_DRIVER_BUFFER_SIZE
50#else
51#define MAX_DATA_BUFFER_SIZE_STM32WL 255
52#endif
53
54extern void set_antenna_switch(RBI_Switch_TypeDef state);
55
57
58public:
60
62
63 /**
64 * Registers radio events with the Mbed LoRaWAN stack and
65 * undergoes initialization steps if any
66 *
67 * @param events Structure containing the driver callback functions
68 */
69 virtual void init_radio(radio_events_t *events);
70
71 /**
72 * Resets the radio module
73 */
74 virtual void radio_reset();
75
76 /**
77 * Put the RF module in sleep mode
78 */
79 virtual void sleep(void);
80
81 /**
82 * Sets the radio in standby mode
83 */
84 virtual void standby(void);
85
86 void set_rx_config(radio_modems_t modem, uint32_t bandwidth,
87 uint32_t datarate, uint8_t coderate,
88 uint32_t bandwidth_afc, uint16_t preamble_len,
89 uint16_t symb_timeout, bool fix_len,
90 uint8_t payload_len,
91 bool crc_on, bool freq_hop_on, uint8_t hop_period,
92 bool iq_inverted, bool rx_continuous) override;
93
94 /**
95 * Sets the transmission parameters
96 *
97 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
98 * @param power Sets the output power [dBm]
99 * @param fdev Sets the frequency deviation ( FSK only )
100 * FSK : [Hz]
101 * LoRa: 0
102 * @param bandwidth Sets the bandwidth ( LoRa only )
103 * FSK : 0
104 * LoRa: [0: 125 kHz, 1: 250 kHz,
105 * 2: 500 kHz, 3: Reserved]
106 * @param datarate Sets the Datarate
107 * FSK : 600..300000 bits/s
108 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
109 * 10: 1024, 11: 2048, 12: 4096 chips]
110 * @param coderate Sets the coding rate ( LoRa only )
111 * FSK : N/A ( set to 0 )
112 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
113 * @param preamble_len Sets the preamble length
114 * @param fix_len Fixed length packets [0: variable, 1: fixed]
115 * @param crc_on Enables disables the CRC [0: OFF, 1: ON]
116 * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
117 * @param hop_period Number of symbols bewteen each hop (LoRa only)
118 * @param iq_inverted Inverts IQ signals ( LoRa only )
119 * FSK : N/A ( set to 0 )
120 * LoRa: [0: not inverted, 1: inverted]
121 * @param timeout Transmission timeout [ms]
122 */
123 virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
124 uint32_t bandwidth, uint32_t datarate,
125 uint8_t coderate, uint16_t preamble_len,
126 bool fix_len, bool crc_on, bool freq_hop_on,
127 uint8_t hop_period, bool iq_inverted, uint32_t timeout);
128
129 /**
130 * Sends the buffer of size
131 *
132 * Prepares the packet to be sent and sets the radio in transmission
133 *
134 * @param buffer Buffer pointer
135 * @param size Buffer size
136 */
137 virtual void send(uint8_t *buffer, uint8_t size);
138
139 /**
140 * For backwards compatibility
141 */
142 virtual void receive(uint32_t timeout)
143 {
144 (void) timeout;
145 receive();
146 }
147
148 /**
149 * Sets the radio to receive
150 *
151 * All necessary configuration options for reception are set in
152 * 'set_rx_config(parameters)' API.
153 */
154 virtual void receive(void);
155
156 /**
157 * Sets the carrier frequency
158 *
159 * @param freq Channel RF frequency
160 */
161 virtual void set_channel(uint32_t freq);
162
163 /**
164 * Generates a 32 bits random value based on the RSSI readings
165 *
166 * Remark this function sets the radio in LoRa modem mode and disables
167 * all interrupts.
168 * After calling this function either Radio.SetRxConfig or
169 * Radio.SetTxConfig functions must be called.
170 *
171 * @return 32 bits random value
172 */
173 virtual uint32_t random(void);
174
175 radio_state_t get_status(void) override;
176
177 /**
178 * Sets the maximum payload length
179 *
180 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
181 * @param max Maximum payload length in bytes
182 */
183 virtual void set_max_payload_length(radio_modems_t modem, uint8_t max);
184
185 /**
186 * Sets the network to public or private
187 *
188 * Updates the sync byte. Applies to LoRa modem only
189 *
190 * @param enable if true, it enables a public network
191 */
192 virtual void set_public_network(bool enable);
193
194 /**
195 * Computes the packet time on air for the given payload
196 *
197 * Remark can only be called once SetRxConfig or SetTxConfig have been called
198 *
199 * @param modem Radio modem to be used [0: FSK, 1: LoRa]
200 * @param pkt_len Packet payload length
201 * @return Computed airTime for the given packet payload length
202 */
203 virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len);
204
205 /**
206 * Perform carrier sensing
207 *
208 * Checks for a certain time if the RSSI is above a given threshold.
209 * This threshold determines if there is already a transmission going on
210 * in the channel or not.
211 *
212 * @param modem Type of the radio modem
213 * @param freq Carrier frequency
214 * @param rssi_threshold Threshold value of RSSI
215 * @param max_carrier_sense_time time to sense the channel
216 *
217 * @return true if there is no active transmission
218 * in the channel, false otherwise
219 */
221 uint32_t freq,
222 int16_t rssi_threshold,
223 uint32_t max_carrier_sense_time);
224
225 /**
226 * Sets the radio in CAD mode
227 *
228 */
229 virtual void start_cad(void);
230
231 /**
232 * Check if the given RF is in range
233 *
234 * @param frequency frequency needed to be checked
235 */
236 virtual bool check_rf_frequency(uint32_t frequency);
237
238 /** Sets the radio in continuous wave transmission mode
239 *
240 * @param freq Channel RF frequency
241 * @param power Sets the output power [dBm]
242 * @param time Transmission mode timeout [s]
243 */
244 virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time);
245
246 /**
247 * Acquire exclusive access
248 */
249 virtual void lock(void);
250
251 /**
252 * Release exclusive access
253 */
254 virtual void unlock(void);
255
256 static void HAL_SUBGHZ_TxCpltCallback(void);
257 static void HAL_SUBGHZ_RxCpltCallback(void);
258 static void HAL_SUBGHZ_CRCErrorCallback(void);
259 static void HAL_SUBGHZ_CADStatusCallback(void);
260 static void HAL_SUBGHZ_RxTxTimeoutCallback(void);
261
262
263 static void clear_irq_status(uint16_t irq);
264 static uint16_t get_irq_status(void);
265 static void read_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
266 static void write_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
267 static void read_fifo(uint8_t *buffer, uint8_t size, uint8_t offset);
268 static void get_rx_buffer_status(uint8_t *payload_len, uint8_t *rx_buffer_ptr);
269 static void get_packet_status(packet_status_t *pkt_status);
270 static uint8_t get_modem();
271 static uint8_t read_register(uint16_t addr);
272
273private:
274
275 // Access protection
276 rtos::Mutex mutex;
277
278 // helper functions
279 void wakeup();
280
281 void SUBGRF_SetTcxoMode(radio_TCXO_ctrl_voltage_t voltage, uint32_t timeout);
282 void set_device_ready(void);
283 int8_t get_rssi();
284 uint8_t get_fsk_bw_reg_val(uint32_t bandwidth);
285 void write_to_register(uint16_t addr, uint8_t data);
286 void write_to_register(uint16_t addr, uint8_t *data, uint8_t size);
287
288 void read_register(uint16_t addr, uint8_t *buffer, uint8_t size);
289 void write_fifo(uint8_t *buffer, uint8_t size);
290
291 void rf_irq_task(void);
292 void set_modem(uint8_t modem);
293
294
295 uint8_t get_frequency_support(void);
296
297
298
299 void set_modulation_params(modulation_params_t *modulationParams);
300 void set_packet_params(packet_params_t *packet_params);
301 void set_cad_params(lora_cad_symbols_t nb_symbols, uint8_t det_peak,
302 uint8_t det_min, cad_exit_modes_t exit_mode,
303 uint32_t timeout);
304 void set_buffer_base_addr(uint8_t tx_base_addr, uint8_t rx_base_addr);
305
306 radio_error_t get_device_errors(void);
307 void clear_device_errors(void);
308
309 void set_crc_seed(uint16_t seed);
310 void set_crc_polynomial(uint16_t polynomial);
311 void set_whitening_seed(uint16_t seed);
312 void set_pa_config(uint8_t pa_DC, uint8_t hp_max, uint8_t device_type,
313 uint8_t pa_LUT);
314 void set_tx_power(int8_t power);
315 void calibrate_image(uint32_t freq);
316 void configure_dio_irq(uint16_t irq_mask, uint16_t dio1_mask,
317 uint16_t dio2_mask, uint16_t dio3_mask);
318 void cold_start_wakeup();
319
320 void SUBGRF_SetSwitch(uint8_t paSelect, RFState_t rxtx);
321 uint8_t SUBGRF_SetRfTxPower(int8_t power);
322 void SUBGRF_SetTxParams(uint8_t paSelect, int8_t power, radio_ramp_time_t rampTime);
323 void Radio_SMPS_Set(uint8_t level);
324 uint32_t RadioGetWakeupTime(void);
325
326
327 uint8_t _standby_mode;
328
329 uint8_t _reception_mode;
330 uint32_t _tx_timeout;
331 uint32_t _rx_timeout;
332 uint8_t _rx_timeout_in_symbols;
333 int8_t _tx_power;
334 uint8_t _antSwitchPaSelect;
335 bool _image_calibrated;
336 bool _force_image_calibration;
337 bool _network_mode_public;
338
339 // Structure containing all user and network specified settings
340 // for radio module
341 modulation_params_t _mod_params;
342 packet_params_t _packet_params;
343
344 RFState_t rfstate;
345
346};
347
348
349
350#endif /* MBED_LORA_RADIO_DRV_STM32WL_LORARADIO_H_ */
STM32WL driver implementation.
RFState_t
Radio driver internal state machine states definition.
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 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 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.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
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
radio_ramp_time_t
Represents the ramping time for power amplifier.
Definition: sx126x_ds.h:249
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