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 
9 #pragma once
10 
23 #include <assert.h>
24 
25 #include_next "sx126x.h"
26 
27 #include "net/netdev.h"
28 
29 #include "periph/gpio.h"
30 #include "periph/spi.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 typedef struct sx126x sx126x_t;
40 
44 typedef enum {
45  SX126X_RF_MODE_RX,
46  SX126X_RF_MODE_TX_LPA,
47  SX126X_RF_MODE_TX_HPA,
49 
54 #define SX126X_SINGLE (( \
55  IS_USED(MODULE_SX1261) \
56  + IS_USED(MODULE_SX1262) \
57  + IS_USED(MODULE_SX1268) \
58  + IS_USED(MODULE_LLCC68) \
59  + IS_USED(MODULE_SX126X_STM32WL) \
60  ) == 1)
61 
65 #if (IS_USED(MODULE_SX1261) || IS_USED(MODULE_SX1262) || \
66  IS_USED(MODULE_SX1268) || IS_USED(MODULE_LLCC68))
67 #define SX126X_SPI 1
68 #endif
69 
73 typedef enum {
74  SX126X_TYPE_SX1261,
75  SX126X_TYPE_SX1262,
76  SX126X_TYPE_SX1268,
77  SX126X_TYPE_LLCC68,
78  SX126X_TYPE_STM32WL,
80 
84 typedef enum {
89 
93 typedef enum {
98 
102 #define SX126X_IRQ_MASK_ALL (SX126X_IRQ_TX_DONE | SX126X_IRQ_RX_DONE | \
103  SX126X_IRQ_PREAMBLE_DETECTED | SX126X_IRQ_SYNC_WORD_VALID | \
104  SX126X_IRQ_HEADER_VALID | SX126X_IRQ_HEADER_ERROR | \
105  SX126X_IRQ_CRC_ERROR | SX126X_IRQ_CAD_DONE | \
106  SX126X_IRQ_CAD_DETECTED | SX126X_IRQ_TIMEOUT)
107 
111 typedef struct {
112  spi_t spi;
113  gpio_t nss_pin;
114  gpio_t reset_pin;
115  gpio_t busy_pin;
116  gpio_t dio1_pin;
117 #if IS_USED(MODULE_SX126X_DIO2)
118  sx126x_dio2_mode_t dio2_mode;
119 #endif
120 #if IS_USED(MODULE_SX126X_DIO3)
121  sx126x_dio3_mode_t dio3_mode;
122  struct {
123  unsigned tcxo_volt :8;
124  unsigned tcxo_timeout :24;
126  } dio3_arg;
127 #endif
128  sx126x_reg_mod_t regulator;
130 #if IS_USED(MODULE_SX126X_RF_SWITCH)
134  void(*set_rf_mode)(sx126x_t *dev, sx126x_rf_mode_t rf_mode);
135  sx126x_rf_mode_t tx_pa_mode;
136 #endif
138 
142 struct sx126x {
145  sx126x_pkt_params_lora_t pkt_params;
146  sx126x_mod_params_lora_t mod_params;
147  uint32_t channel;
148  uint16_t rx_timeout;
149  bool radio_sleep;
150 };
151 
160 void sx126x_setup(sx126x_t *dev, const sx126x_params_t *params, uint8_t index);
161 
170 
179 static inline int sx126x_symbol_to_msec(sx126x_t *dev, uint16_t symbols)
180 {
181  assert(dev && (dev->mod_params.bw <= SX126X_LORA_BW_500) && \
182  (dev->mod_params.bw >= SX126X_LORA_BW_125));
183 
184  /* Refer section 6.1.4 LoRa Time-on-Air in SX1268 datasheet */
185  return (symbols * (1 << (dev->mod_params.sf + 7 - dev->mod_params.bw)) / 1000);
186 }
187 
195 uint32_t sx126x_get_channel(const sx126x_t *dev);
196 
203 void sx126x_set_channel(sx126x_t *dev, uint32_t freq);
204 
212 uint8_t sx126x_get_bandwidth(const sx126x_t *dev);
213 
220 void sx126x_set_bandwidth(sx126x_t *dev, uint8_t bandwidth);
221 
230 
237 void sx126x_set_spreading_factor(sx126x_t *dev, uint8_t sf);
238 
246 uint8_t sx126x_get_coding_rate(const sx126x_t *dev);
247 
254 void sx126x_set_coding_rate(sx126x_t *dev, uint8_t cr);
255 
264 
271 void sx126x_set_lora_payload_length(sx126x_t *dev, uint8_t len);
272 
280 bool sx126x_get_lora_crc(const sx126x_t *dev);
281 
288 void sx126x_set_lora_crc(sx126x_t *dev, bool crc);
289 
298 
306 
315 
322 void sx126x_set_lora_preamble_length(sx126x_t *dev, uint16_t preamble);
323 
332 
339 void sx126x_set_lora_iq_invert(sx126x_t *dev, bool iq_invert);
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:146
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:73
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:179
uint8_t sx126x_get_coding_rate(const sx126x_t *dev)
Gets the LoRa coding rate.
sx126x_dio3_mode_t
Dio3 pin mode.
Definition: sx126x.h:93
sx126x_dio2_mode_t
Dio2 pin mode.
Definition: sx126x.h:84
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:44
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.
@ SX126X_DIO3_UNUSED
Not used.
Definition: sx126x.h:94
@ SX126X_DIO3_IRQ
IRQ pin (ToDo)
Definition: sx126x.h:95
@ SX126X_DIO3_TCXO
TCXO control pin.
Definition: sx126x.h:96
@ SX126X_DIO2_RF_SWITCH
RF switch control pin.
Definition: sx126x.h:87
@ SX126X_DIO2_IRQ
IRQ pin (ToDo)
Definition: sx126x.h:86
@ SX126X_DIO2_UNUSED
Not used.
Definition: sx126x.h:85
Low-level SPI peripheral driver interface definition.
Structure to hold driver state.
Definition: netdev.h:365
Device initialization parameters.
Definition: sx126x.h:111
gpio_t nss_pin
SPI NSS pin.
Definition: sx126x.h:113
gpio_t busy_pin
Busy pin.
Definition: sx126x.h:115
spi_t spi
SPI device.
Definition: sx126x.h:112
gpio_t dio1_pin
Dio1 pin.
Definition: sx126x.h:116
sx126x_type_t type
Variant of sx126x.
Definition: sx126x.h:129
sx126x_reg_mod_t regulator
Power regulator mode.
Definition: sx126x.h:128
gpio_t reset_pin
Reset pin.
Definition: sx126x.h:114
Device descriptor for the driver.
Definition: sx126x.h:142
uint32_t channel
Current channel frequency (in Hz)
Definition: sx126x.h:147
bool radio_sleep
Radio sleep status.
Definition: sx126x.h:149
sx126x_mod_params_lora_t mod_params
Lora modulation parameters.
Definition: sx126x.h:146
sx126x_pkt_params_lora_t pkt_params
Lora packet parameters.
Definition: sx126x.h:145
netdev_t netdev
Netdev parent struct.
Definition: sx126x.h:143
uint16_t rx_timeout
Rx Timeout in terms of symbols.
Definition: sx126x.h:148
sx126x_params_t * params
Initialization parameters.
Definition: sx126x.h:144