Mistake on this page?
Report an issue in GitHub or email us
STM32WL_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  ******************************************************************************
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 "platform/PlatformMutex.h"
41 #include "lorawan/LoRaRadio.h"
42 
43 #include "STM32WL_radio_driver.h"
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 
54 extern void set_antenna_switch(RBI_Switch_TypeDef state);
55 
56 class STM32WL_LoRaRadio : public LoRaRadio {
57 
58 public:
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  /**
87  * Sets the reception parameters
88  *
89  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
90  * @param bandwidth Sets the bandwidth
91  * FSK : >= 2600 and <= 250000 Hz
92  * LoRa: [0: 125 kHz, 1: 250 kHz,
93  * 2: 500 kHz, 3: Reserved]
94  * @param datarate Sets the Datarate
95  * FSK : 600..300000 bits/s
96  * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
97  * 10: 1024, 11: 2048, 12: 4096 chips]
98  * @param coderate Sets the coding rate ( LoRa only )
99  * FSK : N/A ( set to 0 )
100  * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
101  * @param bandwidth_afc Sets the AFC Bandwidth ( FSK only )
102  * FSK : >= 2600 and <= 250000 Hz
103  * LoRa: N/A ( set to 0 )
104  * @param preamble_len Sets the Preamble length ( LoRa only )
105  * FSK : N/A ( set to 0 )
106  * LoRa: Length in symbols ( the hardware adds 4 more symbols )
107  * @param symb_timeout Sets the RxSingle timeout value
108  * FSK : timeout number of bytes
109  * LoRa: timeout in symbols
110  * @param fixLen Fixed length packets [0: variable, 1: fixed]
111  * @param payload_len Sets payload length when fixed lenght is used
112  * @param crc_on Enables/Disables the CRC [0: OFF, 1: ON]
113  * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
114  * @param hop_period Number of symbols bewteen each hop (LoRa only)
115  * @param iq_inverted Inverts IQ signals ( LoRa only )
116  * FSK : N/A ( set to 0 )
117  * LoRa: [0: not inverted, 1: inverted]
118  * @param rx_continuous Sets the reception in continuous mode
119  * [false: single mode, true: continuous mode]
120  */
121  virtual void set_rx_config(radio_modems_t modem, uint32_t bandwidth,
122  uint32_t datarate, uint8_t coderate,
123  uint32_t bandwidth_afc, uint16_t preamble_len,
124  uint16_t symb_timeout, bool fix_len,
125  uint8_t payload_len,
126  bool crc_on, bool freq_hop_on, uint8_t hop_period,
127  bool iq_inverted, bool rx_continuous);
128 
129  /**
130  * Sets the transmission parameters
131  *
132  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
133  * @param power Sets the output power [dBm]
134  * @param fdev Sets the frequency deviation ( FSK only )
135  * FSK : [Hz]
136  * LoRa: 0
137  * @param bandwidth Sets the bandwidth ( LoRa only )
138  * FSK : 0
139  * LoRa: [0: 125 kHz, 1: 250 kHz,
140  * 2: 500 kHz, 3: Reserved]
141  * @param datarate Sets the Datarate
142  * FSK : 600..300000 bits/s
143  * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
144  * 10: 1024, 11: 2048, 12: 4096 chips]
145  * @param coderate Sets the coding rate ( LoRa only )
146  * FSK : N/A ( set to 0 )
147  * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
148  * @param preamble_len Sets the preamble length
149  * @param fix_len Fixed length packets [0: variable, 1: fixed]
150  * @param crc_on Enables disables the CRC [0: OFF, 1: ON]
151  * @param freq_hop_on Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
152  * @param hop_period Number of symbols bewteen each hop (LoRa only)
153  * @param iq_inverted Inverts IQ signals ( LoRa only )
154  * FSK : N/A ( set to 0 )
155  * LoRa: [0: not inverted, 1: inverted]
156  * @param timeout Transmission timeout [ms]
157  */
158  virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
159  uint32_t bandwidth, uint32_t datarate,
160  uint8_t coderate, uint16_t preamble_len,
161  bool fix_len, bool crc_on, bool freq_hop_on,
162  uint8_t hop_period, bool iq_inverted, uint32_t timeout);
163 
164  /**
165  * Sends the buffer of size
166  *
167  * Prepares the packet to be sent and sets the radio in transmission
168  *
169  * @param buffer Buffer pointer
170  * @param size Buffer size
171  */
172  virtual void send(uint8_t *buffer, uint8_t size);
173 
174  /**
175  * For backwards compatibility
176  */
177  virtual void receive(uint32_t timeout)
178  {
179  (void) timeout;
180  receive();
181  }
182 
183  /**
184  * Sets the radio to receive
185  *
186  * All necessary configuration options for reception are set in
187  * 'set_rx_config(parameters)' API.
188  */
189  virtual void receive(void);
190 
191  /**
192  * Sets the carrier frequency
193  *
194  * @param freq Channel RF frequency
195  */
196  virtual void set_channel(uint32_t freq);
197 
198  /**
199  * Generates a 32 bits random value based on the RSSI readings
200  *
201  * Remark this function sets the radio in LoRa modem mode and disables
202  * all interrupts.
203  * After calling this function either Radio.SetRxConfig or
204  * Radio.SetTxConfig functions must be called.
205  *
206  * @return 32 bits random value
207  */
208  virtual uint32_t random(void);
209 
210  /**
211  * Get radio status
212  *
213  * @param status Radio status [RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
214  * @return Return current radio status
215  */
216  virtual uint8_t get_status(void);
217 
218  /**
219  * Sets the maximum payload length
220  *
221  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
222  * @param max Maximum payload length in bytes
223  */
224  virtual void set_max_payload_length(radio_modems_t modem, uint8_t max);
225 
226  /**
227  * Sets the network to public or private
228  *
229  * Updates the sync byte. Applies to LoRa modem only
230  *
231  * @param enable if true, it enables a public network
232  */
233  virtual void set_public_network(bool enable);
234 
235  /**
236  * Computes the packet time on air for the given payload
237  *
238  * Remark can only be called once SetRxConfig or SetTxConfig have been called
239  *
240  * @param modem Radio modem to be used [0: FSK, 1: LoRa]
241  * @param pkt_len Packet payload length
242  * @return Computed airTime for the given packet payload length
243  */
244  virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len);
245 
246  /**
247  * Perform carrier sensing
248  *
249  * Checks for a certain time if the RSSI is above a given threshold.
250  * This threshold determines if there is already a transmission going on
251  * in the channel or not.
252  *
253  * @param modem Type of the radio modem
254  * @param freq Carrier frequency
255  * @param rssi_threshold Threshold value of RSSI
256  * @param max_carrier_sense_time time to sense the channel
257  *
258  * @return true if there is no active transmission
259  * in the channel, false otherwise
260  */
261  virtual bool perform_carrier_sense(radio_modems_t modem,
262  uint32_t freq,
263  int16_t rssi_threshold,
264  uint32_t max_carrier_sense_time);
265 
266  /**
267  * Sets the radio in CAD mode
268  *
269  */
270  virtual void start_cad(void);
271 
272  /**
273  * Check if the given RF is in range
274  *
275  * @param frequency frequency needed to be checked
276  */
277  virtual bool check_rf_frequency(uint32_t frequency);
278 
279  /** Sets the radio in continuous wave transmission mode
280  *
281  * @param freq Channel RF frequency
282  * @param power Sets the output power [dBm]
283  * @param time Transmission mode timeout [s]
284  */
285  virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time);
286 
287  /**
288  * Acquire exclusive access
289  */
290  virtual void lock(void);
291 
292  /**
293  * Release exclusive access
294  */
295  virtual void unlock(void);
296 
297  static void HAL_SUBGHZ_TxCpltCallback(void);
298  static void HAL_SUBGHZ_RxCpltCallback(void);
299  static void HAL_SUBGHZ_CRCErrorCallback(void);
300  static void HAL_SUBGHZ_CADStatusCallback(void);
301  static void HAL_SUBGHZ_RxTxTimeoutCallback(void);
302 
303 
304  static void clear_irq_status(uint16_t irq);
305  static uint16_t get_irq_status(void);
306  static void read_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
307  static void write_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size);
308  static void read_fifo(uint8_t *buffer, uint8_t size, uint8_t offset);
309  static void get_rx_buffer_status(uint8_t *payload_len, uint8_t *rx_buffer_ptr);
310  static void get_packet_status(packet_status_t *pkt_status);
311  static uint8_t get_modem();
312  static uint8_t read_register(uint16_t addr);
313 
314 private:
315 
316  // Access protection
317  PlatformMutex mutex;
318 
319  // helper functions
320  void wakeup();
321 
322  void SUBGRF_SetTcxoMode(radio_TCXO_ctrl_voltage_t voltage, uint32_t timeout);
323  void set_device_ready(void);
324  int8_t get_rssi();
325  uint8_t get_fsk_bw_reg_val(uint32_t bandwidth);
326  void write_to_register(uint16_t addr, uint8_t data);
327  void write_to_register(uint16_t addr, uint8_t *data, uint8_t size);
328 
329  void read_register(uint16_t addr, uint8_t *buffer, uint8_t size);
330  void write_fifo(uint8_t *buffer, uint8_t size);
331 
332  void rf_irq_task(void);
333  void set_modem(uint8_t modem);
334 
335 
336  uint8_t get_frequency_support(void);
337 
338 
339 
340  void set_modulation_params(modulation_params_t *modulationParams);
341  void set_packet_params(packet_params_t *packet_params);
342  void set_cad_params(lora_cad_symbols_t nb_symbols, uint8_t det_peak,
343  uint8_t det_min, cad_exit_modes_t exit_mode,
344  uint32_t timeout);
345  void set_buffer_base_addr(uint8_t tx_base_addr, uint8_t rx_base_addr);
346 
347  radio_error_t get_device_errors(void);
348  void clear_device_errors(void);
349 
350  void set_crc_seed(uint16_t seed);
351  void set_crc_polynomial(uint16_t polynomial);
352  void set_whitening_seed(uint16_t seed);
353  void set_pa_config(uint8_t pa_DC, uint8_t hp_max, uint8_t device_type,
354  uint8_t pa_LUT);
355  void set_tx_power(int8_t power);
356  void calibrate_image(uint32_t freq);
357  void configure_dio_irq(uint16_t irq_mask, uint16_t dio1_mask,
358  uint16_t dio2_mask, uint16_t dio3_mask);
359  void cold_start_wakeup();
360 
361  void SUBGRF_SetSwitch(uint8_t paSelect, RFState_t rxtx);
362  uint8_t SUBGRF_SetRfTxPower(int8_t power);
363  void SUBGRF_SetTxParams(uint8_t paSelect, int8_t power, radio_ramp_time_t rampTime);
364  void Radio_SMPS_Set(uint8_t level);
365  uint32_t RadioGetWakeupTime(void);
366 
367 
368  uint8_t _standby_mode;
369 
370  uint8_t _reception_mode;
371  uint32_t _tx_timeout;
372  uint32_t _rx_timeout;
373  uint8_t _rx_timeout_in_symbols;
374  int8_t _tx_power;
375  uint8_t _antSwitchPaSelect;
376  bool _image_calibrated;
377  bool _force_image_calibration;
378  bool _network_mode_public;
379 
380  // Structure containing all user and network specified settings
381  // for radio module
382  modulation_params_t _mod_params;
383  packet_params_t _packet_params;
384 
385  RFState_t rfstate;
386 
387 };
388 
389 
390 
391 #endif /* MBED_LORA_RADIO_DRV_STM32WL_LORARADIO_H_ */
virtual uint8_t get_status(void)
Get radio status.
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.
The type describing the packet parameters for every packet types.
Definition: sx126x_ds.h:581
virtual void receive(uint32_t timeout)
For backwards compatibility.
virtual void start_cad(void)
Sets the radio in CAD mode.
STM32WL driver implementation.
virtual void receive(void)
Sets the radio to receive.
virtual void send(uint8_t *buffer, uint8_t size)
Sends the buffer of size.
Represents the possible radio system error states.
Definition: sx126x_ds.h:191
virtual void init_radio(radio_events_t *events)
Registers radio events with the Mbed LoRaWAN stack and undergoes initialization steps if any...
enum modem_type radio_modems_t
Type of modem.
virtual void set_max_payload_length(radio_modems_t modem, uint8_t max)
Sets the maximum payload length.
virtual void lock(void)
Acquire exclusive access.
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 radio_reset()
Resets the radio module.
Reporting functions for upper layers.
Definition: LoRaRadio.h:389
virtual void set_channel(uint32_t freq)
Sets the carrier frequency.
virtual void standby(void)
Sets the radio in standby mode.
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 unlock(void)
Release exclusive access.
RFState_t
Radio driver internal state machine states definition.
virtual void set_public_network(bool enable)
Sets the network to public or private.
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 bool check_rf_frequency(uint32_t frequency)
Check if the given RF is in range.
virtual void sleep(void)
Put the RF module in sleep mode.
virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len)
Computes the packet time on air for the given payload.
Interface for the radios, containing the main functions that a radio needs, and five callback functio...
Definition: LoRaRadio.h:440
virtual uint32_t random(void)
Generates a 32 bits random value based on the RSSI readings.
The type describing the modulation parameters for every packet types.
Definition: sx126x_ds.h:557
virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time)
Sets the radio in continuous wave transmission 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.