1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #16597 from jia200x/pr/sx126x_kconfig

sx126x: add support for multiple simultaneous variants
This commit is contained in:
Francisco 2021-07-06 13:19:15 +02:00 committed by GitHub
commit ccfd3ee980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 222 additions and 40 deletions

View File

@ -32,6 +32,27 @@
extern "C" {
#endif
/**
* @brief Whether there's only one variant of this driver at compile time or
* not.
*/
#define SX126X_SINGLE (( \
IS_USED(MODULE_SX1261) \
+ IS_USED(MODULE_SX1262) \
+ IS_USED(MODULE_SX1268) \
+ IS_USED(MODULE_LLCC68) \
) == 1)
/**
* @brief Variant of the SX126x driver.
*/
typedef enum {
SX126X_TYPE_SX1261,
SX126X_TYPE_SX1262,
SX126X_TYPE_SX1268,
SX126X_TYPE_LLCC68,
} sx126x_type_t;
/**
* @brief Device initialization parameters
*/
@ -42,6 +63,7 @@ typedef struct {
gpio_t busy_pin; /**< Busy pin */
gpio_t dio1_pin; /**< Dio1 pin */
sx126x_reg_mod_t regulator; /**< Power regulator mode */
sx126x_type_t type; /**< Variant of sx126x */
} sx126x_params_t;
/**

View File

@ -1,14 +1,44 @@
# Copyright (c) 2021 Inria
# Copyright (c) 2021 HAW Hamburg
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
menu "Driver sx126x"
depends on TEST_KCONFIG
config MODULE_SX126X
bool "SX126X LoRa Sub-GHz radio"
depends on HAS_PERIPH_GPIO_IRQ
depends on PACKAGE_DRIVER_SX126X
depends on TEST_KCONFIG
select MODULE_PERIPH_GPIO_IRQ
bool
select PACKAGE_DRIVER_SX126X
select MODULE_IOLIST
if HAS_PERIPH_SPI && HAS_PERIPH_GPIO_IRQ
config MODULE_SX1261
bool "SX1261"
select MODULE_SX126X
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
config MODULE_SX1262
bool "SX1262"
select MODULE_SX126X
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
config MODULE_SX1268
bool "SX1268"
select MODULE_SX126X
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
config MODULE_LLCC68
bool "LLCC68"
select MODULE_SX126X
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
endif
endmenu

View File

@ -0,0 +1,111 @@
/*
* Copyright (C) 2021 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup drivers_sx126x_internal SX1261/2/8 and LLCC68 internal functions
* @ingroup drivers_sx126x
* @brief Internal functions for the SX1261/2/8 and LLCC68
*
* @{
*
* @file
*
* @author José I. Alamos <jose.alamos@haw-hamburg.de>
*/
#ifndef SX126X_INTERNAL_H
#define SX126X_INTERNAL_H
#include <assert.h>
#include "sx126x.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Check whether the device model is sx1261
*
* @param[in] dev Device descriptor of the driver
*
* @retval true if the device is sx1261
* @retval false otherwise
*/
static inline bool sx126x_is_sx1261(sx126x_t *dev)
{
assert(dev);
if (SX126X_SINGLE) {
return IS_USED(MODULE_SX1261);
}
else {
return dev->params->type == SX126X_TYPE_SX1261;
}
}
/**
* @brief Check whether the device model is sx1262
*
* @param[in] dev Device descriptor of the driver
*
* @retval true if the device is sx1262
* @retval false otherwise
*/
static inline bool sx126x_is_sx1262(sx126x_t *dev)
{
assert(dev);
if (SX126X_SINGLE) {
return IS_USED(MODULE_SX1262);
}
else {
return dev->params->type == SX126X_TYPE_SX1262;
}
}
/**
* @brief Check whether the device model is llcc68
*
* @param[in] dev Device descriptor of the driver
*
* @retval true if the device is llcc68
* @retval false otherwise
*/
static inline bool sx126x_is_llcc68(sx126x_t *dev)
{
assert(dev);
if (SX126X_SINGLE) {
return IS_USED(MODULE_LLCC68);
}
else {
return dev->params->type == SX126X_TYPE_LLCC68;
}
}
/**
* @brief Check whether the device model is sx1268
*
* @param[in] dev Device descriptor of the driver
*
* @retval true if the device is sx1268
* @retval false otherwise
*/
static inline bool sx126x_is_sx1268(sx126x_t *dev)
{
assert(dev);
if (SX126X_SINGLE) {
return IS_USED(MODULE_SX1268);
}
else {
return dev->params->type == SX126X_TYPE_SX1268;
}
}
#ifdef __cplusplus
}
#endif
#endif /* SX126X_INTERNAL_H */
/** @} */

View File

