-
Notifications
You must be signed in to change notification settings - Fork 1
/
dwmac.c
695 lines (608 loc) · 16.5 KB
/
dwmac.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
/*
* libdeca - UWB Library for Qorvo/Decawave DW3000
*
* Copyright (C) 2016 - 2024 Bruno Randolf (br@einfach.org)
*
* This source code is licensed under the GNU Lesser General Public License,
* Version 3. See the file LICENSE.txt for more details.
*/
#include <stdlib.h>
#include <string.h>
#include <deca_device_api.h>
#include <deca_version.h>
#ifdef DW3000_DRIVER_VERSION // == 0x040000
#include <deca_regs.h>
#endif
#include "dwhw.h"
#include "dwmac.h"
#include "dwphy.h"
#include "dwtime.h"
#include "dwutil.h"
#include "mac802154.h"
#include "platform/dwmac_task.h"
#include "log.h"
#define SLOT_SPI_TIME(_x) (_x * 2.7) /* us TODO: old */
#define SLOT_PROC_TIME 500 /* TODO: now used with systime: only TX */
#define SLOT_GAP 5
#ifndef __ZEPHYR__
static const char* LOG_TAG = "DECA";
#endif
static uint16_t panId;
static uint16_t macAddr;
static uint64_t mac64;
static deca_rx_cb dwmac_rx_cb = NULL;
static deca_to_cb dwmac_to_cb = NULL;
static deca_err_cb dwmac_err_cb = NULL;
static uint32_t mac_tx_cnt = 0;
static struct txbuf tx_buffer;
/* shared with dwmac_irq.c */
struct rxbuf rx_buffer;
struct txbuf* current_tx = NULL;
bool rx_reenable = false;
extern void dwmac_irq_rx_ok_cb(const dwt_cb_data_t* dat);
extern void dwmac_irq_rx_to_cb(const dwt_cb_data_t* dat);
extern void dwmac_irq_err_cb(const dwt_cb_data_t* dat);
extern void dwmac_irq_tx_done_cb(const dwt_cb_data_t* dat);
extern void dwmac_irq_spi_err_cb(const dwt_cb_data_t* dat);
extern void dwmac_irq_spi_rdy_cb(const dwt_cb_data_t* dat);
#if DRIVER_VERSION_HEX >= 0x080202
dwt_callbacks_s dwmac_cbs = {
.cbTxDone = dwmac_irq_tx_done_cb,
.cbRxOk = dwmac_irq_rx_ok_cb,
.cbRxTo = dwmac_irq_rx_to_cb,
.cbRxErr = dwmac_irq_err_cb,
.cbSPIErr = dwmac_irq_spi_err_cb,
.cbSPIRDErr = NULL,
.cbSPIRdy = dwmac_irq_spi_rdy_cb,
.cbDualSPIEv = NULL,
.cbFrmRdy = NULL,
.cbCiaDone = NULL,
.devErr = NULL,
.cbSysEvent = NULL,
};
#endif
bool dwmac_init(uint16_t mypanId, uint16_t myAddr, deca_rx_cb rx_cb,
deca_to_cb to_cb, deca_err_cb err_cb)
{
ASSERT_RET(myAddr != 0 && myAddr != 0xffff);
panId = mypanId;
macAddr = myAddr;
rx_reenable = false;
dwmac_rx_cb = rx_cb;
dwmac_to_cb = to_cb;
dwmac_err_cb = err_cb;
LOG_INF("Init PANID: " ADDR_FMT " MAC: " ADDR_FMT, panId, macAddr);
/* frame filtering OFF */
dwt_setaddress16(macAddr);
dwt_setpanid(panId);
dwt_configureframefilter(DWT_FF_DISABLE, 0);
/* for 'dwcnt' command */
dwt_configeventcounters(1);
#if DRIVER_VERSION_HEX >= 0x080202
dwt_setcallbacks(&dwmac_cbs);
dwt_setinterrupt(DWT_INT_RXFCG_BIT_MASK | DWT_INT_TXFRS_BIT_MASK
| DWT_INT_RXPHE_BIT_MASK | DWT_INT_RXFCE_BIT_MASK
| DWT_INT_RXFSL_BIT_MASK | DWT_INT_RXFTO_BIT_MASK
| DWT_INT_CIAERR_BIT_MASK | DWT_INT_RXPTO_BIT_MASK
| DWT_INT_RXSTO_BIT_MASK
#if CONFIG_DECA_DEBUG_FRAME_FILTER
| DWT_INT_ARFE_BIT_MASK
#endif
,
0, DWT_ENABLE_INT_ONLY);
#elif DRIVER_VERSION_HEX >= 0x060007
dwt_setcallbacks(dwmac_irq_tx_done_cb, dwmac_irq_rx_ok_cb,
dwmac_irq_rx_to_cb, dwmac_irq_err_cb, dwmac_irq_spi_err_cb,
dwmac_irq_spi_rdy_cb, NULL);
dwt_setinterrupt(DWT_INT_RXFCG_BIT_MASK | DWT_INT_TXFRS_BIT_MASK
| DWT_INT_RXPHE_BIT_MASK | DWT_INT_RXFCE_BIT_MASK
| DWT_INT_RXFSL_BIT_MASK | DWT_INT_RXFTO_BIT_MASK
| DWT_INT_CIAERR_BIT_MASK | DWT_INT_RXPTO_BIT_MASK
| DWT_INT_RXSTO_BIT_MASK
#if CONFIG_DECA_DEBUG_FRAME_FILTER
| DWT_INT_ARFE_BIT_MASK
#endif
,
0, DWT_ENABLE_INT_ONLY);
#else
dwt_setcallbacks(dwmac_irq_tx_done_cb, dwmac_irq_rx_ok_cb,
dwmac_irq_rx_to_cb, dwmac_irq_err_cb, dwmac_irq_spi_err_cb,
dwmac_irq_spi_rdy_cb);
dwt_setinterrupt(DWT_INT_RFCG | DWT_INT_TFRS | DWT_INT_RPHE | DWT_INT_RFCE
| DWT_INT_RFSL | DWT_INT_RFTO | DWT_INT_LDEERR
| DWT_INT_RXPTO | DWT_INT_SFDT
#if CONFIG_DECA_DEBUG_FRAME_FILTER
| DWT_INT_ARFE
#endif
,
0, DWT_ENABLE_INT_ONLY);
#endif
dwtask_init();
return true;
}
void dwmac_set_frame_filter(void)
{
dwt_configureframefilter(DWT_FF_ENABLE_802_15_4,
DWT_FF_BEACON_EN | DWT_FF_DATA_EN | DWT_FF_ACK_EN
| DWT_FF_COORD_EN);
}
void deca_print_sys_status(uint32_t status)
{
#ifdef DRIVER_VERSION_HEX // >= 0x060007
if (status & DWT_INT_TIMER1_BIT_MASK) {
LOG_INF("- TIMER1 expiry");
}
if (status & DWT_INT_TIMER0_BIT_MASK) {
LOG_INF("- TIMER0 expiry");
}
if (status & DWT_INT_ARFE_BIT_MASK) {
LOG_INF("- Frame filtered");
}
if (status & DWT_INT_CPERR_BIT_MASK) {
LOG_INF("- STS quality warning/error");
}
if (status & DWT_INT_HPDWARN_BIT_MASK) {
LOG_INF("- Half period warning");
}
if (status & DWT_INT_RXSTO_BIT_MASK) {
LOG_INF("- SFD timeout");
}
if (status & DWT_INT_PLL_HILO_BIT_MASK) {
LOG_INF("- Clock PLL Losing Lock");
}
if (status & DWT_INT_RCINIT_BIT_MASK) {
LOG_INF("- Device has entered IDLE_RC");
}
if (status & DWT_INT_SPIRDY_BIT_MASK) {
LOG_INF("- SPI ready");
}
if (status & DWT_INT_RXPTO_BIT_MASK) {
LOG_INF("- RX Preamble timeout");
}
if (status & DWT_INT_RXOVRR_BIT_MASK) {
LOG_INF("- RX overrun (double RX buffer)");
}
if (status & DWT_INT_VWARN_BIT_MASK) {
LOG_INF("- Low voltage warning");
}
if (status & DWT_INT_CIAERR_BIT_MASK) {
LOG_INF("- CIA error");
}
if (status & DWT_INT_RXFTO_BIT_MASK) {
LOG_INF("- RX frame wait timeout");
}
if (status & DWT_INT_RXFSL_BIT_MASK) {
LOG_INF("- RX Reed-Solomon sync loss");
}
if (status & DWT_INT_RXFCE_BIT_MASK) {
LOG_INF("- RX frame CRC error");
}
if (status & DWT_INT_RXFCG_BIT_MASK) {
LOG_INF("- RX frame CRC good");
}
if (status & DWT_INT_RXFR_BIT_MASK) {
LOG_INF("- RX data frame ready");
}
if (status & DWT_INT_RXPHE_BIT_MASK) {
LOG_INF("- RX PHY header error");
}
if (status & DWT_INT_RXPHD_BIT_MASK) {
LOG_INF("- RX PHY header detected");
}
if (status & DWT_INT_CIADONE_BIT_MASK) {
LOG_INF("- CIA done");
}
if (status & DWT_INT_RXSFDD_BIT_MASK) {
LOG_INF("- RX SFD detected");
}
if (status & DWT_INT_RXPRD_BIT_MASK) {
LOG_INF("- RX Preamble detected");
}
if (status & DWT_INT_TXFRS_BIT_MASK) {
LOG_INF("- TX Frame sent");
}
if (status & DWT_INT_TXPHS_BIT_MASK) {
LOG_INF("- TX PHR sent");
}
if (status & DWT_INT_TXPRS_BIT_MASK) {
LOG_INF("- TX preamble sent");
}
if (status & DWT_INT_TXFRB_BIT_MASK) {
LOG_INF("- TX frame begins");
}
if (status & DWT_INT_AAT_BIT_MASK) {
LOG_INF("- Automatic ACK transmission pending");
}
if (status & DWT_INT_SPICRCE_BIT_MASK) {
LOG_INF("- SPI CRC error");
}
if (status & DWT_INT_CP_LOCK_BIT_MASK) {
LOG_INF("- Clock PLL locked");
}
// if (status & DWT_INT_IRQS_BIT_MASK) {
// LOG_INF("- Interrupt set");
// }
#else
if (status & SYS_STATUS_ARFE_BIT_MASK) {
LOG_INF("- RX frame filtered");
}
if (status & SYS_STATUS_CPERR_BIT_MASK) {
LOG_INF("- STS quality warning/error");
}
if (status & SYS_STATUS_HPDWARN_BIT_MASK) {
LOG_INF("- Half period warning flag when delayed TX/RX is used");
}
if (status & SYS_STATUS_RXSTO_BIT_MASK) {
LOG_INF("- SFD timeout");
}
if (status & SYS_STATUS_PLL_HILO_BIT_MASK) {
LOG_INF("- Clock PLL Losing Lock");
}
if (status & SYS_STATUS_RCINIT_BIT_MASK) {
LOG_INF("- Device has entered IDLE_RC");
}
if (status & SYS_STATUS_SPIRDY_BIT_MASK) {
LOG_INF("- SPI ready");
}
if (status & SYS_STATUS_RXPTO_BIT_MASK) {
LOG_INF("- RX Preamble timeout");
}
if (status & SYS_STATUS_RXOVRR_BIT_MASK) {
LOG_INF("- RX overrun (double RX buffer)");
}
if (status & SYS_STATUS_VWARN_BIT_MASK) {
LOG_INF("- Low voltage warning");
}
if (status & SYS_STATUS_CIAERR_BIT_MASK) {
LOG_INF("- CIA error");
}
if (status & SYS_STATUS_RXFTO_BIT_MASK) {
LOG_INF("- RX frame wait timeout");
}
if (status & SYS_STATUS_RXFSL_BIT_MASK) {
LOG_INF("- RX Reed-Solomon sync loss");
}
if (status & SYS_STATUS_RXFCE_BIT_MASK) {
LOG_INF("- RX frame CRC error");
}
if (status & SYS_STATUS_RXFCG_BIT_MASK) {
LOG_INF("- RX frame CRC good");
}
if (status & SYS_STATUS_RXFR_BIT_MASK) {
LOG_INF("- RX data frame ready");
}
if (status & SYS_STATUS_RXPHE_BIT_MASK) {
LOG_INF("- RX PHY header error");
}
if (status & SYS_STATUS_RXPHD_BIT_MASK) {
LOG_INF("- RX PHY header detected");
}
if (status & SYS_STATUS_CIADONE_BIT_MASK) {
LOG_INF("- CIA done");
}
if (status & SYS_STATUS_RXSFDD_BIT_MASK) {
LOG_INF("- RX SFD detected");
}
if (status & SYS_STATUS_RXPRD_BIT_MASK) {
LOG_INF("- RX Preamble detected");
}
if (status & SYS_STATUS_TXFRS_BIT_MASK) {
LOG_INF("- TX Frame sent");
}
if (status & SYS_STATUS_TXPHS_BIT_MASK) {
LOG_INF("- TX PHR sent");
}
if (status & SYS_STATUS_TXPRS_BIT_MASK) {
LOG_INF("- TX preamble sent");
}
if (status & SYS_STATUS_TXFRB_BIT_MASK) {
LOG_INF("- TX frame begins");
}
if (status & SYS_STATUS_AAT_BIT_MASK) {
LOG_INF("- Automatic ACK transmission pending");
}
if (status & SYS_STATUS_SPICRCE_BIT_MASK) {
LOG_INF("- SPI CRC error");
}
if (status & SYS_STATUS_CP_LOCK_BIT_MASK) {
LOG_INF("- Clock PLL locked");
}
// if (status & SYS_STATUS_IRQS_BIT_MASK) {
// LOG_INF("- Interrupt set");
// }
#endif
}
struct txbuf* dwmac_txbuf_get(void)
{
// there is only one TX buffer for now
return &tx_buffer;
}
void dwmac_txbuf_return(struct txbuf* tx)
{
// there is only one TX buffer for now
}
void dwmac_tx_prepare_null(struct txbuf* tx)
{
tx->len = 0;
tx->resp = false;
tx->resp_multi = false;
tx->ranging = false;
tx->sleep_after_tx = false;
tx->rx_timeout = 0;
tx->txtime = 0;
tx->rx_delay = 0;
tx->pto = 0;
tx->to_cb = NULL;
tx->complete_cb = NULL;
}
/* len is without FCS */
void dwmac_tx_prepare(struct txbuf* tx, size_t len)
{
dwmac_tx_prepare_null(tx);
tx->len = len + MAC154_FCS_LEN;
}
void dwmac_tx_set_ranging(struct txbuf* tx)
{
tx->ranging = true;
}
/* expect response (RX on) after delay (may be 0) in UUS */
void dwmac_tx_expect_response(struct txbuf* tx, uint32_t delay)
{
tx->resp = true;
tx->rx_delay = delay;
}
/* expect multiple responses (turn RX on after receive) */
void dwmac_tx_expect_multiple_responses(struct txbuf* tx)
{
tx->resp = true;
tx->resp_multi = true;
}
/** timeout in UUS, implies response expected */
void dwmac_tx_set_rx_timeout(struct txbuf* tx, uint16_t to)
{
tx->resp = true;
tx->rx_timeout = to;
}
/* timeout in units of PAC size, implies response expected */
void dwmac_tx_set_preamble_timeout(struct txbuf* tx, uint16_t pto)
{
tx->resp = true;
tx->pto = pto;
}
void dwmac_tx_set_sleep_after_tx(struct txbuf* tx)
{
tx->sleep_after_tx = true;
}
void dwmac_tx_set_timeout_handler(struct txbuf* tx, deca_to_cb toh)
{
tx->to_cb = toh;
}
void dwmac_tx_set_complete_handler(struct txbuf* tx, deca_tx_complete_cb h)
{
tx->complete_cb = h;
}
/* time resolution of tx time is 8ns: given in DTU with last 9 bits 0 */
void dwmac_tx_set_txtime(struct txbuf* tx, uint64_t time)
{
tx->txtime = time;
}
bool dwmac_tx_raw(struct txbuf* tx)
{
int ret;
decaIrqStatus_t stat = decamutexon();
/* make sure we're out of RX mode before initiating TX */
dwt_forcetrxoff();
if (tx->sleep_after_tx) {
dwhw_enable_tx_interrupt(false);
dwt_entersleepaftertx(1);
}
if (tx->len > 0) {
ret = dwt_writetxdata(tx->len, tx->buf, 0);
if (ret != DWT_SUCCESS) {
return false;
}
// NOTE: this is not necessary to set if we use STS_MODE_ND
dwt_writetxfctrl(tx->len, 0, tx->ranging);
}
dwt_setrxtimeout(tx->rx_timeout);
dwt_setrxaftertxdelay(tx->rx_delay);
dwt_setpreambledetecttimeout(tx->pto);
if (tx->txtime) {
dwt_setdelayedtrxtime(DTU_TO_DELAYEDTRX(tx->txtime));
}
mac_tx_cnt++;
ret = dwt_starttx(
(tx->txtime ? DWT_START_TX_DELAYED : DWT_START_TX_IMMEDIATE)
| (tx->resp || rx_reenable ? DWT_RESPONSE_EXPECTED : 0));
decamutexoff(stat);
// plat_led_act_trigger();
if (ret != DWT_SUCCESS) {
LOG_ERR("TX error (%p)", tx);
if (tx->txtime) {
uint64_t systime = dw_get_systime();
LOG_ERR_TS("\tSYS Time:\t", systime);
LOG_ERR_TS("\tTX Time:\t", tx->txtime);
int diff = tx->txtime - systime;
LOG_ERR("\tDiff:\t\t%x (%d us)", diff, (int)DTU_TO_US(diff));
/* decadriver disables TRX in the error case */
if (rx_reenable)
dwt_rxenable(DWT_START_RX_IMMEDIATE);
return false;
}
}
#if CONFIG_DECA_DEBUG_TX_TIME
if (tx->txtime) {
uint64_t systime = deca_get_sys_time();
LOG_DBG("Delayed TX in %d us (%p)",
(int)DTU_TO_US(tx->txtime - systime), tx);
} else {
LOG_DBG("TX (%p)", tx);
}
#endif
#if CONFIG_DECA_DEBUG_TX_DUMP
LOG_HEXDUMP("TX", tx->buf, tx->len);
#endif
if (tx->sleep_after_tx) {
dwhw_sleep_after_tx();
}
return true;
}
bool dwmac_transmit(struct txbuf* tx)
{
if (tx == NULL) {
LOG_ERR("TX invalid");
return false;
}
current_tx = tx;
return dwmac_tx_raw(tx);
}
void dwmac_handle_rx_frame(const struct rxbuf* rx)
{
#if CONFIG_DECA_DEBUG_IRQ_TIME
uint64_t st = dw_get_systime();
LOG_INF("RX to CB:\t%d us", (int)DTU_TO_US(rx->ts_irq_start - rx->ts));
LOG_INF("RX to Sched:\t%d us", (int)DTU_TO_US(st - rx->ts));
LOG_INF("Time in CB:\t%d us",
(int)DTU_TO_US(rx->ts_irq_end - rx->ts_irq_start));
LOG_DBG("IRQ start %" PRIu32, (uint32_t)rx->ts_irq_start);
LOG_DBG("IRQ end %" PRIu32, (uint32_t)rx->ts_irq_end);
#endif
#if CONFIG_DECA_DEBUG_RX_DUMP
LOG_HEXDUMP("RX", rx->buf, rx->len);
#endif
#if CONFIG_DECA_READ_RXDIAG
LOG_INF("DIAG preamb %d", rx->diag.rxPreamCount);
#endif
#if CONFIG_DECA_XTAL_TRIM
if (rx->len > 0) {
dwphy_xtal_trim();
}
#endif
if (dwmac_rx_cb != NULL) {
dwmac_rx_cb(rx);
}
}
void dwmac_handle_rx_timeout(uint32_t status)
{
#if CONFIG_DECA_DEBUG_RX_STATUS
deca_print_sys_status(status);
#endif
if (current_tx && current_tx->to_cb != NULL) {
current_tx->to_cb(status);
}
if (dwmac_to_cb != NULL) {
dwmac_to_cb(status);
}
}
void dwmac_handle_tx_done(void)
{
// callback after removing current_tx so we can TX again
deca_tx_complete_cb cb = NULL;
if (current_tx != NULL && current_tx->complete_cb != NULL) {
cb = current_tx->complete_cb;
}
// only remove TX if we are not waiting for a RX timeout
if (current_tx != NULL && current_tx->pto == 0
&& current_tx->rx_timeout == 0) {
current_tx = NULL;
}
if (cb) {
cb();
}
}
void dwmac_cleanup_sleep_after_tx(void)
{
if (current_tx != NULL && current_tx->sleep_after_tx) {
dwt_entersleepaftertx(0); // Disable Sleep after TX!
dwhw_enable_tx_interrupt(true);
dwmac_handle_tx_done();
}
}
void dwmac_handle_error(uint32_t status)
{
#if CONFIG_DECA_DEBUG_RX_STATUS | CONFIG_DECA_DEBUG_FRAME_FILTER
deca_print_sys_status(status);
#endif
if (dwmac_err_cb != NULL) {
dwmac_err_cb(status);
}
}
/* return tx delay in usec for slot (starting from 1) */
int dwmac_get_slot_us(size_t pkt_len, int slot_num)
{
ASSERT_RET(slot_num > 0);
ASSERT_RET(pkt_len > 0);
uint8_t plen = dwphy_get_plen();
uint8_t prf = dwphy_get_prf();
uint8_t rate = dwphy_get_rate();
/* first delay is equal for all slots */
int delay = SLOT_PROC_TIME; // constant processing time for RX and TX
delay += SLOT_SPI_TIME(pkt_len);
delay += PKTTIME_TO_USEC(dwphy_calc_preamble_time(plen, prf, rate));
/* then multiply slot duration by slot number */
slot_num--;
int slot_duration
= PKTTIME_TO_USEC(dwphy_calc_packet_time(rate, plen, prf, pkt_len));
slot_duration += SLOT_GAP;
delay += slot_duration * slot_num;
LOG_DBG("Slot %d = %d for %dB", slot_num, delay, pkt_len);
return delay;
}
void dwmac_rx_reenable(void)
{
if (rx_reenable) {
dwt_rxenable(DWT_START_RX_IMMEDIATE);
}
}
void dwmac_set_rx_reenable(bool b)
{
if (b && !rx_reenable) {
dwt_rxenable(DWT_START_RX_IMMEDIATE);
} else if (!b && rx_reenable) {
dwt_forcetrxoff();
}
rx_reenable = b;
}
void dwmac_print_event_counters(void)
{
dwt_deviceentcnts_t counters;
dwt_readeventcounters(&counters);
LOG_INF("PHE %d (header error)", counters.PHE);
LOG_INF("RSL %d (frame sync loss)", counters.RSL);
LOG_INF("CRCG %d (good CRC)", counters.CRCG);
LOG_INF("CRCB %d (CRC error)", counters.CRCB);
LOG_INF("ARFE %d (address filter)", counters.ARFE);
LOG_INF("OVER %d (receive buffer overrun)", counters.OVER);
LOG_INF("SFDTO %d (SFD timeout)", counters.SFDTO);
LOG_INF("PTO %d (Preamble timeout)", counters.PTO);
LOG_INF("RTO %d (RX frame wait timeout)", counters.RTO);
LOG_INF("TXF %d (transmitted frame)", counters.TXF);
LOG_INF("HPW %d (half period warning)", counters.HPW);
LOG_INF("CRCE %d (SPI CRC error)", counters.CRCE);
LOG_INF("PREJ %d (Preamble rejection)", counters.PREJ);
#ifdef DRIVER_VERSION_HEX // >= 0x060007
LOG_INF("SFDD %d (SFD detection)", counters.SFDD);
LOG_INF("STSE %d (STS error/warning)", counters.STSE);
#endif
}
uint16_t dwmac_get_mac16(void)
{
return macAddr;
}
uint16_t dwmac_get_panid(void)
{
return panId;
}
uint64_t dwmac_get_mac64(void)
{
return mac64;
}
void dwmac_set_mac64(uint64_t mac)
{
mac64 = mac;
dwt_seteui((uint8_t*)&mac64);
}
uint32_t dwmac_get_tx_start_cnt(void)
{
return mac_tx_cnt;
}