1 /* ieee802154_nrf5.c - nRF5 802.15.4 driver */
2
3 /*
4 * Copyright (c) 2017 Nordic Semiconductor ASA
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define LOG_MODULE_NAME ieee802154_nrf5
10 #if defined(CONFIG_IEEE802154_DRIVER_LOG_LEVEL)
11 #define LOG_LEVEL CONFIG_IEEE802154_DRIVER_LOG_LEVEL
12 #else
13 #define LOG_LEVEL LOG_LEVEL_NONE
14 #endif
15
16 #include <logging/log.h>
17 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
18
19 #include <errno.h>
20
21 #include <kernel.h>
22 #include <arch/cpu.h>
23 #include <debug/stack.h>
24
25 #include <soc.h>
26 #include <device.h>
27 #include <init.h>
28 #include <debug/stack.h>
29 #include <net/net_if.h>
30 #include <net/net_pkt.h>
31
32 #if defined(CONFIG_NET_L2_OPENTHREAD)
33 #include <net/openthread.h>
34 #endif
35
36 #include <sys/byteorder.h>
37 #include <string.h>
38 #include <random/rand32.h>
39
40 #include <net/ieee802154_radio.h>
41
42 #include "ieee802154_nrf5.h"
43 #include "nrf_802154.h"
44 #include "nrf_802154_const.h"
45
46 #if defined(CONFIG_NRF_802154_SER_HOST)
47 #include "nrf_802154_serialization_error.h"
48 #endif
49
50 struct nrf5_802154_config {
51 void (*irq_config_func)(const struct device *dev);
52 };
53
54 static struct nrf5_802154_data nrf5_data;
55
56 #define ACK_REQUEST_BYTE 1
57 #define ACK_REQUEST_BIT (1 << 5)
58 #define FRAME_PENDING_BYTE 1
59 #define FRAME_PENDING_BIT (1 << 4)
60 #define TXTIME_OFFSET_US (1 * USEC_PER_MSEC)
61
62 #define DRX_SLOT_PH 0 /* Placeholder delayed reception window ID */
63 #define DRX_SLOT_RX 1 /* Actual delayed reception window ID */
64 #define PH_DURATION 10 /* Duration of the placeholder window, in microseconds */
65
66 #if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
67 #if defined(CONFIG_SOC_NRF5340_CPUAPP)
68 #define EUI64_ADDR (NRF_UICR->OTP)
69 #else
70 #define EUI64_ADDR (NRF_UICR->CUSTOMER)
71 #endif /* CONFIG_SOC_NRF5340_CPUAPP */
72 #else
73 #if defined(CONFIG_SOC_NRF5340_CPUAPP) || defined(CONFIG_SOC_NRF5340_CPUNET)
74 #define EUI64_ADDR (NRF_FICR->INFO.DEVICEID)
75 #else
76 #define EUI64_ADDR (NRF_FICR->DEVICEID)
77 #endif /* CONFIG_SOC_NRF5340_CPUAPP || CONFIG_SOC_NRF5340_CPUNET */
78 #endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
79
80 #if defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
81 #define EUI64_ADDR_HIGH CONFIG_IEEE802154_NRF5_UICR_EUI64_REG
82 #define EUI64_ADDR_LOW (CONFIG_IEEE802154_NRF5_UICR_EUI64_REG + 1)
83 #else
84 #define EUI64_ADDR_HIGH 0
85 #define EUI64_ADDR_LOW 1
86 #endif /* CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE */
87
88 /* Convenience defines for RADIO */
89 #define NRF5_802154_DATA(dev) \
90 ((struct nrf5_802154_data * const)(dev)->data)
91
92 #define NRF5_802154_CFG(dev) \
93 ((const struct nrf5_802154_config * const)(dev)->config)
94
95 #if CONFIG_IEEE802154_VENDOR_OUI_ENABLE
96 #define IEEE802154_NRF5_VENDOR_OUI CONFIG_IEEE802154_VENDOR_OUI
97 #else
98 #define IEEE802154_NRF5_VENDOR_OUI (uint32_t)0xF4CE36
99 #endif
100
nrf5_get_eui64(uint8_t * mac)101 static void nrf5_get_eui64(uint8_t *mac)
102 {
103 uint64_t factoryAddress;
104 uint32_t index = 0;
105
106 #if !defined(CONFIG_IEEE802154_NRF5_UICR_EUI64_ENABLE)
107 /* Set the MAC Address Block Larger (MA-L) formerly called OUI. */
108 mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 16) & 0xff;
109 mac[index++] = (IEEE802154_NRF5_VENDOR_OUI >> 8) & 0xff;
110 mac[index++] = IEEE802154_NRF5_VENDOR_OUI & 0xff;
111 #endif
112
113 #if defined(CONFIG_SOC_NRF5340_CPUAPP) && \
114 defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
115 #error Accessing EUI64 on the non-secure mode is not supported at the moment
116 #else
117 /* Use device identifier assigned during the production. */
118 factoryAddress = (uint64_t)EUI64_ADDR[EUI64_ADDR_HIGH] << 32;
119 factoryAddress |= EUI64_ADDR[EUI64_ADDR_LOW];
120 #endif
121 memcpy(mac + index, &factoryAddress, sizeof(factoryAddress) - index);
122 }
123
nrf5_rx_thread(void * arg1,void * arg2,void * arg3)124 static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
125 {
126 struct nrf5_802154_data *nrf5_radio = (struct nrf5_802154_data *)arg1;
127 struct net_pkt *pkt;
128 struct nrf5_802154_rx_frame *rx_frame;
129 uint8_t pkt_len;
130
131 ARG_UNUSED(arg2);
132 ARG_UNUSED(arg3);
133
134 while (1) {
135 pkt = NULL;
136 rx_frame = NULL;
137
138 LOG_DBG("Waiting for frame");
139
140 rx_frame = k_fifo_get(&nrf5_radio->rx_fifo, K_FOREVER);
141
142 __ASSERT_NO_MSG(rx_frame->psdu);
143
144 /* rx_mpdu contains length, psdu, fcs|lqi
145 * The last 2 bytes contain LQI or FCS, depending if
146 * automatic CRC handling is enabled or not, respectively.
147 */
148 if (IS_ENABLED(CONFIG_IEEE802154_RAW_MODE) ||
149 IS_ENABLED(CONFIG_NET_L2_OPENTHREAD)) {
150 pkt_len = rx_frame->psdu[0];
151 } else {
152 pkt_len = rx_frame->psdu[0] - NRF5_FCS_LENGTH;
153 }
154
155 __ASSERT_NO_MSG(pkt_len <= CONFIG_NET_BUF_DATA_SIZE);
156
157 LOG_DBG("Frame received");
158
159 /* Block the RX thread until net_pkt is available, so that we
160 * don't drop already ACKed frame in case of temporary net_pkt
161 * scarcity. The nRF 802154 radio driver will accumulate any
162 * incoming frames until it runs out of internal buffers (and
163 * thus stops acknowledging consecutive frames).
164 */
165 pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, pkt_len,
166 AF_UNSPEC, 0, K_FOREVER);
167
168 if (net_pkt_write(pkt, rx_frame->psdu + 1, pkt_len)) {
169 goto drop;
170 }
171
172 net_pkt_set_ieee802154_lqi(pkt, rx_frame->lqi);
173 net_pkt_set_ieee802154_rssi(pkt, rx_frame->rssi);
174 net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);
175
176 #if defined(CONFIG_NET_PKT_TIMESTAMP)
177 struct net_ptp_time timestamp = {
178 .second = rx_frame->time / USEC_PER_SEC,
179 .nanosecond =
180 (rx_frame->time % USEC_PER_SEC) * NSEC_PER_USEC
181 };
182
183 net_pkt_set_timestamp(pkt, ×tamp);
184 #endif
185
186 LOG_DBG("Caught a packet (%u) (LQI: %u)",
187 pkt_len, rx_frame->lqi);
188
189 if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
190 LOG_ERR("Packet dropped by NET stack");
191 goto drop;
192 }
193
194 nrf_802154_buffer_free_raw(rx_frame->psdu);
195 rx_frame->psdu = NULL;
196
197 if (LOG_LEVEL >= LOG_LEVEL_DBG) {
198 log_stack_usage(&nrf5_radio->rx_thread);
199 }
200
201 continue;
202
203 drop:
204 nrf_802154_buffer_free_raw(rx_frame->psdu);
205 rx_frame->psdu = NULL;
206
207 net_pkt_unref(pkt);
208 }
209 }
210
nrf5_get_capabilities_at_boot(void)211 static void nrf5_get_capabilities_at_boot(void)
212 {
213 nrf_802154_capabilities_t caps = nrf_802154_capabilities_get();
214
215 nrf5_data.capabilities =
216 IEEE802154_HW_FCS |
217 IEEE802154_HW_PROMISC |
218 IEEE802154_HW_FILTER |
219 ((caps & NRF_802154_CAPABILITY_CSMA) ? IEEE802154_HW_CSMA : 0UL) |
220 IEEE802154_HW_2_4_GHZ |
221 IEEE802154_HW_TX_RX_ACK |
222 IEEE802154_HW_ENERGY_SCAN |
223 ((caps & NRF_802154_CAPABILITY_DELAYED_TX) ? IEEE802154_HW_TXTIME : 0UL) |
224 ((caps & NRF_802154_CAPABILITY_DELAYED_RX) ? IEEE802154_HW_RXTIME : 0UL) |
225 IEEE802154_HW_SLEEP_TO_TX |
226 ((caps & NRF_802154_CAPABILITY_SECURITY) ? IEEE802154_HW_TX_SEC : 0UL);
227 }
228
229 /* Radio device API */
230
nrf5_get_capabilities(const struct device * dev)231 static enum ieee802154_hw_caps nrf5_get_capabilities(const struct device *dev)
232 {
233 return nrf5_data.capabilities;
234 }
235
nrf5_cca(const struct device * dev)236 static int nrf5_cca(const struct device *dev)
237 {
238 struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
239
240 if (!nrf_802154_cca()) {
241 LOG_DBG("CCA failed");
242 return -EBUSY;
243 }
244
245 /* The nRF driver guarantees that a callback will be called once
246 * the CCA function is done, thus unlocking the semaphore.
247 */
248 k_sem_take(&nrf5_radio->cca_wait, K_FOREVER);
249
250 LOG_DBG("Channel free? %d", nrf5_radio->channel_free);
251
252 return nrf5_radio->channel_free ? 0 : -EBUSY;
253 }
254
nrf5_set_channel(const struct device * dev,uint16_t channel)255 static int nrf5_set_channel(const struct device *dev, uint16_t channel)
256 {
257 ARG_UNUSED(dev);
258
259 LOG_DBG("%u", channel);
260
261 if (channel < 11 || channel > 26) {
262 return -EINVAL;
263 }
264
265 nrf_802154_channel_set(channel);
266
267 return 0;
268 }
269
nrf5_energy_scan_start(const struct device * dev,uint16_t duration,energy_scan_done_cb_t done_cb)270 static int nrf5_energy_scan_start(const struct device *dev,
271 uint16_t duration,
272 energy_scan_done_cb_t done_cb)
273 {
274 int err = 0;
275
276 ARG_UNUSED(dev);
277
278 if (nrf5_data.energy_scan_done == NULL) {
279 nrf5_data.energy_scan_done = done_cb;
280
281 if (nrf_802154_energy_detection(duration * 1000) == false) {
282 nrf5_data.energy_scan_done = NULL;
283 err = -EPERM;
284 }
285 } else {
286 err = -EALREADY;
287 }
288
289 return err;
290 }
291
nrf5_set_pan_id(const struct device * dev,uint16_t pan_id)292 static int nrf5_set_pan_id(const struct device *dev, uint16_t pan_id)
293 {
294 uint8_t pan_id_le[2];
295
296 ARG_UNUSED(dev);
297
298 sys_put_le16(pan_id, pan_id_le);
299 nrf_802154_pan_id_set(pan_id_le);
300
301 LOG_DBG("0x%x", pan_id);
302
303 return 0;
304 }
305
nrf5_set_short_addr(const struct device * dev,uint16_t short_addr)306 static int nrf5_set_short_addr(const struct device *dev, uint16_t short_addr)
307 {
308 uint8_t short_addr_le[2];
309
310 ARG_UNUSED(dev);
311
312 sys_put_le16(short_addr, short_addr_le);
313 nrf_802154_short_address_set(short_addr_le);
314
315 LOG_DBG("0x%x", short_addr);
316
317 return 0;
318 }
319
nrf5_set_ieee_addr(const struct device * dev,const uint8_t * ieee_addr)320 static int nrf5_set_ieee_addr(const struct device *dev,
321 const uint8_t *ieee_addr)
322 {
323 ARG_UNUSED(dev);
324
325 LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
326 ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
327 ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
328
329 nrf_802154_extended_address_set(ieee_addr);
330
331 return 0;
332 }
333
nrf5_filter(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)334 static int nrf5_filter(const struct device *dev, bool set,
335 enum ieee802154_filter_type type,
336 const struct ieee802154_filter *filter)
337 {
338 LOG_DBG("Applying filter %u", type);
339
340 if (!set) {
341 return -ENOTSUP;
342 }
343
344 if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
345 return nrf5_set_ieee_addr(dev, filter->ieee_addr);
346 } else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
347 return nrf5_set_short_addr(dev, filter->short_addr);
348 } else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
349 return nrf5_set_pan_id(dev, filter->pan_id);
350 }
351
352 return -ENOTSUP;
353 }
354
nrf5_set_txpower(const struct device * dev,int16_t dbm)355 static int nrf5_set_txpower(const struct device *dev, int16_t dbm)
356 {
357 ARG_UNUSED(dev);
358
359 LOG_DBG("%d", dbm);
360
361 nrf_802154_tx_power_set(dbm);
362
363 return 0;
364 }
365
handle_ack(struct nrf5_802154_data * nrf5_radio)366 static int handle_ack(struct nrf5_802154_data *nrf5_radio)
367 {
368 uint8_t ack_len;
369 struct net_pkt *ack_pkt;
370 int err = 0;
371
372 if (IS_ENABLED(CONFIG_IEEE802154_RAW_MODE) || IS_ENABLED(CONFIG_NET_L2_OPENTHREAD)) {
373 ack_len = nrf5_radio->ack_frame.psdu[0];
374 } else {
375 ack_len = nrf5_radio->ack_frame.psdu[0] - NRF5_FCS_LENGTH;
376 }
377
378 ack_pkt = net_pkt_alloc_with_buffer(nrf5_radio->iface, ack_len,
379 AF_UNSPEC, 0, K_NO_WAIT);
380 if (!ack_pkt) {
381 LOG_ERR("No free packet available.");
382 err = -ENOMEM;
383 goto free_nrf_ack;
384 }
385
386 /* Upper layers expect the frame to start at the MAC header, skip the
387 * PHY header (1 byte).
388 */
389 if (net_pkt_write(ack_pkt, nrf5_radio->ack_frame.psdu + 1,
390 ack_len) < 0) {
391 LOG_ERR("Failed to write to a packet.");
392 err = -ENOMEM;
393 goto free_net_ack;
394 }
395
396 net_pkt_set_ieee802154_lqi(ack_pkt, nrf5_radio->ack_frame.lqi);
397 net_pkt_set_ieee802154_rssi(ack_pkt, nrf5_radio->ack_frame.rssi);
398
399 #if defined(CONFIG_NET_PKT_TIMESTAMP)
400 struct net_ptp_time timestamp = {
401 .second = nrf5_radio->ack_frame.time / USEC_PER_SEC,
402 .nanosecond = (nrf5_radio->ack_frame.time % USEC_PER_SEC) * NSEC_PER_USEC
403 };
404
405 net_pkt_set_timestamp(ack_pkt, ×tamp);
406 #endif
407
408 net_pkt_cursor_init(ack_pkt);
409
410 if (ieee802154_radio_handle_ack(nrf5_radio->iface, ack_pkt) != NET_OK) {
411 LOG_INF("ACK packet not handled - releasing.");
412 }
413
414 free_net_ack:
415 net_pkt_unref(ack_pkt);
416
417 free_nrf_ack:
418 nrf_802154_buffer_free_raw(nrf5_radio->ack_frame.psdu);
419 nrf5_radio->ack_frame.psdu = NULL;
420
421 return err;
422 }
423
nrf5_tx_started(const struct device * dev,struct net_pkt * pkt,struct net_buf * frag)424 static void nrf5_tx_started(const struct device *dev,
425 struct net_pkt *pkt,
426 struct net_buf *frag)
427 {
428 ARG_UNUSED(pkt);
429
430 if (nrf5_data.event_handler) {
431 nrf5_data.event_handler(dev, IEEE802154_EVENT_TX_STARTED,
432 (void *)frag);
433 }
434 }
435
nrf5_tx_immediate(struct net_pkt * pkt,uint8_t * payload,bool cca)436 static bool nrf5_tx_immediate(struct net_pkt *pkt, uint8_t *payload, bool cca)
437 {
438 nrf_802154_transmit_metadata_t metadata = {
439 .frame_props = {
440 .is_secured = pkt->ieee802154_frame_secured,
441 .dynamic_data_is_set = pkt->ieee802154_mac_hdr_rdy,
442 },
443 .cca = cca,
444 };
445
446 return nrf_802154_transmit_raw(payload, &metadata);
447 }
448
nrf5_tx_csma_ca(struct net_pkt * pkt,uint8_t * payload)449 static bool nrf5_tx_csma_ca(struct net_pkt *pkt, uint8_t *payload)
450 {
451 nrf_802154_transmit_csma_ca_metadata_t metadata = {
452 .frame_props = {
453 .is_secured = pkt->ieee802154_frame_secured,
454 .dynamic_data_is_set = pkt->ieee802154_mac_hdr_rdy,
455 },
456 };
457
458 return nrf_802154_transmit_csma_ca_raw(payload, &metadata);
459 }
460
461 /* This function cannot be used in the serialized version yet. */
462 #if defined(CONFIG_NET_PKT_TXTIME) && !defined(CONFIG_NRF_802154_SER_HOST)
nrf5_tx_at(struct net_pkt * pkt,uint8_t * payload,bool cca)463 static bool nrf5_tx_at(struct net_pkt *pkt, uint8_t *payload, bool cca)
464 {
465 nrf_802154_transmit_at_metadata_t metadata = {
466 .frame_props = {
467 .is_secured = pkt->ieee802154_frame_secured,
468 .dynamic_data_is_set = pkt->ieee802154_mac_hdr_rdy,
469 },
470 .cca = cca,
471 .channel = nrf_802154_channel_get(),
472 };
473 uint32_t tx_at = net_pkt_txtime(pkt) / NSEC_PER_USEC;
474 bool ret;
475
476 ret = nrf_802154_transmit_raw_at(payload,
477 tx_at - TXTIME_OFFSET_US,
478 TXTIME_OFFSET_US,
479 &metadata);
480 if (nrf5_data.event_handler) {
481 LOG_WRN("TX_STARTED event will be triggered without delay");
482 }
483 return ret;
484 }
485 #endif /* CONFIG_NET_PKT_TXTIME */
486
nrf5_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)487 static int nrf5_tx(const struct device *dev,
488 enum ieee802154_tx_mode mode,
489 struct net_pkt *pkt,
490 struct net_buf *frag)
491 {
492 struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
493 uint8_t payload_len = frag->len;
494 uint8_t *payload = frag->data;
495 bool ret = true;
496
497 if (payload_len > NRF5_PSDU_LENGTH) {
498 LOG_ERR("Payload too large: %d", payload_len);
499 return -EMSGSIZE;
500 }
501
502 LOG_DBG("%p (%u)", payload, payload_len);
503
504 nrf5_radio->tx_psdu[0] = payload_len + NRF5_FCS_LENGTH;
505 memcpy(nrf5_radio->tx_psdu + 1, payload, payload_len);
506
507 /* Reset semaphore in case ACK was received after timeout */
508 k_sem_reset(&nrf5_radio->tx_wait);
509
510 switch (mode) {
511 case IEEE802154_TX_MODE_DIRECT:
512 case IEEE802154_TX_MODE_CCA:
513 ret = nrf5_tx_immediate(pkt, nrf5_radio->tx_psdu,
514 mode == IEEE802154_TX_MODE_CCA);
515 break;
516 case IEEE802154_TX_MODE_CSMA_CA:
517 ret = nrf5_tx_csma_ca(pkt, nrf5_radio->tx_psdu);
518 break;
519 /* This function cannot be used in the serialized version yet. */
520 #if defined(CONFIG_NET_PKT_TXTIME) && !defined(CONFIG_NRF_802154_SER_HOST)
521 case IEEE802154_TX_MODE_TXTIME:
522 case IEEE802154_TX_MODE_TXTIME_CCA:
523 __ASSERT_NO_MSG(pkt);
524 ret = nrf5_tx_at(pkt, nrf5_radio->tx_psdu,
525 mode == IEEE802154_TX_MODE_TXTIME_CCA);
526 break;
527 #endif /* CONFIG_NET_PKT_TXTIME */
528 default:
529 NET_ERR("TX mode %d not supported", mode);
530 return -ENOTSUP;
531 }
532
533 if (!ret) {
534 LOG_ERR("Cannot send frame");
535 return -EIO;
536 }
537
538 nrf5_tx_started(dev, pkt, frag);
539
540 LOG_DBG("Sending frame (ch:%d, txpower:%d)",
541 nrf_802154_channel_get(), nrf_802154_tx_power_get());
542
543 /* Wait for the callback from the radio driver. */
544 k_sem_take(&nrf5_radio->tx_wait, K_FOREVER);
545
546 LOG_DBG("Result: %d", nrf5_data.tx_result);
547
548 #if NRF_802154_ENCRYPTION_ENABLED
549 /*
550 * When frame encryption by the radio driver is enabled, the frame stored in
551 * the tx_psdu buffer is:
552 * 1) authenticated and encrypted in place which causes that after an unsuccessful
553 * TX attempt, this frame must be propagated back to the upper layer for retransmission.
554 * The upper layer must ensure that the exact same secured frame is used for
555 * retransmission
556 * 2) frame counters are updated in place and for keeping the link frame counter up to date,
557 * this information must be propagated back to the upper layer
558 */
559 memcpy(payload, nrf5_radio->tx_psdu + 1, payload_len);
560 #endif
561 net_pkt_set_ieee802154_frame_secured(pkt, nrf5_radio->tx_frame_is_secured);
562 net_pkt_set_ieee802154_mac_hdr_rdy(pkt, nrf5_radio->tx_frame_mac_hdr_rdy);
563
564 switch (nrf5_radio->tx_result) {
565 case NRF_802154_TX_ERROR_NONE:
566 if (nrf5_radio->ack_frame.psdu == NULL) {
567 /* No ACK was requested. */
568 return 0;
569 }
570 /* Handle ACK packet. */
571 return handle_ack(nrf5_radio);
572 case NRF_802154_TX_ERROR_NO_MEM:
573 return -ENOBUFS;
574 case NRF_802154_TX_ERROR_BUSY_CHANNEL:
575 return -EBUSY;
576 case NRF_802154_TX_ERROR_INVALID_ACK:
577 case NRF_802154_TX_ERROR_NO_ACK:
578 return -ENOMSG;
579 case NRF_802154_TX_ERROR_ABORTED:
580 case NRF_802154_TX_ERROR_TIMESLOT_DENIED:
581 case NRF_802154_TX_ERROR_TIMESLOT_ENDED:
582 default:
583 return -EIO;
584 }
585 }
586
nrf5_get_time(const struct device * dev)587 static uint64_t nrf5_get_time(const struct device *dev)
588 {
589 ARG_UNUSED(dev);
590
591 return nrf_802154_time_get();
592 }
593
nrf5_get_acc(const struct device * dev)594 static uint8_t nrf5_get_acc(const struct device *dev)
595 {
596 ARG_UNUSED(dev);
597
598 return CONFIG_IEEE802154_DELAY_TRX_ACC;
599 }
600
nrf5_start(const struct device * dev)601 static int nrf5_start(const struct device *dev)
602 {
603 ARG_UNUSED(dev);
604
605 if (!nrf_802154_receive()) {
606 LOG_ERR("Failed to enter receive state");
607 return -EIO;
608 }
609
610 LOG_DBG("nRF5 802154 radio started (channel: %d)",
611 nrf_802154_channel_get());
612
613 return 0;
614 }
615
nrf5_stop(const struct device * dev)616 static int nrf5_stop(const struct device *dev)
617 {
618 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
619 if (nrf_802154_sleep_if_idle() != NRF_802154_SLEEP_ERROR_NONE) {
620 if (nrf5_data.event_handler) {
621 nrf5_data.event_handler(dev, IEEE802154_EVENT_SLEEP, NULL);
622 } else {
623 LOG_WRN("Transition to radio sleep cannot be handled.");
624 }
625 return 0;
626 }
627 #else
628 ARG_UNUSED(dev);
629 #endif
630
631 if (!nrf_802154_sleep()) {
632 LOG_ERR("Error while stopping radio");
633 return -EIO;
634 }
635
636 LOG_DBG("nRF5 802154 radio stopped");
637
638 return 0;
639 }
640
641 #if !IS_ENABLED(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
nrf5_radio_irq(void * arg)642 static void nrf5_radio_irq(void *arg)
643 {
644 ARG_UNUSED(arg);
645
646 nrf_802154_radio_irq_handler();
647 }
648 #endif
649
nrf5_irq_config(const struct device * dev)650 static void nrf5_irq_config(const struct device *dev)
651 {
652 ARG_UNUSED(dev);
653
654 #if !IS_ENABLED(CONFIG_IEEE802154_NRF5_EXT_IRQ_MGMT)
655 IRQ_CONNECT(RADIO_IRQn, NRF_802154_IRQ_PRIORITY,
656 nrf5_radio_irq, NULL, 0);
657 irq_enable(RADIO_IRQn);
658 #endif
659 }
660
nrf5_init(const struct device * dev)661 static int nrf5_init(const struct device *dev)
662 {
663 const struct nrf5_802154_config *nrf5_radio_cfg = NRF5_802154_CFG(dev);
664 struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
665
666 k_fifo_init(&nrf5_radio->rx_fifo);
667 k_sem_init(&nrf5_radio->tx_wait, 0, 1);
668 k_sem_init(&nrf5_radio->cca_wait, 0, 1);
669
670 nrf_802154_init();
671
672 nrf5_get_capabilities_at_boot();
673
674 nrf5_radio_cfg->irq_config_func(dev);
675
676 k_thread_create(&nrf5_radio->rx_thread, nrf5_radio->rx_stack,
677 CONFIG_IEEE802154_NRF5_RX_STACK_SIZE,
678 nrf5_rx_thread, nrf5_radio, NULL, NULL,
679 K_PRIO_COOP(2), 0, K_NO_WAIT);
680
681 k_thread_name_set(&nrf5_radio->rx_thread, "nrf5_rx");
682
683 LOG_INF("nRF5 802154 radio initialized");
684
685 return 0;
686 }
687
nrf5_iface_init(struct net_if * iface)688 static void nrf5_iface_init(struct net_if *iface)
689 {
690 const struct device *dev = net_if_get_device(iface);
691 struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
692
693 nrf5_get_eui64(nrf5_radio->mac);
694 net_if_set_link_addr(iface, nrf5_radio->mac, sizeof(nrf5_radio->mac),
695 NET_LINK_IEEE802154);
696
697 nrf5_radio->iface = iface;
698
699 ieee802154_init(iface);
700 }
701
702 #if defined(CONFIG_NRF_802154_ENCRYPTION)
nrf5_config_mac_keys(struct ieee802154_key * mac_keys)703 static void nrf5_config_mac_keys(struct ieee802154_key *mac_keys)
704 {
705 nrf_802154_security_error_t err;
706 nrf_802154_key_t key;
707 uint8_t key_id_to_remove;
708
709 __ASSERT(mac_keys, "Invalid argument.");
710
711 /* Remove old invalid key assuming that its index is first_valid_key_id - 1.
712 * TODO: This is Thread specific assumption, need to be changed when RD will provided
713 * API for removing all keys or handling this internally.
714 */
715 key_id_to_remove = mac_keys->key_index == 1 ? 0x80 : mac_keys->key_index - 1;
716
717 key.id.mode = mac_keys->key_id_mode;
718 key.id.p_key_id = &key_id_to_remove;
719
720 nrf_802154_security_key_remove(&key.id);
721
722 for (struct ieee802154_key *keys = mac_keys; keys->key_value; keys++) {
723 key.value.p_cleartext_key = keys->key_value;
724 key.id.mode = keys->key_id_mode;
725 key.id.p_key_id = &(keys->key_index);
726 key.type = NRF_802154_KEY_CLEARTEXT;
727 key.frame_counter = 0;
728 key.use_global_frame_counter = !(keys->frame_counter_per_key);
729
730 nrf_802154_security_key_remove(&key.id);
731 err = nrf_802154_security_key_store(&key);
732 __ASSERT(err == NRF_802154_SECURITY_ERROR_NONE ||
733 err == NRF_802154_SECURITY_ERROR_ALREADY_PRESENT,
734 "Storing key failed, err: %d", err);
735 };
736 }
737 #endif /* CONFIG_NRF_802154_ENCRYPTION */
738
739 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
nrf5_receive_at(uint32_t start,uint32_t duration,uint8_t channel,uint32_t id)740 static void nrf5_receive_at(uint32_t start, uint32_t duration, uint8_t channel, uint32_t id)
741 {
742 nrf_802154_receive_at(start - TXTIME_OFFSET_US, TXTIME_OFFSET_US, duration, channel, id);
743 }
744
nrf5_config_csl_period(uint16_t period)745 static void nrf5_config_csl_period(uint16_t period)
746 {
747 nrf_802154_receive_at_cancel(DRX_SLOT_PH);
748 nrf_802154_receive_at_cancel(DRX_SLOT_RX);
749
750 nrf_802154_csl_writer_period_set(period);
751
752 /* A placeholder reception window is scheduled so that the radio driver is able to inject
753 * the proper CSL Phase in the transmitted CSL Information Elements.
754 */
755 if (period > 0) {
756 nrf5_receive_at(nrf5_data.csl_rx_time, PH_DURATION, nrf_802154_channel_get(),
757 DRX_SLOT_PH);
758 }
759 }
760
nrf5_schedule_rx(uint8_t channel,uint32_t start,uint32_t duration)761 static void nrf5_schedule_rx(uint8_t channel, uint32_t start, uint32_t duration)
762 {
763 nrf5_receive_at(start, duration, channel, DRX_SLOT_RX);
764
765 /* The placeholder reception window is rescheduled for the next period */
766 nrf_802154_receive_at_cancel(DRX_SLOT_PH);
767 nrf5_receive_at(nrf5_data.csl_rx_time, PH_DURATION, channel, DRX_SLOT_PH);
768 }
769 #endif /* CONFIG_IEEE802154_CSL_ENDPOINT */
770
nrf5_configure(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)771 static int nrf5_configure(const struct device *dev,
772 enum ieee802154_config_type type,
773 const struct ieee802154_config *config)
774 {
775 ARG_UNUSED(dev);
776
777 switch (type) {
778 case IEEE802154_CONFIG_AUTO_ACK_FPB:
779 if (config->auto_ack_fpb.enabled) {
780 switch (config->auto_ack_fpb.mode) {
781 case IEEE802154_FPB_ADDR_MATCH_THREAD:
782 nrf_802154_src_addr_matching_method_set(
783 NRF_802154_SRC_ADDR_MATCH_THREAD);
784 break;
785
786 case IEEE802154_FPB_ADDR_MATCH_ZIGBEE:
787 nrf_802154_src_addr_matching_method_set(
788 NRF_802154_SRC_ADDR_MATCH_ZIGBEE);
789 break;
790
791 default:
792 return -EINVAL;
793 }
794 }
795
796 nrf_802154_auto_pending_bit_set(config->auto_ack_fpb.enabled);
797 break;
798
799 case IEEE802154_CONFIG_ACK_FPB:
800 if (config->ack_fpb.enabled) {
801 if (!nrf_802154_pending_bit_for_addr_set(
802 config->ack_fpb.addr,
803 config->ack_fpb.extended)) {
804 return -ENOMEM;
805 }
806
807 break;
808 }
809
810 if (config->ack_fpb.addr != NULL) {
811 if (!nrf_802154_pending_bit_for_addr_clear(
812 config->ack_fpb.addr,
813 config->ack_fpb.extended)) {
814 return -ENOENT;
815 }
816 } else {
817 nrf_802154_pending_bit_for_addr_reset(
818 config->ack_fpb.extended);
819 }
820
821 break;
822
823 case IEEE802154_CONFIG_PAN_COORDINATOR:
824 nrf_802154_pan_coord_set(config->pan_coordinator);
825 break;
826
827 case IEEE802154_CONFIG_PROMISCUOUS:
828 nrf_802154_promiscuous_set(config->promiscuous);
829 break;
830
831 case IEEE802154_CONFIG_EVENT_HANDLER:
832 nrf5_data.event_handler = config->event_handler;
833 break;
834
835 #if defined(CONFIG_NRF_802154_ENCRYPTION)
836 case IEEE802154_CONFIG_MAC_KEYS:
837 nrf5_config_mac_keys(config->mac_keys);
838 break;
839
840 case IEEE802154_CONFIG_FRAME_COUNTER:
841 nrf_802154_security_global_frame_counter_set(config->frame_counter);
842 break;
843 #endif /* CONFIG_NRF_802154_ENCRYPTION */
844
845 case IEEE802154_CONFIG_ENH_ACK_HEADER_IE: {
846 uint8_t short_addr_le[SHORT_ADDRESS_SIZE];
847 uint8_t ext_addr_le[EXTENDED_ADDRESS_SIZE];
848
849 sys_put_le16(config->ack_ie.short_addr, short_addr_le);
850 /**
851 * The extended address field passed to this function starts
852 * with the leftmost octet and ends with the rightmost octet.
853 * The IEEE 802.15.4 transmission order mandates this order to be
854 * reversed in a transmitted frame.
855 *
856 * The nrf_802154_ack_data_set expects extended address in transmission
857 * order.
858 */
859 sys_memcpy_swap(ext_addr_le, config->ack_ie.ext_addr, EXTENDED_ADDRESS_SIZE);
860
861 if (config->ack_ie.data_len > 0) {
862 nrf_802154_ack_data_set(short_addr_le, false, config->ack_ie.data,
863 config->ack_ie.data_len, NRF_802154_ACK_DATA_IE);
864 nrf_802154_ack_data_set(ext_addr_le, true, config->ack_ie.data,
865 config->ack_ie.data_len, NRF_802154_ACK_DATA_IE);
866 } else {
867 nrf_802154_ack_data_clear(short_addr_le, false, NRF_802154_ACK_DATA_IE);
868 nrf_802154_ack_data_clear(ext_addr_le, true, NRF_802154_ACK_DATA_IE);
869 }
870 } break;
871
872 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
873 case IEEE802154_CONFIG_CSL_RX_TIME:
874 nrf5_data.csl_rx_time = config->csl_rx_time;
875 break;
876
877 case IEEE802154_CONFIG_RX_SLOT:
878 nrf5_schedule_rx(config->rx_slot.channel, config->rx_slot.start,
879 config->rx_slot.duration);
880 break;
881
882 case IEEE802154_CONFIG_CSL_PERIOD:
883 nrf5_config_csl_period(config->csl_period);
884 break;
885 #endif /* CONFIG_IEEE802154_CSL_ENDPOINT */
886
887 default:
888 return -EINVAL;
889 }
890
891 return 0;
892 }
893
894 /* nRF5 radio driver callbacks */
895
nrf_802154_received_timestamp_raw(uint8_t * data,int8_t power,uint8_t lqi,uint32_t time)896 void nrf_802154_received_timestamp_raw(uint8_t *data, int8_t power, uint8_t lqi,
897 uint32_t time)
898 {
899 for (uint32_t i = 0; i < ARRAY_SIZE(nrf5_data.rx_frames); i++) {
900 if (nrf5_data.rx_frames[i].psdu != NULL) {
901 continue;
902 }
903
904 nrf5_data.rx_frames[i].psdu = data;
905 nrf5_data.rx_frames[i].time = time;
906 nrf5_data.rx_frames[i].rssi = power;
907 nrf5_data.rx_frames[i].lqi = lqi;
908
909 if (data[ACK_REQUEST_BYTE] & ACK_REQUEST_BIT) {
910 nrf5_data.rx_frames[i].ack_fpb =
911 nrf5_data.last_frame_ack_fpb;
912 } else {
913 nrf5_data.rx_frames[i].ack_fpb = false;
914 }
915
916 nrf5_data.last_frame_ack_fpb = false;
917
918 k_fifo_put(&nrf5_data.rx_fifo, &nrf5_data.rx_frames[i]);
919
920 return;
921 }
922
923 __ASSERT(false, "Not enough rx frames allocated for 15.4 driver");
924 }
925
nrf_802154_receive_failed(nrf_802154_rx_error_t error,uint32_t id)926 void nrf_802154_receive_failed(nrf_802154_rx_error_t error, uint32_t id)
927 {
928 #if defined(CONFIG_IEEE802154_CSL_ENDPOINT)
929 if ((id == DRX_SLOT_PH) || (id == DRX_SLOT_RX)) {
930 nrf5_stop(net_if_get_device(nrf5_data.iface));
931 return;
932 }
933 #else
934 ARG_UNUSED(id);
935 #endif
936
937 enum ieee802154_rx_fail_reason reason;
938
939 switch (error) {
940 case NRF_802154_RX_ERROR_INVALID_FRAME:
941 case NRF_802154_RX_ERROR_DELAYED_TIMEOUT:
942 reason = IEEE802154_RX_FAIL_NOT_RECEIVED;
943 break;
944
945 case NRF_802154_RX_ERROR_INVALID_FCS:
946 reason = IEEE802154_RX_FAIL_INVALID_FCS;
947 break;
948
949 case NRF_802154_RX_ERROR_INVALID_DEST_ADDR:
950 reason = IEEE802154_RX_FAIL_ADDR_FILTERED;
951 break;
952
953 default:
954 reason = IEEE802154_RX_FAIL_OTHER;
955 break;
956 }
957
958 nrf5_data.last_frame_ack_fpb = false;
959 if (nrf5_data.event_handler) {
960 nrf5_data.event_handler(net_if_get_device(nrf5_data.iface),
961 IEEE802154_EVENT_RX_FAILED,
962 (void *)&reason);
963 }
964 }
965
nrf_802154_tx_ack_started(const uint8_t * data)966 void nrf_802154_tx_ack_started(const uint8_t *data)
967 {
968 nrf5_data.last_frame_ack_fpb =
969 data[FRAME_PENDING_BYTE] & FRAME_PENDING_BIT;
970 }
971
nrf_802154_transmitted_raw(uint8_t * frame,const nrf_802154_transmit_done_metadata_t * metadata)972 void nrf_802154_transmitted_raw(uint8_t *frame,
973 const nrf_802154_transmit_done_metadata_t *metadata)
974 {
975 ARG_UNUSED(frame);
976
977 nrf5_data.tx_result = NRF_802154_TX_ERROR_NONE;
978 nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
979 nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
980 nrf5_data.ack_frame.psdu = metadata->data.transmitted.p_ack;
981
982 if (nrf5_data.ack_frame.psdu) {
983 nrf5_data.ack_frame.rssi = metadata->data.transmitted.power;
984 nrf5_data.ack_frame.lqi = metadata->data.transmitted.lqi;
985
986 #if !IS_ENABLED(CONFIG_NRF_802154_SER_HOST) && IS_ENABLED(CONFIG_NET_PKT_TIMESTAMP)
987 nrf5_data.ack_frame.time =
988 nrf_802154_first_symbol_timestamp_get(
989 metadata->data.transmitted.time, nrf5_data.ack_frame.psdu[0]);
990 #endif
991 }
992
993 k_sem_give(&nrf5_data.tx_wait);
994 }
995
nrf_802154_transmit_failed(uint8_t * frame,nrf_802154_tx_error_t error,const nrf_802154_transmit_done_metadata_t * metadata)996 void nrf_802154_transmit_failed(uint8_t *frame,
997 nrf_802154_tx_error_t error,
998 const nrf_802154_transmit_done_metadata_t *metadata)
999 {
1000 ARG_UNUSED(frame);
1001
1002 nrf5_data.tx_result = error;
1003 nrf5_data.tx_frame_is_secured = metadata->frame_props.is_secured;
1004 nrf5_data.tx_frame_mac_hdr_rdy = metadata->frame_props.dynamic_data_is_set;
1005
1006 k_sem_give(&nrf5_data.tx_wait);
1007 }
1008
nrf_802154_cca_done(bool channel_free)1009 void nrf_802154_cca_done(bool channel_free)
1010 {
1011 nrf5_data.channel_free = channel_free;
1012
1013 k_sem_give(&nrf5_data.cca_wait);
1014 }
1015
nrf_802154_cca_failed(nrf_802154_cca_error_t error)1016 void nrf_802154_cca_failed(nrf_802154_cca_error_t error)
1017 {
1018 ARG_UNUSED(error);
1019
1020 nrf5_data.channel_free = false;
1021
1022 k_sem_give(&nrf5_data.cca_wait);
1023 }
1024
nrf_802154_energy_detected(uint8_t result)1025 void nrf_802154_energy_detected(uint8_t result)
1026 {
1027 if (nrf5_data.energy_scan_done != NULL) {
1028 int16_t dbm;
1029 energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
1030
1031 nrf5_data.energy_scan_done = NULL;
1032 dbm = nrf_802154_dbm_from_energy_level_calculate(result);
1033 callback(net_if_get_device(nrf5_data.iface), dbm);
1034 }
1035 }
1036
nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)1037 void nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)
1038 {
1039 if (nrf5_data.energy_scan_done != NULL) {
1040 energy_scan_done_cb_t callback = nrf5_data.energy_scan_done;
1041
1042 nrf5_data.energy_scan_done = NULL;
1043 callback(net_if_get_device(nrf5_data.iface), SHRT_MAX);
1044 }
1045 }
1046
1047 #if defined(CONFIG_NRF_802154_SER_HOST)
nrf_802154_serialization_error(const nrf_802154_ser_err_data_t * p_err)1048 void nrf_802154_serialization_error(const nrf_802154_ser_err_data_t *p_err)
1049 {
1050 __ASSERT(false, "802.15.4 serialization error");
1051 }
1052 #endif
1053
1054 static const struct nrf5_802154_config nrf5_radio_cfg = {
1055 .irq_config_func = nrf5_irq_config,
1056 };
1057
1058 static struct ieee802154_radio_api nrf5_radio_api = {
1059 .iface_api.init = nrf5_iface_init,
1060
1061 .get_capabilities = nrf5_get_capabilities,
1062 .cca = nrf5_cca,
1063 .set_channel = nrf5_set_channel,
1064 .filter = nrf5_filter,
1065 .set_txpower = nrf5_set_txpower,
1066 .start = nrf5_start,
1067 .stop = nrf5_stop,
1068 .tx = nrf5_tx,
1069 .ed_scan = nrf5_energy_scan_start,
1070 .get_time = nrf5_get_time,
1071 .get_sch_acc = nrf5_get_acc,
1072 .configure = nrf5_configure,
1073 };
1074
1075 #if defined(CONFIG_NET_L2_IEEE802154)
1076 #define L2 IEEE802154_L2
1077 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
1078 #define MTU 125
1079 #elif defined(CONFIG_NET_L2_OPENTHREAD)
1080 #define L2 OPENTHREAD_L2
1081 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
1082 #define MTU 1280
1083 #endif
1084
1085 #if defined(CONFIG_NET_L2_IEEE802154) || defined(CONFIG_NET_L2_OPENTHREAD)
1086 NET_DEVICE_INIT(nrf5_154_radio, CONFIG_IEEE802154_NRF5_DRV_NAME,
1087 nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
1088 CONFIG_IEEE802154_NRF5_INIT_PRIO,
1089 &nrf5_radio_api, L2,
1090 L2_CTX_TYPE, MTU);
1091 #else
1092 DEVICE_DEFINE(nrf5_154_radio, CONFIG_IEEE802154_NRF5_DRV_NAME,
1093 nrf5_init, NULL, &nrf5_data, &nrf5_radio_cfg,
1094 POST_KERNEL, CONFIG_IEEE802154_NRF5_INIT_PRIO,
1095 &nrf5_radio_api);
1096 #endif
1097