|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <nrf_error.h> |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +#include <ble_radio_notification.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | + |
| 13 | +#if CONFIG_UNITY |
| 14 | +#include <cmsis.h> |
| 15 | +#define STATIC |
| 16 | +#else |
| 17 | +#define STATIC static |
| 18 | +#endif |
| 19 | + |
| 20 | +LOG_MODULE_REGISTER(ble_radio_ntf, CONFIG_BLE_RADIO_NOTIFICATION_LOG_LEVEL); |
| 21 | + |
| 22 | +/* Current radio state. */ |
| 23 | +#if CONFIG_BLE_RADIO_NOTIFICATION_ON_ACTIVE |
| 24 | +static bool radio_active = true; |
| 25 | +#else |
| 26 | +static bool radio_active; |
| 27 | +#endif |
| 28 | + |
| 29 | +/* Application event handler for handling Radio Notification events. */ |
| 30 | +static ble_radio_notification_evt_handler_t evt_handler; |
| 31 | + |
| 32 | +STATIC void radio_notification_isr(const void *arg) |
| 33 | +{ |
| 34 | + ARG_UNUSED(arg); |
| 35 | + |
| 36 | +#if defined(CONFIG_BLE_RADIO_NOTIFICATION_ON_BOTH) |
| 37 | + radio_active = !radio_active; |
| 38 | +#endif |
| 39 | + |
| 40 | + evt_handler(radio_active); |
| 41 | +} |
| 42 | + |
| 43 | +uint32_t ble_radio_notification_init(uint32_t distance, |
| 44 | + ble_radio_notification_evt_handler_t _evt_handler) |
| 45 | +{ |
| 46 | + uint8_t type = NRF_RADIO_NOTIFICATION_TYPE_INT_ON_BOTH; |
| 47 | + |
| 48 | + if (!_evt_handler) { |
| 49 | + return NRF_ERROR_NULL; |
| 50 | + } |
| 51 | + |
| 52 | + #if defined(CONFIG_BLE_RADIO_NOTIFICATION_ON_ACTIVE) |
| 53 | + type = NRF_RADIO_NOTIFICATION_TYPE_INT_ON_ACTIVE; |
| 54 | +#elif defined(CONFIG_BLE_RADIO_NOTIFICATION_ON_INACTIVE) |
| 55 | + type = NRF_RADIO_NOTIFICATION_TYPE_INT_ON_INACTIVE; |
| 56 | +#endif |
| 57 | + |
| 58 | + evt_handler = _evt_handler; |
| 59 | + |
| 60 | + /* Initialize Radio Notification software interrupt */ |
| 61 | + IRQ_DIRECT_CONNECT(RADIO_NOTIFICATION_IRQn, CONFIG_BLE_RADIO_NOTIFICATION_IRQ_PRIO, |
| 62 | + radio_notification_isr, 0); |
| 63 | + |
| 64 | + NVIC_ClearPendingIRQ(RADIO_NOTIFICATION_IRQn); |
| 65 | + NVIC_EnableIRQ(RADIO_NOTIFICATION_IRQn); |
| 66 | + |
| 67 | + return sd_radio_notification_cfg_set(type, distance); |
| 68 | +} |
0 commit comments