-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathat86rf2xx_netdev.c
1148 lines (1017 loc) · 39.5 KB
/
at86rf2xx_netdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Kaspar Schleiser <[email protected]>
* 2015 Freie Universität Berlin
* 2023 Gerson Fernando Budke
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_at86rf2xx
* @{
*
* @file
* @brief Netdev adaption for the AT86RF2xx drivers
*
* @author Thomas Eichinger <[email protected]>
* @author Hauke Petersen <[email protected]>
* @author Kévin Roussel <[email protected]>
* @author Martine Lenders <[email protected]>
* @author Kaspar Schleiser <[email protected]>
* @author Josua Arndt <[email protected]>
* @author Gerson Fernando Budke <[email protected]>
*
* @}
*/
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "architecture.h"
#include "iolist.h"
#include "net/eui64.h"
#include "net/ieee802154.h"
#include "net/netdev.h"
#include "net/netdev/ieee802154.h"
#include "at86rf2xx.h"
#include "at86rf2xx_netdev.h"
#include "at86rf2xx_internal.h"
#include "at86rf2xx_registers.h"
#if IS_USED(IEEE802154_SECURITY)
#include "net/ieee802154_security.h"
#endif
#if IS_USED(MODULE_AT86RF2XX_AES_SPI)
#include "at86rf2xx_aes.h"
#endif
#define ENABLE_DEBUG 0
#include "debug.h"
static int _send(netdev_t *netdev, const iolist_t *iolist);
static int _recv(netdev_t *netdev, void *buf, size_t len, void *info);
static int _init(netdev_t *netdev);
static void _isr(netdev_t *netdev);
static int _get(netdev_t *netdev, netopt_t opt, void *val, size_t max_len);
static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len);
const netdev_driver_t at86rf2xx_driver = {
.send = _send,
.recv = _recv,
.init = _init,
.isr = _isr,
.get = _get,
.set = _set,
};
/* Default AT86RF2XX channel */
static const uint16_t at86rf2xx_chan_default = AT86RF2XX_DEFAULT_CHANNEL;
#if IS_USED(MODULE_AT86RF2XX_AES_SPI) && \
IS_USED(MODULE_IEEE802154_SECURITY)
/**
* @brief Pass the 802.15.4 encryption key to the transceiver hardware
*
* @param[in] dev Abstract security device descriptor
* @param[in] key Encryption key to be used
* @param[in] key_size Size of the encryption key in bytes
*/
static void _at86rf2xx_set_key(ieee802154_sec_dev_t *dev,
const uint8_t *key, uint8_t key_size)
{
(void)key_size;
at86rf2xx_aes_key_write_encrypt((at86rf2xx_t *)dev->ctx, key);
}
/**
* @brief Compute CBC-MAC from IEEE 802.15.4 security context
*
* @param[in] dev Abstract security device descriptor
* @param[out] cipher Buffer to store cipher blocks
* @param[in] iv Initial vector
* @param[in] plain Input data blocks
* @param[in] nblocks Number of blocks
*/
static void _at86rf2xx_cbc(const ieee802154_sec_dev_t *dev,
uint8_t *cipher,
uint8_t *iv,
const uint8_t *plain,
uint8_t nblocks)
{
at86rf2xx_aes_cbc_encrypt((at86rf2xx_t *)dev->ctx,
(aes_block_t *)cipher,
NULL,
iv,
(aes_block_t *)plain,
nblocks);
}
/**
* @brief Perform ECB encryption
*
* @param[in] dev Abstract security device descriptor
* @param[out] cipher Output cipher blocks
* @param[in] plain Plain blocks
* @param[in] nblocks Number of blocks
*/
static void _at86rf2xx_ecb(const ieee802154_sec_dev_t *dev,
uint8_t *cipher,
const uint8_t *plain,
uint8_t nblocks)
{
at86rf2xx_aes_ecb_encrypt((at86rf2xx_t *)dev->ctx,
(aes_block_t *)cipher,
NULL,
(aes_block_t *)plain,
nblocks);
}
/**
* @brief Struct that contains IEEE 802.15.4 security operations
* which are implemented, using the transceiver´s hardware
* crypto capabilities
*/
static const ieee802154_radio_cipher_ops_t _at86rf2xx_cipher_ops = {
.set_key = _at86rf2xx_set_key,
.ecb = _at86rf2xx_ecb,
.cbc = _at86rf2xx_cbc
};
#endif /* IS_USED(MODULE_AT86RF2XX_AES_SPI) && \
IS_USED(MODULE_IEEE802154_SECURITY) */
/* SOC has radio interrupts, store reference to netdev */
static netdev_t *at86rfmega_dev;
static void _irq_handler(void *arg)
{
netdev_trigger_event_isr(arg);
}
static int _init(netdev_t *netdev)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
if (AT86RF2XX_IS_PERIPH) {
at86rfmega_dev = netdev;
}
else {
at86rf2xx_spi_init(dev, _irq_handler);
}
/* reset hardware into a defined state */
at86rf2xx_hardware_reset(dev);
/* test if the device is responding */
if (at86rf2xx_reg_read(dev, AT86RF2XX_REG__PART_NUM) != AT86RF2XX_PARTNUM) {
DEBUG("[at86rf2xx] error: unable to read correct part number\n");
return -ENOTSUP;
}
/* reset device to default values and put it into RX state */
netdev_ieee802154_reset(&dev->netdev);
at86rf2xx_set_addr_long(dev, (eui64_t *)dev->netdev.long_addr);
at86rf2xx_set_addr_short(dev, (network_uint16_t *)dev->netdev.short_addr);
/* `netdev_ieee802154_reset` does not set the default channel. */
netdev_ieee802154_set(&dev->netdev, NETOPT_CHANNEL,
&at86rf2xx_chan_default, sizeof(at86rf2xx_chan_default));
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
static const netopt_enable_t ack_req =
IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_ACK_REQ) ? NETOPT_ENABLE : NETOPT_DISABLE;
netdev_ieee802154_set(&dev->netdev, NETOPT_ACK_REQ,
&ack_req, sizeof(ack_req));
}
#if IS_USED(MODULE_IEEE802154_SECURITY) && \
IS_USED(MODULE_AT86RF2XX_AES_SPI)
dev->netdev.sec_ctx.dev.cipher_ops = &_at86rf2xx_cipher_ops;
dev->netdev.sec_ctx.dev.ctx = dev;
#endif
at86rf2xx_reset(dev);
/* Initialize CSMA seed with hardware address */
at86rf2xx_set_csma_seed(dev, dev->netdev.long_addr);
/* signal link UP */
netdev->event_callback(netdev, NETDEV_EVENT_LINK_UP);
return 0;
}
static int _send(netdev_t *netdev, const iolist_t *iolist)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
size_t len = 0;
at86rf2xx_tx_prepare(dev);
/* load packet data into FIFO */
for (const iolist_t *iol = iolist; iol; iol = iol->iol_next) {
/* current packet data + FCS too long */
if ((len + iol->iol_len + 2) > AT86RF2XX_MAX_PKT_LENGTH) {
DEBUG("[at86rf2xx] error: packet too large (%" PRIuSIZE
" byte) to be send\n", len + 2);
return -EOVERFLOW;
}
if (iol->iol_len) {
len = at86rf2xx_tx_load(dev, iol->iol_base, iol->iol_len, len);
}
}
/* send data out directly if pre-loading id disabled */
if (!(dev->flags & AT86RF2XX_OPT_PRELOADING)) {
at86rf2xx_tx_exec(dev);
if (netdev->event_callback) {
netdev->event_callback(netdev, NETDEV_EVENT_TX_STARTED);
}
}
/* return the number of bytes that were actually loaded into the frame
* buffer/send out */
return (int)len;
}
static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
uint8_t phr;
size_t pkt_len;
/* frame buffer protection will be unlocked as soon as at86rf2xx_fb_stop() is called,
* Set receiver to PLL_ON state to be able to free the SPI bus and avoid losing data. */
at86rf2xx_set_state(dev, AT86RF2XX_STATE_PLL_ON);
/* start frame buffer access */
at86rf2xx_fb_start(dev);
/* get the size of the received packet */
phr = at86rf2xx_get_rx_len(dev);
/* ignore MSB (refer p.80) and subtract length of FCS field */
pkt_len = (phr & 0x7f) - 2;
/* return length when buf == NULL */
if (buf == NULL) {
/* release SPI bus */
at86rf2xx_fb_stop(dev);
/* drop packet, continue receiving */
if (len > 0) {
/* set device back in operation state which was used before last transmission.
* This state is saved in at86rf2xx.c/at86rf2xx_tx_prepare() e.g RX_AACK_ON */
at86rf2xx_set_state(dev, dev->idle_state);
}
return pkt_len;
}
/* not enough space in buf */
if (pkt_len > len) {
at86rf2xx_fb_stop(dev);
/* set device back in operation state which was used before last transmission.
* This state is saved in at86rf2xx.c/at86rf2xx_tx_prepare() e.g RX_AACK_ON */
at86rf2xx_set_state(dev, dev->idle_state);
return -ENOBUFS;
}
/* copy payload */
at86rf2xx_fb_read(dev, (uint8_t *)buf, pkt_len);
/* Ignore FCS but advance fb read - we must give a temporary buffer here,
* as we are not allowed to issue SPI transfers without any buffer */
uint8_t tmp[2];
at86rf2xx_fb_read(dev, tmp, 2);
(void)tmp;
/* AT86RF212B RSSI_BASE_VAL + 1.03 * ED, base varies for diff. modulation and datarates
* AT86RF232 RSSI_BASE_VAL + ED, base -91dBm
* AT86RF233 RSSI_BASE_VAL + ED, base -94dBm
* AT86RF231 RSSI_BASE_VAL + ED, base -91dBm
* AT86RFA1 RSSI_BASE_VAL + ED, base -90dBm
* AT86RFR2 RSSI_BASE_VAL + ED, base -90dBm
*
* AT86RF231 MAN. p.92, 8.4.3 Data Interpretation
* AT86RF232 MAN. p.91, 8.4.3 Data Interpretation
* AT86RF233 MAN. p.102, 8.5.3 Data Interpretation
*
* for performance reasons we ignore the 1.03 scale factor on the 212B,
* which causes a slight error in the values, but the accuracy of the ED
* value is specified as +/- 5 dB, so it should not matter very much in real
* life.
*/
if (info != NULL) {
uint8_t ed = 0;
netdev_ieee802154_rx_info_t *radio_info = info;
at86rf2xx_fb_read(dev, &(radio_info->lqi), 1);
#if AT86RF2XX_HAVE_ED_REGISTER
/* AT86RF231 does not provide ED at the end of the frame buffer, read
* from separate register instead */
at86rf2xx_fb_stop(dev);
ed = at86rf2xx_reg_read(dev, AT86RF2XX_REG__PHY_ED_LEVEL);
#else
at86rf2xx_fb_read(dev, &ed, 1);
at86rf2xx_fb_stop(dev);
#endif
radio_info->rssi = RSSI_BASE_VAL + ed;
DEBUG("[at86rf2xx] LQI:%d high is good, RSSI:%d high is either good or "
"too much interference.\n", radio_info->lqi, radio_info->rssi);
#if AT86RF2XX_IS_PERIPH && IS_USED(MODULE_NETDEV_IEEE802154_RX_TIMESTAMP)
/* AT86RF2XX_IS_PERIPH means the MCU is ATmegaRFR2 that has symbol counter */
{
uint32_t rx_sc;
rx_sc = at86rf2xx_get_sc(dev);
/* convert counter value to ns */
uint64_t res = SC_TO_NS * (uint64_t)rx_sc;
netdev_ieee802154_rx_info_set_timestamp(radio_info, res);
DEBUG("[at86rf2xx] CS: %" PRIu32 " timestamp: %" PRIu32 ".%09" PRIu32 " ",
rx_sc, (uint32_t)(radio_info->timestamp / NS_PER_SEC),
(uint32_t)(radio_info->timestamp % NS_PER_SEC));
}
#endif
}
else {
at86rf2xx_fb_stop(dev);
}
/* set device back in operation state which was used before last transmission.
* This state is saved in at86rf2xx.c/at86rf2xx_tx_prepare() e.g RX_AACK_ON */
at86rf2xx_set_state(dev, dev->idle_state);
return pkt_len;
}
static int _set_state(at86rf2xx_t *dev, netopt_state_t state)
{
switch (state) {
case NETOPT_STATE_STANDBY:
at86rf2xx_set_state(dev, AT86RF2XX_STATE_TRX_OFF);
break;
case NETOPT_STATE_SLEEP:
at86rf2xx_set_state(dev, AT86RF2XX_STATE_SLEEP);
break;
case NETOPT_STATE_IDLE:
at86rf2xx_set_state(dev, AT86RF2XX_PHY_STATE_RX);
break;
case NETOPT_STATE_TX:
if (dev->flags & AT86RF2XX_OPT_PRELOADING) {
/* The netdev driver ISR switches the transceiver back to the
* previous idle state after a completed TX. If the user tries
* to initiate another transmission (retransmitting the same data)
* without first going to TX_ARET_ON, the command to start TX
* would be ignored, leading to a deadlock in this netdev driver
* thread.
* Additionally, avoids driver thread deadlock when PRELOADING
* is set and the user tries to initiate TX without first calling
* send() to write some frame data.
*/
if (dev->pending_tx == 0) {
/* retransmission of old data, at86rf2xx_tx_prepare normally
* increments this and the ISR for TX_END decrements it, to
* know when to switch back to the idle state. */
++dev->pending_tx;
}
at86rf2xx_set_state(dev, AT86RF2XX_PHY_STATE_TX);
at86rf2xx_tx_exec(dev);
if (dev->netdev.netdev.event_callback) {
dev->netdev.netdev.event_callback(&dev->netdev.netdev, NETDEV_EVENT_TX_STARTED);
}
}
break;
case NETOPT_STATE_RESET:
at86rf2xx_hardware_reset(dev);
netdev_ieee802154_reset(&dev->netdev);
/* set short and long address */
at86rf2xx_set_addr_long(dev, (eui64_t *)dev->netdev.long_addr);
at86rf2xx_set_addr_short(dev, (network_uint16_t *)dev->netdev.short_addr);
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
static const netopt_enable_t ack_req =
IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_ACK_REQ) ? NETOPT_ENABLE : NETOPT_DISABLE;
netdev_ieee802154_set(&dev->netdev, NETOPT_ACK_REQ,
&ack_req, sizeof(ack_req));
}
at86rf2xx_reset(dev);
break;
default:
return -ENOTSUP;
}
return sizeof(netopt_state_t);
}
netopt_state_t _get_state(at86rf2xx_t *dev)
{
switch (at86rf2xx_get_status(dev)) {
case AT86RF2XX_STATE_SLEEP:
return NETOPT_STATE_SLEEP;
case AT86RF2XX_STATE_TRX_OFF:
return NETOPT_STATE_STANDBY;
case AT86RF2XX_PHY_STATE_RX_BUSY:
return NETOPT_STATE_RX;
case AT86RF2XX_PHY_STATE_TX:
case AT86RF2XX_PHY_STATE_TX_BUSY:
return NETOPT_STATE_TX;
case AT86RF2XX_PHY_STATE_RX:
default:
return NETOPT_STATE_IDLE;
}
}
static int _get(netdev_t *netdev, netopt_t opt, void *val, size_t max_len)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
if (netdev == NULL) {
return -ENODEV;
}
/* getting these options doesn't require the transceiver to be responsive */
switch (opt) {
#if AT86RF2XX_HAVE_SUBGHZ
case NETOPT_CHANNEL_PAGE:
assert(max_len >= sizeof(uint16_t));
((uint8_t *)val)[1] = 0;
((uint8_t *)val)[0] = dev->page;
return sizeof(uint16_t);
#endif
case NETOPT_STATE:
assert(max_len >= sizeof(netopt_state_t));
*((netopt_state_t *)val) = _get_state(dev);
return sizeof(netopt_state_t);
case NETOPT_PRELOADING:
if (dev->flags & AT86RF2XX_OPT_PRELOADING) {
*((netopt_enable_t *)val) = NETOPT_ENABLE;
}
else {
*((netopt_enable_t *)val) = NETOPT_DISABLE;
}
return sizeof(netopt_enable_t);
case NETOPT_PROMISCUOUSMODE:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
if (dev->flags & AT86RF2XX_OPT_PROMISCUOUS) {
*((netopt_enable_t *)val) = NETOPT_ENABLE;
}
else {
*((netopt_enable_t *)val) = NETOPT_DISABLE;
}
return sizeof(netopt_enable_t);
}
break;
case NETOPT_RX_START_IRQ:
case NETOPT_TX_START_IRQ:
case NETOPT_TX_END_IRQ:
*((netopt_enable_t *)val) = NETOPT_ENABLE;
return sizeof(netopt_enable_t);
case NETOPT_CSMA:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
*((netopt_enable_t *)val) =
!!(dev->flags & AT86RF2XX_OPT_CSMA);
return sizeof(netopt_enable_t);
}
break;
/* Only radios with the XAH_CTRL_2 register support frame retry reporting */
#if AT86RF2XX_HAVE_RETRIES
case NETOPT_TX_RETRIES_NEEDED:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(max_len >= sizeof(uint8_t));
*((uint8_t *)val) = dev->tx_retries;
return sizeof(uint8_t);
}
break;
#endif
default:
/* Can still be handled in second switch */
break;
}
int res;
if (((res = netdev_ieee802154_get(container_of(netdev, netdev_ieee802154_t, netdev),
opt, val, max_len)) >= 0)
|| (res != -ENOTSUP)) {
return res;
}
uint8_t old_state = at86rf2xx_get_status(dev);
/* temporarily wake up if sleeping */
if (old_state == AT86RF2XX_STATE_SLEEP) {
at86rf2xx_assert_awake(dev);
}
/* these options require the transceiver to be not sleeping*/
switch (opt) {
case NETOPT_TX_POWER:
assert(max_len >= sizeof(int16_t));
*((uint16_t *)val) = netdev_ieee802154->txpower;
res = sizeof(uint16_t);
break;
case NETOPT_RETRANS:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(max_len >= sizeof(uint8_t));
*((uint8_t *)val) = at86rf2xx_get_max_retries(dev);
res = sizeof(uint8_t);
}
break;
case NETOPT_CSMA_RETRIES:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(max_len >= sizeof(uint8_t));
*((uint8_t *)val) = at86rf2xx_get_csma_max_retries(dev);
res = sizeof(uint8_t);
}
break;
case NETOPT_CCA_THRESHOLD:
assert(max_len >= sizeof(int8_t));
*((int8_t *)val) = at86rf2xx_get_cca_threshold(dev);
res = sizeof(int8_t);
break;
case NETOPT_IS_CHANNEL_CLR:
assert(max_len >= sizeof(netopt_enable_t));
*((netopt_enable_t *)val) = at86rf2xx_cca(dev);
res = sizeof(netopt_enable_t);
break;
case NETOPT_LAST_ED_LEVEL:
assert(max_len >= sizeof(int8_t));
*((int8_t *)val) = at86rf2xx_get_ed_level(dev);
res = sizeof(int8_t);
break;
case NETOPT_AUTOACK:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(max_len >= sizeof(netopt_enable_t));
uint8_t tmp = at86rf2xx_reg_read(dev, AT86RF2XX_REG__CSMA_SEED_1);
*((netopt_enable_t *)val) = (tmp & AT86RF2XX_CSMA_SEED_1__AACK_DIS_ACK)
? false : true;
res = sizeof(netopt_enable_t);
}
break;
#ifdef MODULE_NETDEV_IEEE802154_OQPSK
case NETOPT_IEEE802154_PHY:
assert(max_len >= sizeof(int8_t));
*(uint8_t *)val = at86rf2xx_get_phy_mode(dev);
return sizeof(uint8_t);
case NETOPT_OQPSK_RATE:
assert(max_len >= sizeof(int8_t));
*(uint8_t *)val = at86rf2xx_get_rate(dev);
return sizeof(uint8_t);
#endif /* MODULE_NETDEV_IEEE802154_OQPSK */
#if AT86RF2XX_RANDOM_NUMBER_GENERATOR
case NETOPT_RANDOM:
at86rf2xx_get_random(dev, (uint8_t*)val, max_len);
return max_len;
#endif
default:
res = -ENOTSUP;
break;
}
/* go back to sleep if were sleeping */
if (old_state == AT86RF2XX_STATE_SLEEP) {
at86rf2xx_set_state(dev, AT86RF2XX_STATE_SLEEP);
}
return res;
}
static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
if (dev == NULL) {
return -ENODEV;
}
uint8_t old_state = at86rf2xx_get_status(dev);
int res = -ENOTSUP;
/* temporarily wake up if sleeping and opt != NETOPT_STATE.
* opt != NETOPT_STATE check prevents redundant wake-up.
* when opt == NETOPT_STATE, at86rf2xx_set_state() will wake up the
* radio if needed. */
if ((old_state == AT86RF2XX_STATE_SLEEP) && (opt != NETOPT_STATE)) {
at86rf2xx_assert_awake(dev);
}
switch (opt) {
case NETOPT_ADDRESS:
assert(len == sizeof(network_uint16_t));
memcpy(dev->netdev.short_addr, val, len);
#ifdef MODULE_SIXLOWPAN
/* https://tools.ietf.org/html/rfc4944#section-12 requires the first bit to
* 0 for unicast addresses */
dev->netdev.short_addr[0] &= 0x7F;
#endif
at86rf2xx_set_addr_short(dev, val);
/* don't set res to set netdev_ieee802154_t::short_addr */
break;
case NETOPT_ADDRESS_LONG:
assert(len == sizeof(eui64_t));
memcpy(dev->netdev.long_addr, val, len);
at86rf2xx_set_addr_long(dev, val);
/* don't set res to set netdev_ieee802154_t::long_addr */
break;
case NETOPT_NID:
assert(len == sizeof(uint16_t));
at86rf2xx_set_pan(dev, *((const uint16_t *)val));
/* don't set res to set netdev_ieee802154_t::pan */
break;
case NETOPT_CHANNEL:
assert(len == sizeof(uint16_t));
uint8_t chan = (((const uint16_t *)val)[0]) & UINT8_MAX;
#if AT86RF2XX_MIN_CHANNEL
if (chan < AT86RF2XX_MIN_CHANNEL || chan > AT86RF2XX_MAX_CHANNEL) {
#else
if (chan > AT86RF2XX_MAX_CHANNEL) {
#endif /* AT86RF2XX_MIN_CHANNEL */
res = -EINVAL;
break;
}
dev->netdev.chan = chan;
#if AT86RF2XX_HAVE_SUBGHZ
at86rf2xx_configure_phy(dev, chan, dev->page, dev->netdev.txpower);
#else
at86rf2xx_configure_phy(dev, chan, 0, dev->netdev.txpower);
#endif
/* don't set res to set netdev_ieee802154_t::chan */
break;
case NETOPT_CHANNEL_PAGE:
assert(len == sizeof(uint16_t));
uint8_t page = (((const uint16_t *)val)[0]) & UINT8_MAX;
#if AT86RF2XX_HAVE_SUBGHZ
if ((page != 0) && (page != 2)) {
res = -EINVAL;
}
else {
dev->page = page;
at86rf2xx_configure_phy(dev, dev->netdev.chan, page, dev->netdev.txpower);
res = sizeof(uint16_t);
}
#else
/* rf23x only supports page 0, no need to configure anything in the driver. */
if (page != 0) {
res = -EINVAL;
}
else {
res = sizeof(uint16_t);
}
#endif
break;
case NETOPT_TX_POWER:
assert(len <= sizeof(int16_t));
netdev_ieee802154->txpower = *((const int16_t *)val);
#if AT86RF2XX_HAVE_SUBGHZ
at86rf2xx_configure_phy(dev, dev->netdev.chan, dev->page, *((const int16_t *)val));
#else
at86rf2xx_configure_phy(dev, dev->netdev.chan, 0, *((const int16_t *)val));
#endif
res = sizeof(uint16_t);
break;
case NETOPT_STATE:
assert(len <= sizeof(netopt_state_t));
res = _set_state(dev, *((const netopt_state_t *)val));
break;
case NETOPT_AUTOACK:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
at86rf2xx_set_option(dev, AT86RF2XX_OPT_AUTOACK,
((const bool *)val)[0]);
res = sizeof(netopt_enable_t);
}
break;
case NETOPT_ACK_PENDING:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
at86rf2xx_set_option(dev, AT86RF2XX_OPT_ACK_PENDING,
((const bool *)val)[0]);
res = sizeof(netopt_enable_t);
}
break;
case NETOPT_RETRANS:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(len <= sizeof(uint8_t));
at86rf2xx_set_max_retries(dev, *((const uint8_t *)val));
res = sizeof(uint8_t);
}
break;
case NETOPT_PRELOADING:
at86rf2xx_set_option(dev, AT86RF2XX_OPT_PRELOADING,
((const bool *)val)[0]);
res = sizeof(netopt_enable_t);
break;
case NETOPT_PROMISCUOUSMODE:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
at86rf2xx_set_option(dev, AT86RF2XX_OPT_PROMISCUOUS,
((const bool *)val)[0]);
res = sizeof(netopt_enable_t);
}
break;
case NETOPT_CSMA:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
at86rf2xx_set_option(dev, AT86RF2XX_OPT_CSMA,
((const bool *)val)[0]);
res = sizeof(netopt_enable_t);
}
break;
case NETOPT_CSMA_RETRIES:
if (!IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
assert(len <= sizeof(uint8_t));
if (!(dev->flags & AT86RF2XX_OPT_CSMA) ||
(*((uint8_t *)val) > 5)) {
/* If CSMA is disabled, don't allow setting retries */
res = -EINVAL;
}
else {
at86rf2xx_set_csma_max_retries(dev, *((const uint8_t *)val));
res = sizeof(uint8_t);
}
}
break;
case NETOPT_CCA_THRESHOLD:
assert(len <= sizeof(int8_t));
at86rf2xx_set_cca_threshold(dev, *((const int8_t *)val));
res = sizeof(int8_t);
break;
#ifdef MODULE_NETDEV_IEEE802154_OQPSK
case NETOPT_OQPSK_RATE:
assert(len <= sizeof(int8_t));
if (at86rf2xx_set_rate(dev, *((const uint8_t *)val)) < 0) {
res = -EINVAL;
} else {
res = sizeof(uint8_t);
}
break;
#endif /* MODULE_NETDEV_IEEE802154_OQPSK */
default:
break;
}
/* go back to sleep if were sleeping and state hasn't been changed */
if ((old_state == AT86RF2XX_STATE_SLEEP)
&& (opt != NETOPT_STATE)) {
at86rf2xx_set_state(dev, AT86RF2XX_STATE_SLEEP);
}
if (res == -ENOTSUP) {
res = netdev_ieee802154_set(container_of(netdev, netdev_ieee802154_t, netdev),
opt, val, len);
}
return res;
}
static void _isr_send_complete(at86rf2xx_t *dev, uint8_t trac_status)
{
netdev_t *netdev = &dev->netdev.netdev;
if (IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
netdev->event_callback(netdev, NETDEV_EVENT_TX_COMPLETE);
return;
}
/* Only radios with the XAH_CTRL_2 register support frame retry reporting */
#if AT86RF2XX_HAVE_RETRIES_REG
dev->tx_retries = (at86rf2xx_reg_read(dev, AT86RF2XX_REG__XAH_CTRL_2)
& AT86RF2XX_XAH_CTRL_2__ARET_FRAME_RETRIES_MASK) >>
AT86RF2XX_XAH_CTRL_2__ARET_FRAME_RETRIES_OFFSET;
#endif
DEBUG("[at86rf2xx] EVT - TX_END\n");
if (netdev->event_callback) {
switch (trac_status) {
#ifdef MODULE_OPENTHREAD
case AT86RF2XX_TRX_STATE__TRAC_SUCCESS:
netdev->event_callback(netdev, NETDEV_EVENT_TX_COMPLETE);
DEBUG("[at86rf2xx] TX SUCCESS\n");
break;
case AT86RF2XX_TRX_STATE__TRAC_SUCCESS_DATA_PENDING:
netdev->event_callback(netdev, NETDEV_EVENT_TX_COMPLETE_DATA_PENDING);
DEBUG("[at86rf2xx] TX SUCCESS DATA PENDING\n");
break;
#else
case AT86RF2XX_TRX_STATE__TRAC_SUCCESS:
case AT86RF2XX_TRX_STATE__TRAC_SUCCESS_DATA_PENDING:
netdev->event_callback(netdev, NETDEV_EVENT_TX_COMPLETE);
DEBUG("[at86rf2xx] TX SUCCESS\n");
break;
#endif
case AT86RF2XX_TRX_STATE__TRAC_NO_ACK:
netdev->event_callback(netdev, NETDEV_EVENT_TX_NOACK);
DEBUG("[at86rf2xx] TX NO_ACK\n");
break;
case AT86RF2XX_TRX_STATE__TRAC_CHANNEL_ACCESS_FAILURE:
netdev->event_callback(netdev, NETDEV_EVENT_TX_MEDIUM_BUSY);
DEBUG("[at86rf2xx] TX_CHANNEL_ACCESS_FAILURE\n");
break;
default:
DEBUG("[at86rf2xx] Unhandled TRAC_STATUS: %d\n",
trac_status >> 5);
}
}
}
static inline void _isr_recv_complete(netdev_t *netdev)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
if (!netdev->event_callback) {
return;
}
if (IS_ACTIVE(AT86RF2XX_BASIC_MODE)) {
uint8_t phy_status = at86rf2xx_reg_read(dev, AT86RF2XX_REG__PHY_RSSI);
bool crc_ok = phy_status & AT86RF2XX_PHY_RSSI_MASK__RX_CRC_VALID;
if (crc_ok) {
netdev->event_callback(netdev, NETDEV_EVENT_RX_COMPLETE);
}
else {
netdev->event_callback(netdev, NETDEV_EVENT_CRC_ERROR);
}
}
else {
netdev->event_callback(netdev, NETDEV_EVENT_RX_COMPLETE);
}
}
static void _isr(netdev_t *netdev)
{
netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
uint8_t irq_mask;
uint8_t state;
uint8_t trac_status;
/* If transceiver is sleeping register access is impossible and frames are
* lost anyway, so return immediately.
*/
state = at86rf2xx_get_status(dev);
if (state == AT86RF2XX_STATE_SLEEP) {
return;
}
/* read (consume) device status */
irq_mask = at86rf2xx_get_irq_flags(dev);
trac_status = at86rf2xx_reg_read(dev, AT86RF2XX_REG__TRX_STATE)
& AT86RF2XX_TRX_STATE_MASK__TRAC;
if (irq_mask & AT86RF2XX_IRQ_STATUS_MASK__RX_START) {
netdev->event_callback(netdev, NETDEV_EVENT_RX_STARTED);
DEBUG("[at86rf2xx] EVT - RX_START\n");
}
if (irq_mask & AT86RF2XX_IRQ_STATUS_MASK__TRX_END) {
if ((state == AT86RF2XX_PHY_STATE_RX)
|| (state == AT86RF2XX_PHY_STATE_RX_BUSY)) {
DEBUG("[at86rf2xx] EVT - RX_END\n");
_isr_recv_complete(netdev);
}
else if (state == AT86RF2XX_PHY_STATE_TX) {
/* check for more pending TX calls and return to idle state if
* there are none */
assert(dev->pending_tx != 0);
/* Radio is idle, any TX transaction is done */
dev->pending_tx = 0;
at86rf2xx_set_state(dev, dev->idle_state);
DEBUG("[at86rf2xx] return to idle state 0x%x\n", dev->idle_state);
_isr_send_complete(dev, trac_status);
}
/* Only the case when an interrupt was received and the radio is busy
* with a next PDU transmission when _isr is called.
* dev->pending == 1 means a receive and immediately a send happened.
* The receive is discarded as the send already overwrote the internal
* buffer.
* dev->pending == 2 means two transmits occurred and this is the isr for
* the first.
*/
else if (state == AT86RF2XX_PHY_STATE_TX_BUSY) {
if (dev->pending_tx > 1) {
dev->pending_tx--;
_isr_send_complete(dev, trac_status);
}
}
}
}
void at86rf2xx_setup(at86rf2xx_t *dev, const at86rf2xx_params_t *params, uint8_t index)
{
netdev_t *netdev = &dev->netdev.netdev;
netdev->driver = &at86rf2xx_driver;
/* State to return after receiving or transmitting */
dev->idle_state = AT86RF2XX_STATE_TRX_OFF;
/* radio state is P_ON when first powered-on */
dev->state = AT86RF2XX_STATE_P_ON;
dev->pending_tx = 0;
#if AT86RF2XX_IS_PERIPH
(void) params;
/* set all interrupts off */
at86rf2xx_reg_write(dev, AT86RF2XX_REG__IRQ_MASK, 0x00);
#else
/* initialize device descriptor */
dev->params = *params;
#endif
netdev_register(netdev, NETDEV_AT86RF2XX, index);
/* set device address */
netdev_ieee802154_setup(&dev->netdev);
}
#if AT86RF2XX_IS_PERIPH
/**
* @brief ISR for transceiver's TX_START interrupt
*
* In procedure TX_ARET the TRX24_TX_START interrupt is issued separately for every
* frame transmission and frame retransmission.
* Indicates the frame start of a transmitted acknowledge frame in procedure RX_AACK.
*
* Flow Diagram Manual p. 52 / 63
*/
#if AT86RF2XX_HAVE_RETRIES
ISR(TRX24_TX_START_vect){
/* __enter_isr(); is not necessary as there is nothing which causes a
* thread_yield and the interrupt can not be interrupted by an other ISR */
netdev_ieee802154_t *netdev_ieee802154 = container_of(at86rfmega_dev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);
dev->tx_retries++;
}
#endif
/**
* @brief Transceiver PLL Lock
*
* Is triggered when PLL locked successfully.
*
* Manual p. 40
*/
static inline void txr24_pll_lock_handler(void)
{
DEBUG("TRX24_PLL_LOCK\n");
netdev_ieee802154_t *netdev_ieee802154 = container_of(at86rfmega_dev,
netdev_ieee802154_t, netdev);
at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev);