-
Notifications
You must be signed in to change notification settings - Fork 0
/
nRF.c
1174 lines (978 loc) · 35.3 KB
/
nRF.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <err.h>
#include <sys/time.h>
#include "nRF.h"
#include "nRF_internals.h"
#include "nRF_defs.h"
#include "sim_avr.h"
#include "avr_spi.h"
#include "avr_ioport.h"
#include "sim_time.h"
/*
simavr-nRF24
This code simulates one (or more, a single one does not make much sense) nRF24L01+. To be used with simavr.
Please read the fine manual.
(c) 2022 by kittennbfive
AGPLv3+ and NO WARRANTY!
version 11.05.22 00:54
*/
static nRF_log_level_t loglevel=NRF_LOG_WARNING;
static bool stop_on_error=false;
#define LOG(level, msg, ...) \
do \
{ \
if(level==NRF_LOG_ERROR && stop_on_error) \
errx(1, msg, ##__VA_ARGS__); \
if(loglevel>=level) \
printf(msg, ##__VA_ARGS__); \
} while(0)
static nRF_t * modules[NB_NRF_MAX];
static uint8_t nb_modules=0;
static config_lost_packets_t lost;
static packets_stats_t stats;
enum
{
NRF24_CE_IN=0,
NRF24_IRQ_OUT,
NRF24_IRQ_COUNT
};
static const char * irq_names[NRF24_IRQ_COUNT]={
[NRF24_CE_IN]="nRF_CE",
[NRF24_IRQ_OUT]="nRF_IRQ"
};
static const uint8_t regs_len_bytes[30]={1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1};
static void handle_pin_IRQ(nRF_t * nRF);
static void update_nRF(nRF_t * nRF);
static void update_fifo_status(nRF_t * nRF);
static void do_TX(nRF_t * nRF);
static void do_TX_ack(nRF_t * nRF);
static avr_cycle_count_t cb_delay_timer(avr_t * avr, avr_cycle_count_t when, void * param);
static avr_cycle_count_t cb_tx_finished(avr_t * avr, avr_cycle_count_t when, void * param);
static avr_cycle_count_t cb_ard_elapsed(avr_t * avr, avr_cycle_count_t when, void * param);
static void finish_spi(nRF_t * const nRF)
{
LOG(NRF_LOG_DEBUG, "nRF %s: finish_spi called\n", nRF->name);
switch(nRF->state_spi)
{
case NRF_SPI_IDLE: //nothing to do
break;
case NRF_SPI_READ_REGISTER: //nothing to do
break;
case NRF_SPI_WRITE_REGISTER:
switch(nRF->spi_reg_index)
{
case REG_STATUS: //special handling for interrupt flags
if(nRF->spi_value&(1<<TX_DS))
nRF->regs[REG_STATUS]&=~(1<<TX_DS);
if(nRF->spi_value&(1<<RX_DR))
nRF->regs[REG_STATUS]&=~(1<<RX_DR);
if(nRF->spi_value&(1<<MAX_RT))
nRF->regs[REG_STATUS]&=~(1<<MAX_RT);
break;
case REG_RF_CH:
nRF->regs[REG_RF_CH]=nRF->spi_value;
nRF->regs[REG_OBSERVE_TX]&=~(0b1111<<PLOS_CNT);
break;
default:
nRF->regs[nRF->spi_reg_index]=nRF->spi_value;
break;
}
break;
case NRF_SPI_W_TX_PAYLOAD:
nRF->fifo_tx[nRF->fifo_tx_entries].PID=nRF->PID;
nRF->PID=(nRF->PID+1)&3;
nRF->fifo_tx_entries++;
update_fifo_status(nRF);
nRF->tx_in_progress=false;
nRF->tx_finished=false;
nRF->ard_has_elapsed=false;
nRF->nb_retries=0;
nRF->rx_ack_timeout=false;
break;
case NRF_SPI_R_RX_PAYLOAD:
memmove(&nRF->fifo_rx[0], &nRF->fifo_rx[1], 2*sizeof(packet_rx_t));
nRF->fifo_rx_entries--;
update_fifo_status(nRF);
break;
case NRF_SPI_READ_LENGTH_PAYLOAD: //nothing to do
break;
case NRF_SPI_WRITE_ACK_PAYLOAD:
nRF->fifo_tx_entries++;
update_fifo_status(nRF);
break;
}
nRF->state_spi=NRF_SPI_IDLE;
update_nRF(nRF);
handle_pin_IRQ(nRF);
}
static void update_nRF(nRF_t * const nRF)
{
switch(nRF->state)
{
case NRF_POWER_DOWN:
if(nRF->regs[REG_CONFIG]&(1<<PWR_UP))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: waking up...\n", nRF->name);
nRF->state=NRF_START_UP;
nRF->state_next=NRF_STANDBY1;
avr_cycle_timer_register(nRF->avr, MS_TO_CYCLES(nRF->avr, 1.5), &cb_delay_timer, nRF);
}
break;
case NRF_START_UP:
break;
case NRF_STANDBY1:
if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
}
else if(!(nRF->regs[REG_CONFIG]&(1<<PRIM_RX)) && nRF->pin_CE && nRF->fifo_tx_entries)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to TX-mode\n", nRF->name);
nRF->state=NRF_TX_SETTLING;
nRF->state_next=NRF_TX_MODE;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 10+130), &cb_delay_timer, nRF); //HACK, TODO: check if CE was high for >=10µs
}
else if(nRF->regs[REG_CONFIG]&(1<<PRIM_RX) && nRF->pin_CE)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to RX-mode\n", nRF->name);
nRF->state=NRF_RX_SETTLING;
nRF->state_next=NRF_RX_MODE;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 130), &cb_delay_timer, nRF);
}
else if(!(nRF->regs[REG_CONFIG]&(1<<PRIM_RX)) && nRF->pin_CE && nRF->fifo_tx_entries==0)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: no packets to TX, going into Standby2\n", nRF->name);
nRF->state=NRF_STANDBY2;
}
else
LOG(NRF_LOG_DEBUG, "nRF %s: no action, remaining in Standby1, CE=%u CSN=%u IRQ=%u\n", nRF->name, nRF->pin_CE, nRF->pin_CSN, nRF->pin_IRQ);
break;
case NRF_RX_SETTLING:
if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
nRF->state_next=NRF_POWER_DOWN; //there might be a timer firing that will set state to state_next, so write both
}
else if(!nRF->pin_CE)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: RX Settling aborted because CE went low, going into Standby1\n", nRF->name);
nRF->state=NRF_STANDBY1;
nRF->state_next=NRF_STANDBY1;
}
break;
case NRF_TX_SETTLING:
if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
nRF->state_next=NRF_POWER_DOWN; //there might be a timer firing that will set state to state_next, so write both
}
break;
case NRF_TX_MODE:
if(nRF->tx_finished && nRF->tx_wait_for_ack)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going into RX-mode to receive ACK, registering cb_delay_timer\n", nRF->name);
nRF->tx_finished=false;
nRF->state=NRF_RX_SETTLING_FOR_ACK;
nRF->state_next=NRF_RX_MODE_FOR_ACK;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 130), &cb_delay_timer, nRF);
}
else if(nRF->tx_finished && !nRF->pin_CE)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to Standby1-mode\n", nRF->name);
nRF->tx_finished=false;
nRF->state=NRF_STANDBY1;
}
else if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
}
else if(nRF->pin_CE && nRF->fifo_tx_entries==0)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: no packets to TX, going into Standby2\n", nRF->name);
nRF->state=NRF_STANDBY2;
}
break;
case NRF_RX_MODE:
if(!nRF->pin_CE)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: leaving RX-mode for Standby1\n", nRF->name);
nRF->state=NRF_STANDBY1;
}
else if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
}
break;
case NRF_STANDBY2:
if(!(nRF->regs[REG_CONFIG]&(1<<PWR_UP)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to power down\n", nRF->name);
nRF->state=NRF_POWER_DOWN;
}
else if(nRF->pin_CE && nRF->fifo_tx_entries)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going to TX-mode\n", nRF->name);
nRF->state=NRF_TX_SETTLING;
nRF->state_next=NRF_TX_MODE;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 130), &cb_delay_timer, nRF);
}
else if(nRF->tx_wait_for_ack)
{
if(nRF->ard_has_elapsed)
{
nRF->tx_wait_for_ack=false; //prevent cb_delay_timer to register cb_rx_ack_timeout before the new transmission has even started
LOG(NRF_LOG_VERBOSE, "nRF %s: ARD has elapsed\n", nRF->name);
if(nRF->nb_retries==(nRF->regs[REG_SETUP_RETR]&(0b1111<<ARC)))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: ARC reached, setting MAX_RT, going into Standby1\n", nRF->name);
nRF->regs[REG_STATUS]|=(1<<MAX_RT);
handle_pin_IRQ(nRF);
nRF->state=NRF_STANDBY1;
}
else
{
LOG(NRF_LOG_VERBOSE, "nRF %s: going into TX to send again\n", nRF->name);
nRF->nb_retries++;
nRF->state=NRF_TX_SETTLING;
nRF->state_next=NRF_TX_MODE;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 130), &cb_delay_timer, nRF);
}
}
}
break;
case NRF_RX_SETTLING_FOR_ACK: //TODO check CE here?
break;
case NRF_RX_MODE_FOR_ACK:
if(nRF->tx_ack_received)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: ACK received, going into Standby1\n", nRF->name);
nRF->state=NRF_STANDBY1;
nRF->tx_wait_for_ack=false;
nRF->tx_receive_ack_from=NULL;
nRF->rx_send_ack_to=NULL;
}
else if(nRF->rx_ack_timeout)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: timeout while waiting for ACK, going into Standby2\n", nRF->name);
nRF->rx_ack_timeout=false;
nRF->state=NRF_STANDBY2;
}
break;
case NRF_TX_SETTLING_FOR_ACK: //TODO check CE here?
break;
case NRF_TX_MODE_FOR_ACK:
if(nRF->tx_finished)
{
nRF->tx_finished=false;
if(nRF->pin_CE)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: ACK transmitted, going back to RX-mode\n", nRF->name);
nRF->state=NRF_RX_SETTLING;
nRF->state_next=NRF_RX_MODE;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 130), &cb_delay_timer, nRF);
}
else
{
LOG(NRF_LOG_VERBOSE, "nRF %s: ACK transmitted, CE is low, going into Standby1\n", nRF->name);
nRF->state=NRF_STANDBY1;
}
}
break;
}
do_TX(nRF);
do_TX_ack(nRF);
}
static void update_fifo_status(nRF_t * const nRF)
{
//TODO: STATUS_TX_REUSE unimplemented
if(nRF->fifo_rx_entries==0)
{
nRF->regs[REG_STATUS]&=~(1<<RX_DR);
nRF->regs[REG_STATUS]|=(0b111<<RX_P_NO); //"RX FIFO empty"
}
else
{
nRF->regs[REG_STATUS]&=~(0b111<<RX_P_NO);
nRF->regs[REG_STATUS]|=(1<<RX_DR)|(nRF->fifo_rx[0].pipe<<RX_P_NO);
}
if(nRF->fifo_tx_entries==3)
nRF->regs[REG_STATUS]|=(1<<TX_FULL);
else
nRF->regs[REG_STATUS]&=~(1<<TX_FULL);
switch(nRF->fifo_tx_entries)
{
case 0:
nRF->regs[REG_FIFO_STATUS]|=(1<<FIFO_TX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_FULL);
break;
case 1:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_FULL);
break;
case 2:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_FULL);
break;
case 3:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_TX_EMPTY);
nRF->regs[REG_FIFO_STATUS]|=(1<<FIFO_TX_FULL);
break;
default:
errx(1, "nRF: internal error: update_fifo_status: fifo_tx_entries>3 for nRF %s", nRF->name);
}
switch(nRF->fifo_rx_entries)
{
case 0:
nRF->regs[REG_FIFO_STATUS]|=(1<<FIFO_RX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_FULL);
break;
case 1:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_FULL);
break;
case 2:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_EMPTY);
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_FULL);
break;
case 3:
nRF->regs[REG_FIFO_STATUS]&=~(1<<FIFO_RX_EMPTY);
nRF->regs[REG_FIFO_STATUS]|=(1<<FIFO_RX_FULL);
break;
default:
errx(1, "nRF: internal error: update_fifo_status: fifo_rx_entries>3 for nRF %s", nRF->name);
}
}
static void handle_pin_IRQ(nRF_t * const nRF)
{
bool IRQ=1;
if(!(nRF->regs[REG_CONFIG]&(1<<MASK_RX_DR)) && nRF->regs[REG_STATUS]&(1<<RX_DR))
IRQ=0;
if(!(nRF->regs[REG_CONFIG]&(1<<MASK_TX_DS)) && nRF->regs[REG_STATUS]&(1<<TX_DS))
IRQ=0;
if(!(nRF->regs[REG_CONFIG]&(1<<MASK_MAX_RT)) && nRF->regs[REG_STATUS]&(1<<MAX_RT))
IRQ=0;
nRF->pin_IRQ=IRQ;
LOG(NRF_LOG_DEBUG, "handle_pin_IRQ nRF %s: IRQ set to %u\n", nRF->name, nRF->pin_IRQ);
avr_raise_irq(nRF->irq+NRF24_IRQ_OUT, nRF->pin_IRQ);
}
static void log_to_file(nRF_t * const nRF, const bool is_ack_packet, const uint8_t bytes_payload) //TODO improve this
{
if(!is_ack_packet)
fprintf(nRF->log, "[%10.3fms] [delta %7.3fms] TX %2u bytes\n", CYCLES_TO_MS_FLOAT(nRF->avr, nRF->avr->cycle), CYCLES_TO_MS_FLOAT(nRF->avr, nRF->avr->cycle-nRF->avr_cycle_last_tx), bytes_payload);
else
fprintf(nRF->log, "[%10.3fms] [delta %7.3fms] ACK %2u bytes\n", CYCLES_TO_MS_FLOAT(nRF->avr, nRF->avr->cycle), CYCLES_TO_MS_FLOAT(nRF->avr, nRF->avr->cycle-nRF->avr_cycle_last_tx), bytes_payload);
nRF->avr_cycle_last_tx=nRF->avr->cycle;
}
static void do_TX(nRF_t * const nRF)
{
if(nRF->state!=NRF_TX_MODE || nRF->tx_in_progress || nRF->state_spi!=NRF_SPI_IDLE)
return;
nRF->packet_being_sent.PID=nRF->fifo_tx[0].PID;
nRF->packet_being_sent.regular_packet.nb_bytes_addr=nRF->fifo_tx[0].regular_packet.nb_bytes_addr;
nRF->packet_being_sent.regular_packet.addr=nRF->fifo_tx[0].regular_packet.addr;
nRF->packet_being_sent.nb_bytes=nRF->fifo_tx[0].nb_bytes;
memcpy(nRF->packet_being_sent.data, nRF->fifo_tx[0].data, nRF->fifo_tx[0].nb_bytes);
nRF->packet_being_sent_valid=true;
uint8_t bytes_addr=nRF->packet_being_sent.regular_packet.nb_bytes_addr;
uint8_t bytes_payload=nRF->packet_being_sent.nb_bytes;
uint8_t bytes_crc=(nRF->regs[REG_CONFIG]&(1<<CRCO))?2:1;
uint32_t data_rate=(nRF->regs[REG_RF_SETUP]&(1<<RF_DR_LOW))?250E3:((nRF->regs[REG_RF_SETUP]&(1<<RF_DR_HIGH))?2E6:1E6);
uint32_t time_on_air_us=1.0E6*(8*(1+bytes_addr+bytes_payload+bytes_crc)+9)/data_rate;
LOG(NRF_LOG_VERBOSE, "nRF %s: transmitting %u bytes of payload, time on air is %u µs\n", nRF->name, bytes_payload, time_on_air_us);
if(nRF->log_tx_to_file)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: logging TX to file\n", nRF->name);
log_to_file(nRF, false, bytes_payload);
}
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, time_on_air_us), &cb_tx_finished, nRF);
nRF->tx_in_progress=true;
}
static void do_TX_ack(nRF_t * const nRF)
{
if(nRF->state!=NRF_TX_MODE_FOR_ACK || nRF->tx_in_progress || nRF->state_spi!=NRF_SPI_IDLE)
return;
if(!nRF->last_rx_valid)
errx(1, "nRF: internal error: do_TX_ack: last_rx_valid==false for nRF %s", nRF->name);
if(nRF->regs[REG_FEATURE]&(1<<EN_ACK_PAY) && nRF->fifo_tx_entries)
{
LOG(NRF_LOG_DEBUG, "nRF %s: EN_ACK_PAY enabled, pending ACK-payload will be sent\n", nRF->name);
uint8_t i;
bool found=false;
for(i=0; i<nRF->fifo_tx_entries; i++)
{
if(nRF->fifo_tx[i].ack_packet.pipe==nRF->last_rx.pipe)
{
found=true;
break;
}
}
if(!found)
errx(1, "nRF: internal error: do_TX_ack: not found for nRF %s", nRF->name);
nRF->packet_being_sent.ack_packet.pipe=nRF->fifo_tx[i].ack_packet.pipe;
nRF->packet_being_sent.nb_bytes=nRF->fifo_tx[i].nb_bytes;
memcpy(nRF->packet_being_sent.data, nRF->fifo_tx[i].data, nRF->fifo_tx[i].nb_bytes);
nRF->packet_being_sent_valid=true;
if(i!=2)
memmove(&nRF->fifo_tx[i], &nRF->fifo_tx[i+1], (2-i)*sizeof(packet_tx_t));
nRF->fifo_tx_entries--;
update_fifo_status(nRF);
}
else
{
LOG(NRF_LOG_DEBUG, "nRF %s: EN_ACK_PAY not enabled or no pending ACK-payload, sending empty ACK\n", nRF->name);
nRF->packet_being_sent.ack_packet.pipe=nRF->last_rx.pipe;
nRF->packet_being_sent.nb_bytes=0;
nRF->packet_being_sent_valid=true;
}
uint8_t bytes_addr=(nRF->regs[REG_SETUP_AW]&(0b11<<AW))+2;
uint8_t bytes_payload=nRF->packet_being_sent.nb_bytes;
uint8_t bytes_crc=(nRF->regs[REG_CONFIG]&(1<<CRCO))?2:1;
uint32_t data_rate=(nRF->regs[REG_RF_SETUP]&(1<<RF_DR_LOW))?250E3:((nRF->regs[REG_RF_SETUP]&(1<<RF_DR_HIGH))?2E6:1E6);
uint32_t time_on_air_us=1.0E6*(8*(1+bytes_addr+bytes_payload+bytes_crc)+9)/data_rate;
LOG(NRF_LOG_VERBOSE, "nRF %s: transmitting ACK with %u bytes payload to %s, time on air is %u µs\n", nRF->name, bytes_payload, nRF->rx_send_ack_to->name, time_on_air_us);
if(nRF->log_tx_to_file)
{
LOG(NRF_LOG_VERBOSE, "nRF %s: logging TX ACK to file\n", nRF->name);
log_to_file(nRF, true, bytes_payload);
}
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, time_on_air_us), &cb_tx_finished, nRF);
nRF->tx_in_progress=true;
}
static void handle_tx_ack(nRF_t * const nRF_PTX, nRF_t * const nRF_PRX)
{
LOG(NRF_LOG_DEBUG, "handle_tx_ack: setting PRX to TX-settling, registering timer cb_delay_timer\n");
nRF_PTX->tx_receive_ack_from=nRF_PRX;
nRF_PRX->state=NRF_TX_SETTLING_FOR_ACK;
nRF_PRX->state_next=NRF_TX_MODE_FOR_ACK;
nRF_PRX->rx_send_ack=true;
nRF_PRX->rx_send_ack_to=nRF_PTX;
avr_cycle_timer_register(nRF_PRX->avr, US_TO_CYCLES(nRF_PRX->avr, 130), &cb_delay_timer, nRF_PRX);
}
static void dispatch_sent_packet(nRF_t * const nRF)
{
LOG(NRF_LOG_DEBUG, "dispatch_sent_packet: searching for receiver for packet from %s\n", nRF->name);
uint8_t i;
bool found=false;
for(i=0; i<nb_modules; i++)
{
if(modules[i]==nRF)
continue;
if(
modules[i]->state==NRF_RX_MODE //module is in RX mode
&& modules[i]->regs[REG_RF_CH]==nRF->regs[REG_RF_CH] //module is listening on same channel
&& (modules[i]->regs[REG_RF_SETUP]&0b00101000)==(nRF->regs[REG_RF_SETUP]&0b00101000) //module is using same speed
&& (modules[i]->regs[REG_CONFIG]&(1<<CRCO))==(nRF->regs[REG_CONFIG]&(1<<CRCO)) //module is using same CRC-setting
)
{
uint64_t nb_bytes_addr=(modules[i]->regs[REG_SETUP_AW]&(0b11<<AW))+2;
uint64_t addr_mask=(1UL<<(8*nb_bytes_addr))-1;
uint64_t addr_pipes[5];
addr_pipes[0]=(modules[i]->regs[REG_RX_ADDR_P0])&addr_mask;
addr_pipes[1]=(modules[i]->regs[REG_RX_ADDR_P1])&addr_mask;
addr_pipes[2]=((addr_pipes[1]&0xffffffff00)|modules[i]->regs[REG_RX_ADDR_P2])&addr_mask;
addr_pipes[3]=((addr_pipes[1]&0xffffffff00)|modules[i]->regs[REG_RX_ADDR_P3])&addr_mask;
addr_pipes[4]=((addr_pipes[1]&0xffffffff00)|modules[i]->regs[REG_RX_ADDR_P4])&addr_mask;
addr_pipes[5]=((addr_pipes[1]&0xffffffff00)|modules[i]->regs[REG_RX_ADDR_P5])&addr_mask;
uint8_t pipe;
for(pipe=0; pipe<5; pipe++)
{
//addr match for some pipe and this pipe enabled on RX side?
if(nRF->packet_being_sent.regular_packet.addr==addr_pipes[pipe] && modules[i]->regs[REG_EN_RXADDR]&(1<<pipe))
{
found=true;
break;
}
}
if(found)
{
bool discard_packet=false;
if(modules[i]->last_rx_valid && modules[i]->last_rx.PID==nRF->packet_being_sent.PID && modules[i]->last_rx.nb_bytes==nRF->packet_being_sent.nb_bytes && modules[i]->last_rx.pipe==pipe && !memcmp(modules[i]->last_rx.data, nRF->packet_being_sent.data, nRF->packet_being_sent.nb_bytes))
{
LOG(NRF_LOG_VERBOSE, "nRF %s: dropping duplicate packet with %u bytes payload\n", modules[i]->name, nRF->packet_being_sent.nb_bytes);
discard_packet=true;
}
if(modules[i]->fifo_rx_entries<3)
{
if(!discard_packet)
{
modules[i]->fifo_rx[modules[i]->fifo_rx_entries].PID=nRF->packet_being_sent.PID;
modules[i]->fifo_rx[modules[i]->fifo_rx_entries].pipe=pipe;
modules[i]->fifo_rx[modules[i]->fifo_rx_entries].nb_bytes=nRF->packet_being_sent.nb_bytes;
memcpy(modules[i]->fifo_rx[modules[i]->fifo_rx_entries].data, nRF->packet_being_sent.data, nRF->packet_being_sent.nb_bytes);
modules[i]->fifo_rx_entries++;
modules[i]->last_rx.PID=nRF->packet_being_sent.PID;
modules[i]->last_rx.pipe=pipe;
modules[i]->last_rx.nb_bytes=nRF->packet_being_sent.nb_bytes;
memcpy(modules[i]->last_rx.data, nRF->packet_being_sent.data, nRF->packet_being_sent.nb_bytes);
modules[i]->last_rx_valid=true;
modules[i]->regs[REG_STATUS]|=(1<<RX_DR);
update_fifo_status(modules[i]);
LOG(NRF_LOG_DEBUG, "nRF %s has a new packet, fifo_rx_entries is %u\n", modules[i]->name, modules[i]->fifo_rx_entries);
}
if(modules[i]->regs[REG_EN_AA]&(1<<pipe))
{
if(lost.lose_acks && (rand()%lost.divider_acks)==0)
{
lost.nb_lost_acks++;
LOG(NRF_LOG_VERBOSE, "nRF %s: simulating lost ACK-packet, total %u lost\n", nRF->name, lost.nb_lost_acks);
}
else
handle_tx_ack(nRF, modules[i]);
}
else
LOG(NRF_LOG_WARNING, "WARNING: auto-ACK disabled for pipe %u on %s, not sending ACK\n", pipe, modules[i]->name);
}
else
LOG(NRF_LOG_WARNING, "WARNING: nRF %s has no free RX-slot and will miss a packet send by nRF %s\n", modules[i]->name, nRF->name);
}
}
}
if(!found)
LOG(NRF_LOG_WARNING, "WARNING: no receiver found for packet from nRF %s\n", nRF->name);
}
static void cb_ce(struct avr_irq_t * irq, uint32_t value, void * param) //RX/TX-enable
{
(void)irq;
nRF_t * nRF=(nRF_t*)param;
nRF->pin_CE=value;
update_nRF(nRF);
}
static avr_cycle_count_t cb_tx_finished(avr_t * avr, avr_cycle_count_t when, void * param)
{
(void)avr;
(void)when;
nRF_t * nRF=(nRF_t*)param;
LOG(NRF_LOG_DEBUG, "cb_tx_finished called for nRF %s in state %u\n", nRF->name, nRF->state);
if(!nRF->packet_being_sent_valid)
errx(1, "nRF: internal error: cb_tx_finished: packet_being_sent_valid==false for nRF %s", nRF->name);
nRF->tx_in_progress=false;
if(nRF->rx_send_ack) //is this an ACK-packet from a PRX?
{
nRF->tx_finished=true;
LOG(NRF_LOG_DEBUG, "cb_tx_finished: this is an ACK-packet from PRX\n");
if(nRF->rx_send_ack_to->ard_has_elapsed)
{
LOG(NRF_LOG_WARNING, "WARNING: nRF %s timed-out while receiving ACK from %s - did you set ARD correctly?\n", nRF->rx_send_ack_to->name, nRF->name);
nRF->rx_send_ack=false;
update_nRF(nRF->rx_send_ack_to);
return 0;
}
if(nRF->rx_send_ack_to->state!=NRF_RX_MODE_FOR_ACK)
{
LOG(NRF_LOG_WARNING, "WARNING: nRF %s is not in RX-mode (but mode %u) and will miss the ACK from %s - did you set ARD correctly?\n", nRF->rx_send_ack_to->name, nRF->rx_send_ack_to->state, nRF->name);
return 0;
}
nRF->rx_send_ack_to->tx_ack_received=true;
nRF->rx_send_ack_to->regs[REG_STATUS]&=~(1<<TX_FULL);
nRF->rx_send_ack_to->regs[REG_STATUS]|=(1<<TX_DS);
update_fifo_status(nRF);
handle_pin_IRQ(nRF->rx_send_ack_to);
nRF->regs[REG_STATUS]|=(1<<RX_DR);
handle_pin_IRQ(nRF);
stats.nb_acks++;
if(nRF->packet_being_sent.nb_bytes)
{
LOG(NRF_LOG_DEBUG, "ACK has payload\n");
if(nRF->rx_send_ack_to->fifo_rx_entries==3) //TODO confirm with datasheet how to behave
LOG(NRF_LOG_WARNING, "WARNING: nRF %s: no free space in RX fifo for ACK-packet payload, data is lost\n", nRF->rx_send_ack_to->name);
else
{
nRF->rx_send_ack_to->fifo_rx[nRF->rx_send_ack_to->fifo_rx_entries].pipe=nRF->packet_being_sent.ack_packet.pipe;
nRF->rx_send_ack_to->fifo_rx[nRF->rx_send_ack_to->fifo_rx_entries].nb_bytes=nRF->packet_being_sent.nb_bytes;
memcpy(nRF->rx_send_ack_to->fifo_rx[nRF->rx_send_ack_to->fifo_rx_entries].data, nRF->packet_being_sent.data, nRF->packet_being_sent.nb_bytes);
nRF->rx_send_ack_to->fifo_rx_entries++;
update_fifo_status(nRF->rx_send_ack_to);
nRF->rx_send_ack_to->regs[REG_STATUS]|=(1<<TX_DS)|(1<<RX_DR);
handle_pin_IRQ(nRF->rx_send_ack_to);
}
}
nRF->packet_being_sent_valid=false;
LOG(NRF_LOG_DEBUG, "cb_tx_finished: ACK-received, removing packet from TX-fifo\n");
memmove(&nRF->rx_send_ack_to->fifo_tx[0], &nRF->rx_send_ack_to->fifo_tx[1], 2*sizeof(packet_tx_t));
nRF->rx_send_ack_to->fifo_tx_entries--;
nRF->rx_send_ack_to->regs[REG_STATUS]|=(1<<TX_DS);
update_fifo_status(nRF->rx_send_ack_to);
handle_pin_IRQ(nRF->rx_send_ack_to);
stats.nb_packets++;
}
else //no, this is a regular packet from a PTX
{
LOG(NRF_LOG_DEBUG, "cb_tx_finished: this is a regular packet\n");
if(lost.lose_packets && (rand()%lost.divider_packets)==0)
{
lost.nb_lost_packets++;
LOG(NRF_LOG_VERBOSE, "nRF %s: simulating lost packet, total %u lost\n", nRF->name, lost.nb_lost_packets);
}
else
dispatch_sent_packet(nRF);
nRF->packet_being_sent_valid=false;
//signal to state machine
nRF->tx_finished=true;
nRF->tx_in_progress=false;
if(nRF->regs[REG_SETUP_RETR]&(0b1111<<ARC)) //is auto-retransmit enabled? -> wait for ACK
{
nRF->tx_wait_for_ack=true;
nRF->tx_ack_received=false;
nRF->rx_ack_timeout=false;
nRF->ard_has_elapsed=false;
uint16_t auto_retransmit_delay_us=((nRF->regs[REG_SETUP_RETR]>>ARD)+1)*250;
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, auto_retransmit_delay_us), &cb_ard_elapsed, nRF);
LOG(NRF_LOG_DEBUG, "cb_tx_finished: we need to wait for ACK, setting variables, registering timer cb_ard_elapsed for ARD %u µs\n", auto_retransmit_delay_us);
}
else //we are done with this packet
{
LOG(NRF_LOG_DEBUG, "cb_tx_finished: we are done with this packet, removing from TX-fifo\n");
//remove sent entry from FIFO
memmove(&nRF->fifo_tx[0], &nRF->fifo_tx[1], 2*sizeof(packet_tx_t));
nRF->fifo_tx_entries--;
nRF->regs[REG_STATUS]|=(1<<TX_DS);
update_fifo_status(nRF);
handle_pin_IRQ(nRF);
stats.nb_packets++;
}
}
LOG(NRF_LOG_DEBUG, "cb_tx_finished: calling update_nRF for %s\n", nRF->name);
update_nRF(nRF);
LOG(NRF_LOG_DEBUG, "end of cb_tx_finished\n");
return 0; //stop timer
}
static avr_cycle_count_t cb_rx_ack_timeout(avr_t * avr, avr_cycle_count_t when, void * param)
{
(void)avr;
(void)when;
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout fired\n");
nRF_t * nRF=(nRF_t*)param;
if(nRF->tx_receive_ack_from)
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout: tx_in_progress for %s is %u\n", nRF->tx_receive_ack_from->name, nRF->tx_receive_ack_from->tx_in_progress);
else
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout: nRF->tx_receive_ack_from is NULL for nRF %s\n", nRF->name);
if(nRF->ard_has_elapsed)
{
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout: ard has elapsed is true, setting rx_ack_timeout\n");
nRF->rx_ack_timeout=true;
}
else if(!nRF->tx_receive_ack_from || !nRF->tx_receive_ack_from->tx_in_progress)
{
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout: nRF->rx_ack_timeout set to true\n");
nRF->rx_ack_timeout=true;
}
else
LOG(NRF_LOG_DEBUG, "cb_rx_ack_timeout: tx is in progress, not setting timeout\n");
//two times in case ARD is too small and ARC has been reached to allow to go into Standby2 and then set MAX_RT and go into Standby1 - a bit ugly but yeah...
update_nRF(nRF);
update_nRF(nRF);
return 0;
}
static avr_cycle_count_t cb_ard_elapsed(avr_t * avr, avr_cycle_count_t when, void * param)
{
(void)avr;
(void)when;
LOG(NRF_LOG_DEBUG, "cb_ard_elapsed fired\n");
nRF_t * nRF=(nRF_t*)param;
nRF->ard_has_elapsed=true;
if(nRF->tx_receive_ack_from && nRF->tx_receive_ack_from->tx_in_progress)
{
nRF->tx_receive_ack_from->tx_in_progress=false;
nRF->rx_ack_timeout=true;
LOG(NRF_LOG_WARNING, "WARNING: ARD for nRF %s elapsed while nRF %s was still transmitting, ACK is lost\n", nRF->name, nRF->tx_receive_ack_from->name);
}
update_nRF(nRF);
return 0;
}
static avr_cycle_count_t cb_delay_timer(avr_t * avr, avr_cycle_count_t when, void * param)
{
(void)avr;
(void)when;
nRF_t * nRF=(nRF_t*)param;
LOG(NRF_LOG_DEBUG, "cb_delay_timer fired for %s, old state was %u, new is %u\n", nRF->name, nRF->state, nRF->state_next);
nRF->state=nRF->state_next;
if(nRF->tx_wait_for_ack) //PTX waiting for ack
{
LOG(NRF_LOG_DEBUG, "cb_delay_timer: registering timer cb_rx_ack_timeout 250µs for %s\n", nRF->name);
avr_cycle_timer_register(nRF->avr, US_TO_CYCLES(nRF->avr, 250), &cb_rx_ack_timeout, nRF); //see footnote datasheet p. 59
}
update_nRF(nRF);
return 0; //stop timer
}
////////////////////////////////////////////////////////////////////////
//public functions
void nRF_global_init(void)
{
lost.lose_packets=false;
lost.lose_acks=false;
lost.nb_lost_packets=0;
lost.nb_lost_acks=0;
stats.nb_packets=0;
stats.nb_acks=0;
}
void nRF_stop_on_error(const bool yesno)
{
stop_on_error=yesno;
}
void nRF_set_log_level(const nRF_log_level_t level)
{
loglevel=level;
}
void nRF_log_to_file(nRF_t * const nRF, char const * const filename)
{
nRF->log=fopen(filename, "w");
if(nRF->log==NULL)
err(1, "nRF %s: creating logfile %s failed", nRF->name, filename);
nRF->log_tx_to_file=true;
fprintf(nRF->log, "LOGFILE FOR nRF %s\n", nRF->name);
printf("nRF %s: logging enabled\n", nRF->name);
}
void nRF_set_lost_packets(const uint32_t lost_packets, const uint32_t lost_acks)
{
if(lost_packets)
{
lost.lose_packets=true;
lost.divider_packets=lost_packets;
printf("nRF: simulating 1 lost packet for %u packets sent\n", lost_packets);
}
if(lost_acks)
{
lost.lose_acks=true;
lost.divider_acks=lost_acks;
printf("nRF: simulating 1 lost ACK-packet for %u ACK-packets sent\n", lost_acks);
}
}
nRF_t * make_new_nRF(void)
{
if(nb_modules==NB_NRF_MAX)
errx(1, "make_new_nRF: no more space in modules[], increase NB_NRF_MAX");
nRF_t * ptr=malloc(sizeof(nRF_t));
modules[nb_modules++]=ptr;
return ptr;
}
void nRF_init(struct avr_t * avr, nRF_t * const nRF, char const * const name)
{
nRF->avr=avr;
nRF->irq=avr_alloc_irq(&avr->irq_pool, 0, NRF24_IRQ_COUNT, irq_names);
avr_irq_register_notify(nRF->irq+NRF24_CE_IN, &cb_ce, nRF);
strncpy(nRF->name, name, NRF_SZ_NAME);
nRF->pin_CSN=1;
nRF->pin_CE=0;
nRF->pin_IRQ=1;
//default values taken from datasheet
nRF->regs[REG_CONFIG]=(1<<EN_CRC);
nRF->regs[REG_EN_AA]=(1<<ENAA_P0)|(1<<ENAA_P1)|(1<<ENAA_P2)|(1<<ENAA_P3)|(1<<ENAA_P4)|(1<<ENAA_P5);
nRF->regs[REG_EN_RXADDR]=(1<<ERX_P0)|(1<<ERX_P1);
nRF->regs[REG_SETUP_AW]=(0b11<<AW);
nRF->regs[REG_SETUP_RETR]=(0b11<<ARC);
nRF->regs[REG_RF_CH]=2;
nRF->regs[REG_RF_SETUP]=(1<<RF_DR_HIGH)|(0b11<<RF_PWR);
nRF->regs[REG_STATUS]=(0b111<<RX_P_NO);
nRF->regs[REG_OBSERVE_TX]=0;
nRF->regs[REG_RPD]=0;
nRF->regs[REG_RX_ADDR_P0]=0xe7e7e7e7e7;
nRF->regs[REG_RX_ADDR_P1]=0xc2c2c2c2c2;
nRF->regs[REG_RX_ADDR_P2]=0xc3;
nRF->regs[REG_RX_ADDR_P3]=0xc4;
nRF->regs[REG_RX_ADDR_P4]=0xc5;
nRF->regs[REG_RX_ADDR_P5]=0xc6;
nRF->regs[REG_TX_ADDR]=0xe7e7e7e7e7;
nRF->regs[REG_RX_PW_P0]=0;
nRF->regs[REG_RX_PW_P1]=0;
nRF->regs[REG_RX_PW_P2]=0;
nRF->regs[REG_RX_PW_P3]=0;
nRF->regs[REG_RX_PW_P4]=0;
nRF->regs[REG_RX_PW_P5]=0;
nRF->regs[REG_FIFO_STATUS]=(1<<FIFO_TX_EMPTY)|(1<<FIFO_RX_EMPTY);
nRF->regs[REG_DYNPD]=0; //TODO: not checked by code
nRF->regs[REG_FEATURE]=0; //TODO: only partially checked by code
nRF->state=NRF_POWER_DOWN;
nRF->PID=0;
nRF->fifo_rx_entries=0;
nRF->fifo_tx_entries=0;
nRF->tx_in_progress=false;
nRF->tx_finished=false;
nRF->tx_wait_for_ack=false;
nRF->tx_ack_received=false;
nRF->rx_send_ack=false;
nRF->rx_send_ack_to=NULL;
nRF->tx_receive_ack_from=NULL;
nRF->last_rx_valid=false;
nRF->packet_being_sent_valid=false;
nRF->log=NULL;
nRF->log_tx_to_file=false;
nRF->avr_cycle_last_tx=0;
}
void nRF_connect(nRF_t * const nRF, avr_irq_t * pin_ce_irq, avr_irq_t * pin_irq_irq)
{
avr_connect_irq(pin_ce_irq, nRF->irq+NRF24_CE_IN);
avr_connect_irq(nRF->irq+NRF24_IRQ_OUT, pin_irq_irq);
avr_raise_irq(nRF->irq+NRF24_IRQ_OUT, 1);
}