1 /* ieee802154_nrf5.c - nRF5 802.15.4 driver */
2 
3 /*
4  * Copyright (c) 2017-2023 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT nordic_nrf_ieee802154
10 
11 #define LOG_MODULE_NAME ieee802154_nrf5
12 #if defined(CONFIG_IEEE802154_DRIVER_LOG_LEVEL)
13 #define LOG_LEVEL CONFIG_IEEE802154_DRIVER_LOG_LEVEL
14 #else
15 #define LOG_LEVEL LOG_LEVEL_NONE
16 #endif
17 
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
20 
21 #include <errno.h>
22 
23 #include <zephyr/kernel.h>
24 #include <zephyr/arch/cpu.h>
25 #include <zephyr/debug/stack.h>
26 
27 #include <soc.h>
28 
29 #if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) && defined(NRF_FICR_S)
30 #include <soc_secure.h>
31 #else
32 #include <hal/nrf_ficr.h>
33 #endif
34 
35 #include <zephyr/device.h>
36 #include <zephyr/init.h>
37 #include <zephyr/debug/stack.h>
38 #include <zephyr/net/net_if.h>
39 #include <zephyr/net/net_pkt.h>
40 
41 #if defined(CONFIG_NET_L2_OPENTHREAD)
42 #include <zephyr/net/openthread.h>
43 #include <zephyr/net/ieee802154_radio_openthread.h>
44 #endif
45 
46 #include <zephyr/sys/byteorder.h>
47 #include <string.h>
48 #include <zephyr/random/random.h>
49 
50 #include <zephyr/net/ieee802154_radio.h>
51 #include <zephyr/irq.h>
52 
53 #include "ieee802154_nrf5.h"
54 #include "nrf_802154.h"
55 #include "nrf_802154_const.h"
56 
57 #if defined(CONFIG_NRF_802154_SER_HOST)
58 #include "nrf_802154_serialization_error.h"
59 #endif
60 
61 struct nrf5_802154_config {
62 	void (*irq_config_func)(const struct device *dev);
63 };
64 
65 static struct nrf5_802154_data nrf5_data;
66 #if defined(CONFIG_IEEE802154_RAW_MODE)
67 static const struct device *nrf5_dev;
68 #endif
69 
70 #define DRX_SLOT_RX 0 /* Delayed reception window ID */
71 
72 #define NSEC_PER_TEN_SYMBOLS (10 * IEEE802154_PHY_OQPSK_780_TO_2450MHZ_SYMBOL_PERIOD_NS)
73 
74 #if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
75 #if defined(CONFIG_SOC_NRF5340_CPUAPP)
76 #if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
77 #error "NRF_UICR->OTP is not supported to read from non-secure"
78 #else
79 #define EUI64_ADDR (NRF_UICR->OTP)
80 #endif /* CONFIG_TRUSTED_EXECUTION_NONSECURE */
81 #else
82 #define EUI64_ADDR (NRF_UICR->CUSTOMER)
83 #endif /* CONFIG_SOC_NRF5340_CPUAPP */
84 #endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
85 
86 #if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
87 #define EUI64_ADDR_HIGH CONFIG_IEEE802154_NRF5_UICR_EUI64_REG
88 #define EUI64_ADDR_LOW (CONFIG_IEEE802154_NRF5_UICR_EUI64_REG + 1)
89 #else
90 #define EUI64_ADDR_HIGH 0
91 #define EUI64_ADDR_LOW 1
92 #endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
93 
94 /* Convenience defines for RADIO */
95 #define NRF5_802154_DATA(dev) \
96 	((struct nrf5_802154_data * const)(dev)->data)
97 
98 #define NRF5_802154_CFG(dev) \
99 	((const struct nrf5_802154_config * const)(dev)->config)
100 
101 #if CONFIG_IEEE802154_VENDOR_OUI_ENABLE
102 #define IEEE802154_NRF5_VENDOR_OUI CONFIG_IEEE802154_VENDOR_OUI
103 #else
104 #define IEEE802154_NRF5_VENDOR_OUI (uint32_t)0xF4CE36
105 #endif
106 
nrf5_get_device(void)107 static inline const struct device *nrf5_get_device(void)
108 {
109 #if defined(CONFIG_IEEE802154_RAW_MODE)
110 	return nrf5_dev;
111 #else
112 	return net_if_get_device(nrf5_data.iface);
113 #endif
114 }
115 
nrf5_get_eui64(uint8_t * mac)116 static void nrf5_get_eui64(uint8_t *mac)
117 {
118 	uint64_t factoryAddress;
119 	uint32_t index = 0;
120 
121 #if !defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
122 	uint32_t deviceid[2];
123 
124 	/* Set the MAC Address Block Larger (MA-L) formerly called OUI. */
125 	mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 16) & 0xff;
126 	mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 8) & 0xff;
127 	mac[index++] = IEEE802154_NRF5_VENDOR_OUI & 0xff;
128 
129 #if defined(NRF54H_SERIES)
130 	/* Can't access SICR with device id on a radio core. Use BLE.ADDR. */
131 	deviceid[0] = NRF_FICR->BLE.ADDR[0];
132 	deviceid[1] = NRF_FICR->BLE.ADDR[1];
133 #elif defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) && defined(NRF_FICR_S)
134 	soc_secure_read_deviceid(deviceid);
135 #else
136 	deviceid[0] = nrf_ficr_deviceid_get(NRF_FICR, 0);
137 	deviceid[1] = nrf_ficr_deviceid_get(NRF_FICR, 1);
138 #endif
139 
140 	factoryAddress = (uint64_t)deviceid[EUI64_ADDR_HIGH] << 32;
141 	factoryAddress |= deviceid[EUI64_ADDR_LOW];
142 #else
143 	/* Use device identifier assigned during the production. */
144 	factoryAddress = (uint64_t)EUI64_ADDR[EUI64_ADDR_HIGH] << 32;
145 	factoryAddress |= EUI64_ADDR[EUI64_ADDR_LOW];
146 #endif
147 	memcpy(mac + index, &factoryAddress, sizeof(factoryAddress) - index);
148 }
149 
nrf5_rx_thread(void * arg1,void * arg2,void * arg3)150 static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
151 {
152 	struct nrf5_802154_data *nrf5_radio = (struct nrf5_802154_data *)arg1;
153 	struct net_pkt *pkt;
154 	struct nrf5_802154_rx_frame *rx_frame;
155 	uint8_t pkt_len;
156 	uint8_t *psdu;
157 
158 	ARG_UNUSED(arg2);
159 	ARG_UNUSED(arg3);
160 
161 	while (1) {
162 		pkt = NULL;
163 		rx_frame = NULL;
164 
165 		LOG_DBG("Waiting for frame");
166 
167 		rx_frame = k_fifo_get(&nrf5_radio->rx_fifo, K_FOREVER);
168 
169 		__ASSERT_NO_MSG(rx_frame->psdu);
170 
171 		/* rx_mpdu contains length, psdu, fcs|lqi
172 		 * The last 2 bytes contain LQI or FCS, depending if
173 		 * automatic CRC handling is enabled or not, respectively.
174 		 */
175 		if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
176 			pkt_len = rx_frame->psdu[0];
177 		} else {
178 			pkt_len = rx_frame->psdu[0] -  IEEE802154_FCS_LENGTH;
179 		}
180 
181 #if defined(CONFIG_NET_BUF_DATA_SIZE)
182 		__ASSERT_NO_MSG(pkt_len <= CONFIG_NET_BUF_DATA_SIZE);
183 #endif
184 
185 		LOG_DBG("Frame received");
186 
187 		/* Block the RX thread until net_pkt is available, so that we
188 		 * don't drop already ACKed frame in case of temporary net_pkt
189 		 * scarcity. The nRF 802154 radio driver will accumulate any
190 		 * incoming frames until it runs out of internal buffers (and
191 		 * thus stops acknowledging consecutive frames).
192 		 */
193 		pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, pkt_len,
194 						   AF_UNSPEC, 0, K_FOREVER);
195 
196 		if (net_pkt_write(pkt, rx_frame->psdu + 1, pkt_len)) {
197 			goto drop;
198 		}
199 
200 		net_pkt_set_ieee802154_lqi(pkt, rx_frame->lqi);
201 		net_pkt_set_ieee802154_rssi_dbm(pkt, rx_frame->rssi);
202 		net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);
203 
204 #if defined(CONFIG_NET_PKT_TIMESTAMP)
205 		net_pkt_set_timestamp_ns(pkt, rx_frame->time * NSEC_PER_USEC);
206 #endif
207 
208 #if defined(CONFIG_NET_L2_OPENTHREAD)
209 		net_pkt_set_ieee802154_ack_seb(pkt, rx_frame->ack_seb);
210 #endif
211 
212 		LOG_DBG("Caught a packet (%u) (LQI: %u)",
213 			 pkt_len, rx_frame->lqi);
214 
215 		if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
216 			LOG_ERR("Packet dropped by NET stack");
217 			goto drop;
218 		}
219 
220 		psdu = rx_frame->psdu;
221 		rx_frame->psdu = NULL;
222 		nrf_802154_buffer_free_raw(psdu);
223 
224 		if (LOG_LEVEL >= LOG_LEVEL_DBG) {
225 			log_stack_usage(&nrf5_radio->rx_thread);
226 		}
227 
228 		continue;
229 
230 drop:
231 		psdu = rx_frame->psdu;
232 		rx_frame->psdu = NULL;
233 		nrf_802154_buffer_free_raw(psdu);
234 
235 		net_pkt_unref(pkt);
236 	}
237 }
238 
nrf5_get_capabilities_at_boot(void)239 static void nrf5_get_capabilities_at_boot(void)
240 {
241 	nrf_802154_capabilities_t caps = nrf_802154_capabilities_get();
242 
243 	nrf5_data.capabilities =
244 		IEEE802154_HW_FCS |
245 		IEEE802154_HW_PROMISC |
246 		IEEE802154_HW_FILTER |
247 		((caps & NRF_802154_CAPABILITY_CSMA) ? IEEE802154_HW_CSMA : 0UL) |
248 		IEEE802154_HW_TX_RX_ACK |
249 		IEEE802154_HW_RX_TX_ACK |
250 		IEEE802154_HW_ENERGY_SCAN |
251 		((caps & NRF_802154_CAPABILITY_DELAYED_TX) ? IEEE802154_HW_TXTIME : 0UL) |
252 		((caps & NRF_802154_CAPABILITY_DELAYED_RX) ? IEEE802154_HW_RXTIME : 0UL) |
253 		IEEE802154_HW_SLEEP_TO_TX |
254 		IEEE802154_RX_ON_WHEN_IDLE |
255 		((caps & NRF_802154_CAPABILITY_SECURITY) ? IEEE802154_HW_TX_SEC : 0UL)
256 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
257 		| IEEE802154_OPENTHREAD_HW_MULTIPLE_CCA
258 #endif
259 #if defined(CONFIG_IEEE802154_SELECTIVE_TXCHANNEL)
260 		| IEEE802154_HW_SELECTIVE_TXCHANNEL
261 #endif
262 #if defined(CONFIG_IEEE802154_NRF5_CST_ENDPOINT)
263 		| IEEE802154_OPENTHREAD_HW_CST
264 #endif
265 		;
266 }
267 
268 /* Radio device API */
269 
nrf5_get_capabilities(const struct device * dev)270 static enum ieee802154_hw_caps nrf5_get_capabilities(const struct device *dev)
271 {
272 	return nrf5_data.capabilities;
273 }
274 
nrf5_cca(const struct device * dev)275 static int nrf5_cca(const struct device *dev)
276 {
277 	struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
278 
279 	if (!nrf_802154_cca()) {
280 		LOG_DBG("CCA failed");
281 		return -EBUSY;
282 	}
283 
284 	/* The nRF driver guarantees that a callback will be called once
285 	 * the CCA function is done, thus unlocking the semaphore.
286 	 */
287 	k_sem_take(&nrf5_radio->cca_wait, K_FOREVER);
288 
289 	LOG_DBG("Channel free? %d", nrf5_radio->channel_free);
290 
291 	return nrf5_radio->channel_free ? 0 : -EBUSY;
292 }
293 
nrf5_set_channel(const struct device * dev,uint16_t channel)294 static int nrf5_set_channel(const struct device *dev, uint16_t channel)
295 {
296 	ARG_UNUSED(dev);
297 
298 	LOG_DBG("%u", channel);
299 
300 	if (channel < 11 || channel > 26) {
301 		return channel < 11 ? -ENOTSUP : -EINVAL;
302 	}
303 
304 	nrf_802154_channel_set(channel);
305 
306 	return 0;
307 }
308 
nrf5_energy_scan_start(const struct device * dev,uint16_t duration,energy_scan_done_cb_t done_cb)309 static int nrf5_energy_scan_start(const struct device *dev,
310 				  uint16_t duration,
311 				  energy_scan_done_cb_t done_cb)
312 {
313 	int err = 0;
314 
315 	ARG_UNUSED(dev);
316 
317 	if (nrf5_data.energy_scan_done == NULL) {
318 		nrf5_data.energy_scan_done = done_cb;
319 
320 		if (nrf_802154_energy_detection(duration * 1000) == false) {
321 			nrf5_data.energy_scan_done = NULL;
322 			err = -EBUSY;
323 		}
324 	} else {
325 		err = -EALREADY;
326 	}
327 
328 	return err;
329 }
330 
nrf5_set_pan_id(const struct device * dev,uint16_t pan_id)331 static int nrf5_set_pan_id(const struct device *dev, uint16_t pan_id)
332 {
333 	uint8_t pan_id_le[2];
334 
335 	ARG_UNUSED(dev);
336 
337 	sys_put_le16(pan_id, pan_id_le);
338 	nrf_802154_pan_id_set(pan_id_le);
339 
340 	LOG_DBG("0x%x", pan_id);
341 
342 	return 0;
343 }
344 
nrf5_set_short_addr(const struct device * dev,uint16_t short_addr)345 static int nrf5_set_short_addr(const struct device *dev, uint16_t short_addr)
346 {
347 	uint8_t short_addr_le[2];
348 
349 	ARG_UNUSED(dev);
350 
351 	sys_put_le16(short_addr, short_addr_le);
352 	nrf_802154_short_address_set(short_addr_le);
353 
354 	LOG_DBG("0x%x", short_addr);
355 
356 	return 0;
357 }
358 
nrf5_set_ieee_addr(const struct device * dev,const uint8_t * ieee_addr)359 static int nrf5_set_ieee_addr(const struct device *dev,
360 			      const uint8_t *ieee_addr)
361 {
362 	ARG_UNUSED(dev);
363 
364 	LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
365 		    ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
366 		    ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
367 
368 	nrf_802154_extended_address_set(ieee_addr);
369 
370 	return 0;
371 }
372 
nrf5_filter(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)373 static int nrf5_filter(const struct device *dev, bool set,
374 		       enum ieee802154_filter_type type,
375 		       const struct ieee802154_filter *filter)
376 {
377 	LOG_DBG("Applying filter %u", type);
378 
379 	if (!set) {
380 		return -ENOTSUP;
381 	}
382 
383 	if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
384 		return nrf5_set_ieee_addr(dev, filter->ieee_addr);
385 	} else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
386 		return nrf5_set_short_addr(dev, filter->short_addr);
387 	} else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
388 		return nrf5_set_pan_id(dev, filter->pan_id);
389 	}
390 
391 	return -ENOTSUP;
392 }
393 
nrf5_set_txpower(const struct device * dev,int16_t dbm)394 static int nrf5_set_txpower(const struct device *dev, int16_t dbm)
395 {
396 	ARG_UNUSED(dev);
397 
398 	LOG_DBG("%d", dbm);
399 
400 	nrf5_data.txpwr = dbm;
401 
402 	return 0;
403 }
404 
handle_ack(struct nrf5_802154_data * nrf5_radio)405 static int handle_ack(struct nrf5_802154_data *nrf5_radio)
406 {
407 	uint8_t ack_len;
408 	struct net_pkt *ack_pkt;
409 	int err = 0;
410 
411 #if defined(CONFIG_NET_PKT_TIMESTAMP)
412 	if (nrf5_radio->ack_frame.time == NRF_802154_NO_TIMESTAMP) {
413 		/* Ack timestamp is invalid and cannot be used by the upper layer.
414 		 * Report the transmission as failed as if the Ack was not received at all.
415 		 */
416 		LOG_WRN("Invalid ACK timestamp.");
417 		err = -ENOMSG;
418 		goto free_nrf_ack;
419 	}
420 #endif
421 
422 	if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
423 		ack_len = nrf5_radio->ack_frame.psdu[0];
424 	} else {
425 		ack_len = nrf5_radio->ack_frame.psdu[0] - IEEE802154_FCS_LENGTH;
426 	}
427 
428 	ack_pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, ack_len,
429 					       AF_UNSPEC, 0, K_NO_WAIT);
430 	if (!ack_pkt) {
431 		LOG_ERR("No free packet available.");
432 		err = -ENOMEM;
433 		goto free_nrf_ack;
434 	}
435 
436 	/* Upper layers expect the frame to start at the MAC header, skip the
437 	 * PHY header (1 byte).
438 	 */
439 	if (net_pkt_write(ack_pkt, nrf5_radio->ack_frame.psdu + 1,
440 			  ack_len) < 0) {
441 		LOG_ERR("Failed to write to a packet.");
442 		err = -ENOMEM;
443 		goto free_net_ack;
444 	}
445 
446 	net_pkt_set_ieee802154_lqi(ack_pkt, nrf5_radio->ack_frame.lqi);
447 	net_pkt_set_ieee802154_rssi_dbm(ack_pkt, nrf5_radio->ack_frame.rssi);
448 
449 #if defined(CONFIG_NET_PKT_TIMESTAMP)
450 	net_pkt_set_timestamp_ns(ack_pkt, nrf5_radio->ack_frame.time * NSEC_PER_USEC);
451 #endif
452 
453 	net_pkt_cursor_init(ack_pkt);
454 
455 	if (ieee802154_handle_ack(nrf5_radio->iface, ack_pkt) != NET_OK) {
456 		LOG_INF("ACK packet not handled - releasing.");
457 	}
458 
459 free_net_ack:
460 	net_pkt_unref(ack_pkt);
461 
462 free_nrf_ack:
463 	nrf_802154_buffer_free_raw(nrf5_radio->ack_frame.psdu);
464 	nrf5_radio->ack_frame.psdu = NULL;
465 
466 	return err;
467 }
468 
nrf5_tx_started(const struct device * dev,struct net_pkt * pkt,struct net_buf * frag)469 static void nrf5_tx_started(const struct device *dev,
470 			    struct net_pkt *pkt,
471 			    struct net_buf *frag)
472 {
473 	ARG_UNUSED(pkt);
474 
475 	if (nrf5_data.event_handler) {
476 		nrf5_data.event_handler(dev, IEEE802154_EVENT_TX_STARTED,
477 					(void *)frag);
478 	}
479 }
480 
nrf5_tx_immediate(struct net_pkt * pkt,uint8_t * payload,bool cca)481 static bool nrf5_tx_immediate(struct net_pkt *pkt, uint8_t *payload, bool cca)
482 {
483 	nrf_802154_transmit_metadata_t metadata = {
484 		.frame_props = {
485 			.is_secured = net_pkt_ieee802154_frame_secured(pkt),
486 			.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
487 		},
488 		.cca = cca,
489 		.tx_power = {
490 			.use_metadata_value = true,
491 			.power = nrf5_data.txpwr,
492 		},
493 	};
494 
495 	return nrf_802154_transmit_raw(payload, &metadata);
496 }
497 
498 #if NRF_802154_CSMA_CA_ENABLED
nrf5_tx_csma_ca(struct net_pkt * pkt,uint8_t * payload)499 static bool nrf5_tx_csma_ca(struct net_pkt *pkt, uint8_t *payload)
500 {
501 	nrf_802154_transmit_csma_ca_metadata_t metadata = {
502 		.frame_props = {
503 			.is_secured = net_pkt_ieee802154_frame_secured(pkt),
504 			.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
505 		},
506 		.tx_power = {
507 			.use_metadata_value = true,
508 			.power = nrf5_data.txpwr,
509 		},
510 	};
511 
512 	return nrf_802154_transmit_csma_ca_raw(payload, &metadata);
513 }
514 #endif
515 
516 #if defined(CONFIG_NET_PKT_TXTIME)
nrf5_tx_at(struct nrf5_802154_data * nrf5_radio,struct net_pkt * pkt,uint8_t * payload,enum ieee802154_tx_mode mode)517 static bool nrf5_tx_at(struct nrf5_802154_data *nrf5_radio, struct net_pkt *pkt,
518 		   uint8_t *payload, enum ieee802154_tx_mode mode)
519 {
520 	bool cca = false;
521 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
522 	uint8_t max_extra_cca_attempts = 0;
523 #endif
524 
525 	switch (mode) {
526 	case IEEE802154_TX_MODE_TXTIME:
527 		break;
528 	case IEEE802154_TX_MODE_TXTIME_CCA:
529 		cca = true;
530 		break;
531 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
532 	case IEEE802154_OPENTHREAD_TX_MODE_TXTIME_MULTIPLE_CCA:
533 		cca = true;
534 		max_extra_cca_attempts = nrf5_data.max_extra_cca_attempts;
535 		break;
536 #endif
537 		break;
538 	default:
539 		__ASSERT_NO_MSG(false);
540 		return false;
541 	}
542 
543 	nrf_802154_transmit_at_metadata_t metadata = {
544 		.frame_props = {
545 			.is_secured = net_pkt_ieee802154_frame_secured(pkt),
546 			.dynamic_data_is_set = net_pkt_ieee802154_mac_hdr_rdy(pkt),
547 		},
548 		.cca = cca,
549 #if defined(CONFIG_IEEE802154_SELECTIVE_TXCHANNEL)
550 		.channel = net_pkt_ieee802154_txchannel(pkt),
551 #else
552 		.channel = nrf_802154_channel_get(),
553 #endif
554 		.tx_power = {
555 			.use_metadata_value = true,
556 			.power = nrf5_data.txpwr,
557 		},
558 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
559 		.extra_cca_attempts = max_extra_cca_attempts,
560 #endif
561 	};
562 
563 	/* The timestamp points to the start of PHR but `nrf_802154_transmit_raw_at`
564 	 * expects a timestamp pointing to start of SHR.
565 	 */
566 	uint64_t tx_at = nrf_802154_timestamp_phr_to_shr_convert(
567 		net_pkt_timestamp_ns(pkt) / NSEC_PER_USEC);
568 
569 	return nrf_802154_transmit_raw_at(payload, tx_at, &metadata);
570 }
571 #endif /* CONFIG_NET_PKT_TXTIME */
572 
nrf5_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)573 static int nrf5_tx(const struct device *dev,
574 		   enum ieee802154_tx_mode mode,
575 		   struct net_pkt *pkt,
576 		   struct net_buf *frag)
577 {
578 	struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
579 	uint8_t payload_len = frag->len;
580 	uint8_t *payload = frag->data;
581 	bool ret = true;
582 
583 	if (payload_len > IEEE802154_MTU) {
584 		LOG_ERR("Payload too large: %d", payload_len);
585 		return -EMSGSIZE;
586 	}
587 
588 	LOG_DBG("%p (%u)", payload, payload_len);
589 
590 	nrf5_radio->tx_psdu[0] = payload_len + IEEE802154_FCS_LENGTH;
591 	memcpy(nrf5_radio->tx_psdu + 1, payload, payload_len);
592 
593 	/* Reset semaphore in case ACK was received after timeout */
594 	k_sem_reset(&nrf5_radio->tx_wait);
595 
596 	switch (mode) {
597 	case IEEE802154_TX_MODE_DIRECT:
598 	case IEEE802154_TX_MODE_CCA:
599 		ret = nrf5_tx_immediate(pkt, nrf5_radio->tx_psdu,
600 					mode == IEEE802154_TX_MODE_CCA);
601 		break;
602 #if NRF_802154_CSMA_CA_ENABLED
603 	case IEEE802154_TX_MODE_CSMA_CA:
604 		ret = nrf5_tx_csma_ca(pkt, nrf5_radio->tx_psdu);
605 		break;
606 #endif
607 #if defined(CONFIG_NET_PKT_TXTIME)
608 	case IEEE802154_TX_MODE_TXTIME:
609 	case IEEE802154_TX_MODE_TXTIME_CCA:
610 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
611 	case IEEE802154_OPENTHREAD_TX_MODE_TXTIME_MULTIPLE_CCA:
612 #endif
613 		__ASSERT_NO_MSG(pkt);
614 		ret = nrf5_tx_at(nrf5_radio, pkt, nrf5_radio->tx_psdu, mode);
615 		break;
616 #endif /* CONFIG_NET_PKT_TXTIME */
617 	default:
618 		NET_ERR("TX mode %d not supported", mode);
619 		return -ENOTSUP;
620 	}
621 
622 	if (!ret) {
623 		LOG_ERR("Cannot send frame");
624 		return -EIO;
625 	}
626 
627 	nrf5_tx_started(dev, pkt, frag);
628 
629 	LOG_DBG("Sending frame (ch:%d, txpower:%d)",
630 		nrf_802154_channel_get(), nrf_802154_tx_power_get());
631 
632 	/* Wait for the callback from the radio driver. */
633 	k_sem_take(&nrf5_radio->tx_wait, K_FOREVER);
634 
635 	LOG_DBG("Result: %d", nrf5_data.tx_result);
636 
637 #if defined(CONFIG_NRF_802154_ENCRYPTION)
638 	/*
639 	 * When frame encryption by the radio driver is enabled, the frame stored in
640 	 * the tx_psdu buffer is:
641 	 * 1) authenticated and encrypted in place which causes that after an unsuccessful
642 	 *    TX attempt, this frame must be propagated back to the upper layer for retransmission.
643 	 *    The upper layer must ensure that the exact same secured frame is used for
644 	 *    retransmission
645 	 * 2) frame counters are updated in place and for keeping the link frame counter up to date,
646 	 *    this information must be propagated back to the upper layer
647 	 */
648 	memcpy(payload, nrf5_radio->tx_psdu + 1, payload_len);
649 #endif
650 	net_pkt_set_ieee802154_frame_secured(pkt, nrf5_radio->tx_frame_is_secured);
651 	net_pkt_set_ieee802154_mac_hdr_rdy(pkt, nrf5_radio->tx_frame_mac_hdr_rdy);
652 
653 	switch (nrf5_radio->tx_result) {
654 	case NRF_802154_TX_ERROR_NONE:
655 		if (nrf5_radio->ack_frame.psdu == NULL) {
656 			/* No ACK was requested. */
657 			return 0;
658 		}
659 		/* Handle ACK packet. */
660 		return handle_ack(nrf5_radio);
661 	case NRF_802154_TX_ERROR_NO_MEM:
662 		return -ENOBUFS;
663 	case NRF_802154_TX_ERROR_BUSY_CHANNEL:
664 		return -EBUSY;
665 	case NRF_802154_TX_ERROR_INVALID_ACK:
666 	case NRF_802154_TX_ERROR_NO_ACK:
667 		return -ENOMSG;
668 	case NRF_802154_TX_ERROR_ABORTED:
669 	case NRF_802154_TX_ERROR_TIMESLOT_DENIED:
670 	case NRF_802154_TX_ERROR_TIMESLOT_ENDED:
671 	default:
672 		return -EIO;
673 	}
674 }
675 
nrf5_get_time(const struct device * dev)676 static net_time_t nrf5_get_time(const struct device *dev)
677 {
678 	ARG_UNUSED(dev);
679 
680 	return (net_time_t)nrf_802154_time_get() * NSEC_PER_USEC;
681 }
682 
nrf5_get_acc(const struct device * dev)683 static uint8_t nrf5_get_acc(const struct device *dev)
684 {
685 	ARG_UNUSED(dev);
686 
687 	return CONFIG_IEEE802154_NRF5_DELAY_TRX_ACC;
688 }
689 
nrf5_start(const struct device * dev)690 static int nrf5_start(const struct device *dev)
691 {
692 	ARG_UNUSED(dev);
693 
694 	nrf_802154_tx_power_set(nrf5_data.txpwr);
695 
696 	if (!nrf_802154_receive()) {
697 		LOG_ERR("Failed to enter receive state");
698 		return -EIO;
699 	}
700 
701 	LOG_DBG("nRF5 802154 radio started (channel: %d)",
702 		nrf_802154_channel_get());
703 
704 	return 0;
705 }
706 
nrf5_stop(const struct device * dev)707 static int nrf5_stop(const struct device *dev)
708 {
709 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
710 	if (nrf_802154_sleep_if_idle() != NRF_802154_SLEEP_ERROR_NONE) {
711 		if (nrf5_data.event_handler) {
712 			nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_OFF, NULL);
713 		} else {
714 			LOG_WRN("Transition to radio sleep cannot be handled.");
715 		}
716 		Z_SPIN_DELAY(1);
717 		return 0;
718 	}
719 #else
720 	ARG_UNUSED(dev);
721 
722 	if (!nrf_802154_sleep()) {
723 		LOG_ERR("Error while stopping radio");
724 		return -EIO;
725 	}
726 #endif
727 
728 	LOG_DBG("nRF5 802154 radio stopped");
729 
730 	return 0;
731 }
732 
733 #if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS)
nrf5_continuous_carrier(const struct device * dev)734 static int nrf5_continuous_carrier(const struct device *dev)
735 {
736 	ARG_UNUSED(dev);
737 
738 	nrf_802154_tx_power_set(nrf5_data.txpwr);
739 
740 	if (!nrf_802154_continuous_carrier()) {
741 		LOG_ERR("Failed to enter continuous carrier state");
742 		return -EIO;
743 	}
744 
745 	LOG_DBG("Continuous carrier wave transmission started (channel: %d)",
746 		nrf_802154_channel_get());
747 
748 	return 0;
749 }
750 
nrf_modulated_carrier(const struct device * dev,const uint8_t * data)751 static int nrf_modulated_carrier(const struct device *dev, const uint8_t *data)
752 {
753 	ARG_UNUSED(dev);
754 
755 	nrf_802154_tx_power_set(nrf5_data.txpwr);
756 
757 	if (!nrf_802154_modulated_carrier(data)) {
758 		LOG_ERR("Failed to enter modulated carrier state");
759 		return -EIO;
760 	}
761 
762 	LOG_DBG("Modulated carrier wave transmission started (channel: %d)",
763 		nrf_802154_channel_get());
764 
765 	return 0;
766 }
767 #endif
768 
769 #if !defined(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
nrf5_radio_irq(const void * arg)770 static void nrf5_radio_irq(const void *arg)
771 {
772 	ARG_UNUSED(arg);
773 
774 	nrf_802154_radio_irq_handler();
775 }
776 #endif
777 
nrf5_irq_config(const struct device * dev)778 static void nrf5_irq_config(const struct device *dev)
779 {
780 	ARG_UNUSED(dev);
781 
782 #if !defined(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
783 	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(radio)), NRF_802154_IRQ_PRIORITY, nrf5_radio_irq, NULL, 0);
784 	irq_enable(DT_IRQN(DT_NODELABEL(radio)));
785 #endif
786 }
787 
nrf5_init(const struct device * dev)788 static int nrf5_init(const struct device *dev)
789 {
790 	const struct nrf5_802154_config *nrf5_radio_cfg = NRF5_802154_CFG(dev);
791 	struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
792 #if defined(CONFIG_IEEE802154_RAW_MODE)
793 	nrf5_dev = dev;
794 #endif
795 
796 	k_fifo_init(&nrf5_radio->rx_fifo);
797 	k_sem_init(&nrf5_radio->tx_wait, 0, 1);
798 	k_sem_init(&nrf5_radio->cca_wait, 0, 1);
799 
800 	nrf_802154_init();
801 
802 	nrf5_get_capabilities_at_boot();
803 
804 	nrf5_radio->rx_on_when_idle = true;
805 	nrf5_radio_cfg->irq_config_func(dev);
806 
807 	k_thread_create(&nrf5_radio->rx_thread, nrf5_radio->rx_stack,
808 			CONFIG_IEEE802154_NRF5_RX_STACK_SIZE,
809 			nrf5_rx_thread, nrf5_radio, NULL, NULL,
810 			K_PRIO_COOP(2), 0, K_NO_WAIT);
811 
812 	k_thread_name_set(&nrf5_radio->rx_thread, "nrf5_rx");
813 
814 	LOG_INF("nRF5 802154 radio initialized");
815 
816 	return 0;
817 }
818 
nrf5_iface_init(struct net_if * iface)819 static void nrf5_iface_init(struct net_if *iface)
820 {
821 	const struct device *dev = net_if_get_device(iface);
822 	struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
823 
824 	nrf5_get_eui64(nrf5_radio->mac);
825 	net_if_set_link_addr(iface, nrf5_radio->mac, sizeof(nrf5_radio->mac),
826 			     NET_LINK_IEEE802154);
827 
828 	nrf5_radio->iface = iface;
829 
830 	ieee802154_init(iface);
831 }
832 
833 #if defined(CONFIG_NRF_802154_ENCRYPTION)
nrf5_config_mac_keys(struct ieee802154_key * mac_keys)834 static void nrf5_config_mac_keys(struct ieee802154_key *mac_keys)
835 {
836 	nrf_802154_security_key_remove_all();
837 
838 	for (uint8_t i = 0; mac_keys->key_value
839 			&& i < NRF_802154_SECURITY_KEY_STORAGE_SIZE; mac_keys++, i++) {
840 		nrf_802154_key_t key = {
841 			.value.p_cleartext_key = mac_keys->key_value,
842 			.id.mode = mac_keys->key_id_mode,
843 			.id.p_key_id = mac_keys->key_id,
844 			.type = NRF_802154_KEY_CLEARTEXT,
845 			.frame_counter = 0,
846 			.use_global_frame_counter = !(mac_keys->frame_counter_per_key),
847 		};
848 
849 		__ASSERT_EVAL((void)nrf_802154_security_key_store(&key),
850 			nrf_802154_security_error_t err = nrf_802154_security_key_store(&key),
851 			err == NRF_802154_SECURITY_ERROR_NONE ||
852 			err == NRF_802154_SECURITY_ERROR_ALREADY_PRESENT,
853 			"Storing key failed, err: %d", err);
854 	};
855 }
856 #endif /* CONFIG_NRF_802154_ENCRYPTION */
857 
nrf5_configure(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)858 static int nrf5_configure(const struct device *dev,
859 			  enum ieee802154_config_type type,
860 			  const struct ieee802154_config *config)
861 {
862 	ARG_UNUSED(dev);
863 
864 	switch (type) {
865 	case IEEE802154_CONFIG_AUTO_ACK_FPB:
866 		if (config->auto_ack_fpb.enabled) {
867 			switch (config->auto_ack_fpb.mode) {
868 			case IEEE802154_FPB_ADDR_MATCH_THREAD:
869 				nrf_802154_src_addr_matching_method_set(
870 					NRF_802154_SRC_ADDR_MATCH_THREAD);
871 				break;
872 
873 			case IEEE802154_FPB_ADDR_MATCH_ZIGBEE:
874 				nrf_802154_src_addr_matching_method_set(
875 					NRF_802154_SRC_ADDR_MATCH_ZIGBEE);
876 				break;
877 
878 			default:
879 				return -EINVAL;
880 			}
881 		}
882 
883 		nrf_802154_auto_pending_bit_set(config->auto_ack_fpb.enabled);
884 		break;
885 
886 	case IEEE802154_CONFIG_ACK_FPB:
887 		if (config->ack_fpb.enabled) {
888 			if (!nrf_802154_pending_bit_for_addr_set(
889 						config->ack_fpb.addr,
890 						config->ack_fpb.extended)) {
891 				return -ENOMEM;
892 			}
893 
894 			break;
895 		}
896 
897 		if (config->ack_fpb.addr != NULL) {
898 			if (!nrf_802154_pending_bit_for_addr_clear(
899 						config->ack_fpb.addr,
900 						config->ack_fpb.extended)) {
901 				return -ENOENT;
902 			}
903 		} else {
904 			nrf_802154_pending_bit_for_addr_reset(
905 						config->ack_fpb.extended);
906 		}
907 
908 		break;
909 
910 	case IEEE802154_CONFIG_PAN_COORDINATOR:
911 		nrf_802154_pan_coord_set(config->pan_coordinator);
912 		break;
913 
914 	case IEEE802154_CONFIG_PROMISCUOUS:
915 		nrf_802154_promiscuous_set(config->promiscuous);
916 		break;
917 
918 	case IEEE802154_CONFIG_EVENT_HANDLER:
919 		nrf5_data.event_handler = config->event_handler;
920 		break;
921 
922 #if defined(CONFIG_NRF_802154_ENCRYPTION)
923 	case IEEE802154_CONFIG_MAC_KEYS:
924 		nrf5_config_mac_keys(config->mac_keys);
925 		break;
926 
927 	case IEEE802154_CONFIG_FRAME_COUNTER:
928 		nrf_802154_security_global_frame_counter_set(config->frame_counter);
929 		break;
930 
931 	case IEEE802154_CONFIG_FRAME_COUNTER_IF_LARGER:
932 		nrf_802154_security_global_frame_counter_set_if_larger(config->frame_counter);
933 		break;
934 #endif /* CONFIG_NRF_802154_ENCRYPTION */
935 
936 	case IEEE802154_CONFIG_ENH_ACK_HEADER_IE: {
937 		uint8_t ext_addr_le[EXTENDED_ADDRESS_SIZE];
938 		uint8_t short_addr_le[SHORT_ADDRESS_SIZE];
939 		uint8_t element_id;
940 		bool valid_vendor_specific_ie = false;
941 
942 		if (config->ack_ie.purge_ie) {
943 			nrf_802154_ack_data_remove_all(false, NRF_802154_ACK_DATA_IE);
944 			nrf_802154_ack_data_remove_all(true, NRF_802154_ACK_DATA_IE);
945 			break;
946 		}
947 
948 		if (config->ack_ie.short_addr == IEEE802154_BROADCAST_ADDRESS ||
949 		    config->ack_ie.ext_addr == NULL) {
950 			return -ENOTSUP;
951 		}
952 
953 		sys_put_le16(config->ack_ie.short_addr, short_addr_le);
954 		sys_memcpy_swap(ext_addr_le, config->ack_ie.ext_addr, EXTENDED_ADDRESS_SIZE);
955 
956 		if (config->ack_ie.header_ie == NULL || config->ack_ie.header_ie->length == 0) {
957 			if (config->ack_ie.short_addr != IEEE802154_NO_SHORT_ADDRESS_ASSIGNED) {
958 				nrf_802154_ack_data_clear(short_addr_le, false,
959 					NRF_802154_ACK_DATA_IE);
960 			}
961 			nrf_802154_ack_data_clear(ext_addr_le, true, NRF_802154_ACK_DATA_IE);
962 		} else {
963 			element_id = ieee802154_header_ie_get_element_id(config->ack_ie.header_ie);
964 
965 #if defined(CONFIG_NET_L2_OPENTHREAD)
966 			uint8_t vendor_oui_le[IEEE802154_OPENTHREAD_VENDOR_OUI_LEN] =
967 				IEEE802154_OPENTHREAD_THREAD_IE_VENDOR_OUI;
968 
969 			if (element_id == IEEE802154_HEADER_IE_ELEMENT_ID_VENDOR_SPECIFIC_IE &&
970 			    memcmp(config->ack_ie.header_ie->content.vendor_specific.vendor_oui,
971 				   vendor_oui_le, sizeof(vendor_oui_le)) == 0) {
972 				valid_vendor_specific_ie = true;
973 			}
974 #endif
975 
976 			if (element_id != IEEE802154_HEADER_IE_ELEMENT_ID_CSL_IE &&
977 			    !valid_vendor_specific_ie) {
978 				return -ENOTSUP;
979 			}
980 
981 			if (config->ack_ie.short_addr != IEEE802154_NO_SHORT_ADDRESS_ASSIGNED) {
982 				nrf_802154_ack_data_set(
983 					short_addr_le, false, config->ack_ie.header_ie,
984 					config->ack_ie.header_ie->length +
985 						IEEE802154_HEADER_IE_HEADER_LENGTH,
986 					NRF_802154_ACK_DATA_IE);
987 			}
988 			nrf_802154_ack_data_set(ext_addr_le, true, config->ack_ie.header_ie,
989 						config->ack_ie.header_ie->length +
990 							IEEE802154_HEADER_IE_HEADER_LENGTH,
991 						NRF_802154_ACK_DATA_IE);
992 		}
993 	} break;
994 
995 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
996 	case IEEE802154_CONFIG_EXPECTED_RX_TIME: {
997 
998 #if defined(CONFIG_NRF_802154_SER_HOST)
999 		net_time_t period_ns = nrf5_data.csl_period * NSEC_PER_TEN_SYMBOLS;
1000 		bool changed = (config->expected_rx_time - nrf5_data.csl_rx_time) % period_ns;
1001 
1002 		nrf5_data.csl_rx_time = config->expected_rx_time;
1003 
1004 		if (changed)
1005 #endif /* CONFIG_NRF_802154_SER_HOST */
1006 		{
1007 			nrf_802154_csl_writer_anchor_time_set(
1008 				nrf_802154_timestamp_phr_to_mhr_convert(config->expected_rx_time /
1009 									NSEC_PER_USEC));
1010 		}
1011 	} break;
1012 
1013 	case IEEE802154_CONFIG_RX_SLOT: {
1014 		/* Note that even if the nrf_802154_receive_at function is not called in time
1015 		 * (for example due to the call being blocked by higher priority threads) and
1016 		 * the delayed reception window is not scheduled, the CSL phase will still be
1017 		 * calculated as if the following reception windows were at times
1018 		 * anchor_time + n * csl_period. The previously set
1019 		 * anchor_time will be used for calculations.
1020 		 */
1021 		nrf_802154_receive_at(config->rx_slot.start / NSEC_PER_USEC,
1022 				      config->rx_slot.duration / NSEC_PER_USEC,
1023 				      config->rx_slot.channel, DRX_SLOT_RX);
1024 	} break;
1025 
1026 	case IEEE802154_CONFIG_CSL_PERIOD: {
1027 		nrf_802154_csl_writer_period_set(config->csl_period);
1028 #if defined(CONFIG_NRF_802154_SER_HOST)
1029 		nrf5_data.csl_period = config->csl_period;
1030 #endif
1031 	} break;
1032 #endif /* CONFIG_IEEE802154_CSL_ENDPOINT */
1033 
1034 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
1035 	case IEEE802154_OPENTHREAD_CONFIG_MAX_EXTRA_CCA_ATTEMPTS:
1036 		nrf5_data.max_extra_cca_attempts =
1037 			((const struct ieee802154_openthread_config *)config)
1038 				->max_extra_cca_attempts;
1039 		break;
1040 #endif /* CONFIG_IEEE802154_NRF5_MULTIPLE_CCA */
1041 
1042 	case IEEE802154_CONFIG_RX_ON_WHEN_IDLE:
1043 		nrf_802154_rx_on_when_idle_set(config->rx_on_when_idle);
1044 		nrf5_data.rx_on_when_idle = config->rx_on_when_idle;
1045 
1046 		if (config->rx_on_when_idle == false) {
1047 			(void)nrf_802154_sleep_if_idle();
1048 		}
1049 		break;
1050 
1051 #if defined(CONFIG_IEEE802154_NRF5_CST_ENDPOINT)
1052 	case IEEE802154_OPENTHREAD_CONFIG_CST_PERIOD:
1053 		nrf_802154_cst_writer_period_set(config->cst_period);
1054 		break;
1055 
1056 	case IEEE802154_OPENTHREAD_CONFIG_EXPECTED_TX_TIME:
1057 		nrf_802154_cst_writer_anchor_time_set(nrf_802154_timestamp_phr_to_mhr_convert(
1058 			config->expected_tx_time / NSEC_PER_USEC));
1059 		break;
1060 #endif /* CONFIG_IEEE802154_NRF5_CST_ENDPOINT */
1061 
1062 	default:
1063 		return -EINVAL;
1064 	}
1065 
1066 	return 0;
1067 }
1068 
1069 /* driver-allocated attribute memory - constant across all driver instances */
1070 IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, 11, 26);
1071 
nrf5_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)1072 static int nrf5_attr_get(const struct device *dev,
1073 			 enum ieee802154_attr attr,
1074 			 struct ieee802154_attr_value *value)
1075 {
1076 	ARG_UNUSED(dev);
1077 
1078 	if (ieee802154_attr_get_channel_page_and_range(
1079 		    attr, IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915,
1080 		    &drv_attr.phy_supported_channels, value) == 0) {
1081 		return 0;
1082 	}
1083 
1084 	switch ((uint32_t)attr) {
1085 #if defined(CONFIG_IEEE802154_NRF5_MULTIPLE_CCA)
1086 	/* TODO: t_recca and t_ccatx should be provided by the public API of the
1087 	 * nRF 802.15.4 Radio Driver.
1088 	 */
1089 	case IEEE802154_OPENTHREAD_ATTR_T_RECCA:
1090 		((struct ieee802154_openthread_attr_value *)value)->t_recca = 0;
1091 		break;
1092 	case IEEE802154_OPENTHREAD_ATTR_T_CCATX:
1093 		((struct ieee802154_openthread_attr_value *)value)->t_ccatx = 20;
1094 		break;
1095 #endif
1096 	default:
1097 		return -ENOENT;
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 /* nRF5 radio driver callbacks */
1104 
nrf_802154_received_timestamp_raw(uint8_t * data,int8_t power,uint8_t lqi,uint64_t time)1105 void nrf_802154_received_timestamp_raw(uint8_t *data, int8_t power, uint8_t lqi, uint64_t time)
1106 {
1107 	for (uint32_t i = 0; i < ARRAY_SIZE(nrf5_data.rx_frames); i++) {
1108 		if (nrf5_data.rx_frames[i].psdu != NULL) {
1109 			continue;
1110 		}
1111 
1112 		nrf5_data.rx_frames[i].psdu = data;
1113 		nrf5_data.rx_frames[i].rssi = power;
1114 		nrf5_data.rx_frames[i].lqi = lqi;
1115 
1116 #if defined(CONFIG_NET_PKT_TIMESTAMP)
1117 		nrf5_data.rx_frames[i].time =
1118 			nrf_802154_timestamp_end_to_phr_convert(time, data[0]);
1119 #endif
1120 
1121 		nrf5_data.rx_frames[i].ack_fpb = nrf5_data.last_frame_ack_fpb;
1122 		nrf5_data.rx_frames[i].ack_seb = nrf5_data.last_frame_ack_seb;
1123 		nrf5_data.last_frame_ack_fpb = false;
1124 		nrf5_data.last_frame_ack_seb = false;
1125 
1126 		k_fifo_put(&nrf5_data.rx_fifo, &nrf5_data.rx_frames[i]);
1127 
1128 		return;
1129 	}
1130 
1131 	__ASSERT(false, "Not enough rx frames allocated for 15.4 driver");
1132 }
1133 
nrf_802154_receive_failed(nrf_802154_rx_error_t error,uint32_t id)1134 void nrf_802154_receive_failed(nrf_802154_rx_error_t error, uint32_t id)
1135 {
1136 	const struct device *dev = nrf5_get_device();
1137 
1138 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
1139 	if (id == DRX_SLOT_RX && error == NRF_802154_RX_ERROR_DELAYED_TIMEOUT) {
1140 		if (!nrf5_data.rx_on_when_idle) {
1141 			/* Transition to RxOff done automatically by the driver */
1142 			return;
1143 		} else if (nrf5_data.event_handler) {
1144 			/* Notify the higher layer to allow it to transition if needed */
1145 			nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_OFF, NULL);
1146 		}
1147 	}
1148 #else
1149 	ARG_UNUSED(id);
1150 #endif
1151 
1152 	enum ieee802154_rx_fail_reason reason;
1153 
1154 	switch (error) {
1155 	case NRF_802154_RX_ERROR_INVALID_FRAME:
1156 	case NRF_802154_RX_ERROR_DELAYED_TIMEOUT:
1157 		reason = IEEE802154_RX_FAIL_NOT_RECEIVED;
1158 		break;
1159 
1160 	case NRF_802154_RX_ERROR_INVALID_FCS:
1161 		reason = IEEE802154_RX_FAIL_INVALID_FCS;
1162 		break;
1163 
1164 	case NRF_802154_RX_ERROR_INVALID_DEST_ADDR:
1165 		reason = IEEE802154_RX_FAIL_ADDR_FILTERED;
1166 		break;
1167 
1168 	default:
1169 		reason = IEEE802154_RX_FAIL_OTHER;
1170 		break;
1171 	}
1172 
1173 	if (IS_ENABLED(CONFIG_IEEE802154_NRF5_LOG_RX_FAILURES)) {
1174 		LOG_INF("Rx failed, error = %d", error);
1175 	}
1176 
1177 	nrf5_data.last_frame_ack_fpb = false;
1178 	nrf5_data.last_frame_ack_seb = false;
1179 
1180 	if (nrf5_data.event_handler) {
1181 		nrf5_data.event_handler(dev, IEEE802154_EVENT_RX_FAILED, (void *)&reason);
1182 	}
1183 }
1184 
nrf_802154_tx_ack_started(const uint8_t * data)1185 void nrf_802154_tx_ack_started(const uint8_t *data)
1186 {
1187 	nrf5_data.last_frame_ack_fpb = data[FRAME_PENDING_OFFSET] & FRAME_PENDING_BIT;
1188 	nrf5_data.last_frame_ack_seb = data[SECURITY_ENABLED_OFFSET] & SECURITY_ENABLED_BIT;
1189 }
1190 
nrf_802154_transmitted_raw(uint8_t * frame,const nrf_802154_transmit_done_metadata_t * metadata)1191 void nrf_802154_transmitted_raw(uint8_t *frame,
1192 				const nrf_802154_transmit_done_metadata_t *metadata)
1193 {
1194 	ARG_UNUSED(frame);
1195 
1196 	nrf5_data.tx_result = NRF_802154_TX_ERROR_NONE;
1197 	nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
1198 	nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
1199 	nrf5_data.ack_frame.psdu = metadata->data.transmitted.p_ack;
1200 
1201 	if (nrf5_data.ack_frame.psdu) {
1202 		nrf5_data.ack_frame.rssi = metadata->data.transmitted.power;
1203 		nrf5_data.ack_frame.lqi = metadata->data.transmitted.lqi;
1204 
1205 #if defined(CONFIG_NET_PKT_TIMESTAMP)
1206 		if (metadata->data.transmitted.time == NRF_802154_NO_TIMESTAMP) {
1207 			/* Ack timestamp is invalid. Keep this value to detect it when handling Ack
1208 			 */
1209 			nrf5_data.ack_frame.time = NRF_802154_NO_TIMESTAMP;
1210 		} else {
1211 			nrf5_data.ack_frame.time = nrf_802154_timestamp_end_to_phr_convert(
1212 				metadata->data.transmitted.time, nrf5_data.ack_frame.psdu[0]);
1213 		}
1214 #endif
1215 	}
1216 
1217 	k_sem_give(&nrf5_data.tx_wait);
1218 }
1219 
nrf_802154_transmit_failed(uint8_t * frame,nrf_802154_tx_error_t error,const nrf_802154_transmit_done_metadata_t * metadata)1220 void nrf_802154_transmit_failed(uint8_t *frame,
1221 				nrf_802154_tx_error_t error,
1222 				const nrf_802154_transmit_done_metadata_t *metadata)
1223 {
1224 	ARG_UNUSED(frame);
1225 
1226 	nrf5_data.tx_result = error;
1227 	nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
1228 	nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
1229 
1230 	k_sem_give(&nrf5_data.tx_wait);
1231 }
1232 
nrf_802154_cca_done(bool channel_free)1233 void nrf_802154_cca_done(bool channel_free)
1234 {
1235 	nrf5_data.channel_free = channel_free;
1236 
1237 	k_sem_give(&nrf5_data.cca_wait);
1238 }
1239 
nrf_802154_cca_failed(nrf_802154_cca_error_t error)1240 void nrf_802154_cca_failed(nrf_802154_cca_error_t error)
1241 {
1242 	ARG_UNUSED(error);
1243 
1244 	nrf5_data.channel_free = false;
1245 
1246 	k_sem_give(&nrf5_data.cca_wait);
1247 }
1248 
nrf_802154_energy_detected(const nrf_802154_energy_detected_t * result)1249 void nrf_802154_energy_detected(const nrf_802154_energy_detected_t *result)
1250 {
1251 	if (nrf5_data.energy_scan_done != NULL) {
1252 		energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
1253 
1254 		nrf5_data.energy_scan_done = NULL;
1255 		callback(nrf5_get_device(), result->ed_dbm);
1256 	}
1257 }
1258 
nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)1259 void nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)
1260 {
1261 	if (nrf5_data.energy_scan_done != NULL) {
1262 		energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
1263 
1264 		nrf5_data.energy_scan_done = NULL;
1265 		callback(nrf5_get_device(), SHRT_MAX);
1266 	}
1267 }
1268 
1269 #if defined(CONFIG_NRF_802154_SER_HOST)
nrf_802154_serialization_error(const nrf_802154_ser_err_data_t * err)1270 void nrf_802154_serialization_error(const nrf_802154_ser_err_data_t *err)
1271 {
1272 	__ASSERT(false, "802.15.4 serialization error: %d", err->reason);
1273 	k_oops();
1274 }
1275 #endif
1276 
1277 static const struct nrf5_802154_config nrf5_radio_cfg = {
1278 	.irq_config_func = nrf5_irq_config,
1279 };
1280 
1281 static const struct ieee802154_radio_api nrf5_radio_api = {
1282 	.iface_api.init = nrf5_iface_init,
1283 
1284 	.get_capabilities = nrf5_get_capabilities,
1285 	.cca = nrf5_cca,
1286 	.set_channel = nrf5_set_channel,
1287 	.filter = nrf5_filter,
1288 	.set_txpower = nrf5_set_txpower,
1289 	.start = nrf5_start,
1290 	.stop = nrf5_stop,
1291 #if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS)
1292 	.continuous_carrier = nrf5_continuous_carrier,
1293 	.modulated_carrier = nrf_modulated_carrier,
1294 #endif
1295 	.tx = nrf5_tx,
1296 	.ed_scan = nrf5_energy_scan_start,
1297 	.get_time = nrf5_get_time,
1298 	.get_sch_acc = nrf5_get_acc,
1299 	.configure = nrf5_configure,
1300 	.attr_get = nrf5_attr_get,
1301 };
1302 
1303 #if defined(CONFIG_NET_L2_IEEE802154)
1304 #define L2 IEEE802154_L2
1305 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
1306 #define MTU IEEE802154_MTU
1307 #elif defined(CONFIG_NET_L2_OPENTHREAD)
1308 #define L2 OPENTHREAD_L2
1309 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
1310 #define MTU 1280
1311 #elif defined(CONFIG_NET_L2_CUSTOM_IEEE802154)
1312 #define L2 CUSTOM_IEEE802154_L2
1313 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(CUSTOM_IEEE802154_L2)
1314 #define MTU CONFIG_NET_L2_CUSTOM_IEEE802154_MTU
1315 #endif
1316 
1317 #if defined(CONFIG_NET_L2_PHY_IEEE802154)
1318 NET_DEVICE_DT_INST_DEFINE(0, nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
1319 			  CONFIG_IEEE802154_NRF5_INIT_PRIO, &nrf5_radio_api, L2,
1320 			  L2_CTX_TYPE, MTU);
1321 #else
1322 DEVICE_DT_INST_DEFINE(0, nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
1323 		      POST_KERNEL, CONFIG_IEEE802154_NRF5_INIT_PRIO,
1324 		      &nrf5_radio_api);
1325 #endif
1326