forked from nRF24/RF24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24.cpp
1840 lines (1491 loc) · 57.2 KB
/
RF24.cpp
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) 2011 J. Coliz <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "nRF24L01.h"
#include "RF24_config.h"
#include "RF24.h"
/****************************************************************************/
void RF24::csn(bool mode)
{
#if defined(RF24_TINY)
if (ce_pin != csn_pin) {
digitalWrite(csn_pin, mode);
}
else {
if (mode == HIGH) {
PORTB |= (1<<PINB2); // SCK->CSN HIGH
delayMicroseconds(RF24_CSN_SETTLE_HIGH_DELAY); // allow csn to settle.
}
else {
PORTB &= ~(1<<PINB2); // SCK->CSN LOW
delayMicroseconds(RF24_CSN_SETTLE_LOW_DELAY); // allow csn to settle
}
}
// Return, CSN toggle complete
return;
#elif defined(ARDUINO) && !defined(RF24_SPI_TRANSACTIONS)
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#if !defined(SOFTSPI)
// applies to SPI_UART and inherent hardware SPI
#if defined (RF24_SPI_PTR)
_spi->setBitOrder(MSBFIRST);
_spi->setDataMode(SPI_MODE0);
#if !defined(F_CPU) || F_CPU < 20000000
_spi->setClockDivider(SPI_CLOCK_DIV2);
#elif F_CPU < 40000000
_spi->setClockDivider(SPI_CLOCK_DIV4);
#elif F_CPU < 80000000
_spi->setClockDivider(SPI_CLOCK_DIV8);
#elif F_CPU < 160000000
_spi->setClockDivider(SPI_CLOCK_DIV16);
#elif F_CPU < 320000000
_spi->setClockDivider(SPI_CLOCK_DIV32);
#elif F_CPU < 640000000
_spi->setClockDivider(SPI_CLOCK_DIV64);
#elif F_CPU < 1280000000
_spi->setClockDivider(SPI_CLOCK_DIV128);
#else // F_CPU >= 1280000000
#error "Unsupported CPU frequency. Please set correct SPI divider."
#endif // F_CPU to SPI_CLOCK_DIV translation
#else // !defined(RF24_SPI_PTR)
_SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0);
#if !defined(F_CPU) || F_CPU < 20000000
_SPI.setClockDivider(SPI_CLOCK_DIV2);
#elif F_CPU < 40000000
_SPI.setClockDivider(SPI_CLOCK_DIV4);
#elif F_CPU < 80000000
_SPI.setClockDivider(SPI_CLOCK_DIV8);
#elif F_CPU < 160000000
_SPI.setClockDivider(SPI_CLOCK_DIV16);
#elif F_CPU < 320000000
_SPI.setClockDivider(SPI_CLOCK_DIV32);
#elif F_CPU < 640000000
_SPI.setClockDivider(SPI_CLOCK_DIV64);
#elif F_CPU < 1280000000
_SPI.setClockDivider(SPI_CLOCK_DIV128);
#else // F_CPU >= 1280000000
#error "Unsupported CPU frequency. Please set correct SPI divider."
#endif // F_CPU to SPI_CLOCK_DIV translation
#endif // !defined(RF24_SPI_PTR)
#endif // !defined(SOFTSPI)
#elif defined (RF24_RPi)
if(!mode)
_SPI.chipSelect(csn_pin);
#endif // defined(RF24_RPi)
#if !defined(RF24_LINUX)
digitalWrite(csn_pin, mode);
delayMicroseconds(csDelay);
#endif // !defined(RF24_LINUX)
}
/****************************************************************************/
void RF24::ce(bool level)
{
#if defined USE_MCP23XXX_AS_CE
USE_MCP23XXX_AS_CE.digitalWrite(ce_pin, level);
#else // !defined(USE_MCP23XXX_AS_CE)
if (ce_pin != csn_pin)
{
digitalWrite(ce_pin, level);
}
#endif // !defined(USE_MCP23XXX_AS_CE)
}
/****************************************************************************/
inline void RF24::beginTransaction()
{
#if defined (RF24_SPI_TRANSACTIONS)
#if defined (RF24_SPI_PTR)
_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
#else // !defined(RF24_SPI_PTR)
_SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
#endif // !defined(RF24_SPI_PTR)
#endif // defined (RF24_SPI_TRANSACTIONS)
csn(LOW);
}
/****************************************************************************/
inline void RF24::endTransaction()
{
csn(HIGH);
#if defined (RF24_SPI_TRANSACTIONS)
#if defined (RF24_SPI_PTR)
_spi->endTransaction();
#else // !defined(RF24_SPI_PTR)
_SPI.endTransaction();
#endif // !defined(RF24_SPI_PTR)
#endif // defined (RF24_SPI_TRANSACTIONS)
}
/****************************************************************************/
void RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
{
#if defined(RF24_LINUX)
beginTransaction(); //configures the spi settings for RPi, locks mutex and setting csn low
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer
*ptx++ = (R_REGISTER | reg);
while (len--){ *ptx++ = RF24_NOP; } // Dummy operation, just for reading
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
status = *prx++; // status is 1st byte of receive buffer
// decrement before to skip status byte
while (--size) { *buf++ = *prx++; }
endTransaction(); // unlocks mutex and setting csn high
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(R_REGISTER | reg);
while (len--) { *buf++ = _spi->transfer(0xFF); }
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(R_REGISTER | reg);
while (len--) { *buf++ = _SPI.transfer(0xFF); }
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
}
/****************************************************************************/
uint8_t RF24::read_register(uint8_t reg)
{
uint8_t result;
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
*ptx++ = (R_REGISTER | reg);
*ptx++ = RF24_NOP ; // Dummy operation, just for reading
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, 2);
status = *prx; // status is 1st byte of receive buffer
result = *++prx; // result is 2nd byte of receive buffer
endTransaction();
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(R_REGISTER | reg);
result = _spi->transfer(0xff);
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(R_REGISTER | reg);
result = _SPI.transfer(0xff);
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
return result;
}
/****************************************************************************/
void RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
{
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size = len + 1; // Add register value to transmit buffer
*ptx++ = (W_REGISTER | (REGISTER_MASK & reg));
while (len--)
*ptx++ = *buf++;
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer
endTransaction();
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(W_REGISTER | reg);
while (len--) { _spi->transfer(*buf++); }
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(W_REGISTER | reg);
while (len--) { _SPI.transfer(*buf++); }
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
}
/****************************************************************************/
void RF24::write_register(uint8_t reg, uint8_t value, bool is_cmd_only)
{
if (is_cmd_only) {
if (reg != RF24_NOP) { // don't print the get_status() operation
IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x)\r\n"), reg));
}
beginTransaction();
#if defined (RF24_LINUX)
status = _SPI.transfer(W_REGISTER | reg);
#else
#if defined (RF24_SPI_PTR)
status = _spi->transfer(W_REGISTER | reg);
#else // !defined (RF24_SPI_PTR)
status = _SPI.transfer(W_REGISTER | reg);
#endif // !defined (RF24_SPI_PTR)
#endif // !defined(RF24_LINUX)
endTransaction();
}
else {
IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x,%02x)\r\n"), reg, value));
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
*ptx++ = (W_REGISTER | reg);
*ptx = value;
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, 2);
status = *prx++; // status is 1st byte of receive buffer
endTransaction();
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(W_REGISTER | reg);
_spi->transfer(value);
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(W_REGISTER | reg);
_SPI.transfer(value);
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
}
}
/****************************************************************************/
void RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t writeType)
{
const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
uint8_t blank_len = !data_len ? 1 : 0;
if (!dynamic_payloads_enabled) {
data_len = rf24_min(data_len, payload_size);
blank_len = payload_size - data_len;
}
else {
data_len = rf24_min(data_len, 32);
}
//printf("[Writing %u bytes %u blanks]",data_len,blank_len);
IF_SERIAL_DEBUG(printf("[Writing %u bytes %u blanks]\n", data_len, blank_len); );
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size;
size = data_len + blank_len + 1 ; // Add register value to transmit buffer
*ptx++ = writeType;
while (data_len--) { *ptx++ = *current++; }
while (blank_len--) { *ptx++ = 0; }
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer
endTransaction();
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(writeType);
while (data_len--) { _spi->transfer(*current++); }
while (blank_len--) { _spi->transfer(0); }
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(writeType);
while (data_len--) { _SPI.transfer(*current++); }
while (blank_len--) { _SPI.transfer(0); }
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
}
/****************************************************************************/
void RF24::read_payload(void* buf, uint8_t data_len)
{
uint8_t* current = reinterpret_cast<uint8_t*>(buf);
uint8_t blank_len = 0;
if (!dynamic_payloads_enabled) {
data_len = rf24_min(data_len, payload_size);
blank_len = payload_size - data_len;
}
else {
data_len = rf24_min(data_len, 32);
}
//printf("[Reading %u bytes %u blanks]",data_len,blank_len);
IF_SERIAL_DEBUG(printf("[Reading %u bytes %u blanks]\n", data_len, blank_len); );
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff;
uint8_t size;
size = data_len + blank_len + 1; // Add register value to transmit buffer
*ptx++ = R_RX_PAYLOAD;
while(--size) { *ptx++ = RF24_NOP; }
size = data_len + blank_len + 1; // Size has been lost during while, re affect
_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size);
status = *prx++; // 1st byte is status
if (data_len > 0) {
while (--data_len) // Decrement before to skip 1st status byte
*current++ = *prx++;
*current = *prx;
}
endTransaction();
#else // !defined(RF24_LINUX)
beginTransaction();
#if defined (RF24_SPI_PTR)
status = _spi->transfer(R_RX_PAYLOAD);
while (data_len--) { *current++ = _spi->transfer(0xFF); }
while (blank_len--) { _spi->transfer(0xff); }
#else // !defined(RF24_SPI_PTR)
status = _SPI.transfer(R_RX_PAYLOAD);
while (data_len--) { *current++ = _SPI.transfer(0xFF); }
while (blank_len--) { _SPI.transfer(0xff); }
#endif // !defined(RF24_SPI_PTR)
endTransaction();
#endif // !defined(RF24_LINUX)
}
/****************************************************************************/
uint8_t RF24::flush_rx(void)
{
write_register(FLUSH_RX, RF24_NOP, true);
return status;
}
/****************************************************************************/
uint8_t RF24::flush_tx(void)
{
write_register(FLUSH_TX, RF24_NOP, true);
return status;
}
/****************************************************************************/
uint8_t RF24::get_status(void)
{
write_register(RF24_NOP, RF24_NOP, true);
return status;
}
/****************************************************************************/
#if !defined(MINIMAL)
void RF24::print_status(uint8_t _status)
{
printf_P(PSTR("STATUS\t\t= 0x%02x RX_DR=%x TX_DS=%x MAX_RT=%x RX_P_NO=%x TX_FULL=%x\r\n"), _status, (_status & _BV(RX_DR)) ? 1 : 0,
(_status & _BV(TX_DS)) ? 1 : 0, (_status & _BV(MAX_RT)) ? 1 : 0, ((_status >> RX_P_NO) & 0x07), (_status & _BV(TX_FULL)) ? 1 : 0);
}
/****************************************************************************/
void RF24::print_observe_tx(uint8_t value)
{
printf_P(PSTR("OBSERVE_TX=%02x: POLS_CNT=%x ARC_CNT=%x\r\n"), value, (value >> PLOS_CNT) & 0x0F, (value >> ARC_CNT) & 0x0F);
}
/****************************************************************************/
void RF24::print_byte_register(const char* name, uint8_t reg, uint8_t qty)
{
//char extra_tab = strlen_P(name) < 8 ? '\t' : 0;
//printf_P(PSTR(PRIPSTR"\t%c ="),name,extra_tab);
#if defined(RF24_LINUX)
printf("%s\t=", name);
#else // !defined(RF24_LINUX)
printf_P(PSTR(PRIPSTR"\t="),name);
#endif // !defined(RF24_LINUX)
while (qty--) {
printf_P(PSTR(" 0x%02x"), read_register(reg++));
}
printf_P(PSTR("\r\n"));
}
/****************************************************************************/
void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)
{
#if defined(RF24_LINUX)
printf("%s\t=", name);
#else // !defined(RF24_LINUX)
printf_P(PSTR(PRIPSTR"\t="), name);
#endif // !defined(RF24_LINUX)
while (qty--) {
uint8_t buffer[addr_width];
read_register(reg++ & REGISTER_MASK, buffer, sizeof(buffer));
printf_P(PSTR(" 0x"));
uint8_t* bufptr = buffer + sizeof(buffer);
while (--bufptr >= buffer) {
printf_P(PSTR("%02x"), *bufptr);
}
}
printf_P(PSTR("\r\n"));
}
#endif // !defined(MINIMAL)
/****************************************************************************/
RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed)
:ce_pin(_cepin), csn_pin(_cspin), spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
csDelay(5)
{
_init_obj();
}
/****************************************************************************/
RF24::RF24(uint32_t _spi_speed)
:ce_pin(0xFFFF), csn_pin(0xFFFF), spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
csDelay(5)
{
_init_obj();
}
/****************************************************************************/
void RF24::_init_obj()
{
// Use a pointer on the Arduino platform
#if defined (RF24_SPI_PTR)
_spi = &SPI;
#endif // defined (RF24_SPI_PTR)
pipe0_reading_address[0] = 0;
if(spi_speed <= 35000){ //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
spi_speed = RF24_SPI_SPEED;
}
}
/****************************************************************************/
void RF24::setChannel(uint8_t channel)
{
const uint8_t max_channel = 125;
write_register(RF_CH, rf24_min(channel, max_channel));
}
uint8_t RF24::getChannel()
{
return read_register(RF_CH);
}
/****************************************************************************/
void RF24::setPayloadSize(uint8_t size)
{
// payload size must be in range [1, 32]
payload_size = rf24_max(1, rf24_min(32, size));
// write static payload size setting for all pipes
for (uint8_t i = 0; i < 6; ++i)
write_register(RX_PW_P0 + i, payload_size);
}
/****************************************************************************/
uint8_t RF24::getPayloadSize(void)
{
return payload_size;
}
/****************************************************************************/
#if !defined(MINIMAL)
static const PROGMEM char rf24_datarate_e_str_0[] = "= 1 MBPS";
static const PROGMEM char rf24_datarate_e_str_1[] = "= 2 MBPS";
static const PROGMEM char rf24_datarate_e_str_2[] = "= 250 KBPS";
static const PROGMEM char * const rf24_datarate_e_str_P[] = {
rf24_datarate_e_str_0,
rf24_datarate_e_str_1,
rf24_datarate_e_str_2,
};
static const PROGMEM char rf24_model_e_str_0[] = "nRF24L01";
static const PROGMEM char rf24_model_e_str_1[] = "nRF24L01+";
static const PROGMEM char * const rf24_model_e_str_P[] = {
rf24_model_e_str_0,
rf24_model_e_str_1,
};
static const PROGMEM char rf24_crclength_e_str_0[] = "= Disabled";
static const PROGMEM char rf24_crclength_e_str_1[] = "= 8 bits";
static const PROGMEM char rf24_crclength_e_str_2[] = "= 16 bits" ;
static const PROGMEM char * const rf24_crclength_e_str_P[] = {
rf24_crclength_e_str_0,
rf24_crclength_e_str_1,
rf24_crclength_e_str_2,
};
static const PROGMEM char rf24_pa_dbm_e_str_0[] = "= PA_MIN";
static const PROGMEM char rf24_pa_dbm_e_str_1[] = "= PA_LOW";
static const PROGMEM char rf24_pa_dbm_e_str_2[] = "= PA_HIGH";
static const PROGMEM char rf24_pa_dbm_e_str_3[] = "= PA_MAX";
static const PROGMEM char * const rf24_pa_dbm_e_str_P[] = {
rf24_pa_dbm_e_str_0,
rf24_pa_dbm_e_str_1,
rf24_pa_dbm_e_str_2,
rf24_pa_dbm_e_str_3,
};
#if defined(RF24_LINUX)
static const char rf24_csn_e_str_0[] = "CE0 (PI Hardware Driven)";
static const char rf24_csn_e_str_1[] = "CE1 (PI Hardware Driven)";
static const char rf24_csn_e_str_2[] = "CE2 (PI Hardware Driven)";
static const char rf24_csn_e_str_3[] = "Custom GPIO Software Driven";
static const char * const rf24_csn_e_str_P[] = {
rf24_csn_e_str_0,
rf24_csn_e_str_1,
rf24_csn_e_str_2,
rf24_csn_e_str_3,
};
#endif // defined(RF24_LINUX)
static const PROGMEM char rf24_feature_e_str_on[] = "= Enabled";
static const PROGMEM char rf24_feature_e_str_allowed[] = "= Allowed";
static const PROGMEM char rf24_feature_e_str_open[] = " open ";
static const PROGMEM char rf24_feature_e_str_closed[] = "closed";
static const PROGMEM char * const rf24_feature_e_str_P[] = {
rf24_crclength_e_str_0,
rf24_feature_e_str_on,
rf24_feature_e_str_allowed,
rf24_feature_e_str_closed,
rf24_feature_e_str_open
};
void RF24::printDetails(void)
{
#if defined(RF24_LINUX)
printf("================ SPI Configuration ================\n" );
uint8_t bus_ce = csn_pin % 10;
uint8_t bus_numb = (csn_pin - bus_ce) / 10;
printf("CSN Pin\t\t= /dev/spidev%d.%d\n", bus_numb, bus_ce);
printf("CE Pin\t\t= Custom GPIO%d\n", ce_pin);
#endif
printf_P(PSTR("SPI Speedz\t= %d Mhz\n"),(uint8_t)(spi_speed/1000000)); //Print the SPI speed on non-Linux devices
#if defined(RF24_LINUX)
printf("================ NRF Configuration ================\n");
#endif // defined(RF24_LINUX)
print_status(get_status());
print_address_register(PSTR("RX_ADDR_P0-1"), RX_ADDR_P0, 2);
print_byte_register(PSTR("RX_ADDR_P2-5"), RX_ADDR_P2, 4);
print_address_register(PSTR("TX_ADDR\t"), TX_ADDR);
print_byte_register(PSTR("RX_PW_P0-6"), RX_PW_P0, 6);
print_byte_register(PSTR("EN_AA\t"), EN_AA);
print_byte_register(PSTR("EN_RXADDR"), EN_RXADDR);
print_byte_register(PSTR("RF_CH\t"), RF_CH);
print_byte_register(PSTR("RF_SETUP"), RF_SETUP);
print_byte_register(PSTR("CONFIG\t"), NRF_CONFIG);
print_byte_register(PSTR("DYNPD/FEATURE"), DYNPD, 2);
printf_P(PSTR("Data Rate\t"
PRIPSTR
"\r\n"),(char*)pgm_read_ptr(&rf24_datarate_e_str_P[getDataRate()]));
printf_P(PSTR("Model\t\t= "
PRIPSTR
"\r\n"),(char*)pgm_read_ptr(&rf24_model_e_str_P[isPVariant()]));
printf_P(PSTR("CRC Length\t"
PRIPSTR
"\r\n"),(char*)pgm_read_ptr(&rf24_crclength_e_str_P[getCRCLength()]));
printf_P(PSTR("PA Power\t"
PRIPSTR
"\r\n"),(char*)pgm_read_ptr(&rf24_pa_dbm_e_str_P[getPALevel()]));
printf_P(PSTR("ARC\t\t= %d\r\n"), getARC());
}
void RF24::printPrettyDetails(void) {
#if defined(RF24_LINUX)
printf("================ SPI Configuration ================\n");
uint8_t bus_ce = csn_pin % 10;
uint8_t bus_numb = (csn_pin - bus_ce) / 10;
printf("CSN Pin\t\t\t= /dev/spidev%d.%d\n", bus_numb, bus_ce);
printf("CE Pin\t\t\t= Custom GPIO%d\n", ce_pin);
#endif
printf_P(PSTR("SPI Frequency\t\t= %d Mhz\n"), (uint8_t)(spi_speed / 1000000)); //Print the SPI speed on non-Linux devices
#if defined(RF24_LINUX)
printf("================ NRF Configuration ================\n");
#endif // defined(RF24_LINUX)
uint8_t channel = getChannel();
uint16_t frequency = (uint16_t)channel + 2400;
printf_P(PSTR("Channel\t\t\t= %u (~ %u MHz)\r\n"), channel, frequency);
printf_P(PSTR("RF Data Rate\t\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_datarate_e_str_P[getDataRate()]));
printf_P(PSTR("RF Power Amplifier\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_pa_dbm_e_str_P[getPALevel()]));
printf_P(PSTR("RF Low Noise Amplifier\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_feature_e_str_P[(bool)(read_register(RF_SETUP) & 1) * 1]));
printf_P(PSTR("CRC Length\t\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_crclength_e_str_P[getCRCLength()]));
printf_P(PSTR("Address Length\t\t= %d bytes\r\n"), (read_register(SETUP_AW) & 3) + 2);
printf_P(PSTR("Static Payload Length\t= %d bytes\r\n"), getPayloadSize());
uint8_t setupRetry = read_register(SETUP_RETR);
printf_P(PSTR("Auto Retry Delay\t= %d microseconds\r\n"), (uint16_t)(setupRetry >> ARD) * 250 + 250);
printf_P(PSTR("Auto Retry Attempts\t= %d maximum\r\n"), setupRetry & 0x0F);
uint8_t observeTx = read_register(OBSERVE_TX);
printf_P(PSTR("Packets lost on\n current channel\t= %d\r\n"), observeTx >> 4);
printf_P(PSTR("Retry attempts made for\n last transmission\t= %d\r\n"), observeTx & 0x0F);
uint8_t features = read_register(FEATURE);
printf_P(PSTR("Multicast\t\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_feature_e_str_P[(bool)(features & _BV(EN_DYN_ACK)) * 2]));
printf_P(PSTR("Custom ACK Payload\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_feature_e_str_P[(bool)(features & _BV(EN_ACK_PAY)) * 1]));
uint8_t dynPl = read_register(DYNPD);
printf_P(PSTR("Dynamic Payloads\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_feature_e_str_P[(dynPl && (features &_BV(EN_DPL))) * 1]));
uint8_t autoAck = read_register(EN_AA);
if (autoAck == 0x3F || autoAck == 0) {
// all pipes have the same configuration about auto-ack feature
printf_P(PSTR("Auto Acknowledgment\t"
PRIPSTR
"\r\n"), (char*)pgm_read_ptr(&rf24_feature_e_str_P[(bool)(autoAck) * 1]));
} else {
// representation per pipe
printf_P(PSTR("Auto Acknowledgment\t= 0b%c%c%c%c%c%c\r\n"),
(char)((bool)(autoAck & _BV(ENAA_P5)) + 48),
(char)((bool)(autoAck & _BV(ENAA_P4)) + 48),
(char)((bool)(autoAck & _BV(ENAA_P3)) + 48),
(char)((bool)(autoAck & _BV(ENAA_P2)) + 48),
(char)((bool)(autoAck & _BV(ENAA_P1)) + 48),
(char)((bool)(autoAck & _BV(ENAA_P0)) + 48));
}
config_reg = read_register(NRF_CONFIG);
printf_P(PSTR("Primary Mode\t\t= %cX\r\n"), config_reg & _BV(PRIM_RX) ? 'R' : 'T');
print_address_register(PSTR("TX address\t"), TX_ADDR);
uint8_t openPipes = read_register(EN_RXADDR);
for (uint8_t i = 0; i < 6; ++i) {
bool isOpen = openPipes & _BV(i);
printf_P(PSTR("pipe %u ("
PRIPSTR
") bound"), i, (char*)pgm_read_ptr(&rf24_feature_e_str_P[isOpen + 3]));
if (i < 2) {
print_address_register(PSTR(""), RX_ADDR_P0 + i);
}
else {
print_byte_register(PSTR(""), RX_ADDR_P0 + i);
}
}
}
#endif // !defined(MINIMAL)
/****************************************************************************/
#if defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)
// does not apply to RF24_LINUX
bool RF24::begin(_SPI* spiBus)
{
_spi = spiBus;
if (_init_pins())
return _init_radio();
return false;
}
/****************************************************************************/
bool RF24::begin(_SPI* spiBus, uint16_t _cepin, uint16_t _cspin)
{
ce_pin = _cepin;
csn_pin = _cspin;
return begin(spiBus);
}
#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)
/****************************************************************************/
bool RF24::begin(uint16_t _cepin, uint16_t _cspin)
{
ce_pin = _cepin;
csn_pin = _cspin;
return begin();
}
/****************************************************************************/
bool RF24::begin(void)
{
#if defined (RF24_LINUX)
#if defined (RF24_RPi)
switch(csn_pin) { // Ensure valid hardware CS pin
case 0: break;
case 1: break;
// Allow BCM2835 enums for RPi
case 8: csn_pin = 0; break;
case 7: csn_pin = 1; break;
case 18: csn_pin = 10; break; // to make it work on SPI1
case 17: csn_pin = 11; break;
case 16: csn_pin = 12; break;
default: csn_pin = 0; break;
}
#endif // RF24_RPi
_SPI.begin(csn_pin, spi_speed);
#elif defined (XMEGA_D3)
_spi->begin(csn_pin);
#else // using an Arduino platform || defined (LITTLEWIRE)
#if defined (RF24_SPI_PTR)
_spi->begin();
#else // !defined(RF24_SPI_PTR)
_SPI.begin();
#endif // !defined(RF24_SPI_PTR)
#endif // !defined(XMEGA_D3) && !defined(RF24_LINUX)
return _init_pins() && _init_radio();
}
/****************************************************************************/
// This modification assumes you passed the MCP23017 pin number to the RF24 constructor's _ce_pin parameter
bool RF24::_init_pins()
{
if (!isValid()) {
// didn't specify the CSN & CE pins to c'tor nor begin()
return false;
}
#if defined (RF24_LINUX)
#if defined (MRAA)
GPIO();
gpio.begin(ce_pin, csn_pin);
#endif
pinMode(ce_pin, OUTPUT);
ce(LOW);
delay(100);
#elif defined (LITTLEWIRE)
pinMode(csn_pin, OUTPUT);
csn(HIGH);
#elif defined (XMEGA_D3)
if (ce_pin != csn_pin) {
pinMode(ce_pin, OUTPUT);
};
ce(LOW);
csn(HIGH);
delay(200);
#else // using an Arduino platform
// Initialize pins
if (ce_pin != csn_pin) {
#if !defined (USE_MCP23XXX_AS_CE)
pinMode(ce_pin, OUTPUT);
#endif // !defined (USE_MCP23XXX_AS_CE)
pinMode(csn_pin, OUTPUT);
}
#if defined (USE_MCP23XXX_AS_CE)
USE_MCP23XXX_AS_CE.pinMode(ce_pin, OUTPUT);
#endif // defined(USE_MCP23XXX_AS_CE)
ce(LOW);
csn(HIGH);
#if defined (__ARDUINO_X86__)
delay(100);
#endif
#endif // !defined(XMEGA_D3) && !defined(LITTLEWIRE) && !defined(RF24_LINUX)
return true; // assuming pins are connected properly
}
/****************************************************************************/
bool RF24::_init_radio()
{
// Must allow the radio time to settle else configuration bits will not necessarily stick.
// This is actually only required following power up but some settling time also appears to
// be required after resets too. For full coverage, we'll always assume the worst.
// Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
// Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
// WARNING: Delay is based on P-variant whereby non-P *may* require different timing.
delay(5);
// Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
// WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
// sizes must never be used. See datasheet for a more complete explanation.
setRetries(5, 15);
// Then set the data rate to the slowest (and most reliable) speed supported by all
// hardware.
setDataRate(RF24_1MBPS);
// detect if is a plus variant & use old toggle features command accordingly
uint8_t before_toggle = read_register(FEATURE);
toggle_features();
uint8_t after_toggle = read_register(FEATURE);
_is_p_variant = before_toggle == after_toggle;
if (after_toggle){
if (_is_p_variant){
// module did not experience power-on-reset (#401)
toggle_features();
}
// allow use of multicast parameter and dynamic payloads by default
write_register(FEATURE, 0);
}
ack_payloads_enabled = false; // ack payloads disabled by default
write_register(DYNPD, 0); // disable dynamic payloads by default (for all pipes)
dynamic_payloads_enabled = false;
write_register(EN_AA, 0x3F); // enable auto-ack on all pipes
write_register(EN_RXADDR, 3); // only open RX pipes 0 & 1
setPayloadSize(32); // set static payload size to 32 (max) bytes by default
setAddressWidth(5); // set default address length to (max) 5 bytes
// Set up default configuration. Callers can always change it later.
// This channel should be universally safe and not bleed over into adjacent
// spectrum.
setChannel(76);
// Reset current status
// Notice reset and flush is the last thing we do
write_register(NRF_STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT));
// Flush buffers
flush_rx();
flush_tx();
// Clear CONFIG register:
// Reflect all IRQ events on IRQ pin
// Enable PTX
// Power Up
// 16-bit CRC (CRC required by auto-ack)
// Do not write CE high so radio will remain in standby I mode
// PTX should use only 22uA of power
write_register(NRF_CONFIG, (_BV(EN_CRC) | _BV(CRCO)) );
config_reg = read_register(NRF_CONFIG);
powerUp();
// if config is not set correctly then there was a bad response from module
return config_reg == (_BV(EN_CRC) | _BV(CRCO) | _BV(PWR_UP)) ? true : false;
}
/****************************************************************************/
bool RF24::isChipConnected()
{
uint8_t setup = read_register(SETUP_AW);
if (setup >= 1 && setup <= 3) {
return true;
}
return false;
}
/****************************************************************************/
bool RF24::isValid()
{
return ce_pin != 0xFFFF && csn_pin != 0xFFFF;
}
/****************************************************************************/
void RF24::startListening(void)
{
#if !defined(RF24_TINY) && !defined(LITTLEWIRE)
powerUp();
#endif
config_reg |= _BV(PRIM_RX);
write_register(NRF_CONFIG, config_reg);
write_register(NRF_STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT));
ce(HIGH);
// Restore the pipe0 address, if exists
if (pipe0_reading_address[0] > 0) {
write_register(RX_ADDR_P0, pipe0_reading_address, addr_width);
} else {
closeReadingPipe(0);
}
}
/****************************************************************************/
static const PROGMEM uint8_t child_pipe_enable[] = {ERX_P0, ERX_P1, ERX_P2,
ERX_P3, ERX_P4, ERX_P5};