|
7 | 7 | #include <stdint.h>
|
8 | 8 | #include "cs_antenna_switch.h"
|
9 | 9 |
|
10 |
| -#include <hal/nrf_gpio.h> |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/devicetree.h> |
| 12 | +#include <zephyr/drivers/gpio.h> |
11 | 13 |
|
12 |
| -#define DEFAULT_CS_ANTENNA_GPIO_PORT NRF_P1 |
13 |
| -#define DEFAULT_CS_ANTENNA_BASE_PIN (11) |
14 |
| -#define DEFAULT_CS_ANTENNA_PIN_MASK (0xF << DEFAULT_CS_ANTENNA_BASE_PIN) |
| 14 | +#if DT_NODE_EXISTS(DT_NODELABEL(cs_antenna_switch)) |
| 15 | +#define ANTENNA_SWITCH_NODE DT_NODELABEL(cs_antenna_switch) |
| 16 | +#else |
| 17 | +#define ANTENNA_SWITCH_NODE DT_INVALID_NODE |
| 18 | +#error No channel sounding antenna switch nodes registered in DTS. |
| 19 | +#endif |
| 20 | + |
| 21 | +#if !DT_NODE_HAS_COMPAT(ANTENNA_SWITCH_NODE, nordic_bt_cs_antenna_switch) |
| 22 | +#error Configured antenna switch node is not compatible. |
| 23 | +#endif |
| 24 | + |
| 25 | +#if DT_NODE_HAS_PROP(ANTENNA_SWITCH_NODE, multiplexing_mode) |
| 26 | +#define MULTIPLEXED DT_PROP(ANTENNA_SWITCH_NODE, multiplexing_mode) |
| 27 | +#else |
| 28 | +#define MULTIPLEXED false |
| 29 | +#endif |
| 30 | + |
| 31 | +#define ANTENNA_NOT_SET 0xFF |
| 32 | + |
| 33 | +BUILD_ASSERT(DT_NODE_HAS_PROP(ANTENNA_SWITCH_NODE, ant_gpios)); |
| 34 | + |
| 35 | +#if MULTIPLEXED |
| 36 | +#if CONFIG_BT_CTLR_SDC_CS_NUM_ANTENNAS == 2 |
| 37 | +BUILD_ASSERT(DT_PROP_LEN(ANTENNA_SWITCH_NODE, ant_gpios) >= 1); |
| 38 | +#else |
| 39 | +BUILD_ASSERT(DT_PROP_LEN(ANTENNA_SWITCH_NODE, ant_gpios) >= 2); |
| 40 | +#endif |
| 41 | +#else |
| 42 | +BUILD_ASSERT(DT_PROP_LEN(ANTENNA_SWITCH_NODE, ant_gpios) >= CONFIG_BT_CTLR_SDC_CS_NUM_ANTENNAS); |
| 43 | +#endif /* MULTIPLEXED */ |
| 44 | + |
| 45 | +static uint8_t currently_active_antenna = ANTENNA_NOT_SET; |
| 46 | +static const struct gpio_dt_spec gpio_dt_spec_table[] = { |
| 47 | + GPIO_DT_SPEC_GET_BY_IDX_OR(ANTENNA_SWITCH_NODE, ant_gpios, 0, {0}), |
| 48 | + GPIO_DT_SPEC_GET_BY_IDX_OR(ANTENNA_SWITCH_NODE, ant_gpios, 1, {0}), |
| 49 | +#if !MULTIPLEXED |
| 50 | + GPIO_DT_SPEC_GET_BY_IDX_OR(ANTENNA_SWITCH_NODE, ant_gpios, 2, {0}), |
| 51 | + GPIO_DT_SPEC_GET_BY_IDX_OR(ANTENNA_SWITCH_NODE, ant_gpios, 3, {0}), |
| 52 | +#endif |
| 53 | +}; |
15 | 54 |
|
16 | 55 | /* Antenna control below is implemented as described in the CS documentation.
|
17 |
| - * https://docs.nordicsemi.com/bundle/ncs-latest/page/nrfxlib/softdevice_controller/doc/channel_sounding.html#multiple_antennas_support |
18 |
| - * The board has four antenna ports (ANT1-ANT4) that can be individually |
19 |
| - * enabled by setting the connected gpio pin high, while setting the gpio |
20 |
| - * pins for the other antenna ports low. |
21 | 56 | *
|
22 |
| - * Map of antenna ports to gpio pins for nrf54xDK: |
23 |
| - * ANT1 <----> P1.11 |
24 |
| - * ANT2 <----> P1.12 |
25 |
| - * ANT3 <----> P1.13 |
26 |
| - * ANT4 <----> P1.14 |
| 57 | + * Example valid device tree configuration for the nRF54L15: |
| 58 | + * |
| 59 | + * / { |
| 60 | + * cs_antenna_switch: cs-antenna-config { |
| 61 | + * status = "okay"; |
| 62 | + * compatible = "nordic,bt-cs-antenna-switch"; |
| 63 | + * ant-gpios = <&gpio1 11 (GPIO_ACTIVE_HIGH)>, |
| 64 | + * <&gpio1 12 (GPIO_ACTIVE_HIGH)>, |
| 65 | + * <&gpio1 13 (GPIO_ACTIVE_HIGH)>, |
| 66 | + * <&gpio1 14 (GPIO_ACTIVE_HIGH)>; |
| 67 | + * multiplexing-mode = <0>; |
| 68 | + * }; |
| 69 | + * }; |
| 70 | + * |
27 | 71 | */
|
28 | 72 | void cs_antenna_switch_func(uint8_t antenna_number)
|
29 | 73 | {
|
30 |
| - uint32_t out = nrf_gpio_port_out_read(DEFAULT_CS_ANTENNA_GPIO_PORT); |
| 74 | + int err; |
| 75 | +#if MULTIPLEXED |
| 76 | + err = gpio_pin_set_dt(&gpio_dt_spec_table[0], antenna_number & (1 << 0)); |
| 77 | + __ASSERT_NO_MSG(err == 0); |
31 | 78 |
|
32 |
| - out &= ~DEFAULT_CS_ANTENNA_PIN_MASK; |
33 |
| - out |= 1 << (DEFAULT_CS_ANTENNA_BASE_PIN + antenna_number); |
34 |
| - nrf_gpio_port_out_write(DEFAULT_CS_ANTENNA_GPIO_PORT, out); |
| 79 | + err = gpio_pin_set_dt(&gpio_dt_spec_table[1], antenna_number & (1 << 1)); |
| 80 | + __ASSERT_NO_MSG(err == 0); |
| 81 | +#else |
| 82 | + if (currently_active_antenna != antenna_number) { |
| 83 | + if (currently_active_antenna != ANTENNA_NOT_SET) { |
| 84 | + err = gpio_pin_set_dt(&gpio_dt_spec_table[currently_active_antenna], false); |
| 85 | + __ASSERT_NO_MSG(err == 0); |
| 86 | + } |
| 87 | + |
| 88 | + err = gpio_pin_set_dt(&gpio_dt_spec_table[antenna_number], true); |
| 89 | + __ASSERT_NO_MSG(err == 0); |
| 90 | + } |
| 91 | + |
| 92 | + currently_active_antenna = antenna_number; |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +void cs_antenna_switch_init(void) |
| 97 | +{ |
| 98 | + int err; |
| 99 | + |
| 100 | + for (uint8_t i = 0; i < DT_PROP_LEN(ANTENNA_SWITCH_NODE, ant_gpios); i++) { |
| 101 | + err = gpio_pin_configure_dt(&gpio_dt_spec_table[i], GPIO_OUTPUT_INACTIVE); |
| 102 | + __ASSERT(err == 0, "Failed to initialize GPIOs for CS (%d)", ret); |
| 103 | + } |
35 | 104 | }
|
36 | 105 |
|
37 |
| -void cs_antenna_switch_enable(void) |
| 106 | +void cs_antenna_switch_clear(void) |
38 | 107 | {
|
39 |
| - nrf_gpio_port_dir_output_set(DEFAULT_CS_ANTENNA_GPIO_PORT, DEFAULT_CS_ANTENNA_PIN_MASK); |
| 108 | + int err; |
| 109 | + |
| 110 | + for (uint8_t i = 0; i < DT_PROP_LEN(ANTENNA_SWITCH_NODE, ant_gpios); i++) { |
| 111 | + err = gpio_pin_set_dt(&gpio_dt_spec_table[i], false); |
| 112 | + __ASSERT_NO_MSG(err == 0); |
| 113 | + } |
| 114 | + |
| 115 | + currently_active_antenna = ANTENNA_NOT_SET; |
40 | 116 | }
|
0 commit comments