|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file |
| 9 | + * This file implements a generic 1-wire coexistence interface. |
| 10 | + */ |
| 11 | + |
| 12 | +#if defined(CONFIG_MPSL_CX_PIN_FORWARDER) |
| 13 | +#include <string.h> |
| 14 | +#include <soc_secure.h> |
| 15 | +#else |
| 16 | +#include <mpsl_cx_abstract_interface.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include <stddef.h> |
| 20 | +#include <stdint.h> |
| 21 | + |
| 22 | +#include <zephyr/device.h> |
| 23 | +#include <zephyr/devicetree.h> |
| 24 | +#include <zephyr/drivers/gpio.h> |
| 25 | + |
| 26 | +#include "hal/nrf_gpio.h" |
| 27 | +#include <nrfx_gpiote.h> |
| 28 | + |
| 29 | +#if DT_NODE_EXISTS(DT_NODELABEL(nrf_radio_coex)) |
| 30 | +#define CX_NODE DT_NODELABEL(nrf_radio_coex) |
| 31 | +#else |
| 32 | +#define CX_NODE DT_INVALID_NODE |
| 33 | +#error No enabled coex nodes registered in DTS. |
| 34 | +#endif |
| 35 | + |
| 36 | +#if DT_NODE_HAS_PROP(CX_NODE, concurrency_mode) |
| 37 | +#define ALLOW_RX DT_PROP(CX_NODE, concurrency_mode) |
| 38 | +#else |
| 39 | +#define ALLOW_RX false |
| 40 | +#endif |
| 41 | + |
| 42 | +#if !(DT_NODE_HAS_COMPAT(CX_NODE, generic_radio_coex_one_wire)) |
| 43 | +#error Selected coex node is not compatible with generic-radio-coex-one-wire. |
| 44 | +#endif |
| 45 | + |
| 46 | +#if !defined(CONFIG_MPSL_CX_PIN_FORWARDER) |
| 47 | + |
| 48 | +#define GRANT_PIN_PORT_NO DT_PROP(DT_GPIO_CTLR(CX_NODE, grant_gpios), port) |
| 49 | +#define GRANT_PIN_PIN_NO DT_GPIO_PIN(CX_NODE, grant_gpios) |
| 50 | + |
| 51 | +static const nrfx_gpiote_t gpiote = |
| 52 | + NRFX_GPIOTE_INSTANCE(NRF_DT_GPIOTE_INST(CX_NODE, grant_gpios)); |
| 53 | + |
| 54 | +static const struct gpio_dt_spec gra_spec = GPIO_DT_SPEC_GET(CX_NODE, grant_gpios); |
| 55 | + |
| 56 | +static mpsl_cx_cb_t callback; |
| 57 | +static struct gpio_callback grant_cb; |
| 58 | +static uint32_t grant_abs_pin; |
| 59 | + |
| 60 | +static bool grant_pin_is_asserted(void) |
| 61 | +{ |
| 62 | + int ret = gpio_pin_get_dt(&gra_spec); |
| 63 | + |
| 64 | + __ASSERT(ret >= 0, "Error while reading GPIO state."); |
| 65 | + |
| 66 | + return ret; |
| 67 | +} |
| 68 | + |
| 69 | +static mpsl_cx_op_map_t granted_ops_map_get(void) |
| 70 | +{ |
| 71 | +#if ALLOW_RX |
| 72 | + mpsl_cx_op_map_t granted_ops = MPSL_CX_OP_IDLE_LISTEN | MPSL_CX_OP_RX; |
| 73 | +#else |
| 74 | + mpsl_cx_op_map_t granted_ops = MPSL_CX_OP_IDLE_LISTEN; |
| 75 | +#endif |
| 76 | + |
| 77 | + if (grant_pin_is_asserted()) { |
| 78 | + granted_ops |= MPSL_CX_OP_TX | MPSL_CX_OP_RX; |
| 79 | + } |
| 80 | + |
| 81 | + return granted_ops; |
| 82 | +} |
| 83 | + |
| 84 | +static int32_t granted_ops_get(mpsl_cx_op_map_t *granted_ops) |
| 85 | +{ |
| 86 | + *granted_ops = granted_ops_map_get(); |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +static void gpiote_irq_handler(const struct device *gpiob, struct gpio_callback *cb, uint32_t pins) |
| 91 | +{ |
| 92 | + ARG_UNUSED(gpiob); |
| 93 | + ARG_UNUSED(cb); |
| 94 | + ARG_UNUSED(pins); |
| 95 | + |
| 96 | + if (callback) { |
| 97 | + mpsl_cx_op_map_t granted_ops = granted_ops_map_get(); |
| 98 | + |
| 99 | + callback(granted_ops); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +static int32_t request(const mpsl_cx_request_t *req_params) |
| 104 | +{ |
| 105 | + ARG_UNUSED(req_params); |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int32_t release(void) |
| 110 | +{ |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static uint32_t req_grant_delay_get(void) |
| 115 | +{ |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +static int32_t register_callback(mpsl_cx_cb_t cb) |
| 120 | +{ |
| 121 | + callback = cb; |
| 122 | + |
| 123 | + if (cb) { |
| 124 | + nrfx_gpiote_trigger_enable(&gpiote, grant_abs_pin, true); |
| 125 | + } else { |
| 126 | + nrfx_gpiote_trigger_disable(&gpiote, grant_abs_pin); |
| 127 | + } |
| 128 | + |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +static const mpsl_cx_interface_t m_mpsl_cx_methods = { |
| 133 | + .p_request = request, |
| 134 | + .p_release = release, |
| 135 | + .p_granted_ops_get = granted_ops_get, |
| 136 | + .p_req_grant_delay_get = req_grant_delay_get, |
| 137 | + .p_register_callback = register_callback, |
| 138 | +}; |
| 139 | + |
| 140 | +static int mpsl_cx_init(void) |
| 141 | +{ |
| 142 | + int32_t ret; |
| 143 | + |
| 144 | + callback = NULL; |
| 145 | + |
| 146 | + ret = mpsl_cx_interface_set(&m_mpsl_cx_methods); |
| 147 | + if (ret != 0) { |
| 148 | + return ret; |
| 149 | + } |
| 150 | + |
| 151 | + ret = gpio_pin_configure_dt(&gra_spec, GPIO_INPUT); |
| 152 | + if (ret != 0) { |
| 153 | + return ret; |
| 154 | + } |
| 155 | + |
| 156 | + ret = gpio_pin_interrupt_configure_dt(&gra_spec, |
| 157 | + GPIO_INT_ENABLE | GPIO_INT_EDGE | GPIO_INT_EDGE_BOTH); |
| 158 | + if (ret != 0) { |
| 159 | + return ret; |
| 160 | + } |
| 161 | + |
| 162 | + grant_abs_pin = NRF_GPIO_PIN_MAP(GRANT_PIN_PORT_NO, GRANT_PIN_PIN_NO); |
| 163 | + nrfx_gpiote_trigger_disable(&gpiote, grant_abs_pin); |
| 164 | + |
| 165 | + gpio_init_callback(&grant_cb, gpiote_irq_handler, BIT(gra_spec.pin)); |
| 166 | + gpio_add_callback(gra_spec.port, &grant_cb); |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +SYS_INIT(mpsl_cx_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); |
| 172 | + |
| 173 | +#else /* !defined(CONFIG_MPSL_CX_PIN_FORWARDER) */ |
| 174 | +static int mpsl_cx_init(void) |
| 175 | +{ |
| 176 | +#if DT_NODE_HAS_PROP(CX_NODE, grant_gpios) |
| 177 | + uint8_t grant_pin = NRF_DT_GPIOS_TO_PSEL(CX_NODE, grant_gpios); |
| 178 | + |
| 179 | + soc_secure_gpio_pin_mcu_select(grant_pin, NRF_GPIO_PIN_SEL_NETWORK); |
| 180 | +#endif |
| 181 | + |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +SYS_INIT(mpsl_cx_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); |
| 186 | + |
| 187 | +#endif /* !defined(CONFIG_MPSL_CX_PIN_FORWARDER) */ |
0 commit comments