2017-02-06 12:42:34 +01:00
|
|
|
/* ieee802154_nrf5.c - nRF5 802.15.4 driver */
|
|
|
|
|
|
|
|
/*
|
2023-07-18 12:28:33 +02:00
|
|
|
* Copyright (c) 2017-2023 Nordic Semiconductor ASA
|
2017-02-06 12:42:34 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-07-15 16:26:35 +02:00
|
|
|
#define DT_DRV_COMPAT nordic_nrf_ieee802154
|
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
#define LOG_MODULE_NAME ieee802154_nrf5
|
2019-05-10 23:00:03 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_DRIVER_LOG_LEVEL)
|
2018-11-09 15:30:56 +01:00
|
|
|
#define LOG_LEVEL CONFIG_IEEE802154_DRIVER_LOG_LEVEL
|
2019-05-10 23:00:03 +02:00
|
|
|
#else
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_NONE
|
|
|
|
#endif
|
2018-09-03 16:28:47 +02:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/arch/cpu.h>
|
|
|
|
#include <zephyr/debug/stack.h>
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-10-31 18:44:45 +01:00
|
|
|
#include <soc.h>
|
2024-03-08 13:26:21 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) && defined(NRF_FICR_S)
|
2022-03-18 12:57:08 +01:00
|
|
|
#include <soc_secure.h>
|
2024-03-08 13:26:21 +01:00
|
|
|
#else
|
|
|
|
#include <hal/nrf_ficr.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/debug/stack.h>
|
|
|
|
#include <zephyr/net/net_if.h>
|
|
|
|
#include <zephyr/net/net_pkt.h>
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-01-15 11:39:54 +01:00
|
|
|
#if defined(CONFIG_NET_L2_OPENTHREAD)
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/net/openthread.h>
|
2023-07-18 12:28:33 +02:00
|
|
|
#include <zephyr/net/ieee802154_radio_openthread.h>
|
2018-01-15 11:39:54 +01:00
|
|
|
#endif
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/sys/byteorder.h>
|
2017-02-06 12:42:34 +01:00
|
|
|
#include <string.h>
|
2023-10-07 00:38:53 +02:00
|
|
|
#include <zephyr/random/random.h>
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/net/ieee802154_radio.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
#include "ieee802154_nrf5.h"
|
2019-01-17 14:12:27 +01:00
|
|
|
#include "nrf_802154.h"
|
2021-05-18 14:41:39 +02:00
|
|
|
#include "nrf_802154_const.h"
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2021-04-23 13:51:00 +02:00
|
|
|
#if defined(CONFIG_NRF_802154_SER_HOST)
|
2020-11-13 16:39:45 +01:00
|
|
|
#include "nrf_802154_serialization_error.h"
|
|
|
|
#endif
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
struct nrf5_802154_config {
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2017-02-06 12:42:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct nrf5_802154_data nrf5_data;
|
2023-11-22 09:56:03 +01:00
|
|
|
#if defined(CONFIG_IEEE802154_RAW_MODE)
|
|
|
|
static const struct device *nrf5_dev;
|
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2022-10-28 12:22:13 +02:00
|
|
|
#define DRX_SLOT_RX 0 /* Delayed reception window ID */
|
2020-03-20 11:39:21 +01:00
|
|
|
|
2023-10-30 09:21:12 +01:00
|
|
|
#define NSEC_PER_TEN_SYMBOLS (10 * IEEE802154_PHY_OQPSK_780_TO_2450MHZ_SYMBOL_PERIOD_NS)
|
|
|
|
|
2021-02-11 09:09:52 +01:00
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
|
|
|
|
#if defined(CONFIG_SOC_NRF5340_CPUAPP)
|
2022-03-18 12:57:08 +01:00
|
|
|
#if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
|
|
|
|
#error "NRF_UICR->OTP is not supported to read from non-secure"
|
|
|
|
#else
|
2021-02-11 09:09:52 +01:00
|
|
|
#define EUI64_ADDR (NRF_UICR->OTP)
|
2022-03-18 12:57:08 +01:00
|
|
|
#endif /* CONFIG_TRUSTED_EXECUTION_NONSECURE */
|
2021-02-11 09:09:52 +01:00
|
|
|
#else
|
|
|
|
#define EUI64_ADDR (NRF_UICR->CUSTOMER)
|
|
|
|
#endif /* CONFIG_SOC_NRF5340_CPUAPP */
|
|
|
|
#endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
|
|
|
|
|
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
|
|
|
|
#define EUI64_ADDR_HIGH CONFIG_IEEE802154_NRF5_UICR_EUI64_REG
|
|
|
|
#define EUI64_ADDR_LOW (CONFIG_IEEE802154_NRF5_UICR_EUI64_REG + 1)
|
|
|
|
#else
|
|
|
|
#define EUI64_ADDR_HIGH 0
|
|
|
|
#define EUI64_ADDR_LOW 1
|
|
|
|
#endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
|
2020-11-13 16:39:45 +01:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
/* Convenience defines for RADIO */
|
|
|
|
#define NRF5_802154_DATA(dev) \
|
2020-05-28 21:23:02 +02:00
|
|
|
((struct nrf5_802154_data * const)(dev)->data)
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
#define NRF5_802154_CFG(dev) \
|
2020-05-28 20:44:16 +02:00
|
|
|
((const struct nrf5_802154_config * const)(dev)->config)
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2020-07-28 12:36:26 +02:00
|
|
|
#if CONFIG_IEEE802154_VENDOR_OUI_ENABLE
|
|
|
|
#define IEEE802154_NRF5_VENDOR_OUI CONFIG_IEEE802154_VENDOR_OUI
|
|
|
|
#else
|
|
|
|
#define IEEE802154_NRF5_VENDOR_OUI (uint32_t)0xF4CE36
|
|
|
|
#endif
|
|
|
|
|
2023-11-22 09:56:03 +01:00
|
|
|
static inline const struct device *nrf5_get_device(void)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_IEEE802154_RAW_MODE)
|
|
|
|
return nrf5_dev;
|
|
|
|
#else
|
|
|
|
return net_if_get_device(nrf5_data.iface);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static void nrf5_get_eui64(uint8_t *mac)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2020-07-28 12:36:26 +02:00
|
|
|
uint64_t factoryAddress;
|
|
|
|
uint32_t index = 0;
|
|
|
|
|
2021-02-11 09:09:52 +01:00
|
|
|
#if !defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
|
2022-03-18 12:57:08 +01:00
|
|
|
uint32_t deviceid[2];
|
|
|
|
|
2020-07-28 12:36:26 +02:00
|
|
|
/* Set the MAC Address Block Larger (MA-L) formerly called OUI. */
|
|
|
|
mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 16) & 0xff;
|
|
|
|
mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 8) & 0xff;
|
|
|
|
mac[index++] = IEEE802154_NRF5_VENDOR_OUI & 0xff;
|
|
|
|
|
2024-03-08 13:26:21 +01:00
|
|
|
#if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) && defined(NRF_FICR_S)
|
2022-03-18 12:57:08 +01:00
|
|
|
soc_secure_read_deviceid(deviceid);
|
2024-03-08 13:26:21 +01:00
|
|
|
#else
|
|
|
|
deviceid[0] = nrf_ficr_deviceid_get(NRF_FICR, 0);
|
|
|
|
deviceid[1] = nrf_ficr_deviceid_get(NRF_FICR, 1);
|
|
|
|
#endif
|
2022-03-18 12:57:08 +01:00
|
|
|
|
|
|
|
factoryAddress = (uint64_t)deviceid[EUI64_ADDR_HIGH] << 32;
|
|
|
|
factoryAddress |= deviceid[EUI64_ADDR_LOW];
|
2020-11-16 10:50:44 +01:00
|
|
|
#else
|
2020-07-28 12:36:26 +02:00
|
|
|
/* Use device identifier assigned during the production. */
|
2021-02-11 09:09:52 +01:00
|
|
|
factoryAddress = (uint64_t)EUI64_ADDR[EUI64_ADDR_HIGH] << 32;
|
|
|
|
factoryAddress |= EUI64_ADDR[EUI64_ADDR_LOW];
|
2020-11-16 10:50:44 +01:00
|
|
|
#endif
|
2020-07-28 12:36:26 +02:00
|
|
|
memcpy(mac + index, &factoryAddress, sizeof(factoryAddress) - index);
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
2020-07-08 11:31:38 +02:00
|
|
|
struct nrf5_802154_data *nrf5_radio = (struct nrf5_802154_data *)arg1;
|
2017-04-05 08:37:44 +02:00
|
|
|
struct net_pkt *pkt;
|
2019-01-17 14:12:27 +01:00
|
|
|
struct nrf5_802154_rx_frame *rx_frame;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t pkt_len;
|
2022-06-07 14:28:26 +02:00
|
|
|
uint8_t *psdu;
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
ARG_UNUSED(arg2);
|
|
|
|
ARG_UNUSED(arg3);
|
|
|
|
|
|
|
|
while (1) {
|
2017-04-05 08:37:44 +02:00
|
|
|
pkt = NULL;
|
2019-01-17 14:12:27 +01:00
|
|
|
rx_frame = NULL;
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("Waiting for frame");
|
2019-01-17 14:12:27 +01:00
|
|
|
|
|
|
|
rx_frame = k_fifo_get(&nrf5_radio->rx_fifo, K_FOREVER);
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(rx_frame->psdu);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2017-02-22 11:25:39 +01:00
|
|
|
/* rx_mpdu contains length, psdu, fcs|lqi
|
|
|
|
* The last 2 bytes contain LQI or FCS, depending if
|
|
|
|
* automatic CRC handling is enabled or not, respectively.
|
2017-02-06 12:42:34 +01:00
|
|
|
*/
|
2021-11-18 17:17:10 +01:00
|
|
|
if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
|
2019-01-17 14:12:27 +01:00
|
|
|
pkt_len = rx_frame->psdu[0];
|
2017-11-16 11:11:51 +01:00
|
|
|
} else {
|
2023-07-19 09:15:08 +02:00
|
|
|
pkt_len = rx_frame->psdu[0] - IEEE802154_FCS_LENGTH;
|
2017-11-16 11:11:51 +01:00
|
|
|
}
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2023-02-19 17:31:18 +01:00
|
|
|
#if defined(CONFIG_NET_BUF_DATA_SIZE)
|
2019-02-20 12:08:00 +01:00
|
|
|
__ASSERT_NO_MSG(pkt_len <= CONFIG_NET_BUF_DATA_SIZE);
|
2023-02-19 17:31:18 +01:00
|
|
|
#endif
|
2019-02-20 12:08:00 +01:00
|
|
|
|
|
|
|
LOG_DBG("Frame received");
|
|
|
|
|
2021-01-22 09:35:33 +01:00
|
|
|
/* Block the RX thread until net_pkt is available, so that we
|
|
|
|
* don't drop already ACKed frame in case of temporary net_pkt
|
|
|
|
* scarcity. The nRF 802154 radio driver will accumulate any
|
|
|
|
* incoming frames until it runs out of internal buffers (and
|
|
|
|
* thus stops acknowledging consecutive frames).
|
|
|
|
*/
|
2021-01-21 15:11:42 +01:00
|
|
|
pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, pkt_len,
|
2021-01-22 09:35:33 +01:00
|
|
|
AF_UNSPEC, 0, K_FOREVER);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2019-02-20 10:01:57 +01:00
|
|
|
if (net_pkt_write(pkt, rx_frame->psdu + 1, pkt_len)) {
|
2019-02-20 12:08:00 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
net_pkt_set_ieee802154_lqi(pkt, rx_frame->lqi);
|
2023-06-11 14:32:55 +02:00
|
|
|
net_pkt_set_ieee802154_rssi_dbm(pkt, rx_frame->rssi);
|
2020-03-20 11:39:21 +01:00
|
|
|
net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2020-04-03 09:38:15 +02:00
|
|
|
#if defined(CONFIG_NET_PKT_TIMESTAMP)
|
2023-09-25 10:01:52 +02:00
|
|
|
net_pkt_set_timestamp_ns(pkt, rx_frame->time * NSEC_PER_USEC);
|
2020-04-03 09:38:15 +02:00
|
|
|
#endif
|
|
|
|
|
2023-11-02 12:47:38 +01:00
|
|
|
#if defined(CONFIG_NET_L2_OPENTHREAD)
|
|
|
|
net_pkt_set_ieee802154_ack_seb(pkt, rx_frame->ack_seb);
|
|
|
|
#endif
|
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("Caught a packet (%u) (LQI: %u)",
|
2019-01-17 14:12:27 +01:00
|
|
|
pkt_len, rx_frame->lqi);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2017-04-05 08:37:44 +02:00
|
|
|
if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
|
2019-01-17 14:12:27 +01:00
|
|
|
LOG_ERR("Packet dropped by NET stack");
|
|
|
|
goto drop;
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 14:28:26 +02:00
|
|
|
psdu = rx_frame->psdu;
|
2019-01-17 14:12:27 +01:00
|
|
|
rx_frame->psdu = NULL;
|
2022-06-07 14:28:26 +02:00
|
|
|
nrf_802154_buffer_free_raw(psdu);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2019-05-10 23:00:03 +02:00
|
|
|
if (LOG_LEVEL >= LOG_LEVEL_DBG) {
|
2020-03-12 22:18:42 +01:00
|
|
|
log_stack_usage(&nrf5_radio->rx_thread);
|
2018-12-18 12:29:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
continue;
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
drop:
|
2022-06-07 14:28:26 +02:00
|
|
|
psdu = rx_frame->psdu;
|
2019-01-17 14:12:27 +01:00
|
|
|
rx_frame->psdu = NULL;
|
2022-06-07 14:28:26 +02:00
|
|
|
nrf_802154_buffer_free_raw(psdu);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2021-03-05 12:01:51 +01:00
|
|
|
net_pkt_unref(pkt);
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:33:17 +01:00
|
|
|
static void nrf5_get_capabilities_at_boot(void)
|
|
|
|
{
|
|
|
|
nrf_802154_capabilities_t caps = nrf_802154_capabilities_get();
|
|
|
|
|
|
|
|
nrf5_data.capabilities =
|
|
|
|
IEEE802154_HW_FCS |
|
|
|
|
IEEE802154_HW_PROMISC |
|
|
|
|
IEEE802154_HW_FILTER |
|
|
|
|
((caps & NRF_802154_CAPABILITY_CSMA) ? IEEE802154_HW_CSMA : 0UL) |
|
|
|
|
IEEE802154_HW_TX_RX_ACK |
|
2023-06-03 19:31:31 +02:00
|
|
|
IEEE802154_HW_RX_TX_ACK |
|
2021-01-25 16:33:17 +01:00
|
|
|
IEEE802154_HW_ENERGY_SCAN |
|
|
|
|
((caps & NRF_802154_CAPABILITY_DELAYED_TX) ? IEEE802154_HW_TXTIME : 0UL) |
|
2021-06-23 16:52:35 +02:00
|
|
|
((caps & NRF_802154_CAPABILITY_DELAYED_RX) ? IEEE802154_HW_RXTIME : 0UL) |
|
2021-05-28 12:26:51 +02:00
|
|
|
IEEE802154_HW_SLEEP_TO_TX |
|
2023-10-31 10:23:51 +01:00
|
|
|
IEEE802154_RX_ON_WHEN_IDLE |
|
2023-07-18 12:28:33 +02:00
|
|
|
((caps & NRF_802154_CAPABILITY_SECURITY) ? IEEE802154_HW_TX_SEC : 0UL)
|
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
| IEEE802154_OPENTHREAD_HW_MULTIPLE_CCA
|
|
|
|
#endif
|
|
|
|
;
|
2021-01-25 16:33:17 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
/* Radio device API */
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static enum ieee802154_hw_caps nrf5_get_capabilities(const struct device *dev)
|
2017-09-04 15:24:36 +02:00
|
|
|
{
|
2021-01-25 16:33:17 +01:00
|
|
|
return nrf5_data.capabilities;
|
2017-09-04 15:24:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_cca(const struct device *dev)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
|
|
|
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
if (!nrf_802154_cca()) {
|
|
|
|
LOG_DBG("CCA failed");
|
2017-02-06 12:42:34 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The nRF driver guarantees that a callback will be called once
|
2019-01-17 14:12:27 +01:00
|
|
|
* the CCA function is done, thus unlocking the semaphore.
|
2017-02-06 12:42:34 +01:00
|
|
|
*/
|
|
|
|
k_sem_take(&nrf5_radio->cca_wait, K_FOREVER);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
LOG_DBG("Channel free? %d", nrf5_radio->channel_free);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
return nrf5_radio->channel_free ? 0 : -EBUSY;
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_set_channel(const struct device *dev, uint16_t channel)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2019-01-17 14:12:27 +01:00
|
|
|
ARG_UNUSED(dev);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("%u", channel);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
if (channel < 11 || channel > 26) {
|
2023-09-02 14:26:44 +02:00
|
|
|
return channel < 11 ? -ENOTSUP : -EINVAL;
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2024-02-07 11:57:33 +01:00
|
|
|
nrf_802154_channel_set(channel);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_energy_scan_start(const struct device *dev,
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t duration,
|
2020-02-25 11:17:45 +01:00
|
|
|
energy_scan_done_cb_t done_cb)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
if (nrf5_data.energy_scan_done == NULL) {
|
|
|
|
nrf5_data.energy_scan_done = done_cb;
|
|
|
|
|
|
|
|
if (nrf_802154_energy_detection(duration * 1000) == false) {
|
|
|
|
nrf5_data.energy_scan_done = NULL;
|
2023-07-13 00:15:45 +02:00
|
|
|
err = -EBUSY;
|
2020-02-25 11:17:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_set_pan_id(const struct device *dev, uint16_t pan_id)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t pan_id_le[2];
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
sys_put_le16(pan_id, pan_id_le);
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_pan_id_set(pan_id_le);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("0x%x", pan_id);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_set_short_addr(const struct device *dev, uint16_t short_addr)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t short_addr_le[2];
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
sys_put_le16(short_addr, short_addr_le);
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_short_address_set(short_addr_le);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("0x%x", short_addr);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_set_ieee_addr(const struct device *dev,
|
|
|
|
const uint8_t *ieee_addr)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
|
2017-02-06 12:42:34 +01:00
|
|
|
ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
|
|
|
|
ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_extended_address_set(ieee_addr);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_filter(const struct device *dev, bool set,
|
2018-03-19 09:53:29 +01:00
|
|
|
enum ieee802154_filter_type type,
|
|
|
|
const struct ieee802154_filter *filter)
|
2017-09-05 14:09:15 +02:00
|
|
|
{
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("Applying filter %u", type);
|
2017-09-05 14:09:15 +02:00
|
|
|
|
2018-03-19 09:53:29 +01:00
|
|
|
if (!set) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:09:15 +02:00
|
|
|
if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
|
|
|
|
return nrf5_set_ieee_addr(dev, filter->ieee_addr);
|
|
|
|
} else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
|
|
|
|
return nrf5_set_short_addr(dev, filter->short_addr);
|
|
|
|
} else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
|
|
|
|
return nrf5_set_pan_id(dev, filter->pan_id);
|
|
|
|
}
|
|
|
|
|
2018-03-19 09:53:29 +01:00
|
|
|
return -ENOTSUP;
|
2017-09-05 14:09:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_set_txpower(const struct device *dev, int16_t dbm)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2019-01-17 14:12:27 +01:00
|
|
|
ARG_UNUSED(dev);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("%d", dbm);
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2023-10-31 09:42:36 +01:00
|
|
|
nrf5_data.txpwr = dbm;
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:29:25 +02:00
|
|
|
static int handle_ack(struct nrf5_802154_data *nrf5_radio)
|
|
|
|
{
|
2021-05-27 16:30:27 +02:00
|
|
|
uint8_t ack_len;
|
2019-04-11 13:29:25 +02:00
|
|
|
struct net_pkt *ack_pkt;
|
|
|
|
int err = 0;
|
|
|
|
|
2024-02-23 13:13:10 +01:00
|
|
|
#if defined(CONFIG_NET_PKT_TIMESTAMP)
|
|
|
|
if (nrf5_radio->ack_frame.time == NRF_802154_NO_TIMESTAMP) {
|
|
|
|
/* Ack timestamp is invalid and cannot be used by the upper layer.
|
|
|
|
* Report the transmission as failed as if the Ack was not received at all.
|
|
|
|
*/
|
|
|
|
LOG_WRN("Invalid ACK timestamp.");
|
|
|
|
err = -ENOMSG;
|
|
|
|
goto free_nrf_ack;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-18 17:17:10 +01:00
|
|
|
if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
|
2021-05-27 16:30:27 +02:00
|
|
|
ack_len = nrf5_radio->ack_frame.psdu[0];
|
|
|
|
} else {
|
2023-07-19 09:15:08 +02:00
|
|
|
ack_len = nrf5_radio->ack_frame.psdu[0] - IEEE802154_FCS_LENGTH;
|
2021-05-27 16:30:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 13:59:28 +02:00
|
|
|
ack_pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, ack_len,
|
|
|
|
AF_UNSPEC, 0, K_NO_WAIT);
|
2019-04-11 13:29:25 +02:00
|
|
|
if (!ack_pkt) {
|
|
|
|
LOG_ERR("No free packet available.");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto free_nrf_ack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Upper layers expect the frame to start at the MAC header, skip the
|
|
|
|
* PHY header (1 byte).
|
|
|
|
*/
|
|
|
|
if (net_pkt_write(ack_pkt, nrf5_radio->ack_frame.psdu + 1,
|
|
|
|
ack_len) < 0) {
|
|
|
|
LOG_ERR("Failed to write to a packet.");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto free_net_ack;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_pkt_set_ieee802154_lqi(ack_pkt, nrf5_radio->ack_frame.lqi);
|
2023-06-11 14:32:55 +02:00
|
|
|
net_pkt_set_ieee802154_rssi_dbm(ack_pkt, nrf5_radio->ack_frame.rssi);
|
2019-04-11 13:29:25 +02:00
|
|
|
|
2021-06-09 10:06:02 +02:00
|
|
|
#if defined(CONFIG_NET_PKT_TIMESTAMP)
|
2023-09-25 10:01:52 +02:00
|
|
|
net_pkt_set_timestamp_ns(ack_pkt, nrf5_radio->ack_frame.time * NSEC_PER_USEC);
|
2021-06-02 16:00:37 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-11 13:29:25 +02:00
|
|
|
net_pkt_cursor_init(ack_pkt);
|
|
|
|
|
2023-06-02 19:56:49 +02:00
|
|
|
if (ieee802154_handle_ack(nrf5_radio->iface, ack_pkt) != NET_OK) {
|
2019-04-11 13:29:25 +02:00
|
|
|
LOG_INF("ACK packet not handled - releasing.");
|
|
|
|
}
|
|
|
|
|
|
|
|
free_net_ack:
|
|
|
|
net_pkt_unref(ack_pkt);
|
|
|
|
|
|
|
|
free_nrf_ack:
|
|
|
|
nrf_802154_buffer_free_raw(nrf5_radio->ack_frame.psdu);
|
|
|
|
nrf5_radio->ack_frame.psdu = NULL;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void nrf5_tx_started(const struct device *dev,
|
2020-02-28 15:48:57 +01:00
|
|
|
struct net_pkt *pkt,
|
|
|
|
struct net_buf *frag)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(pkt);
|
|
|
|
|
|
|
|
if (nrf5_data.event_handler) {
|
|
|
|
nrf5_data.event_handler(dev, IEEE802154_EVENT_TX_STARTED,
|
|
|
|
(void *)frag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:00:37 +02:00
|
|
|
static bool nrf5_tx_immediate(struct net_pkt *pkt, uint8_t *payload, bool cca)
|
|
|
|
{
|
|
|
|
nrf_802154_transmit_metadata_t metadata = {
|
|
|
|
.frame_props = {
|
2022-10-12 16:35:34 +02:00
|
|
|
.is_secured = net_pkt_ieee802154_frame_secured(pkt),
|
|
|
|
.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
|
2021-07-12 15:00:37 +02:00
|
|
|
},
|
|
|
|
.cca = cca,
|
2022-03-28 15:08:55 +02:00
|
|
|
.tx_power = {
|
2023-10-31 09:42:36 +01:00
|
|
|
.use_metadata_value = true,
|
|
|
|
.power = nrf5_data.txpwr,
|
2022-03-28 15:08:55 +02:00
|
|
|
},
|
2021-07-12 15:00:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return nrf_802154_transmit_raw(payload, &metadata);
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:38:24 +01:00
|
|
|
#if NRF_802154_CSMA_CA_ENABLED
|
2021-07-12 15:00:37 +02:00
|
|
|
static bool nrf5_tx_csma_ca(struct net_pkt *pkt, uint8_t *payload)
|
|
|
|
{
|
|
|
|
nrf_802154_transmit_csma_ca_metadata_t metadata = {
|
|
|
|
.frame_props = {
|
2022-10-12 16:35:34 +02:00
|
|
|
.is_secured = net_pkt_ieee802154_frame_secured(pkt),
|
|
|
|
.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
|
2021-07-12 15:00:37 +02:00
|
|
|
},
|
2022-03-28 15:08:55 +02:00
|
|
|
.tx_power = {
|
2023-10-31 09:42:36 +01:00
|
|
|
.use_metadata_value = true,
|
|
|
|
.power = nrf5_data.txpwr,
|
2022-03-28 15:08:55 +02:00
|
|
|
},
|
2021-07-12 15:00:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return nrf_802154_transmit_csma_ca_raw(payload, &metadata);
|
|
|
|
}
|
2022-02-28 13:38:24 +01:00
|
|
|
#endif
|
2021-07-12 15:00:37 +02:00
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_NET_PKT_TXTIME)
|
2023-07-18 12:28:33 +02:00
|
|
|
static bool nrf5_tx_at(struct nrf5_802154_data *nrf5_radio, struct net_pkt *pkt,
|
|
|
|
uint8_t *payload, enum ieee802154_tx_mode mode)
|
2021-07-12 15:00:37 +02:00
|
|
|
{
|
2023-07-18 12:28:33 +02:00
|
|
|
bool cca = false;
|
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
uint8_t max_extra_cca_attempts = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case IEEE802154_TX_MODE_TXTIME:
|
|
|
|
break;
|
|
|
|
case IEEE802154_TX_MODE_TXTIME_CCA:
|
|
|
|
cca = true;
|
|
|
|
break;
|
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
case IEEE802154_OPENTHREAD_TX_MODE_TXTIME_MULTIPLE_CCA:
|
|
|
|
cca = true;
|
2023-08-08 12:57:48 +02:00
|
|
|
max_extra_cca_attempts = nrf5_data.max_extra_cca_attempts;
|
2023-07-18 12:28:33 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__ASSERT_NO_MSG(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:00:37 +02:00
|
|
|
nrf_802154_transmit_at_metadata_t metadata = {
|
|
|
|
.frame_props = {
|
2022-10-12 16:35:34 +02:00
|
|
|
.is_secured = net_pkt_ieee802154_frame_secured(pkt),
|
|
|
|
.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
|
2021-07-12 15:00:37 +02:00
|
|
|
},
|
|
|
|
.cca = cca,
|
2024-02-07 11:57:33 +01:00
|
|
|
.channel = nrf_802154_channel_get(),
|
2022-03-28 15:08:55 +02:00
|
|
|
.tx_power = {
|
2023-10-31 09:42:36 +01:00
|
|
|
.use_metadata_value = true,
|
|
|
|
.power = nrf5_data.txpwr,
|
2022-03-28 15:08:55 +02:00
|
|
|
},
|
2023-07-18 12:28:33 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
.extra_cca_attempts = max_extra_cca_attempts,
|
|
|
|
#endif
|
2021-07-12 15:00:37 +02:00
|
|
|
};
|
2023-07-13 16:37:55 +02:00
|
|
|
|
|
|
|
/* The timestamp points to the start of PHR but `nrf_802154_transmit_raw_at`
|
|
|
|
* expects a timestamp pointing to start of SHR.
|
|
|
|
*/
|
|
|
|
uint64_t tx_at = nrf_802154_timestamp_phr_to_shr_convert(
|
2023-09-25 10:01:52 +02:00
|
|
|
net_pkt_timestamp_ns(pkt) / NSEC_PER_USEC);
|
2020-09-28 12:01:30 +02:00
|
|
|
|
2023-03-17 10:44:56 +01:00
|
|
|
return nrf_802154_transmit_raw_at(payload, tx_at, &metadata);
|
2020-09-28 12:01:30 +02:00
|
|
|
}
|
2021-01-25 16:21:48 +01:00
|
|
|
#endif /* CONFIG_NET_PKT_TXTIME */
|
2020-09-28 12:01:30 +02:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_tx(const struct device *dev,
|
net: ieee802154_radio: Allow to specify TX mode
Even though radio driver can report in its capabilities that it does
support CSMA CA, there's no way in the driver to select how the frame
should be transmitted (with CSMA or without). As layers above radio
driver (Thread, Zigbee) can expect that both TX modes are available, we
need to extend the API to allow either of these modes.
This commits extends the API `tx` function with an extra parameter,
`ieee802154_tx_mode`, which informs the driver how the packet should be
transmitted. Currently, the following modes are specified:
* direct (regular tx, no cca, just how it worked so far),
* CCA before transmission,
* CSMA CA before transmission,
* delayed TX,
* delayed TX with CCA
Assume that radios that reported CSMA CA capability transmit in CSMA CA
mode by default, all others will support direct mode.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2020-02-28 13:57:49 +01:00
|
|
|
enum ieee802154_tx_mode mode,
|
2017-04-05 08:37:44 +02:00
|
|
|
struct net_pkt *pkt,
|
2017-02-06 12:42:34 +01:00
|
|
|
struct net_buf *frag)
|
|
|
|
{
|
|
|
|
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t payload_len = frag->len;
|
|
|
|
uint8_t *payload = frag->data;
|
2020-03-13 09:21:55 +01:00
|
|
|
bool ret = true;
|
net: ieee802154_radio: Allow to specify TX mode
Even though radio driver can report in its capabilities that it does
support CSMA CA, there's no way in the driver to select how the frame
should be transmitted (with CSMA or without). As layers above radio
driver (Thread, Zigbee) can expect that both TX modes are available, we
need to extend the API to allow either of these modes.
This commits extends the API `tx` function with an extra parameter,
`ieee802154_tx_mode`, which informs the driver how the packet should be
transmitted. Currently, the following modes are specified:
* direct (regular tx, no cca, just how it worked so far),
* CCA before transmission,
* CSMA CA before transmission,
* delayed TX,
* delayed TX with CCA
Assume that radios that reported CSMA CA capability transmit in CSMA CA
mode by default, all others will support direct mode.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2020-02-28 13:57:49 +01:00
|
|
|
|
2023-07-19 09:15:08 +02:00
|
|
|
if (payload_len > IEEE802154_MTU) {
|
2023-07-18 15:52:11 +02:00
|
|
|
LOG_ERR("Payload too large: %d", payload_len);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("%p (%u)", payload, payload_len);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2023-07-19 09:15:08 +02:00
|
|
|
nrf5_radio->tx_psdu[0] = payload_len + IEEE802154_FCS_LENGTH;
|
2017-02-06 12:42:34 +01:00
|
|
|
memcpy(nrf5_radio->tx_psdu + 1, payload, payload_len);
|
|
|
|
|
2017-09-29 14:22:26 +02:00
|
|
|
/* Reset semaphore in case ACK was received after timeout */
|
|
|
|
k_sem_reset(&nrf5_radio->tx_wait);
|
|
|
|
|
2020-03-13 09:21:55 +01:00
|
|
|
switch (mode) {
|
|
|
|
case IEEE802154_TX_MODE_DIRECT:
|
|
|
|
case IEEE802154_TX_MODE_CCA:
|
2021-07-12 15:00:37 +02:00
|
|
|
ret = nrf5_tx_immediate(pkt, nrf5_radio->tx_psdu,
|
|
|
|
mode == IEEE802154_TX_MODE_CCA);
|
2020-03-13 09:21:55 +01:00
|
|
|
break;
|
2022-02-28 13:38:24 +01:00
|
|
|
#if NRF_802154_CSMA_CA_ENABLED
|
2020-03-13 09:21:55 +01:00
|
|
|
case IEEE802154_TX_MODE_CSMA_CA:
|
2021-07-12 15:00:37 +02:00
|
|
|
ret = nrf5_tx_csma_ca(pkt, nrf5_radio->tx_psdu);
|
2020-03-13 09:21:55 +01:00
|
|
|
break;
|
2022-02-28 13:38:24 +01:00
|
|
|
#endif
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_NET_PKT_TXTIME)
|
2020-03-13 09:21:55 +01:00
|
|
|
case IEEE802154_TX_MODE_TXTIME:
|
|
|
|
case IEEE802154_TX_MODE_TXTIME_CCA:
|
2023-07-18 12:28:33 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
case IEEE802154_OPENTHREAD_TX_MODE_TXTIME_MULTIPLE_CCA:
|
|
|
|
#endif
|
2020-09-28 12:01:30 +02:00
|
|
|
__ASSERT_NO_MSG(pkt);
|
2023-07-18 12:28:33 +02:00
|
|
|
ret = nrf5_tx_at(nrf5_radio, pkt, nrf5_radio->tx_psdu, mode);
|
2020-09-28 12:01:30 +02:00
|
|
|
break;
|
2021-01-25 16:21:48 +01:00
|
|
|
#endif /* CONFIG_NET_PKT_TXTIME */
|
2020-03-13 09:21:55 +01:00
|
|
|
default:
|
|
|
|
NET_ERR("TX mode %d not supported", mode);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_ERR("Cannot send frame");
|
2017-02-06 12:42:34 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-02-28 15:48:57 +01:00
|
|
|
nrf5_tx_started(dev, pkt, frag);
|
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("Sending frame (ch:%d, txpower:%d)",
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_channel_get(), nrf_802154_tx_power_get());
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2020-04-03 13:04:11 +02:00
|
|
|
/* Wait for the callback from the radio driver. */
|
|
|
|
k_sem_take(&nrf5_radio->tx_wait, K_FOREVER);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
LOG_DBG("Result: %d", nrf5_data.tx_result);
|
|
|
|
|
2023-05-31 14:02:38 +02:00
|
|
|
#if defined(CONFIG_NRF_802154_ENCRYPTION)
|
2021-06-11 08:41:30 +02:00
|
|
|
/*
|
|
|
|
* When frame encryption by the radio driver is enabled, the frame stored in
|
|
|
|
* the tx_psdu buffer is:
|
|
|
|
* 1) authenticated and encrypted in place which causes that after an unsuccessful
|
|
|
|
* TX attempt, this frame must be propagated back to the upper layer for retransmission.
|
|
|
|
* The upper layer must ensure that the exact same secured frame is used for
|
|
|
|
* retransmission
|
|
|
|
* 2) frame counters are updated in place and for keeping the link frame counter up to date,
|
|
|
|
* this information must be propagated back to the upper layer
|
|
|
|
*/
|
|
|
|
memcpy(payload, nrf5_radio->tx_psdu + 1, payload_len);
|
|
|
|
#endif
|
2021-07-12 15:00:37 +02:00
|
|
|
net_pkt_set_ieee802154_frame_secured(pkt, nrf5_radio->tx_frame_is_secured);
|
|
|
|
net_pkt_set_ieee802154_mac_hdr_rdy(pkt, nrf5_radio->tx_frame_mac_hdr_rdy);
|
2021-06-11 08:41:30 +02:00
|
|
|
|
2020-09-17 14:28:22 +02:00
|
|
|
switch (nrf5_radio->tx_result) {
|
|
|
|
case NRF_802154_TX_ERROR_NONE:
|
2019-04-11 13:29:25 +02:00
|
|
|
if (nrf5_radio->ack_frame.psdu == NULL) {
|
|
|
|
/* No ACK was requested. */
|
|
|
|
return 0;
|
2019-01-17 14:12:27 +01:00
|
|
|
}
|
2019-04-11 13:29:25 +02:00
|
|
|
/* Handle ACK packet. */
|
|
|
|
return handle_ack(nrf5_radio);
|
2020-09-17 14:28:22 +02:00
|
|
|
case NRF_802154_TX_ERROR_NO_MEM:
|
2021-06-15 09:39:14 +02:00
|
|
|
return -ENOBUFS;
|
2020-09-17 14:28:22 +02:00
|
|
|
case NRF_802154_TX_ERROR_BUSY_CHANNEL:
|
2021-06-15 09:39:14 +02:00
|
|
|
return -EBUSY;
|
2020-09-17 14:28:22 +02:00
|
|
|
case NRF_802154_TX_ERROR_INVALID_ACK:
|
|
|
|
case NRF_802154_TX_ERROR_NO_ACK:
|
2021-06-15 09:39:14 +02:00
|
|
|
return -ENOMSG;
|
2020-09-17 14:28:22 +02:00
|
|
|
case NRF_802154_TX_ERROR_ABORTED:
|
|
|
|
case NRF_802154_TX_ERROR_TIMESLOT_DENIED:
|
|
|
|
case NRF_802154_TX_ERROR_TIMESLOT_ENDED:
|
2021-06-08 10:20:02 +02:00
|
|
|
default:
|
2021-06-15 09:39:14 +02:00
|
|
|
return -EIO;
|
2019-01-17 14:12:27 +01:00
|
|
|
}
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 17:54:00 +02:00
|
|
|
static net_time_t nrf5_get_time(const struct device *dev)
|
2021-04-23 13:51:00 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2023-07-11 17:54:00 +02:00
|
|
|
return (net_time_t)nrf_802154_time_get() * NSEC_PER_USEC;
|
2021-04-23 13:51:00 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 16:52:35 +02:00
|
|
|
static uint8_t nrf5_get_acc(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2022-03-30 08:16:42 +02:00
|
|
|
return CONFIG_IEEE802154_NRF5_DELAY_TRX_ACC;
|
2021-06-23 16:52:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_start(const struct device *dev)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2019-01-17 14:12:27 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2023-10-31 09:42:36 +01:00
|
|
|
nrf_802154_tx_power_set(nrf5_data.txpwr);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
if (!nrf_802154_receive()) {
|
|
|
|
LOG_ERR("Failed to enter receive state");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("nRF5 802154 radio started (channel: %d)",
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_channel_get());
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_stop(const struct device *dev)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2021-06-23 16:52:35 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
|
|
|
|
if (nrf_802154_sleep_if_idle() != NRF_802154_SLEEP_ERROR_NONE) {
|
|
|
|
if (nrf5_data.event_handler) {
|
2023-09-22 18:47:13 +02:00
|
|
|
nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_OFF, NULL);
|
2021-06-23 16:52:35 +02:00
|
|
|
} else {
|
|
|
|
LOG_WRN("Transition to radio sleep cannot be handled.");
|
|
|
|
}
|
drivers ieee802154_nrf5: Fix infinite loop for simulation on stop
With CSL enabled, when nrf5_stop is called,
nrf_802154_sleep_if_idle() will be called, and if the radio is
busy with another task, another IEEE802154_EVENT_RX_OFF event
will be pended right away, resulting in another call
to nrf5_stop(), effectively busy waiting until the
radio has reached idle.
In simulation, this whole operation (busy wait loop) is
done without letting the CPU sleep, in an infinite loop,
and therefore without letting any time pass
(note that in the POSIX architecture,
no time passes if the CPU does not go to sleep).
And therefore the radio will never be done with whatever
it is doing, resulting in the simulation being stuck
in this loop.
Let's add a very minor delay to this loop, which is
conditionally compiled only for the POSIX architecture.
Which effectively mimics the time it takes for the CPU
to loop thru, let's time pass, and allows the radio
to eventually be done.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-11-07 08:49:28 +01:00
|
|
|
Z_SPIN_DELAY(1);
|
2021-06-23 16:52:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2017-02-06 12:42:34 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
if (!nrf_802154_sleep()) {
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_ERR("Error while stopping radio");
|
2017-02-06 12:42:34 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
2022-01-10 09:30:54 +01:00
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_DBG("nRF5 802154 radio stopped");
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:13:01 +01:00
|
|
|
#if defined(CONFIG_NRF_802154_CARRIER_FUNCTIONS)
|
|
|
|
static int nrf5_continuous_carrier(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2023-10-31 09:42:36 +01:00
|
|
|
nrf_802154_tx_power_set(nrf5_data.txpwr);
|
|
|
|
|
2023-02-21 14:13:01 +01:00
|
|
|
if (!nrf_802154_continuous_carrier()) {
|
|
|
|
LOG_ERR("Failed to enter continuous carrier state");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DBG("Continuous carrier wave transmission started (channel: %d)",
|
|
|
|
nrf_802154_channel_get());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-24 09:34:50 +01:00
|
|
|
#if !IS_ENABLED(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
|
2023-01-27 11:34:04 +01:00
|
|
|
static void nrf5_radio_irq(const void *arg)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_radio_irq_handler();
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
2020-06-01 12:08:03 +02:00
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void nrf5_irq_config(const struct device *dev)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2021-02-24 09:34:50 +01:00
|
|
|
#if !IS_ENABLED(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
|
2024-02-05 12:13:10 +01:00
|
|
|
IRQ_CONNECT(DT_IRQN(DT_NODELABEL(radio)), NRF_802154_IRQ_PRIORITY, nrf5_radio_irq, NULL, 0);
|
|
|
|
irq_enable(DT_IRQN(DT_NODELABEL(radio)));
|
2020-06-01 12:08:03 +02:00
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_init(const struct device *dev)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
|
|
|
const struct nrf5_802154_config *nrf5_radio_cfg = NRF5_802154_CFG(dev);
|
|
|
|
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
|
2023-11-22 09:56:03 +01:00
|
|
|
#if defined(CONFIG_IEEE802154_RAW_MODE)
|
|
|
|
nrf5_dev = dev;
|
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
k_fifo_init(&nrf5_radio->rx_fifo);
|
2017-02-06 12:42:34 +01:00
|
|
|
k_sem_init(&nrf5_radio->tx_wait, 0, 1);
|
|
|
|
k_sem_init(&nrf5_radio->cca_wait, 0, 1);
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf_802154_init();
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2021-02-24 10:55:12 +01:00
|
|
|
nrf5_get_capabilities_at_boot();
|
|
|
|
|
2024-01-18 11:34:34 +01:00
|
|
|
nrf5_radio->rx_on_when_idle = true;
|
2017-02-06 12:42:34 +01:00
|
|
|
nrf5_radio_cfg->irq_config_func(dev);
|
|
|
|
|
2017-05-09 21:15:00 +02:00
|
|
|
k_thread_create(&nrf5_radio->rx_thread, nrf5_radio->rx_stack,
|
|
|
|
CONFIG_IEEE802154_NRF5_RX_STACK_SIZE,
|
2020-07-08 11:31:38 +02:00
|
|
|
nrf5_rx_thread, nrf5_radio, NULL, NULL,
|
2019-09-19 14:16:34 +02:00
|
|
|
K_PRIO_COOP(2), 0, K_NO_WAIT);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2020-05-26 10:48:53 +02:00
|
|
|
k_thread_name_set(&nrf5_radio->rx_thread, "nrf5_rx");
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2018-09-03 16:28:47 +02:00
|
|
|
LOG_INF("nRF5 802154 radio initialized");
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nrf5_iface_init(struct net_if *iface)
|
|
|
|
{
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *dev = net_if_get_device(iface);
|
2017-02-06 12:42:34 +01:00
|
|
|
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
|
|
|
|
|
|
|
|
nrf5_get_eui64(nrf5_radio->mac);
|
2019-01-17 14:12:27 +01:00
|
|
|
net_if_set_link_addr(iface, nrf5_radio->mac, sizeof(nrf5_radio->mac),
|
2017-02-23 13:47:44 +01:00
|
|
|
NET_LINK_IEEE802154);
|
2017-02-06 12:42:34 +01:00
|
|
|
|
|
|
|
nrf5_radio->iface = iface;
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
ieee802154_init(iface);
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:02:38 +02:00
|
|
|
#if defined(CONFIG_NRF_802154_ENCRYPTION)
|
2021-05-18 14:41:39 +02:00
|
|
|
static void nrf5_config_mac_keys(struct ieee802154_key *mac_keys)
|
|
|
|
{
|
2023-11-21 14:49:42 +01:00
|
|
|
nrf_802154_security_key_remove_all();
|
2021-05-18 14:41:39 +02:00
|
|
|
|
2023-11-21 14:49:42 +01:00
|
|
|
for (uint8_t i = 0; mac_keys->key_value
|
|
|
|
&& i < NRF_802154_SECURITY_KEY_STORAGE_SIZE; mac_keys++, i++) {
|
2021-11-04 13:58:08 +01:00
|
|
|
nrf_802154_key_t key = {
|
2023-11-21 14:49:42 +01:00
|
|
|
.value.p_cleartext_key = mac_keys->key_value,
|
|
|
|
.id.mode = mac_keys->key_id_mode,
|
|
|
|
.id.p_key_id = mac_keys->key_id,
|
2021-11-04 13:58:08 +01:00
|
|
|
.type = NRF_802154_KEY_CLEARTEXT,
|
|
|
|
.frame_counter = 0,
|
2023-11-21 14:49:42 +01:00
|
|
|
.use_global_frame_counter = !(mac_keys->frame_counter_per_key),
|
2021-11-04 13:58:08 +01:00
|
|
|
};
|
|
|
|
|
2022-01-17 15:01:50 +01:00
|
|
|
__ASSERT_EVAL((void)nrf_802154_security_key_store(&key),
|
|
|
|
nrf_802154_security_error_t err = nrf_802154_security_key_store(&key),
|
|
|
|
err == NRF_802154_SECURITY_ERROR_NONE ||
|
|
|
|
err == NRF_802154_SECURITY_ERROR_ALREADY_PRESENT,
|
|
|
|
"Storing key failed, err: %d", err);
|
2021-05-18 14:41:39 +02:00
|
|
|
};
|
|
|
|
}
|
2023-05-31 14:02:38 +02:00
|
|
|
#endif /* CONFIG_NRF_802154_ENCRYPTION */
|
2021-05-18 14:41:39 +02:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int nrf5_configure(const struct device *dev,
|
|
|
|
enum ieee802154_config_type type,
|
2020-03-31 15:38:41 +02:00
|
|
|
const struct ieee802154_config *config)
|
2019-04-16 14:52:59 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case IEEE802154_CONFIG_AUTO_ACK_FPB:
|
2020-03-31 15:38:41 +02:00
|
|
|
if (config->auto_ack_fpb.enabled) {
|
|
|
|
switch (config->auto_ack_fpb.mode) {
|
|
|
|
case IEEE802154_FPB_ADDR_MATCH_THREAD:
|
|
|
|
nrf_802154_src_addr_matching_method_set(
|
|
|
|
NRF_802154_SRC_ADDR_MATCH_THREAD);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IEEE802154_FPB_ADDR_MATCH_ZIGBEE:
|
|
|
|
nrf_802154_src_addr_matching_method_set(
|
|
|
|
NRF_802154_SRC_ADDR_MATCH_ZIGBEE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:52:59 +02:00
|
|
|
nrf_802154_auto_pending_bit_set(config->auto_ack_fpb.enabled);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IEEE802154_CONFIG_ACK_FPB:
|
|
|
|
if (config->ack_fpb.enabled) {
|
|
|
|
if (!nrf_802154_pending_bit_for_addr_set(
|
|
|
|
config->ack_fpb.addr,
|
|
|
|
config->ack_fpb.extended)) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->ack_fpb.addr != NULL) {
|
|
|
|
if (!nrf_802154_pending_bit_for_addr_clear(
|
|
|
|
config->ack_fpb.addr,
|
|
|
|
config->ack_fpb.extended)) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nrf_802154_pending_bit_for_addr_reset(
|
|
|
|
config->ack_fpb.extended);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2020-03-19 14:47:22 +01:00
|
|
|
case IEEE802154_CONFIG_PAN_COORDINATOR:
|
|
|
|
nrf_802154_pan_coord_set(config->pan_coordinator);
|
|
|
|
break;
|
|
|
|
|
2020-03-19 13:12:36 +01:00
|
|
|
case IEEE802154_CONFIG_PROMISCUOUS:
|
|
|
|
nrf_802154_promiscuous_set(config->promiscuous);
|
|
|
|
break;
|
|
|
|
|
2020-02-28 15:48:57 +01:00
|
|
|
case IEEE802154_CONFIG_EVENT_HANDLER:
|
|
|
|
nrf5_data.event_handler = config->event_handler;
|
2021-05-18 14:41:39 +02:00
|
|
|
break;
|
|
|
|
|
2023-05-31 14:02:38 +02:00
|
|
|
#if defined(CONFIG_NRF_802154_ENCRYPTION)
|
2021-05-18 14:41:39 +02:00
|
|
|
case IEEE802154_CONFIG_MAC_KEYS:
|
|
|
|
nrf5_config_mac_keys(config->mac_keys);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IEEE802154_CONFIG_FRAME_COUNTER:
|
|
|
|
nrf_802154_security_global_frame_counter_set(config->frame_counter);
|
|
|
|
break;
|
2023-02-20 15:04:10 +01:00
|
|
|
|
|
|
|
case IEEE802154_CONFIG_FRAME_COUNTER_IF_LARGER:
|
|
|
|
nrf_802154_security_global_frame_counter_set_if_larger(config->frame_counter);
|
|
|
|
break;
|
2023-05-31 14:02:38 +02:00
|
|
|
#endif /* CONFIG_NRF_802154_ENCRYPTION */
|
2020-02-28 15:48:57 +01:00
|
|
|
|
2021-06-23 16:52:35 +02:00
|
|
|
case IEEE802154_CONFIG_ENH_ACK_HEADER_IE: {
|
|
|
|
uint8_t ext_addr_le[EXTENDED_ADDRESS_SIZE];
|
2023-09-20 19:31:47 +02:00
|
|
|
uint8_t short_addr_le[SHORT_ADDRESS_SIZE];
|
|
|
|
uint8_t element_id;
|
2024-01-23 14:36:55 +01:00
|
|
|
bool valid_vendor_specific_ie = false;
|
2023-09-20 19:31:47 +02:00
|
|
|
|
2024-01-23 14:56:03 +01:00
|
|
|
if (config->ack_ie.purge_ie) {
|
|
|
|
nrf_802154_ack_data_remove_all(false, NRF_802154_ACK_DATA_IE);
|
|
|
|
nrf_802154_ack_data_remove_all(true, NRF_802154_ACK_DATA_IE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-20 19:31:47 +02:00
|
|
|
if (config->ack_ie.short_addr == IEEE802154_BROADCAST_ADDRESS ||
|
|
|
|
config->ack_ie.ext_addr == NULL) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2024-01-23 14:36:55 +01:00
|
|
|
sys_put_le16(config->ack_ie.short_addr, short_addr_le);
|
|
|
|
sys_memcpy_swap(ext_addr_le, config->ack_ie.ext_addr, EXTENDED_ADDRESS_SIZE);
|
2023-09-20 19:31:47 +02:00
|
|
|
|
2024-01-23 14:36:55 +01:00
|
|
|
if (config->ack_ie.header_ie == NULL || config->ack_ie.header_ie->length == 0) {
|
|
|
|
nrf_802154_ack_data_clear(short_addr_le, false, NRF_802154_ACK_DATA_IE);
|
|
|
|
nrf_802154_ack_data_clear(ext_addr_le, true, NRF_802154_ACK_DATA_IE);
|
|
|
|
} else {
|
|
|
|
element_id = ieee802154_header_ie_get_element_id(config->ack_ie.header_ie);
|
2023-09-20 19:31:47 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_NET_L2_OPENTHREAD)
|
2024-01-23 14:36:55 +01:00
|
|
|
uint8_t vendor_oui_le[IEEE802154_OPENTHREAD_VENDOR_OUI_LEN] =
|
|
|
|
IEEE802154_OPENTHREAD_THREAD_IE_VENDOR_OUI;
|
2023-09-20 19:31:47 +02:00
|
|
|
|
2024-01-23 14:36:55 +01:00
|
|
|
if (element_id == IEEE802154_HEADER_IE_ELEMENT_ID_VENDOR_SPECIFIC_IE &&
|
|
|
|
memcmp(config->ack_ie.header_ie->content.vendor_specific.vendor_oui,
|
|
|
|
vendor_oui_le, sizeof(vendor_oui_le)) == 0) {
|
|
|
|
valid_vendor_specific_ie = true;
|
|
|
|
}
|
2023-09-20 19:31:47 +02:00
|
|
|
#endif
|
2021-06-23 16:52:35 +02:00
|
|
|
|
2024-01-23 14:36:55 +01:00
|
|
|
if (element_id != IEEE802154_HEADER_IE_ELEMENT_ID_CSL_IE &&
|
|
|
|
!valid_vendor_specific_ie) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2021-06-23 16:52:35 +02:00
|
|
|
|
2023-09-20 19:31:47 +02:00
|
|
|
nrf_802154_ack_data_set(short_addr_le, false, config->ack_ie.header_ie,
|
|
|
|
config->ack_ie.header_ie->length +
|
|
|
|
IEEE802154_HEADER_IE_HEADER_LENGTH,
|
|
|
|
NRF_802154_ACK_DATA_IE);
|
|
|
|
nrf_802154_ack_data_set(ext_addr_le, true, config->ack_ie.header_ie,
|
|
|
|
config->ack_ie.header_ie->length +
|
|
|
|
IEEE802154_HEADER_IE_HEADER_LENGTH,
|
|
|
|
NRF_802154_ACK_DATA_IE);
|
2021-06-23 16:52:35 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
#if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
|
2023-09-21 14:51:07 +02:00
|
|
|
case IEEE802154_CONFIG_EXPECTED_RX_TIME: {
|
2023-10-30 09:21:12 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_NRF_802154_SER_HOST)
|
|
|
|
net_time_t period_ns = nrf5_data.csl_period * NSEC_PER_TEN_SYMBOLS;
|
2023-11-20 16:00:59 +01:00
|
|
|
bool changed = (config->expected_rx_time - nrf5_data.csl_rx_time) % period_ns;
|
2023-10-30 09:21:12 +01:00
|
|
|
|
2023-11-20 16:00:59 +01:00
|
|
|
nrf5_data.csl_rx_time = config->expected_rx_time;
|
2023-10-30 09:21:12 +01:00
|
|
|
|
|
|
|
if (changed)
|
|
|
|
#endif /* CONFIG_NRF_802154_SER_HOST */
|
|
|
|
{
|
|
|
|
nrf_802154_csl_writer_anchor_time_set(
|
|
|
|
nrf_802154_timestamp_phr_to_mhr_convert(config->expected_rx_time /
|
|
|
|
NSEC_PER_USEC));
|
|
|
|
}
|
2023-07-07 09:34:18 +02:00
|
|
|
} break;
|
2021-06-23 16:52:35 +02:00
|
|
|
|
2023-07-07 09:34:18 +02:00
|
|
|
case IEEE802154_CONFIG_RX_SLOT: {
|
2023-03-17 09:30:41 +01:00
|
|
|
/* Note that even if the nrf_802154_receive_at function is not called in time
|
|
|
|
* (for example due to the call being blocked by higher priority threads) and
|
|
|
|
* the delayed reception window is not scheduled, the CSL phase will still be
|
|
|
|
* calculated as if the following reception windows were at times
|
|
|
|
* anchor_time + n * csl_period. The previously set
|
|
|
|
* anchor_time will be used for calculations.
|
|
|
|
*/
|
2023-07-13 00:02:49 +02:00
|
|
|
nrf_802154_receive_at(config->rx_slot.start / NSEC_PER_USEC,
|
|
|
|
config->rx_slot.duration / NSEC_PER_USEC,
|
2023-07-11 17:54:00 +02:00
|
|
|
config->rx_slot.channel, DRX_SLOT_RX);
|
2023-07-07 09:34:18 +02:00
|
|
|
} break;
|
2021-06-23 16:52:35 +02:00
|
|
|
|
2023-10-30 09:21:12 +01:00
|
|
|
case IEEE802154_CONFIG_CSL_PERIOD: {
|
2023-03-17 09:30:41 +01:00
|
|
|
nrf_802154_csl_writer_period_set(config->csl_period);
|
2023-10-30 09:21:12 +01:00
|
|
|
#if defined(CONFIG_NRF_802154_SER_HOST)
|
|
|
|
nrf5_data.csl_period = config->csl_period;
|
|
|
|
#endif
|
|
|
|
} break;
|
2021-06-23 16:52:35 +02:00
|
|
|
#endif /* CONFIG_IEEE802154_CSL_ENDPOINT */
|
|
|
|
|
2023-08-08 12:57:48 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
|
|
|
case IEEE802154_OPENTHREAD_CONFIG_MAX_EXTRA_CCA_ATTEMPTS:
|
2023-07-18 12:28:33 +02:00
|
|
|
nrf5_data.max_extra_cca_attempts =
|
|
|
|
((const struct ieee802154_openthread_config *)config)
|
|
|
|
->max_extra_cca_attempts;
|
|
|
|
break;
|
2023-08-08 12:57:48 +02:00
|
|
|
#endif /* CONFIG_IEEE802154_NRF5_MULTIPLE_CCA */
|
2023-07-18 12:28:33 +02:00
|
|
|
|
2023-10-31 10:23:51 +01:00
|
|
|
case IEEE802154_CONFIG_RX_ON_WHEN_IDLE:
|
|
|
|
nrf_802154_rx_on_when_idle_set(config->rx_on_when_idle);
|
2024-01-18 11:34:34 +01:00
|
|
|
nrf5_data.rx_on_when_idle = config->rx_on_when_idle;
|
2023-10-31 10:23:51 +01:00
|
|
|
break;
|
|
|
|
|
2019-04-16 14:52:59 +02:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-02 14:26:44 +02:00
|
|
|
/* driver-allocated attribute memory - constant across all driver instances */
|
|
|
|
IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, 11, 26);
|
|
|
|
|
2023-07-18 12:28:33 +02:00
|
|
|
static int nrf5_attr_get(const struct device *dev,
|
|
|
|
enum ieee802154_attr attr,
|
|
|
|
struct ieee802154_attr_value *value)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
2023-09-02 14:26:44 +02:00
|
|
|
|
|
|
|
if (ieee802154_attr_get_channel_page_and_range(
|
|
|
|
attr, IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915,
|
|
|
|
&drv_attr.phy_supported_channels, value) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-18 12:28:33 +02:00
|
|
|
|
2023-08-08 12:57:48 +02:00
|
|
|
switch ((uint32_t)attr) {
|
|
|
|
#if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
|
2023-07-18 12:28:33 +02:00
|
|
|
/* TODO: t_recca and t_ccatx should be provided by the public API of the
|
|
|
|
* nRF 802.15.4 Radio Driver.
|
|
|
|
*/
|
|
|
|
case IEEE802154_OPENTHREAD_ATTR_T_RECCA:
|
|
|
|
((struct ieee802154_openthread_attr_value *)value)->t_recca = 0;
|
|
|
|
break;
|
|
|
|
case IEEE802154_OPENTHREAD_ATTR_T_CCATX:
|
|
|
|
((struct ieee802154_openthread_attr_value *)value)->t_ccatx = 20;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
/* nRF5 radio driver callbacks */
|
|
|
|
|
2022-02-23 12:16:19 +01:00
|
|
|
void nrf_802154_received_timestamp_raw(uint8_t *data, int8_t power, uint8_t lqi, uint64_t time)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(nrf5_data.rx_frames); i++) {
|
2019-01-17 14:12:27 +01:00
|
|
|
if (nrf5_data.rx_frames[i].psdu != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nrf5_data.rx_frames[i].psdu = data;
|
|
|
|
nrf5_data.rx_frames[i].rssi = power;
|
|
|
|
nrf5_data.rx_frames[i].lqi = lqi;
|
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_NET_PKT_TIMESTAMP)
|
2023-07-13 10:30:26 +02:00
|
|
|
nrf5_data.rx_frames[i].time =
|
|
|
|
nrf_802154_timestamp_end_to_phr_convert(time, data[0]);
|
2021-09-15 08:36:02 +02:00
|
|
|
#endif
|
|
|
|
|
2023-11-02 12:47:38 +01:00
|
|
|
nrf5_data.rx_frames[i].ack_fpb = nrf5_data.last_frame_ack_fpb;
|
|
|
|
nrf5_data.rx_frames[i].ack_seb = nrf5_data.last_frame_ack_seb;
|
2020-03-20 11:39:21 +01:00
|
|
|
nrf5_data.last_frame_ack_fpb = false;
|
2023-11-02 12:47:38 +01:00
|
|
|
nrf5_data.last_frame_ack_seb = false;
|
2020-03-20 11:39:21 +01:00
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
k_fifo_put(&nrf5_data.rx_fifo, &nrf5_data.rx_frames[i]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
__ASSERT(false, "Not enough rx frames allocated for 15.4 driver");
|
|
|
|
}
|
2017-02-06 12:42:34 +01:00
|
|
|
|
2021-04-21 10:14:26 +02:00
|
|
|
void nrf_802154_receive_failed(nrf_802154_rx_error_t error, uint32_t id)
|
2019-01-17 14:12:27 +01:00
|
|
|
{
|
2023-11-22 09:56:03 +01:00
|
|
|
const struct device *dev = nrf5_get_device();
|
2022-02-04 11:38:04 +01:00
|
|
|
|
2021-06-23 16:52:35 +02:00
|
|
|
#if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
|
2023-10-31 10:23:51 +01:00
|
|
|
if (id == DRX_SLOT_RX && error == NRF_802154_RX_ERROR_DELAYED_TIMEOUT) {
|
2024-01-18 11:34:34 +01:00
|
|
|
if (!nrf5_data.rx_on_when_idle) {
|
|
|
|
/* Transition to RxOff done automatically by the driver */
|
|
|
|
return;
|
|
|
|
} else if (nrf5_data.event_handler) {
|
|
|
|
/* Notify the higher layer to allow it to transition if needed */
|
|
|
|
nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_OFF, NULL);
|
|
|
|
}
|
2021-06-23 16:52:35 +02:00
|
|
|
}
|
|
|
|
#else
|
2021-04-21 10:14:26 +02:00
|
|
|
ARG_UNUSED(id);
|
2021-06-23 16:52:35 +02:00
|
|
|
#endif
|
2021-04-21 10:14:26 +02:00
|
|
|
|
2020-10-15 12:20:28 +02:00
|
|
|
enum ieee802154_rx_fail_reason reason;
|
|
|
|
|
|
|
|
switch (error) {
|
|
|
|
case NRF_802154_RX_ERROR_INVALID_FRAME:
|
|
|
|
case NRF_802154_RX_ERROR_DELAYED_TIMEOUT:
|
|
|
|
reason = IEEE802154_RX_FAIL_NOT_RECEIVED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NRF_802154_RX_ERROR_INVALID_FCS:
|
|
|
|
reason = IEEE802154_RX_FAIL_INVALID_FCS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NRF_802154_RX_ERROR_INVALID_DEST_ADDR:
|
|
|
|
reason = IEEE802154_RX_FAIL_ADDR_FILTERED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
reason = IEEE802154_RX_FAIL_OTHER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:55:26 +01:00
|
|
|
if (IS_ENABLED(CONFIG_IEEE802154_NRF5_LOG_RX_FAILURES)) {
|
|
|
|
LOG_INF("Rx failed, error = %d", error);
|
|
|
|
}
|
|
|
|
|
2020-03-20 11:39:21 +01:00
|
|
|
nrf5_data.last_frame_ack_fpb = false;
|
2023-11-02 12:47:38 +01:00
|
|
|
nrf5_data.last_frame_ack_seb = false;
|
|
|
|
|
2020-10-15 12:20:28 +02:00
|
|
|
if (nrf5_data.event_handler) {
|
2022-02-04 11:38:04 +01:00
|
|
|
nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_FAILED, (void *)&reason);
|
2020-10-15 12:20:28 +02:00
|
|
|
}
|
2020-03-20 11:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void nrf_802154_tx_ack_started(const uint8_t *data)
|
|
|
|
{
|
2023-11-02 12:47:38 +01:00
|
|
|
nrf5_data.last_frame_ack_fpb = data[FRAME_PENDING_OFFSET] & FRAME_PENDING_BIT;
|
|
|
|
nrf5_data.last_frame_ack_seb = data[SECURITY_ENABLED_OFFSET] & SECURITY_ENABLED_BIT;
|
2017-02-06 12:42:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-12 15:00:37 +02:00
|
|
|
void nrf_802154_transmitted_raw(uint8_t *frame,
|
|
|
|
const nrf_802154_transmit_done_metadata_t *metadata)
|
2021-06-09 10:06:02 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(frame);
|
|
|
|
|
|
|
|
nrf5_data.tx_result = NRF_802154_TX_ERROR_NONE;
|
2021-07-12 15:00:37 +02:00
|
|
|
nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
|
|
|
|
nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
|
|
|
|
nrf5_data.ack_frame.psdu = metadata->data.transmitted.p_ack;
|
2021-06-09 10:06:02 +02:00
|
|
|
|
2021-11-09 13:24:45 +01:00
|
|
|
if (nrf5_data.ack_frame.psdu) {
|
|
|
|
nrf5_data.ack_frame.rssi = metadata->data.transmitted.power;
|
|
|
|
nrf5_data.ack_frame.lqi = metadata->data.transmitted.lqi;
|
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_NET_PKT_TIMESTAMP)
|
2024-02-23 13:13:10 +01:00
|
|
|
if (metadata->data.transmitted.time == NRF_802154_NO_TIMESTAMP) {
|
|
|
|
/* Ack timestamp is invalid. Keep this value to detect it when handling Ack
|
|
|
|
*/
|
|
|
|
nrf5_data.ack_frame.time = NRF_802154_NO_TIMESTAMP;
|
|
|
|
} else {
|
|
|
|
nrf5_data.ack_frame.time = nrf_802154_timestamp_end_to_phr_convert(
|
|
|
|
metadata->data.transmitted.time, nrf5_data.ack_frame.psdu[0]);
|
|
|
|
}
|
2021-06-02 16:00:37 +02:00
|
|
|
#endif
|
2021-11-09 13:24:45 +01:00
|
|
|
}
|
2021-06-02 16:00:37 +02:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
k_sem_give(&nrf5_data.tx_wait);
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:00:37 +02:00
|
|
|
void nrf_802154_transmit_failed(uint8_t *frame,
|
|
|
|
nrf_802154_tx_error_t error,
|
|
|
|
const nrf_802154_transmit_done_metadata_t *metadata)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2019-01-17 14:12:27 +01:00
|
|
|
ARG_UNUSED(frame);
|
|
|
|
|
|
|
|
nrf5_data.tx_result = error;
|
2021-07-12 15:00:37 +02:00
|
|
|
nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
|
|
|
|
nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
|
2019-01-17 14:12:27 +01:00
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
k_sem_give(&nrf5_data.tx_wait);
|
|
|
|
}
|
|
|
|
|
2019-01-17 14:12:27 +01:00
|
|
|
void nrf_802154_cca_done(bool channel_free)
|
2017-02-06 12:42:34 +01:00
|
|
|
{
|
2019-01-17 14:12:27 +01:00
|
|
|
nrf5_data.channel_free = channel_free;
|
|
|
|
|
|
|
|
k_sem_give(&nrf5_data.cca_wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nrf_802154_cca_failed(nrf_802154_cca_error_t error)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(error);
|
|
|
|
|
|
|
|
nrf5_data.channel_free = false;
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
k_sem_give(&nrf5_data.cca_wait);
|
|
|
|
}
|
|
|
|
|
2023-07-10 16:25:53 +02:00
|
|
|
void nrf_802154_energy_detected(const nrf_802154_energy_detected_t *result)
|
2020-02-25 11:17:45 +01:00
|
|
|
{
|
|
|
|
if (nrf5_data.energy_scan_done != NULL) {
|
|
|
|
energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
|
|
|
|
|
|
|
|
nrf5_data.energy_scan_done = NULL;
|
2023-11-22 09:56:03 +01:00
|
|
|
callback(nrf5_get_device(), result->ed_dbm);
|
2020-02-25 11:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)
|
|
|
|
{
|
|
|
|
if (nrf5_data.energy_scan_done != NULL) {
|
|
|
|
energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
|
|
|
|
|
|
|
|
nrf5_data.energy_scan_done = NULL;
|
2023-11-22 09:56:03 +01:00
|
|
|
callback(nrf5_get_device(), SHRT_MAX);
|
2020-02-25 11:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 13:51:00 +02:00
|
|
|
#if defined(CONFIG_NRF_802154_SER_HOST)
|
2022-06-03 15:10:58 +02:00
|
|
|
void nrf_802154_serialization_error(const nrf_802154_ser_err_data_t *err)
|
2020-11-13 16:39:45 +01:00
|
|
|
{
|
2022-06-03 15:10:58 +02:00
|
|
|
__ASSERT(false, "802.15.4 serialization error: %d", err->reason);
|
2023-05-23 08:11:16 +02:00
|
|
|
k_oops();
|
2020-11-13 16:39:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-06 12:42:34 +01:00
|
|
|
static const struct nrf5_802154_config nrf5_radio_cfg = {
|
2019-04-25 15:10:10 +02:00
|
|
|
.irq_config_func = nrf5_irq_config,
|
2017-02-06 12:42:34 +01:00
|
|
|
};
|
|
|
|
|
2023-12-25 16:16:28 +01:00
|
|
|
static const struct ieee802154_radio_api nrf5_radio_api = {
|
2017-02-06 12:42:34 +01:00
|
|
|
.iface_api.init = nrf5_iface_init,
|
|
|
|
|
2017-09-04 15:24:36 +02:00
|
|
|
.get_capabilities = nrf5_get_capabilities,
|
2017-02-06 12:42:34 +01:00
|
|
|
.cca = nrf5_cca,
|
|
|
|
.set_channel = nrf5_set_channel,
|
2018-03-19 09:53:29 +01:00
|
|
|
.filter = nrf5_filter,
|
2017-02-06 12:42:34 +01:00
|
|
|
.set_txpower = nrf5_set_txpower,
|
|
|
|
.start = nrf5_start,
|
|
|
|
.stop = nrf5_stop,
|
2023-02-21 14:13:01 +01:00
|
|
|
#if defined(CONFIG_NRF_802154_CARRIER_FUNCTIONS)
|
|
|
|
.continuous_carrier = nrf5_continuous_carrier,
|
|
|
|
#endif
|
2017-02-06 12:42:34 +01:00
|
|
|
.tx = nrf5_tx,
|
2020-02-25 11:17:45 +01:00
|
|
|
.ed_scan = nrf5_energy_scan_start,
|
2021-04-23 13:51:00 +02:00
|
|
|
.get_time = nrf5_get_time,
|
2021-06-23 16:52:35 +02:00
|
|
|
.get_sch_acc = nrf5_get_acc,
|
2019-04-16 14:52:59 +02:00
|
|
|
.configure = nrf5_configure,
|
2023-07-18 12:28:33 +02:00
|
|
|
.attr_get = nrf5_attr_get
|
2017-02-06 12:42:34 +01:00
|
|
|
};
|
|
|
|
|
2018-01-15 11:39:54 +01:00
|
|
|
#if defined(CONFIG_NET_L2_IEEE802154)
|
|
|
|
#define L2 IEEE802154_L2
|
|
|
|
#define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
|
2022-08-27 09:34:54 +02:00
|
|
|
#define MTU IEEE802154_MTU
|
2018-01-15 11:39:54 +01:00
|
|
|
#elif defined(CONFIG_NET_L2_OPENTHREAD)
|
|
|
|
#define L2 OPENTHREAD_L2
|
|
|
|
#define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
|
|
|
|
#define MTU 1280
|
2021-11-19 10:59:55 +01:00
|
|
|
#elif defined(CONFIG_NET_L2_CUSTOM_IEEE802154)
|
|
|
|
#define L2 CUSTOM_IEEE802154_L2
|
|
|
|
#define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(CUSTOM_IEEE802154_L2)
|
|
|
|
#define MTU CONFIG_NET_L2_CUSTOM_IEEE802154_MTU
|
2018-01-15 11:39:54 +01:00
|
|
|
#endif
|
|
|
|
|
2021-11-19 10:59:55 +01:00
|
|
|
#if defined(CONFIG_NET_L2_PHY_IEEE802154)
|
2022-07-15 16:26:35 +02:00
|
|
|
NET_DEVICE_DT_INST_DEFINE(0, nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
|
|
|
|
CONFIG_IEEE802154_NRF5_INIT_PRIO, &nrf5_radio_api, L2,
|
|
|
|
L2_CTX_TYPE, MTU);
|
2018-01-15 11:39:54 +01:00
|
|
|
#else
|
2022-07-15 16:26:35 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
|
|
|
|
POST_KERNEL, CONFIG_IEEE802154_NRF5_INIT_PRIO,
|
|
|
|
&nrf5_radio_api);
|
2017-03-06 16:20:19 +01:00
|
|
|
#endif
|