|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <devicetree.h> |
| 10 | +#include <sys/util_macro.h> |
| 11 | +#include <nrfx/hal/nrf_radio.h> |
| 12 | + |
| 13 | +#include "radio_df.h" |
| 14 | + |
| 15 | +/* @brief Minimum antennas number required if antenna switching is enabled */ |
| 16 | +#define DF_ANT_NUM_MIN 2 |
| 17 | +/* @brief Value to set antenna GPIO pin as not connected */ |
| 18 | +#define DF_GPIO_PIN_NOT_SET 0xFF |
| 19 | +/* @brief Number of PSEL_DFEGPIO registers in Radio peripheral */ |
| 20 | +#define DF_PSEL_GPIO_NUM 8 |
| 21 | + |
| 22 | +/* @brief Direction Finding antenna matrix configuration */ |
| 23 | +struct df_ant_cfg { |
| 24 | + uint8_t ant_num; |
| 25 | + /* Selection of GPIOs to be used to switch antennas by Radio */ |
| 26 | + uint8_t dfe_gpio[DF_PSEL_GPIO_NUM]; |
| 27 | +}; |
| 28 | + |
| 29 | +#define RADIO DT_NODELABEL(radio) |
| 30 | +#define DFE_GPIO_PIN(idx) \ |
| 31 | + COND_CODE_1(DT_NODE_HAS_PROP(RADIO, dfegpio##idx##_gpios), \ |
| 32 | + (NRF_DT_GPIOS_TO_PSEL(RADIO, dfegpio##idx##_gpios)), \ |
| 33 | + (DF_GPIO_PIN_NOT_SET)) |
| 34 | + |
| 35 | +#define DFE_GPIO_PIN_DISCONNECT (RADIO_PSEL_DFEGPIO_CONNECT_Disconnected << \ |
| 36 | + RADIO_PSEL_DFEGPIO_CONNECT_Pos) |
| 37 | + |
| 38 | +#define COUNT_GPIO(idx, _) + DT_NODE_HAS_PROP(RADIO, dfegpio##idx##_gpios) |
| 39 | +#define DFE_GPIO_NUM (UTIL_LISTIFY(DF_PSEL_GPIO_NUM, COUNT_GPIO, _)) |
| 40 | + |
| 41 | +/* DFE_GPIO_NUM_IS_ZERO is required to correctly compile COND_CODE_1 in |
| 42 | + * DFE_GPIO_ALLOWED_ANT_NUM macro. DFE_GPIO_NUM does not expand to literal 1 |
| 43 | + * So it is not possible to use it as a conditional in COND_CODE_1 argument. |
| 44 | + */ |
| 45 | +#if (DFE_GPIO_NUM > 0) |
| 46 | +#define DFE_GPIO_NUM_IS_ZERO 1 |
| 47 | +#else |
| 48 | +#define DFE_GPIO_NUM_IS_ZERO EMPTY |
| 49 | +#endif |
| 50 | + |
| 51 | +#define DFE_GPIO_ALLOWED_ANT_NUM COND_CODE_1(DFE_GPIO_NUM_IS_ZERO, \ |
| 52 | + (BIT(DFE_GPIO_NUM)), (0)) |
| 53 | + |
| 54 | +#if IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX) || \ |
| 55 | + IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX) |
| 56 | + |
| 57 | +/* Check if there is enough pins configured to represent each pattern |
| 58 | + * for given antennas number. |
| 59 | + */ |
| 60 | +BUILD_ASSERT((DT_PROP(RADIO, dfe_ant_num) <= DFE_GPIO_ALLOWED_ANT_NUM), "Insufficient " |
| 61 | + "number of GPIO pins configured."); |
| 62 | +BUILD_ASSERT((DT_PROP(RADIO, dfe_ant_num) >= DF_ANT_NUM_MIN), "Insufficient " |
| 63 | + "number of antennas provided."); |
| 64 | + |
| 65 | +/* Check if dfegpio#-gios property has flag cell set to zero */ |
| 66 | +#define DFE_GPIO_PIN_FLAGS(idx) (DT_GPIO_FLAGS(RADIO, dfegpio##idx##_gpios)) |
| 67 | +#define DFE_GPIO_PIN_IS_FLAG_ZERO(idx) \ |
| 68 | + COND_CODE_1(DT_NODE_HAS_PROP(RADIO, dfegpio##idx##_gpios), \ |
| 69 | + (BUILD_ASSERT((DFE_GPIO_PIN_FLAGS(idx) == 0), \ |
| 70 | + "Flags value of GPIO pin property must be" \ |
| 71 | + "zero.")), \ |
| 72 | + (EMPTY)) |
| 73 | + |
| 74 | +#define DFE_GPIO_PIN_LIST(idx, _) idx, |
| 75 | +FOR_EACH(DFE_GPIO_PIN_IS_FLAG_ZERO, (;), |
| 76 | + UTIL_LISTIFY(DF_PSEL_GPIO_NUM, DFE_GPIO_PIN_LIST)) |
| 77 | + |
| 78 | +#if DT_NODE_HAS_STATUS(RADIO, okay) |
| 79 | +const static struct df_ant_cfg ant_cfg = { |
| 80 | + .ant_num = DT_PROP(RADIO, dfe_ant_num), |
| 81 | + .dfe_gpio = { |
| 82 | + DFE_GPIO_PIN(0), |
| 83 | + DFE_GPIO_PIN(1), |
| 84 | + DFE_GPIO_PIN(2), |
| 85 | + DFE_GPIO_PIN(3), |
| 86 | + DFE_GPIO_PIN(4), |
| 87 | + DFE_GPIO_PIN(5), |
| 88 | + DFE_GPIO_PIN(6), |
| 89 | + DFE_GPIO_PIN(7), |
| 90 | + } |
| 91 | +}; |
| 92 | +#else |
| 93 | +#error "DF antenna switching feature requires dfe_ant to be enabled in DTS" |
| 94 | +#endif |
| 95 | + |
| 96 | +#endif /* CONFIG_BT_CTLR_DF_ANT_SWITCH_TX || CONFIG_BT_CTLR_DF_ANT_SWITCH_RX */ |
| 97 | + |
| 98 | +/* @brief Function performs steps related with DF antennas configuration. |
| 99 | + * |
| 100 | + * Sets up DF related PSEL.DFEGPIO registers to give possibility to Radio |
| 101 | + * to drivee antennas switches. |
| 102 | + */ |
| 103 | +void radio_df_ant_configure(void) |
| 104 | +{ |
| 105 | +#if IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX) || \ |
| 106 | + IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX) |
| 107 | + |
| 108 | + for (uint8_t idx = 0; idx < DF_PSEL_GPIO_NUM; ++idx) { |
| 109 | + if (ant_cfg.dfe_gpio[idx] != DF_GPIO_PIN_NOT_SET) { |
| 110 | + nrf_radio_dfe_pattern_pin_set(NRF_RADIO, |
| 111 | + ant_cfg.dfe_gpio[idx], |
| 112 | + idx); |
| 113 | + } else { |
| 114 | + nrf_radio_dfe_pattern_pin_set(NRF_RADIO, |
| 115 | + DFE_GPIO_PIN_DISCONNECT, |
| 116 | + idx); |
| 117 | + } |
| 118 | + } |
| 119 | +#endif /* CONFIG_BT_CTLR_DF_ANT_SWITCH_TX || CONFIG_BT_CTLR_DF_ANT_SWITCH_RX */ |
| 120 | +} |
| 121 | + |
| 122 | +/* @brief Function provides number of available antennas for Direction Finding. |
| 123 | + * |
| 124 | + * The number of antennas is hardware defined. It is provided via devicetree. |
| 125 | + * |
| 126 | + * @return Number of available antennas. |
| 127 | + */ |
| 128 | +uint8_t radio_df_ant_num_get(void) |
| 129 | +{ |
| 130 | +#if IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX) || \ |
| 131 | + IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX) |
| 132 | + return ant_cfg.ant_num; |
| 133 | +#else |
| 134 | + return 0; |
| 135 | +#endif |
| 136 | +} |
0 commit comments