From 17ad0fc5a72818b2c50d57f1c102d94d3d77fea1 Mon Sep 17 00:00:00 2001 From: Akshai M Date: Wed, 7 Jul 2021 13:38:23 +0200 Subject: [PATCH] driver/sx126x : Add set_rf_mode interface --- drivers/include/sx126x.h | 24 ++++++++++++++++++++++-- drivers/sx126x/include/sx126x_params.h | 14 +++++++++++++- drivers/sx126x/sx126x_netdev.c | 11 +++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/drivers/include/sx126x.h b/drivers/include/sx126x.h index 0221d92272..78d0eba604 100644 --- a/drivers/include/sx126x.h +++ b/drivers/include/sx126x.h @@ -34,6 +34,20 @@ extern "C" { #endif +/** + * * @note Forward declaration of the SX126x device descriptor + */ +typedef struct sx126x sx126x_t; + +/** + * @brief RF switch states + */ +typedef enum { + SX126X_RF_MODE_RX, + SX126X_RF_MODE_TX_LPA, + SX126X_RF_MODE_TX_HPA, +} sx126x_rf_mode_t; + /** * @brief Whether there's only one variant of this driver at compile time or * not. @@ -76,12 +90,18 @@ typedef struct { gpio_t dio1_pin; /**< Dio1 pin */ sx126x_reg_mod_t regulator; /**< Power regulator mode */ sx126x_type_t type; /**< Variant of sx126x */ +#if IS_USED(MODULE_SX126X_RF_SWITCH) + /** + * @ brief Interface to set RF switch parameters + */ + void(*set_rf_mode)(sx126x_t *dev, sx126x_rf_mode_t rf_mode); +#endif } sx126x_params_t; /** * @brief Device descriptor for the driver */ -typedef struct { +struct sx126x { netdev_t netdev; /**< Netdev parent struct */ sx126x_params_t *params; /**< Initialization parameters */ sx126x_pkt_params_lora_t pkt_params; /**< Lora packet parameters */ @@ -89,7 +109,7 @@ typedef struct { uint32_t channel; /**< Current channel frequency (in Hz) */ uint8_t rx_timeout; /**< Rx Timeout in terms of symbols */ bool radio_sleep; /**< Radio sleep status */ -} sx126x_t; +}; /** * @brief Setup the radio device diff --git a/drivers/sx126x/include/sx126x_params.h b/drivers/sx126x/include/sx126x_params.h index c8cd74d8e3..bf40e87919 100644 --- a/drivers/sx126x/include/sx126x_params.h +++ b/drivers/sx126x/include/sx126x_params.h @@ -57,6 +57,10 @@ extern "C" { #define SX126X_PARAM_REGULATOR SX126X_REG_MODE_DCDC #endif +#ifndef SX126X_PARAM_SET_RF_MODE_CB +#define SX126X_PARAM_SET_RF_MODE_CB NULL +#endif + #ifndef SX126X_PARAM_TYPE # if IS_USED(MODULE_SX1261) # define SX126X_PARAM_TYPE SX126X_TYPE_SX1261 @@ -73,13 +77,21 @@ extern "C" { # endif #endif +#if IS_USED(MODULE_SX126X_RF_SWITCH) +#define SX126X_SET_RF_MODE .set_rf_mode = SX126X_PARAM_SET_RF_MODE_CB +#else +#define SX126X_SET_RF_MODE +#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 } + .regulator = SX126X_PARAM_REGULATOR, \ + SX126X_SET_RF_MODE } + /**@}*/ /** diff --git a/drivers/sx126x/sx126x_netdev.c b/drivers/sx126x/sx126x_netdev.c index 2287fe7921..cc11b3f388 100644 --- a/drivers/sx126x/sx126x_netdev.c +++ b/drivers/sx126x/sx126x_netdev.c @@ -311,6 +311,12 @@ static int _set_state(sx126x_t *dev, netopt_state_t state) case NETOPT_STATE_IDLE: case NETOPT_STATE_RX: DEBUG("[sx126x] netdev: set NETOPT_STATE_RX state\n"); +#if IS_USED(MODULE_SX126X_RF_SWITCH) + /* Refer Section 4.2 RF Switch in Application Note (AN5406) */ + if (dev->params->set_rf_mode) { + dev->params->set_rf_mode(dev, SX126X_RF_MODE_RX); + } +#endif sx126x_cfg_rx_boosted(dev, true); int _timeout = (sx126x_symbol_to_msec(dev, dev->rx_timeout)); if (_timeout != 0) { @@ -323,6 +329,11 @@ static int _set_state(sx126x_t *dev, netopt_state_t state) case NETOPT_STATE_TX: DEBUG("[sx126x] netdev: set NETOPT_STATE_TX state\n"); +#if IS_USED(MODULE_SX126X_RF_SWITCH) + if (dev->params->set_rf_mode) { + dev->params->set_rf_mode(dev, SX126X_RF_MODE_TX_LPA); + } +#endif sx126x_set_tx(dev, 0); break;