sx126x.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef SX126X_H
22 #define SX126X_H
23 
24 #include <assert.h>
25 
26 #include "sx126x_driver.h"
27 
28 #include "net/netdev.h"
29 
30 #include "periph/gpio.h"
31 #include "periph/spi.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 typedef struct sx126x sx126x_t;
41 
45 typedef enum {
46  SX126X_RF_MODE_RX,
47  SX126X_RF_MODE_TX_LPA,
48  SX126X_RF_MODE_TX_HPA,
50 
55 #define SX126X_SINGLE (( \
56  IS_USED(MODULE_SX1261) \
57  + IS_USED(MODULE_SX1262) \
58  + IS_USED(MODULE_SX1268) \
59  + IS_USED(MODULE_LLCC68) \
60  + IS_USED(MODULE_SX126X_STM32WL) \
61  ) == 1)
62 
66 #if (IS_USED(MODULE_SX1261) || IS_USED(MODULE_SX1262) || \
67  IS_USED(MODULE_SX1268) || IS_USED(MODULE_LLCC68))
68 #define SX126X_SPI 1
69 #endif
70 
74 typedef enum {
75  SX126X_TYPE_SX1261,
76  SX126X_TYPE_SX1262,
77  SX126X_TYPE_SX1268,
78  SX126X_TYPE_LLCC68,
79  SX126X_TYPE_STM32WL,
81 
85 typedef struct {
86  spi_t spi;
87  gpio_t nss_pin;
88  gpio_t reset_pin;
89  gpio_t busy_pin;
90  gpio_t dio1_pin;
91  sx126x_reg_mod_t regulator;
93 #if IS_USED(MODULE_SX126X_RF_SWITCH)
97  void(*set_rf_mode)(sx126x_t *dev, sx126x_rf_mode_t rf_mode);
98 #endif
100 
104 struct sx126x {
107  sx126x_pkt_params_lora_t pkt_params;
108  sx126x_mod_params_lora_t mod_params;
109  uint32_t channel;
110  uint16_t rx_timeout;
111  bool radio_sleep;
112 };
113 
122 void sx126x_setup(sx126x_t *dev, const sx126x_params_t *params, uint8_t index);
123 
132 
141 static inline int sx126x_symbol_to_msec(sx126x_t *dev, uint16_t symbols)
142 {
143  assert(dev && (dev->mod_params.bw <= SX126X_LORA_BW_500) && \
144  (dev->mod_params.bw >= SX126X_LORA_BW_125));
145 
146  /* Refer section 6.1.4 LoRa Time-on-Air in SX1268 datasheet */
147  return (symbols * (1 << (dev->mod_params.sf + 7 - dev->mod_params.bw)) / 1000);
148 }
149 
157 uint32_t sx126x_get_channel(const sx126x_t *dev);
158 
165 void sx126x_set_channel(sx126x_t *dev, uint32_t freq);
166 
174 uint8_t sx126x_get_bandwidth(const sx126x_t *dev);
175 
182 void sx126x_set_bandwidth(sx126x_t *dev, uint8_t bandwidth);
183 
192 
199 void sx126x_set_spreading_factor(sx126x_t *dev, uint8_t sf);
200 
208 uint8_t sx126x_get_coding_rate(const sx126x_t *dev);
209 
216 void sx126x_set_coding_rate(sx126x_t *dev, uint8_t cr);
217 
226 
233 void sx126x_set_lora_payload_length(sx126x_t *dev, uint8_t len);
234 
242 bool sx126x_get_lora_crc(const sx126x_t *dev);
243 
250 void sx126x_set_lora_crc(sx126x_t *dev, bool crc);
251 
260 
268 
277 
284 void sx126x_set_lora_preamble_length(sx126x_t *dev, uint16_t preamble);
285 
294 
301 void sx126x_set_lora_iq_invert(sx126x_t *dev, bool iq_invert);
302 
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif /* SX126X_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
Definitions low-level network driver interface.
Low-level GPIO peripheral driver interface definitions.
int sx126x_init(sx126x_t *dev)
Initialize the given device.
void sx126x_set_spreading_factor(sx126x_t *dev, uint8_t sf)
Sets the LoRa spreading factor.
void sx126x_set_bandwidth(sx126x_t *dev, uint8_t bandwidth)
Sets the LoRa bandwidth.
void sx126x_set_lora_implicit_header(sx126x_t *dev, bool mode)
Sets LoRa implicit header mode.
void sx126x_set_lora_iq_invert(sx126x_t *dev, bool iq_invert)
Enable/disable the LoRa IQ inverted mode.
uint16_t sx126x_get_lora_preamble_length(const sx126x_t *dev)
Gets the LoRa preamble length.
void sx126x_set_coding_rate(sx126x_t *dev, uint8_t cr)
Sets the LoRa coding rate.
void sx126x_set_lora_crc(sx126x_t *dev, bool crc)
Enable/Disable CRC verification mode.
sx126x_type_t
Used to identify if its a generic SPI module.
Definition: sx126x.h:74
void sx126x_setup(sx126x_t *dev, const sx126x_params_t *params, uint8_t index)
Setup the radio device.
void sx126x_set_lora_preamble_length(sx126x_t *dev, uint16_t preamble)
Sets the LoRa preamble length.
void sx126x_set_lora_payload_length(sx126x_t *dev, uint8_t len)
Sets the payload length.
uint8_t sx126x_get_spreading_factor(const sx126x_t *dev)
Gets the LoRa spreading factor.
static int sx126x_symbol_to_msec(sx126x_t *dev, uint16_t symbols)
Converts symbol value to time in milliseconds.
Definition: sx126x.h:141
uint8_t sx126x_get_coding_rate(const sx126x_t *dev)
Gets the LoRa coding rate.
void sx126x_set_channel(sx126x_t *dev, uint32_t freq)
Sets the channel RF frequency.
sx126x_rf_mode_t
RF switch states.
Definition: sx126x.h:45
uint8_t sx126x_get_bandwidth(const sx126x_t *dev)
Gets the LoRa bandwidth.
uint32_t sx126x_get_channel(const sx126x_t *dev)
Gets the channel RF frequency.
bool sx126x_get_lora_implicit_header(const sx126x_t *dev)
Gets the LoRa implicit header mode.
bool sx126x_get_lora_crc(const sx126x_t *dev)
Checks if CRC verification mode is enabled.
uint8_t sx126x_get_lora_payload_length(const sx126x_t *dev)
Gets the payload length.
bool sx126x_get_lora_iq_invert(const sx126x_t *dev)
Checks if the LoRa inverted IQ mode is enabled/disabled.
Low-level SPI peripheral driver interface definition.
Structure to hold driver state.
Definition: netdev.h:351
Device initialization parameters.
Definition: sx126x.h:85
gpio_t nss_pin
SPI NSS pin.
Definition: sx126x.h:87
gpio_t busy_pin
Busy pin.
Definition: sx126x.h:89
spi_t spi
SPI device.
Definition: sx126x.h:86
gpio_t dio1_pin
Dio1 pin.
Definition: sx126x.h:90
sx126x_type_t type
Variant of sx126x.
Definition: sx126x.h:92
sx126x_reg_mod_t regulator
Power regulator mode.
Definition: sx126x.h:91
gpio_t reset_pin
Reset pin.
Definition: sx126x.h:88
Device descriptor for the driver.
Definition: sx126x.h:104
uint32_t channel
Current channel frequency (in Hz)
Definition: sx126x.h:109
bool radio_sleep
Radio sleep status.
Definition: sx126x.h:111
sx126x_mod_params_lora_t mod_params
Lora modulation parameters.
Definition: sx126x.h:108
sx126x_pkt_params_lora_t pkt_params
Lora packet parameters.
Definition: sx126x.h:107
netdev_t netdev
Netdev parent struct.
Definition: sx126x.h:105
uint16_t rx_timeout
Rx Timeout in terms of symbols.
Definition: sx126x.h:110
sx126x_params_t * params
Initialization parameters.
Definition: sx126x.h:106