drivers: lora: sx12xx: use gpio_dt_spec

Simplify driver implementation by using gpio_dt_spec.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-01-28 23:25:29 +01:00 committed by Anas Nashif
commit ca7f291dd4
5 changed files with 74 additions and 95 deletions

View file

@ -228,7 +228,7 @@ void SX126xAntSwOn(void)
{
#if HAVE_GPIO_ANTENNA_ENABLE
LOG_DBG("Enabling antenna switch");
gpio_pin_set(dev_data.antenna_enable, GPIO_ANTENNA_ENABLE_PIN, 1);
gpio_pin_set_dt(&dev_config.antenna_enable, 1);
#else
LOG_DBG("No antenna switch configured");
#endif
@ -238,7 +238,7 @@ void SX126xAntSwOff(void)
{
#if HAVE_GPIO_ANTENNA_ENABLE
LOG_DBG("Disabling antenna switch");
gpio_pin_set(dev_data.antenna_enable, GPIO_ANTENNA_ENABLE_PIN, 0);
gpio_pin_set_dt(&dev_config.antenna_enable, 0);
#else
LOG_DBG("No antenna switch configured");
#endif
@ -247,14 +247,14 @@ void SX126xAntSwOff(void)
static void sx126x_set_tx_enable(int value)
{
#if HAVE_GPIO_TX_ENABLE
gpio_pin_set(dev_data.tx_enable, GPIO_TX_ENABLE_PIN, value);
gpio_pin_set_dt(&dev_config.tx_enable, value);
#endif
}
static void sx126x_set_rx_enable(int value)
{
#if HAVE_GPIO_RX_ENABLE
gpio_pin_set(dev_data.rx_enable, GPIO_RX_ENABLE_PIN, value);
gpio_pin_set_dt(&dev_config.rx_enable, value);
#endif
}

View file

@ -34,27 +34,23 @@
#define HAVE_GPIO_TX_ENABLE DT_INST_NODE_HAS_PROP(0, tx_enable_gpios)
#define HAVE_GPIO_RX_ENABLE DT_INST_NODE_HAS_PROP(0, rx_enable_gpios)
#define GPIO_ANTENNA_ENABLE_PIN DT_INST_GPIO_PIN(0, antenna_enable_gpios)
#define GPIO_TX_ENABLE_PIN DT_INST_GPIO_PIN(0, tx_enable_gpios)
#define GPIO_RX_ENABLE_PIN DT_INST_GPIO_PIN(0, rx_enable_gpios)
struct sx126x_config {
struct spi_dt_spec bus;
#if HAVE_GPIO_ANTENNA_ENABLE
struct gpio_dt_spec antenna_enable;
#endif
#if HAVE_GPIO_TX_ENABLE
struct gpio_dt_spec tx_enable;
#endif
#if HAVE_GPIO_RX_ENABLE
struct gpio_dt_spec rx_enable;
#endif
};
struct sx126x_data {
struct gpio_callback dio1_irq_callback;
struct k_work dio1_irq_work;
DioIrqHandler *radio_dio_irq;
#if HAVE_GPIO_ANTENNA_ENABLE
const struct device *antenna_enable;
#endif
#if HAVE_GPIO_TX_ENABLE
const struct device *tx_enable;
#endif
#if HAVE_GPIO_RX_ENABLE
const struct device *rx_enable;
#endif
RadioOperatingModes_t mode;
};

View file

@ -97,19 +97,6 @@ BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(semtech_sx1272) +
DT_NUM_INST_STATUS_OKAY(semtech_sx1276) <= 1,
"Multiple SX127x instances in DT");
#define GPIO_RESET_PIN DT_INST_GPIO_PIN(0, reset_gpios)
#define GPIO_ANTENNA_ENABLE_PIN \
DT_INST_GPIO_PIN(0, antenna_enable_gpios)
#define GPIO_RFI_ENABLE_PIN \
DT_INST_GPIO_PIN(0, rfi_enable_gpios)
#define GPIO_RFO_ENABLE_PIN \
DT_INST_GPIO_PIN(0, rfo_enable_gpios)
#define GPIO_PA_BOOST_ENABLE_PIN \
DT_INST_GPIO_PIN(0, pa_boost_enable_gpios)
#define GPIO_TCXO_POWER_PIN DT_INST_GPIO_PIN(0, tcxo_power_gpios)
#if DT_INST_NODE_HAS_PROP(0, tcxo_power_startup_delay_ms)
#define TCXO_POWER_STARTUP_DELAY_MS \
DT_INST_PROP(0, tcxo_power_startup_delay_ms)
@ -153,61 +140,67 @@ BUILD_ASSERT(0, "None of rfo-enable-gpios, pa-boost-enable-gpios and "
extern DioIrqHandler *DioIrq[];
struct sx127x_dio {
const char *port;
gpio_pin_t pin;
gpio_dt_flags_t flags;
};
/* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
#define SX127X_DIO_GPIO_LEN(inst) \
DT_INST_PROP_LEN(inst, dio_gpios)
#define SX127X_DIO_GPIO_ELEM(idx, inst) \
{ \
DT_INST_GPIO_LABEL_BY_IDX(inst, dio_gpios, idx), \
DT_INST_GPIO_PIN_BY_IDX(inst, dio_gpios, idx), \
DT_INST_GPIO_FLAGS_BY_IDX(inst, dio_gpios, idx), \
},
GPIO_DT_SPEC_INST_GET_BY_IDX(inst, dio_gpios, idx),
#define SX127X_DIO_GPIO_INIT(n) \
UTIL_LISTIFY(SX127X_DIO_GPIO_LEN(n), SX127X_DIO_GPIO_ELEM, n)
static const struct sx127x_dio sx127x_dios[] = { SX127X_DIO_GPIO_INIT(0) };
static const struct gpio_dt_spec sx127x_dios[] = { SX127X_DIO_GPIO_INIT(0) };
#define SX127X_MAX_DIO ARRAY_SIZE(sx127x_dios)
struct sx127x_config {
struct spi_dt_spec bus;
struct gpio_dt_spec reset;
#if DT_INST_NODE_HAS_PROP(0, antenna_enable_gpios)
struct gpio_dt_spec antenna_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, rfi_enable_gpios)
struct gpio_dt_spec rfi_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, rfo_enable_gpios)
struct gpio_dt_spec rfo_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, pa_boost_enable_gpios)
struct gpio_dt_spec pa_boost_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, tcxo_power_gpios)
struct gpio_dt_spec tcxo_power;
#endif
};
static const struct sx127x_config dev_config = {
.bus = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0),
#if DT_INST_NODE_HAS_PROP(0, antenna_enable_gpios)
.antenna_enable = GPIO_DT_SPEC_INST_GET(0, antenna_enable_gpios),
#endif
#if DT_INST_NODE_HAS_PROP(0, rfi_enable_gpios)
.rfi_enable = GPIO_DT_SPEC_INST_GET(0, rfi_enable_gpios),
#endif
#if DT_INST_NODE_HAS_PROP(0, rfo_enable_gpios)
.rfo_enable = GPIO_DT_SPEC_INST_GET(0, rfo_enable_gpios),
#endif
#if DT_INST_NODE_HAS_PROP(0, pa_boost_enable_gpios)
.pa_boost_enable = GPIO_DT_SPEC_INST_GET(0, pa_boost_enable_gpios),
#endif
#if DT_INST_NODE_HAS_PROP(0, tcxo_power_gpios)
.tcxo_power = GPIO_DT_SPEC_INST_GET(0, tcxo_power_gpios),
#endif
};
static struct sx127x_data {
const struct device *reset;
#if DT_INST_NODE_HAS_PROP(0, antenna_enable_gpios)
const struct device *antenna_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, rfi_enable_gpios)
const struct device *rfi_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, rfo_enable_gpios)
const struct device *rfo_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, pa_boost_enable_gpios)
const struct device *pa_boost_enable;
#endif
#if DT_INST_NODE_HAS_PROP(0, rfo_enable_gpios) && \
DT_INST_NODE_HAS_PROP(0, pa_boost_enable_gpios)
uint8_t tx_power;
#endif
#if DT_INST_NODE_HAS_PROP(0, tcxo_power_gpios)
const struct device *tcxo_power;
bool tcxo_power_enabled;
#endif
const struct device *dio_dev[SX127X_MAX_DIO];
struct k_work dio_work[SX127X_MAX_DIO];
} dev_data;
@ -236,28 +229,28 @@ uint32_t SX127xGetBoardTcxoWakeupTime(void)
static inline void sx127x_antenna_enable(int val)
{
#if DT_INST_NODE_HAS_PROP(0, antenna_enable_gpios)
gpio_pin_set(dev_data.antenna_enable, GPIO_ANTENNA_ENABLE_PIN, val);
gpio_pin_set_dt(&dev_config.antenna_enable, val);
#endif
}
static inline void sx127x_rfi_enable(int val)
{
#if DT_INST_NODE_HAS_PROP(0, rfi_enable_gpios)
gpio_pin_set(dev_data.rfi_enable, GPIO_RFI_ENABLE_PIN, val);
gpio_pin_set_dt(&dev_config.rfi_enable, val);
#endif
}
static inline void sx127x_rfo_enable(int val)
{
#if DT_INST_NODE_HAS_PROP(0, rfo_enable_gpios)
gpio_pin_set(dev_data.rfo_enable, GPIO_RFO_ENABLE_PIN, val);
gpio_pin_set_dt(&dev_config.rfo_enable, val);
#endif
}
static inline void sx127x_pa_boost_enable(int val)
{
#if DT_INST_NODE_HAS_PROP(0, pa_boost_enable_gpios)
gpio_pin_set(dev_data.pa_boost_enable, GPIO_PA_BOOST_ENABLE_PIN, val);
gpio_pin_set_dt(&dev_config.pa_boost_enable, val);
#endif
}
@ -287,13 +280,13 @@ void SX127xSetBoardTcxo(uint8_t state)
}
if (enable) {
gpio_pin_set(dev_data.tcxo_power, GPIO_TCXO_POWER_PIN, 1);
gpio_pin_set_dt(&dev_config.tcxo_power, 1);
if (TCXO_POWER_STARTUP_DELAY_MS > 0) {
k_sleep(K_MSEC(TCXO_POWER_STARTUP_DELAY_MS));
}
} else {
gpio_pin_set(dev_data.tcxo_power, GPIO_TCXO_POWER_PIN, 0);
gpio_pin_set_dt(&dev_config.tcxo_power, 0);
}
dev_data.tcxo_power_enabled = enable;
@ -326,11 +319,11 @@ void SX127xReset(void)
{
SX127xSetBoardTcxo(true);
gpio_pin_set(dev_data.reset, GPIO_RESET_PIN, 1);
gpio_pin_set_dt(&dev_config.reset, 1);
k_sleep(K_MSEC(1));
gpio_pin_set(dev_data.reset, GPIO_RESET_PIN, 0);
gpio_pin_set_dt(&dev_config.reset, 0);
k_sleep(K_MSEC(6));
}
@ -350,7 +343,7 @@ static void sx127x_irq_callback(const struct device *dev,
pin = find_lsb_set(pins) - 1;
for (i = 0; i < SX127X_MAX_DIO; i++) {
if (dev == dev_data.dio_dev[i] &&
if (dev == sx127x_dios[i].port &&
pin == sx127x_dios[i].pin) {
k_work_submit(&dev_data.dio_work[i]);
}
@ -368,30 +361,27 @@ void SX127xIoIrqInit(DioIrqHandler **irqHandlers)
continue;
}
dev_data.dio_dev[i] = device_get_binding(sx127x_dios[i].port);
if (dev_data.dio_dev[i] == NULL) {
LOG_ERR("Cannot get pointer to %s device",
sx127x_dios[i].port);
if (!device_is_ready(sx127x_dios[i].port)) {
LOG_ERR("GPIO port %s not ready",
sx127x_dios[i].port->name);
return;
}
k_work_init(&dev_data.dio_work[i], sx127x_dio_work_handle);
gpio_pin_configure(dev_data.dio_dev[i], sx127x_dios[i].pin,
GPIO_INPUT | GPIO_INT_DEBOUNCE
| sx127x_dios[i].flags);
gpio_pin_configure_dt(&sx127x_dios[i],
GPIO_INPUT | GPIO_INT_DEBOUNCE);
gpio_init_callback(&callbacks[i],
sx127x_irq_callback,
BIT(sx127x_dios[i].pin));
if (gpio_add_callback(dev_data.dio_dev[i], &callbacks[i]) < 0) {
if (gpio_add_callback(sx127x_dios[i].port, &callbacks[i]) < 0) {
LOG_ERR("Could not set gpio callback.");
return;
}
gpio_pin_interrupt_configure(dev_data.dio_dev[i],
sx127x_dios[i].pin,
GPIO_INT_EDGE_TO_ACTIVE);
gpio_pin_interrupt_configure_dt(&sx127x_dios[i],
GPIO_INT_EDGE_TO_ACTIVE);
}
}
@ -523,7 +513,7 @@ void SX127xSetRfTxPower(int8_t power)
uint32_t SX127xGetDio1PinState(void)
{
#if SX127X_DIO_GPIO_LEN(0) >= 2
if (gpio_pin_get(dev_data.dio_dev[1], sx127x_dios[1].pin) > 0) {
if (gpio_pin_get_dt(&sx127x_dios[1]) > 0) {
return 1U;
}
#endif
@ -609,7 +599,7 @@ static int sx127x_lora_init(const struct device *dev)
}
k_sleep(K_MSEC(100));
gpio_pin_set(dev_data.reset, GPIO_RESET_PIN, 0);
gpio_pin_set_dt(&dev_config.reset, 0);
k_sleep(K_MSEC(100));
ret = sx127x_read(REG_VERSION, &regval, 1);

View file

@ -39,21 +39,19 @@ static struct sx12xx_data {
struct sx12xx_rx_params rx_params;
} dev_data;
int __sx12xx_configure_pin(const struct device **dev, const char *controller,
gpio_pin_t pin, gpio_flags_t flags)
int __sx12xx_configure_pin(const struct gpio_dt_spec *gpio, gpio_flags_t flags)
{
int err;
*dev = device_get_binding(controller);
if (!(*dev)) {
LOG_ERR("Cannot get pointer to %s device", controller);
return -EIO;
if (!device_is_ready(gpio->port)) {
LOG_ERR("GPIO device not ready %s", gpio->port->name);
return -ENODEV;
}
err = gpio_pin_configure(*dev, pin, flags);
err = gpio_pin_configure_dt(gpio, flags);
if (err) {
LOG_ERR("Cannot configure gpio %s %d: %d", controller, pin,
err);
LOG_ERR("Cannot configure gpio %s %d: %d", gpio->port->name,
gpio->pin, err);
return err;
}

View file

@ -13,16 +13,11 @@
#include <drivers/lora.h>
#include <device.h>
int __sx12xx_configure_pin(const struct device * *dev, const char *controller,
gpio_pin_t pin, gpio_flags_t flags);
int __sx12xx_configure_pin(const struct gpio_dt_spec *gpio, gpio_flags_t flags);
#define sx12xx_configure_pin(_name, _flags) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(0, _name##_gpios), \
(__sx12xx_configure_pin(&dev_data._name, \
DT_INST_GPIO_LABEL(0, _name##_gpios), \
DT_INST_GPIO_PIN(0, _name##_gpios), \
DT_INST_GPIO_FLAGS(0, _name##_gpios) | \
_flags)), \
(__sx12xx_configure_pin(&dev_config._name, _flags)),\
(0))
int sx12xx_lora_send(const struct device *dev, uint8_t *data,