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 struct {
85  spi_t spi;
86  gpio_t nss_pin;
87  gpio_t reset_pin;
88  gpio_t busy_pin;
89  gpio_t dio1_pin;
90  sx126x_reg_mod_t regulator;
92 #if IS_USED(MODULE_SX126X_RF_SWITCH)
96  void(*set_rf_mode)(sx126x_t *dev, sx126x_rf_mode_t rf_mode);
97  sx126x_rf_mode_t tx_pa_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 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
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: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: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.
Low-level SPI peripheral driver interface definition.
Structure to hold driver state.
Definition: netdev.h:364
Device initialization parameters.
Definition: sx126x.h:84
gpio_t nss_pin
SPI NSS pin.
Definition: sx126x.h:86
gpio_t busy_pin
Busy pin.
Definition: sx126x.h:88
spi_t spi
SPI device.
Definition: sx126x.h:85
gpio_t dio1_pin
Dio1 pin.
Definition: sx126x.h:89
sx126x_type_t type
Variant of sx126x.
Definition: sx126x.h:91
sx126x_reg_mod_t regulator
Power regulator mode.
Definition: sx126x.h:90
gpio_t reset_pin
Reset pin.
Definition: sx126x.h:87
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