From 23e4a8b64e4a2fe811549d0e793577aeb3000961 Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Thu, 9 Jul 2020 22:01:57 +0100 Subject: [PATCH] drivers: lora: Move sx1276 pin helpers to sx12xx common The new sx1276 pin configuration helpers can be used by the sx126x driver as well. Move them to sx126xx_common.{c,h} to facilitate reuse. Signed-off-by: Andreas Sandberg --- drivers/lora/sx1276.c | 43 +++++------------------------------- drivers/lora/sx12xx_common.c | 25 +++++++++++++++++++++ drivers/lora/sx12xx_common.h | 14 ++++++++++++ 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/drivers/lora/sx1276.c b/drivers/lora/sx1276.c index 98a4251950a..8cae924e386 100644 --- a/drivers/lora/sx1276.c +++ b/drivers/lora/sx1276.c @@ -445,57 +445,26 @@ const struct Radio_s Radio = { .SetTxContinuousWave = SX1276SetTxContinuousWave, }; -static inline int __sx1276_configure_pin(struct device **dev, - const char *controller, - gpio_pin_t pin, gpio_flags_t flags) -{ - int err; - - *dev = device_get_binding(controller); - if (!(*dev)) { - LOG_ERR("Cannot get pointer to %s device", controller); - return -EIO; - } - - err = gpio_pin_configure(*dev, pin, flags); - if (err) { - LOG_ERR("Cannot configure gpio %s %d: %d", controller, pin, - err); - return err; - } - - return 0; -} - -#define sx1276_configure_pin(_name, _flags) \ - COND_CODE_1(DT_INST_NODE_HAS_PROP(0, _name##_gpios), \ - (__sx1276_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)), \ - (0)) - static int sx1276_antenna_configure(void) { int ret; - ret = sx1276_configure_pin(antenna_enable, GPIO_OUTPUT_INACTIVE); + ret = sx12xx_configure_pin(antenna_enable, GPIO_OUTPUT_INACTIVE); if (ret) { return ret; } - ret = sx1276_configure_pin(rfi_enable, GPIO_OUTPUT_INACTIVE); + ret = sx12xx_configure_pin(rfi_enable, GPIO_OUTPUT_INACTIVE); if (ret) { return ret; } - ret = sx1276_configure_pin(rfo_enable, GPIO_OUTPUT_INACTIVE); + ret = sx12xx_configure_pin(rfo_enable, GPIO_OUTPUT_INACTIVE); if (ret) { return ret; } - ret = sx1276_configure_pin(pa_boost_enable, GPIO_OUTPUT_INACTIVE); + ret = sx12xx_configure_pin(pa_boost_enable, GPIO_OUTPUT_INACTIVE); if (ret) { return ret; } @@ -538,13 +507,13 @@ static int sx1276_lora_init(struct device *dev) dev_data.spi_cfg.cs = &spi_cs; #endif - ret = sx1276_configure_pin(tcxo_power, GPIO_OUTPUT_INACTIVE); + ret = sx12xx_configure_pin(tcxo_power, GPIO_OUTPUT_INACTIVE); if (ret) { return ret; } /* Setup Reset gpio and perform soft reset */ - ret = sx1276_configure_pin(reset, GPIO_OUTPUT_ACTIVE); + ret = sx12xx_configure_pin(reset, GPIO_OUTPUT_ACTIVE); if (ret) { return ret; } diff --git a/drivers/lora/sx12xx_common.c b/drivers/lora/sx12xx_common.c index 9a414b8b98c..3432e18daf5 100644 --- a/drivers/lora/sx12xx_common.c +++ b/drivers/lora/sx12xx_common.c @@ -1,9 +1,11 @@ /* * Copyright (c) 2019 Manivannan Sadhasivam + * Copyright (c) 2020 Grinn * * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include #include @@ -11,6 +13,8 @@ /* LoRaMac-node specific includes */ #include +#include "sx12xx_common.h" + LOG_MODULE_REGISTER(sx12xx_common, CONFIG_LORA_LOG_LEVEL); static struct sx12xx_data { @@ -22,6 +26,27 @@ static struct sx12xx_data { int16_t rssi; } dev_data; +int __sx12xx_configure_pin(struct device **dev, const char *controller, + gpio_pin_t pin, gpio_flags_t flags) +{ + int err; + + *dev = device_get_binding(controller); + if (!(*dev)) { + LOG_ERR("Cannot get pointer to %s device", controller); + return -EIO; + } + + err = gpio_pin_configure(*dev, pin, flags); + if (err) { + LOG_ERR("Cannot configure gpio %s %d: %d", controller, pin, + err); + return err; + } + + return 0; +} + static void sx12xx_ev_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { diff --git a/drivers/lora/sx12xx_common.h b/drivers/lora/sx12xx_common.h index e435a473be6..a2fc52aeb12 100644 --- a/drivers/lora/sx12xx_common.h +++ b/drivers/lora/sx12xx_common.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020 Andreas Sandberg + * Copyright (c) 2020 Grinn * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,9 +9,22 @@ #define ZEPHYR_DRIVERS_SX12XX_COMMON_H_ #include +#include #include #include +int __sx12xx_configure_pin(struct device **dev, const char *controller, + gpio_pin_t pin, 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)), \ + (0)) + int sx12xx_lora_send(struct device *dev, uint8_t *data, uint32_t data_len); int sx12xx_lora_recv(struct device *dev, uint8_t *data, uint8_t size,