@ -57,11 +57,26 @@ extern "C" {
#define SX126X_PARAM_REGULATOR SX126X_REG_MODE_DCDC
#endif
#define SX126X_PARAMS { .spi = SX126X_PARAM_SPI, \
.nss_pin = SX126X_PARAM_SPI_NSS, \
#ifndef SX126X_PARAM_TYPE
# if IS_USED(MODULE_SX1261)
# define SX126X_PARAM_TYPE SX126X_TYPE_SX1261
# elif IS_USED(MODULE_SX1262)
# define SX126X_PARAM_TYPE SX126X_TYPE_SX1262
# elif IS_USED(MODULE_SX1268)
# define SX126X_PARAM_TYPE SX126X_TYPE_SX1268
# elif IS_USED(MODULE_LLCC68)
# define SX126X_PARAM_TYPE SX126X_TYPE_LLCC68
# else
# error "You should select at least one of the SX126x variants."
# endif
#endif
#define SX126X_PARAMS { .spi = SX126X_PARAM_SPI, \
.nss_pin = SX126X_PARAM_SPI_NSS, \
.reset_pin = SX126X_PARAM_RESET, \
.busy_pin = SX126X_PARAM_BUSY, \
.dio1_pin = SX126X_PARAM_DIO1, \
.type = SX126X_PARAM_TYPE, \
.regulator = SX126X_PARAM_REGULATOR }
/**@}*/

View File

@ -28,6 +28,7 @@
#include "sx126x_driver.h"
#include "sx126x.h"
#include "sx126x_params.h"
#include "sx126x_internal.h"
#define ENABLE_DEBUG 0
#include "debug.h"
@ -48,6 +49,27 @@
#define CONFIG_SX126X_RAMP_TIME_DEFAULT (SX126X_RAMP_10_US)
#endif
const sx126x_pa_cfg_params_t sx1262_pa_cfg = {
.pa_duty_cycle = 0x02,
.hp_max = 0x02,
.device_sel = 0x00,
.pa_lut = 0x01
};
const sx126x_pa_cfg_params_t sx1268_pa_cfg = {
.pa_duty_cycle = 0x04,
.hp_max = 0x06,
.device_sel = 0x00,
.pa_lut = 0x01
};
const sx126x_pa_cfg_params_t sx1261_pa_cfg = {
.pa_duty_cycle = 0x04,
.hp_max = 0x00,
.device_sel = 0x01,
.pa_lut = 0x01
};
void sx126x_setup(sx126x_t *dev, const sx126x_params_t *params, uint8_t index)
{
netdev_t *netdev = (netdev_t *)dev;
@ -83,32 +105,14 @@ static void sx126x_init_default_config(sx126x_t *dev)
* Values used here comes from the datasheet, section 13.1.14 SetPaConfig
* and are optimal for a TX output power of 14dBm.
*/
if (IS_USED(MODULE_LLCC68) || IS_USED(MODULE_SX1262)) {
sx126x_pa_cfg_params_t pa_cfg = {
.pa_duty_cycle = 0x02,
.hp_max = 0x02,
.device_sel = 0x00,
.pa_lut = 0x01
};
sx126x_set_pa_cfg(dev, &pa_cfg);
if (sx126x_is_llcc68(dev) || sx126x_is_sx1262(dev)) {
sx126x_set_pa_cfg(dev, &sx1262_pa_cfg);
}
else if (IS_USED(MODULE_SX1268)) {
sx126x_pa_cfg_params_t pa_cfg = {
.pa_duty_cycle = 0x04,
.hp_max = 0x06,
.device_sel = 0x00,
.pa_lut = 0x01
};
sx126x_set_pa_cfg(dev, &pa_cfg);
else if (sx126x_is_sx1268(dev)) {
sx126x_set_pa_cfg(dev, &sx1268_pa_cfg);
}
else { /* IS_USED(MODULE_SX1261) */
sx126x_pa_cfg_params_t pa_cfg = {
.pa_duty_cycle = 0x04,
.hp_max = 0x00,
.device_sel = 0x01,
.pa_lut = 0x01
};
sx126x_set_pa_cfg(dev, &pa_cfg);
else { /* sx126x_is_sx1261(dev) */
sx126x_set_pa_cfg(dev, &sx1261_pa_cfg);
}
sx126x_set_tx_params(dev, CONFIG_SX126X_TX_POWER_DEFAULT, CONFIG_SX126X_RAMP_TIME_DEFAULT);

View File

@ -29,15 +29,13 @@
#include "sx126x.h"
#include "sx126x_netdev.h"
#include "sx126x_internal.h"
#define ENABLE_DEBUG 0
#include "debug.h"
#if IS_USED(MODULE_LLCC68)
#define SX126X_MAX_SF LORA_SF11
#else
#define SX126X_MAX_SF LORA_SF12
#endif
const uint8_t llcc68_max_sf = LORA_SF11;
const uint8_t sx126x_max_sf = LORA_SF12;
static int _send(netdev_t *netdev, const iolist_t *iolist)
{
@ -358,7 +356,10 @@ static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len)
case NETOPT_SPREADING_FACTOR:
assert(len <= sizeof(uint8_t));
uint8_t sf = *((const uint8_t *)val);
if ((sf < LORA_SF6) || (sf > SX126X_MAX_SF)) {
const uint8_t max_sf = sx126x_is_llcc68(dev)
? llcc68_max_sf
: sx126x_max_sf;
if ((sf < LORA_SF6) || (sf > max_sf)) {
res = -EINVAL;
break;
}

View File

@ -1,7 +1,6 @@
# this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration.
CONFIG_MODULE_SX126X=y
CONFIG_PACKAGE_DRIVER_SX126X=y
CONFIG_MODULE_SX1261=y
CONFIG_MODULE_SHELL=y
CONFIG_MODULE_SHELL_COMMANDS=y