Optimized high speed nRF24L01+ driver class documentation 1.5.0
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
RF24.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 */
8
9#include "nRF24L01.h"
10#include "RF24_config.h"
11#include "RF24.h"
12
13using namespace nRF24L01;
14
15/****************************************************************************/
16
17void RF24::csn(bool mode)
18{
19#if defined(RF24_TINY)
20 if (ce_pin != csn_pin) {
21 digitalWrite(csn_pin, mode);
22 }
23 else {
24 if (mode == HIGH) {
25 PORTB |= (1 << PINB2); // SCK->CSN HIGH
26 delayMicroseconds(RF24_CSN_SETTLE_HIGH_DELAY); // allow csn to settle.
27 }
28 else {
29 PORTB &= ~(1 << PINB2); // SCK->CSN LOW
30 delayMicroseconds(RF24_CSN_SETTLE_LOW_DELAY); // allow csn to settle
31 }
32 }
33 // Return, CSN toggle complete
34 return;
35
36#elif defined(ARDUINO) && !defined(RF24_SPI_TRANSACTIONS)
37 // Minimum ideal SPI bus speed is 2x data rate
38 // If we assume 2Mbs data rate and 16Mhz clock, a
39 // divider of 4 is the minimum we want.
40 // CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
41
42 #if !defined(SOFTSPI)
43 // applies to SPI_UART and inherent hardware SPI
44 #if defined(RF24_SPI_PTR)
45 _spi->setBitOrder(MSBFIRST);
46 _spi->setDataMode(SPI_MODE0);
47
48 #if !defined(F_CPU) || F_CPU < 20000000
49 _spi->setClockDivider(SPI_CLOCK_DIV2);
50 #elif F_CPU < 40000000
51 _spi->setClockDivider(SPI_CLOCK_DIV4);
52 #elif F_CPU < 80000000
53 _spi->setClockDivider(SPI_CLOCK_DIV8);
54 #elif F_CPU < 160000000
55 _spi->setClockDivider(SPI_CLOCK_DIV16);
56 #elif F_CPU < 320000000
57 _spi->setClockDivider(SPI_CLOCK_DIV32);
58 #elif F_CPU < 640000000
59 _spi->setClockDivider(SPI_CLOCK_DIV64);
60 #elif F_CPU < 1280000000
61 _spi->setClockDivider(SPI_CLOCK_DIV128);
62 #else // F_CPU >= 1280000000
63 #error "Unsupported CPU frequency. Please set correct SPI divider."
64 #endif // F_CPU to SPI_CLOCK_DIV translation
65
66 #else // !defined(RF24_SPI_PTR)
67 _SPI.setBitOrder(MSBFIRST);
68 _SPI.setDataMode(SPI_MODE0);
69
70 #if !defined(F_CPU) || F_CPU < 20000000
71 _SPI.setClockDivider(SPI_CLOCK_DIV2);
72 #elif F_CPU < 40000000
73 _SPI.setClockDivider(SPI_CLOCK_DIV4);
74 #elif F_CPU < 80000000
75 _SPI.setClockDivider(SPI_CLOCK_DIV8);
76 #elif F_CPU < 160000000
77 _SPI.setClockDivider(SPI_CLOCK_DIV16);
78 #elif F_CPU < 320000000
79 _SPI.setClockDivider(SPI_CLOCK_DIV32);
80 #elif F_CPU < 640000000
81 _SPI.setClockDivider(SPI_CLOCK_DIV64);
82 #elif F_CPU < 1280000000
83 _SPI.setClockDivider(SPI_CLOCK_DIV128);
84 #else // F_CPU >= 1280000000
85 #error "Unsupported CPU frequency. Please set correct SPI divider."
86 #endif // F_CPU to SPI_CLOCK_DIV translation
87 #endif // !defined(RF24_SPI_PTR)
88 #endif // !defined(SOFTSPI)
89
90#elif defined(RF24_RPi)
91 if (!mode)
92 _SPI.chipSelect(csn_pin);
93#endif // defined(RF24_RPi)
94
95#if !defined(RF24_LINUX)
96 digitalWrite(csn_pin, mode);
98#else
99 static_cast<void>(mode); // ignore -Wunused-parameter
100#endif // !defined(RF24_LINUX)
101}
102
103/****************************************************************************/
104
105void RF24::ce(bool level)
106{
107#ifndef RF24_LINUX
108 //Allow for 3-pin use on ATTiny
109 if (ce_pin != csn_pin) {
110#endif
111 digitalWrite(ce_pin, level);
112#ifndef RF24_LINUX
113 }
114#endif
115}
116
117/****************************************************************************/
118
120{
121#if defined(RF24_SPI_TRANSACTIONS)
122 #if defined(RF24_SPI_PTR)
123 #if defined(RF24_RP2)
124 _spi->beginTransaction(spi_speed);
125 #else // ! defined (RF24_RP2)
126 _spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
127 #endif // ! defined (RF24_RP2)
128 #else // !defined(RF24_SPI_PTR)
129 _SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
130 #endif // !defined(RF24_SPI_PTR)
131#endif // defined (RF24_SPI_TRANSACTIONS)
132 csn(LOW);
133}
134
135/****************************************************************************/
136
138{
139 csn(HIGH);
140#if defined(RF24_SPI_TRANSACTIONS)
141 #if defined(RF24_SPI_PTR)
142 _spi->endTransaction();
143 #else // !defined(RF24_SPI_PTR)
144 _SPI.endTransaction();
145 #endif // !defined(RF24_SPI_PTR)
146#endif // defined (RF24_SPI_TRANSACTIONS)
147}
148
149/****************************************************************************/
150
151void RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
152{
153#if defined(RF24_LINUX) || defined(RF24_RP2)
154 beginTransaction(); //configures the spi settings for RPi, locks mutex and setting csn low
155 uint8_t* prx = spi_rxbuff;
156 uint8_t* ptx = spi_txbuff;
157 uint8_t size = static_cast<uint8_t>(len + 1); // Add register value to transmit buffer
158
159 *ptx++ = reg;
160
161 while (len--) {
162 *ptx++ = NOP; // Dummy operation, just for reading
163 }
164
165 #if defined(RF24_RP2)
166 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, size);
167 #else // !defined (RF24_RP2)
168 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), size);
169 #endif // !defined (RF24_RP2)
170
171 status = *prx++; // status is 1st byte of receive buffer
172
173 // decrement before to skip status byte
174 while (--size) {
175 *buf++ = *prx++;
176 }
177
178 endTransaction(); // unlocks mutex and setting csn high
179
180#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
181
183 #if defined(RF24_SPI_PTR)
184 status = _spi->transfer(reg);
185 while (len--) {
186 *buf++ = _spi->transfer(0xFF);
187 }
188
189 #else // !defined(RF24_SPI_PTR)
190 status = _SPI.transfer(reg);
191 while (len--) {
192 *buf++ = _SPI.transfer(0xFF);
193 }
194
195 #endif // !defined(RF24_SPI_PTR)
197#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
198}
199
200/****************************************************************************/
201
202uint8_t RF24::read_register(uint8_t reg)
203{
204 uint8_t result;
205
206#if defined(RF24_LINUX) || defined(RF24_RP2)
208
209 uint8_t* prx = spi_rxbuff;
210 uint8_t* ptx = spi_txbuff;
211 *ptx++ = reg;
212 *ptx++ = NOP; // Dummy operation, just for reading
213
214 #if defined(RF24_RP2)
215 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, 2);
216 #else // !defined(RF24_RP2)
217 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), 2);
218 #endif // !defined(RF24_RP2)
219
220 status = *prx; // status is 1st byte of receive buffer
221 result = *++prx; // result is 2nd byte of receive buffer
222
224#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
225
227 #if defined(RF24_SPI_PTR)
228 status = _spi->transfer(reg);
229 result = _spi->transfer(0xff);
230
231 #else // !defined(RF24_SPI_PTR)
232 status = _SPI.transfer(reg);
233 result = _SPI.transfer(0xff);
234
235 #endif // !defined(RF24_SPI_PTR)
237#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
238
239 return result;
240}
241
242/****************************************************************************/
243
244void RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
245{
246#if defined(RF24_LINUX) || defined(RF24_RP2)
248 uint8_t* prx = spi_rxbuff;
249 uint8_t* ptx = spi_txbuff;
250 uint8_t size = static_cast<uint8_t>(len + 1); // Add register value to transmit buffer
251
252 *ptx++ = (W_REGISTER | reg);
253 while (len--) {
254 *ptx++ = *buf++;
255 }
256
257 #if defined(RF24_RP2)
258 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, size);
259 #else // !defined(RF24_RP2)
260 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), size);
261 #endif // !defined(RF24_RP2)
262
263 status = *prx; // status is 1st byte of receive buffer
265#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
266
268 #if defined(RF24_SPI_PTR)
269 status = _spi->transfer(W_REGISTER | reg);
270 while (len--) {
271 _spi->transfer(*buf++);
272 }
273
274 #else // !defined(RF24_SPI_PTR)
275 status = _SPI.transfer(W_REGISTER | reg);
276 while (len--) {
277 _SPI.transfer(*buf++);
278 }
279
280 #endif // !defined(RF24_SPI_PTR)
282#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
283}
284
285/****************************************************************************/
286
287void RF24::write_register(uint8_t reg, uint8_t value)
288{
289 IF_RF24_DEBUG(printf_P(PSTR("write_register(%02x,%02x)\r\n"), reg, value));
290#if defined(RF24_LINUX) || defined(RF24_RP2)
292 uint8_t* prx = spi_rxbuff;
293 uint8_t* ptx = spi_txbuff;
294 *ptx++ = (W_REGISTER | reg);
295 *ptx = value;
296
297 #if defined(RF24_RP2)
298 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, 2);
299 #else // !defined(RF24_RP2)
300 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), 2);
301 #endif // !defined(RF24_RP2)
302
303 status = *prx++; // status is 1st byte of receive buffer
305#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
306
308 #if defined(RF24_SPI_PTR)
309 status = _spi->transfer(W_REGISTER | reg);
310 _spi->transfer(value);
311 #else // !defined(RF24_SPI_PTR)
312 status = _SPI.transfer(W_REGISTER | reg);
313 _SPI.transfer(value);
314 #endif // !defined(RF24_SPI_PTR)
316#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
317}
318
319/****************************************************************************/
320
321void RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t writeType)
322{
323 const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
324
325 uint8_t blank_len = !data_len ? 1 : 0;
327 data_len = rf24_min(data_len, payload_size);
328 blank_len = static_cast<uint8_t>(payload_size - data_len);
329 }
330 else {
331 data_len = rf24_min(data_len, static_cast<uint8_t>(32));
332 }
333
334 //printf("[Writing %u bytes %u blanks]",data_len,blank_len);
335 IF_RF24_DEBUG(printf_P("[Writing %u bytes %u blanks]\n", data_len, blank_len););
336
337#if defined(RF24_LINUX) || defined(RF24_RP2)
339 uint8_t* prx = spi_rxbuff;
340 uint8_t* ptx = spi_txbuff;
341 uint8_t size;
342 size = static_cast<uint8_t>(data_len + blank_len + 1); // Add register value to transmit buffer
343
344 *ptx++ = writeType;
345 while (data_len--) {
346 *ptx++ = *current++;
347 }
348
349 while (blank_len--) {
350 *ptx++ = 0;
351 }
352
353 #if defined(RF24_RP2)
354 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, size);
355 #else // !defined(RF24_RP2)
356 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), size);
357 #endif // !defined(RF24_RP2)
358
359 status = *prx; // status is 1st byte of receive buffer
361
362#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
363
365 #if defined(RF24_SPI_PTR)
366 status = _spi->transfer(writeType);
367 while (data_len--) {
368 _spi->transfer(*current++);
369 }
370
371 while (blank_len--) {
372 _spi->transfer(0);
373 }
374
375 #else // !defined(RF24_SPI_PTR)
376 status = _SPI.transfer(writeType);
377 while (data_len--) {
378 _SPI.transfer(*current++);
379 }
380
381 while (blank_len--) {
382 _SPI.transfer(0);
383 }
384
385 #endif // !defined(RF24_SPI_PTR)
387#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
388}
389
390/****************************************************************************/
391
392void RF24::read_payload(void* buf, uint8_t data_len)
393{
394 uint8_t* current = reinterpret_cast<uint8_t*>(buf);
395
396 uint8_t blank_len = 0;
398 data_len = rf24_min(data_len, payload_size);
399 blank_len = static_cast<uint8_t>(payload_size - data_len);
400 }
401 else {
402 data_len = rf24_min(data_len, static_cast<uint8_t>(32));
403 }
404
405 //printf("[Reading %u bytes %u blanks]",data_len,blank_len);
406
407 IF_RF24_DEBUG(printf_P("[Reading %u bytes %u blanks]\n", data_len, blank_len););
408
409#if defined(RF24_LINUX) || defined(RF24_RP2)
411 uint8_t* prx = spi_rxbuff;
412 uint8_t* ptx = spi_txbuff;
413 uint8_t size;
414 size = static_cast<uint8_t>(data_len + blank_len + 1); // Add register value to transmit buffer
415
416 *ptx++ = R_RX_PAYLOAD;
417 while (--size) {
418 *ptx++ = NOP;
419 }
420
421 size = static_cast<uint8_t>(data_len + blank_len + 1); // Size has been lost during while, re affect
422
423 #if defined(RF24_RP2)
424 _spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, size);
425 #else // !defined(RF24_RP2)
426 _SPI.transfernb(reinterpret_cast<char*>(spi_txbuff), reinterpret_cast<char*>(spi_rxbuff), size);
427 #endif // !defined(RF24_RP2)
428
429 status = *prx++; // 1st byte is status
430
431 if (data_len > 0) {
432 // Decrement before to skip 1st status byte
433 while (--data_len) {
434 *current++ = *prx++;
435 }
436
437 *current = *prx;
438 }
440#else // !defined(RF24_LINUX) && !defined(RF24_RP2)
441
443 #if defined(RF24_SPI_PTR)
444 status = _spi->transfer(R_RX_PAYLOAD);
445 while (data_len--) {
446 *current++ = _spi->transfer(0xFF);
447 }
448
449 while (blank_len--) {
450 _spi->transfer(0xFF);
451 }
452
453 #else // !defined(RF24_SPI_PTR)
454 status = _SPI.transfer(R_RX_PAYLOAD);
455 while (data_len--) {
456 *current++ = _SPI.transfer(0xFF);
457 }
458
459 while (blank_len--) {
460 _SPI.transfer(0xff);
461 }
462
463 #endif // !defined(RF24_SPI_PTR)
465
466#endif // !defined(RF24_LINUX) && !defined(RF24_RP2)
467}
468
469/****************************************************************************/
470
471uint8_t RF24::flush_rx(void)
472{
473 read_register(FLUSH_RX, (uint8_t*)nullptr, 0);
474 IF_RF24_DEBUG(printf_P("[Flushing RX FIFO]"););
475 return status;
476}
477
478/****************************************************************************/
479
480uint8_t RF24::flush_tx(void)
481{
482 read_register(FLUSH_TX, (uint8_t*)nullptr, 0);
483 IF_RF24_DEBUG(printf_P("[Flushing RX FIFO]"););
484 return status;
485}
486
487/****************************************************************************/
488#if !defined(MINIMAL)
489
490void RF24::printStatus(uint8_t flags)
491{
492 printf_P(PSTR("RX_DR=%x TX_DS=%x TX_DF=%x RX_PIPE=%x TX_FULL=%x\r\n"),
493 (flags & RF24_RX_DR) ? 1 : 0,
494 (flags & RF24_TX_DS) ? 1 : 0,
495 (flags & RF24_TX_DF) ? 1 : 0,
496 (flags >> RX_P_NO) & 0x07,
497 (flags & _BV(TX_FULL)) ? 1 : 0);
498}
499
500/****************************************************************************/
501
502void RF24::print_observe_tx(uint8_t value)
503{
504 printf_P(PSTR("OBSERVE_TX=%02x: PLOS_CNT=%x ARC_CNT=%x\r\n"), value, (value >> PLOS_CNT) & 0x0F, (value >> ARC_CNT) & 0x0F);
505}
506
507/****************************************************************************/
508
509void RF24::print_byte_register(const char* name, uint8_t reg, uint8_t qty)
510{
512 "\t="),
513 name);
514 while (qty--) {
515 printf_P(PSTR(" 0x%02x"), read_register(reg++));
516 }
517 printf_P(PSTR("\r\n"));
518}
519
520/****************************************************************************/
521
522void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)
523{
524
526 "\t="),
527 name);
528 while (qty--) {
529 uint8_t* buffer = new uint8_t[addr_width];
530 read_register(reg++, buffer, addr_width);
531
532 printf_P(PSTR(" 0x"));
533 uint8_t* bufptr = buffer + addr_width;
534 while (--bufptr >= buffer) {
535 printf_P(PSTR("%02x"), *bufptr); // NOLINT: clang-tidy seems to emit a false positive about zero-allocated memory here (*bufptr)
536 }
537 delete[] buffer;
538 }
539 printf_P(PSTR("\r\n"));
540}
541
542/****************************************************************************/
543
544uint8_t RF24::sprintf_address_register(char* out_buffer, uint8_t reg, uint8_t qty)
545{
546 uint8_t offset = 0;
547 uint8_t* read_buffer = new uint8_t[addr_width];
548 while (qty--) {
549 read_register(reg++, read_buffer, addr_width);
550 uint8_t* bufptr = read_buffer + addr_width;
551 while (--bufptr >= read_buffer) {
552 offset += sprintf_P(out_buffer + offset, PSTR("%02X"), *bufptr); // NOLINT(clang-analyzer-cplusplus.NewDelete)
553 }
554 }
555 delete[] read_buffer;
556 return offset;
557}
558#endif // !defined(MINIMAL)
559
560/****************************************************************************/
561
562RF24::RF24(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin, uint32_t _spi_speed)
563 : ce_pin(_cepin),
564 csn_pin(_cspin),
565 spi_speed(_spi_speed),
566 payload_size(32),
567 _is_p_variant(false),
568 _is_p0_rx(false),
569 addr_width(5),
571#if defined FAILURE_HANDLING
573#endif
574 csDelay(5)
575{
576 _init_obj();
577}
578
579/****************************************************************************/
580
581RF24::RF24(uint32_t _spi_speed)
582 : ce_pin(RF24_PIN_INVALID),
583 csn_pin(RF24_PIN_INVALID),
584 spi_speed(_spi_speed),
585 payload_size(32),
586 _is_p_variant(false),
587 _is_p0_rx(false),
588 addr_width(5),
590#if defined FAILURE_HANDLING
592#endif
593 csDelay(5)
594{
595 _init_obj();
596}
597
598/****************************************************************************/
599
600void RF24::_init_obj()
601{
602 // Use a pointer on the Arduino platform
603
604#if defined(RF24_SPI_PTR) && !defined(RF24_RP2)
605 _spi = &SPI;
606#endif // defined (RF24_SPI_PTR)
607
608 if (spi_speed <= 35000) { //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
609 spi_speed = RF24_SPI_SPEED;
610 }
611}
612
613/****************************************************************************/
614
615void RF24::setChannel(uint8_t channel)
616{
617 const uint8_t max_channel = 125;
618 write_register(RF_CH, rf24_min(channel, max_channel));
619}
620
622{
623 return read_register(RF_CH);
624}
625
626/****************************************************************************/
627
628void RF24::setPayloadSize(uint8_t size)
629{
630 // payload size must be in range [1, 32]
631 payload_size = static_cast<uint8_t>(rf24_max(1, rf24_min(32, size)));
632
633 // write static payload size setting for all pipes
634 for (uint8_t i = 0; i < 6; ++i) {
635 write_register(static_cast<uint8_t>(RX_PW_P0 + i), payload_size);
636 }
637}
638
639/****************************************************************************/
640
642{
643 return payload_size;
644}
645
646/****************************************************************************/
647
648#if !defined(MINIMAL)
649
650static const PROGMEM char rf24_datarate_e_str_0[] = "= 1 MBPS";
651static const PROGMEM char rf24_datarate_e_str_1[] = "= 2 MBPS";
652static const PROGMEM char rf24_datarate_e_str_2[] = "= 250 KBPS";
658static const PROGMEM char rf24_model_e_str_0[] = "nRF24L01";
659static const PROGMEM char rf24_model_e_str_1[] = "nRF24L01+";
660static const PROGMEM char* const rf24_model_e_str_P[] = {
663};
664static const PROGMEM char rf24_crclength_e_str_0[] = "= Disabled";
665static const PROGMEM char rf24_crclength_e_str_1[] = "= 8 bits";
666static const PROGMEM char rf24_crclength_e_str_2[] = "= 16 bits";
672static const PROGMEM char rf24_pa_dbm_e_str_0[] = "= PA_MIN";
673static const PROGMEM char rf24_pa_dbm_e_str_1[] = "= PA_LOW";
674static const PROGMEM char rf24_pa_dbm_e_str_2[] = "= PA_HIGH";
675static const PROGMEM char rf24_pa_dbm_e_str_3[] = "= PA_MAX";
682
683static const PROGMEM char rf24_feature_e_str_on[] = "= Enabled";
684static const PROGMEM char rf24_feature_e_str_allowed[] = "= Allowed";
685static const PROGMEM char rf24_feature_e_str_open[] = " open ";
686static const PROGMEM char rf24_feature_e_str_closed[] = "closed";
694
696{
697
698 #if defined(RF24_LINUX)
699 printf("================ SPI Configuration ================\n");
700 uint8_t bus_ce = static_cast<uint8_t>(csn_pin % 10);
701 uint8_t bus_numb = static_cast<uint8_t>((csn_pin - bus_ce) / 10);
702 printf("CSN Pin\t\t= /dev/spidev%d.%d\n", bus_numb, bus_ce);
703 printf("CE Pin\t\t= Custom GPIO%d\n", ce_pin);
704 #endif
705 printf_P(PSTR("SPI Speedz\t= %d Mhz\n"), static_cast<uint8_t>(spi_speed / 1000000)); //Print the SPI speed on non-Linux devices
706 #if defined(RF24_LINUX)
707 printf("================ NRF Configuration ================\n");
708 #endif // defined(RF24_LINUX)
709
710 uint8_t status = update();
711 printf_P(PSTR("STATUS\t\t= 0x%02x "), status);
712 printStatus(status);
713
714 print_address_register(PSTR("RX_ADDR_P0-1"), RX_ADDR_P0, 2);
715 print_byte_register(PSTR("RX_ADDR_P2-5"), RX_ADDR_P2, 4);
716 print_address_register(PSTR("TX_ADDR\t"), TX_ADDR);
717
718 print_byte_register(PSTR("RX_PW_P0-6"), RX_PW_P0, 6);
719 print_byte_register(PSTR("EN_AA\t"), EN_AA);
720 print_byte_register(PSTR("EN_RXADDR"), EN_RXADDR);
721 print_byte_register(PSTR("RF_CH\t"), RF_CH);
722 print_byte_register(PSTR("RF_SETUP"), RF_SETUP);
723 print_byte_register(PSTR("CONFIG\t"), CONFIG);
724 print_byte_register(PSTR("DYNPD/FEATURE"), DYNPD, 2);
725
726 printf_P(PSTR("Data Rate\t" PRIPSTR
727 "\r\n"),
728 (char*)(pgm_read_ptr(&rf24_datarate_e_str_P[getDataRate()])));
729 printf_P(PSTR("Model\t\t= " PRIPSTR
730 "\r\n"),
731 (char*)(pgm_read_ptr(&rf24_model_e_str_P[isPVariant()])));
732 printf_P(PSTR("CRC Length\t" PRIPSTR
733 "\r\n"),
734 (char*)(pgm_read_ptr(&rf24_crclength_e_str_P[getCRCLength()])));
735 printf_P(PSTR("PA Power\t" PRIPSTR
736 "\r\n"),
737 (char*)(pgm_read_ptr(&rf24_pa_dbm_e_str_P[getPALevel()])));
738 printf_P(PSTR("ARC\t\t= %d\r\n"), getARC());
739}
740
742{
743
744 #if defined(RF24_LINUX)
745 printf("================ SPI Configuration ================\n");
746 uint8_t bus_ce = static_cast<uint8_t>(csn_pin % 10);
747 uint8_t bus_numb = static_cast<uint8_t>((csn_pin - bus_ce) / 10);
748 printf("CSN Pin\t\t\t= /dev/spidev%d.%d\n", bus_numb, bus_ce);
749 printf("CE Pin\t\t\t= Custom GPIO%d\n", ce_pin);
750 #endif
751 printf_P(PSTR("SPI Frequency\t\t= %d Mhz\n"), static_cast<uint8_t>(spi_speed / 1000000)); //Print the SPI speed on non-Linux devices
752 #if defined(RF24_LINUX)
753 printf("================ NRF Configuration ================\n");
754 #endif // defined(RF24_LINUX)
755
756 uint8_t channel = getChannel();
757 uint16_t frequency = static_cast<uint16_t>(channel + 2400);
758 printf_P(PSTR("Channel\t\t\t= %u (~ %u MHz)\r\n"), channel, frequency);
759 printf_P(PSTR("Model\t\t\t= " PRIPSTR
760 "\r\n"),
761 (char*)(pgm_read_ptr(&rf24_model_e_str_P[isPVariant()])));
762
763 printf_P(PSTR("RF Data Rate\t\t" PRIPSTR
764 "\r\n"),
765 (char*)(pgm_read_ptr(&rf24_datarate_e_str_P[getDataRate()])));
766 printf_P(PSTR("RF Power Amplifier\t" PRIPSTR
767 "\r\n"),
768 (char*)(pgm_read_ptr(&rf24_pa_dbm_e_str_P[getPALevel()])));
769 printf_P(PSTR("RF Low Noise Amplifier\t" PRIPSTR
770 "\r\n"),
771 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>((read_register(RF_SETUP) & 1) * 1)])));
772 printf_P(PSTR("CRC Length\t\t" PRIPSTR
773 "\r\n"),
774 (char*)(pgm_read_ptr(&rf24_crclength_e_str_P[getCRCLength()])));
775 printf_P(PSTR("Address Length\t\t= %d bytes\r\n"), (read_register(SETUP_AW) & 3) + 2);
776 printf_P(PSTR("Static Payload Length\t= %d bytes\r\n"), getPayloadSize());
777
778 uint8_t setupRetry = read_register(SETUP_RETR);
779 printf_P(PSTR("Auto Retry Delay\t= %d microseconds\r\n"), (setupRetry >> ARD) * 250 + 250);
780 printf_P(PSTR("Auto Retry Attempts\t= %d maximum\r\n"), setupRetry & 0x0F);
781
782 uint8_t observeTx = read_register(OBSERVE_TX);
783 printf_P(PSTR("Packets lost on\n current channel\t= %d\r\n"), observeTx >> 4);
784 printf_P(PSTR("Retry attempts made for\n last transmission\t= %d\r\n"), observeTx & 0x0F);
785
786 uint8_t features = read_register(FEATURE);
787 printf_P(PSTR("Multicast\t\t" PRIPSTR
788 "\r\n"),
789 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(features & _BV(EN_DYN_ACK)) * 2)])));
790 printf_P(PSTR("Custom ACK Payload\t" PRIPSTR
791 "\r\n"),
792 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(features & _BV(EN_ACK_PAY)) * 1)])));
793
794 uint8_t dynPl = read_register(DYNPD);
795 printf_P(PSTR("Dynamic Payloads\t" PRIPSTR
796 "\r\n"),
797 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>((dynPl && (features & _BV(EN_DPL))) * 1)])));
798
799 uint8_t autoAck = read_register(EN_AA);
800 if (autoAck == 0x3F || autoAck == 0) {
801 // all pipes have the same configuration about auto-ack feature
802 printf_P(PSTR("Auto Acknowledgment\t" PRIPSTR
803 "\r\n"),
804 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(autoAck) * 1)])));
805 }
806 else {
807 // representation per pipe
808 printf_P(PSTR("Auto Acknowledgment\t= 0b%c%c%c%c%c%c\r\n"),
809 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P5)) + 48),
810 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P4)) + 48),
811 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P3)) + 48),
812 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P2)) + 48),
813 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P1)) + 48),
814 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P0)) + 48));
815 }
816
817 config_reg = read_register(CONFIG);
818 printf_P(PSTR("Primary Mode\t\t= %cX\r\n"), config_reg & _BV(PRIM_RX) ? 'R' : 'T');
819 print_address_register(PSTR("TX address\t"), TX_ADDR);
820
821 uint8_t openPipes = read_register(EN_RXADDR);
822 for (uint8_t i = 0; i < 6; ++i) {
823 bool isOpen = openPipes & _BV(i);
824 printf_P(PSTR("pipe %u (" PRIPSTR
825 ") bound"),
826 i, (char*)(pgm_read_ptr(&rf24_feature_e_str_P[isOpen + 3])));
827 if (i < 2) {
828 print_address_register(PSTR(""), static_cast<uint8_t>(RX_ADDR_P0 + i));
829 }
830 else {
831 print_byte_register(PSTR(""), static_cast<uint8_t>(RX_ADDR_P0 + i));
832 }
833 }
834}
835
836/****************************************************************************/
837
838uint16_t RF24::sprintfPrettyDetails(char* debugging_information)
839{
840 const char* format_string = PSTR(
841 "================ SPI Configuration ================\n"
842 "CSN Pin\t\t\t= %d\n"
843 "CE Pin\t\t\t= %d\n"
844 "SPI Frequency\t\t= %d Mhz\n"
845 "================ NRF Configuration ================\n"
846 "Channel\t\t\t= %u (~ %u MHz)\n"
847 "RF Data Rate\t\t" PRIPSTR "\n"
848 "RF Power Amplifier\t" PRIPSTR "\n"
849 "RF Low Noise Amplifier\t" PRIPSTR "\n"
850 "CRC Length\t\t" PRIPSTR "\n"
851 "Address Length\t\t= %d bytes\n"
852 "Static Payload Length\t= %d bytes\n"
853 "Auto Retry Delay\t= %d microseconds\n"
854 "Auto Retry Attempts\t= %d maximum\n"
855 "Packets lost on\n current channel\t= %d\r\n"
856 "Retry attempts made for\n last transmission\t= %d\r\n"
857 "Multicast\t\t" PRIPSTR "\n"
858 "Custom ACK Payload\t" PRIPSTR "\n"
859 "Dynamic Payloads\t" PRIPSTR "\n"
860 "Auto Acknowledgment\t");
861 const char* format_str2 = PSTR("\nPrimary Mode\t\t= %cX\nTX address\t\t= 0x");
862 const char* format_str3 = PSTR("\nPipe %d (" PRIPSTR ") bound\t= 0x");
863
864 uint16_t offset = sprintf_P(
865 debugging_information, format_string, csn_pin, ce_pin,
866 static_cast<uint8_t>(spi_speed / 1000000), getChannel(),
867 static_cast<uint16_t>(getChannel() + 2400),
868 (char*)(pgm_read_ptr(&rf24_datarate_e_str_P[getDataRate()])),
869 (char*)(pgm_read_ptr(&rf24_pa_dbm_e_str_P[getPALevel()])),
870 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>((read_register(RF_SETUP) & 1) * 1)])),
871 (char*)(pgm_read_ptr(&rf24_crclength_e_str_P[getCRCLength()])),
872 ((read_register(SETUP_AW) & 3) + 2), getPayloadSize(),
873 ((read_register(SETUP_RETR) >> ARD) * 250 + 250),
875 (read_register(OBSERVE_TX) & 0x0F),
876 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(read_register(FEATURE) & _BV(EN_DYN_ACK)) * 2)])),
877 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(read_register(FEATURE) & _BV(EN_ACK_PAY)) * 1)])),
878 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>((read_register(DYNPD) && (read_register(FEATURE) & _BV(EN_DPL))) * 1)])));
879 uint8_t autoAck = read_register(EN_AA);
880 if (autoAck == 0x3F || autoAck == 0) {
881 // all pipes have the same configuration about auto-ack feature
882 offset += sprintf_P(
883 debugging_information + offset, PSTR("" PRIPSTR ""),
884 (char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<uint8_t>(static_cast<bool>(autoAck) * 1)])));
885 }
886 else {
887 // representation per pipe
888 offset += sprintf_P(
889 debugging_information + offset, PSTR("= 0b%c%c%c%c%c%c"),
890 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P5)) + 48),
891 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P4)) + 48),
892 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P3)) + 48),
893 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P2)) + 48),
894 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P1)) + 48),
895 static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P0)) + 48));
896 }
897 offset += sprintf_P(
898 debugging_information + offset, format_str2,
899 (read_register(CONFIG) & _BV(PRIM_RX) ? 'R' : 'T'));
900 offset += sprintf_address_register(debugging_information + offset, TX_ADDR);
901 uint8_t openPipes = read_register(EN_RXADDR);
902 for (uint8_t i = 0; i < 6; ++i) {
903 offset += sprintf_P(
904 debugging_information + offset, format_str3,
905 i, ((char*)(pgm_read_ptr(&rf24_feature_e_str_P[static_cast<bool>(openPipes & _BV(i)) + 3]))));
906 if (i < 2) {
907 offset += sprintf_address_register(
908 debugging_information + offset, static_cast<uint8_t>(RX_ADDR_P0 + i));
909 }
910 else {
911 offset += sprintf_P(
912 debugging_information + offset, PSTR("%02X"),
913 read_register(static_cast<uint8_t>(RX_ADDR_P0 + i)));
914 }
915 }
916 return offset;
917}
918
919/****************************************************************************/
920
921void RF24::encodeRadioDetails(uint8_t* encoded_details)
922{
923 uint8_t end = FEATURE + 1;
924 for (uint8_t i = CONFIG; i < end; ++i) {
925 if (i == RX_ADDR_P0 || i == RX_ADDR_P1 || i == TX_ADDR) {
926 // get 40-bit registers
927 read_register(i, encoded_details, 5);
928 encoded_details += 5;
929 }
930 else if (i != 0x18 && i != 0x19 && i != 0x1a && i != 0x1b) { // skip undocumented registers
931 // get single byte registers
932 *encoded_details++ = read_register(i);
933 }
934 }
935 *encoded_details++ = ce_pin >> 4;
936 *encoded_details++ = ce_pin & 0xFF;
937 *encoded_details++ = csn_pin >> 4;
938 *encoded_details++ = csn_pin & 0xFF;
939 *encoded_details = static_cast<uint8_t>((spi_speed / 1000000) | _BV(_is_p_variant * 4));
940}
941#endif // !defined(MINIMAL)
942
943/****************************************************************************/
944#if defined(RF24_SPI_PTR) || defined(DOXYGEN_FORCED)
945// does not apply to RF24_LINUX
946
947bool RF24::begin(_SPI* spiBus)
948{
949 _spi = spiBus;
950 return _init_pins() && _init_radio();
951}
952
953/****************************************************************************/
954
955bool RF24::begin(_SPI* spiBus, rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin)
956{
957 ce_pin = _cepin;
958 csn_pin = _cspin;
959 return begin(spiBus);
960}
961
962#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)
963
964/****************************************************************************/
965
967{
968 ce_pin = _cepin;
969 csn_pin = _cspin;
970 return begin();
971}
972
973/****************************************************************************/
974
975bool RF24::begin(void)
976{
977#if defined(RF24_LINUX)
978 #if defined(RF24_RPi)
979 switch (csn_pin) { // Ensure valid hardware CS pin
980 case 0: break;
981 case 1: break;
982 // Allow BCM2835 enums for RPi
983 case 8: csn_pin = 0; break;
984 case 7: csn_pin = 1; break;
985 case 18: csn_pin = 10; break; // to make it work on SPI1
986 case 17: csn_pin = 11; break;
987 case 16: csn_pin = 12; break;
988 default: csn_pin = 0; break;
989 }
990 #endif // RF24_RPi
991
992 _SPI.begin(csn_pin, spi_speed);
993
994#elif defined(XMEGA_D3)
995 _spi->begin(csn_pin);
996
997#elif defined(RF24_RP2)
998 _spi = new SPI();
999 _spi->begin(PICO_DEFAULT_SPI ? spi1 : spi0);
1000
1001#else // using an Arduino platform || defined (LITTLEWIRE)
1002
1003 #if defined(RF24_SPI_PTR)
1004 _spi->begin();
1005 #else // !defined(RF24_SPI_PTR)
1006 _SPI.begin();
1007 #endif // !defined(RF24_SPI_PTR)
1008
1009#endif // !defined(XMEGA_D3) && !defined(RF24_LINUX)
1010
1011 return _init_pins() && _init_radio();
1012}
1013
1014/****************************************************************************/
1015
1016bool RF24::_init_pins()
1017{
1018 if (!isValid()) {
1019 // didn't specify the CSN & CE pins to c'tor nor begin()
1020 return false;
1021 }
1022
1023#if defined(RF24_LINUX)
1024
1025 pinMode(ce_pin, OUTPUT);
1026 ce(LOW);
1027 delay(100);
1028
1029#elif defined(LITTLEWIRE)
1030 pinMode(csn_pin, OUTPUT);
1031 csn(HIGH);
1032
1033#elif defined(XMEGA_D3)
1034 if (ce_pin != csn_pin) {
1035 pinMode(ce_pin, OUTPUT);
1036 };
1037 ce(LOW);
1038 csn(HIGH);
1039 delay(200);
1040
1041#else // using an Arduino platform
1042
1043 // Initialize pins
1044 if (ce_pin != csn_pin) {
1045 pinMode(ce_pin, OUTPUT);
1046 pinMode(csn_pin, OUTPUT);
1047 }
1048
1049 ce(LOW);
1050 csn(HIGH);
1051
1052 #if defined(__ARDUINO_X86__)
1053 delay(100);
1054 #endif
1055#endif // !defined(XMEGA_D3) && !defined(LITTLEWIRE) && !defined(RF24_LINUX)
1056
1057 return true; // assuming pins are connected properly
1058}
1059
1060/****************************************************************************/
1061
1062bool RF24::_init_radio()
1063{
1064 // Must allow the radio time to settle else configuration bits will not necessarily stick.
1065 // This is actually only required following power up but some settling time also appears to
1066 // be required after resets too. For full coverage, we'll always assume the worst.
1067 // Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
1068 // Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
1069 // WARNING: Delay is based on P-variant whereby non-P *may* require different timing.
1070 delay(5);
1071
1072 // Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
1073 // WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
1074 // sizes must never be used. See datasheet for a more complete explanation.
1075 setRetries(5, 15);
1076
1077 // Then set the data rate to the slowest (and most reliable) speed supported by all hardware.
1079
1080 // detect if is a plus variant & use old toggle features command accordingly
1081 uint8_t before_toggle = read_register(FEATURE);
1082 toggle_features();
1083 uint8_t after_toggle = read_register(FEATURE);
1084 _is_p_variant = before_toggle == after_toggle;
1085 if (after_toggle) {
1086 if (_is_p_variant) {
1087 // module did not experience power-on-reset (#401)
1088 toggle_features();
1089 }
1090 // allow use of multicast parameter and dynamic payloads by default
1091 write_register(FEATURE, 0);
1092 }
1093 ack_payloads_enabled = false; // ack payloads disabled by default
1094 write_register(DYNPD, 0); // disable dynamic payloads by default (for all pipes)
1096 write_register(EN_AA, 0x3F); // enable auto-ack on all pipes
1097 write_register(EN_RXADDR, 3); // only open RX pipes 0 & 1
1098 setPayloadSize(32); // set static payload size to 32 (max) bytes by default
1099 setAddressWidth(5); // set default address length to (max) 5 bytes
1100
1101 // Set up default configuration. Callers can always change it later.
1102 // This channel should be universally safe and not bleed over into adjacent
1103 // spectrum.
1104 setChannel(76);
1105
1106 // Reset current status
1107 // Notice reset and flush is the last thing we do
1108 write_register(STATUS, RF24_IRQ_ALL);
1109
1110 // Flush buffers
1111 flush_rx();
1112 flush_tx();
1113
1114 // Clear CONFIG register:
1115 // Reflect NO IRQ events on IRQ pin
1116 // Enable PTX
1117 // Power Up
1118 // 16-bit CRC (CRC required by auto-ack)
1119 // Do not write CE high so radio will remain in standby I mode
1120 // PTX should use only 22uA of power
1121 write_register(CONFIG, (_BV(EN_CRC) | _BV(CRCO) | _BV(MASK_RX_DR) | _BV(MASK_TX_DS) | _BV(MASK_MAX_RT)));
1122 config_reg = read_register(CONFIG);
1123
1124 powerUp();
1125
1126 // if config is not set correctly then there was a bad response from module
1127 return config_reg == (_BV(EN_CRC) | _BV(CRCO) | _BV(PWR_UP) | _BV(MASK_RX_DR) | _BV(MASK_TX_DS) | _BV(MASK_MAX_RT)) ? true : false;
1128}
1129
1130/****************************************************************************/
1131
1133{
1134 return read_register(SETUP_AW) == (addr_width - static_cast<uint8_t>(2));
1135}
1136
1137/****************************************************************************/
1138
1140{
1141 return ce_pin != RF24_PIN_INVALID && csn_pin != RF24_PIN_INVALID;
1142}
1143
1144/****************************************************************************/
1145
1147{
1148#if !defined(RF24_TINY) && !defined(LITTLEWIRE)
1149 powerUp();
1150#endif
1151 config_reg |= _BV(PRIM_RX);
1152 write_register(CONFIG, config_reg);
1153 write_register(STATUS, RF24_IRQ_ALL);
1154 ce(HIGH);
1155
1156 // Restore the pipe0 address, if exists
1157 if (_is_p0_rx) {
1158 write_register(RX_ADDR_P0, pipe0_reading_address, addr_width);
1159 }
1160 else {
1162 }
1163}
1164
1165/****************************************************************************/
1166
1167static const PROGMEM uint8_t child_pipe_enable[] = {ERX_P0, ERX_P1, ERX_P2,
1168 ERX_P3, ERX_P4, ERX_P5};
1169
1171{
1172 ce(LOW);
1173
1174 //delayMicroseconds(100);
1175 delayMicroseconds(static_cast<int>(txDelay));
1177 flush_tx();
1178 }
1179
1180 config_reg = static_cast<uint8_t>(config_reg & ~_BV(PRIM_RX));
1181 write_register(CONFIG, config_reg);
1182
1183#if defined(RF24_TINY) || defined(LITTLEWIRE)
1184 // for 3 pins solution TX mode is only left with additional powerDown/powerUp cycle
1185 if (ce_pin == csn_pin) {
1186 powerDown();
1187 powerUp();
1188 }
1189#endif
1190 write_register(RX_ADDR_P0, pipe0_writing_address, addr_width);
1191 write_register(EN_RXADDR, static_cast<uint8_t>(read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[0])))); // Enable RX on pipe0
1192}
1193
1194/****************************************************************************/
1195
1196void RF24::stopListening(const uint64_t txAddress)
1197{
1198 memcpy(pipe0_writing_address, &txAddress, addr_width);
1199 stopListening();
1200 write_register(TX_ADDR, pipe0_writing_address, addr_width);
1201}
1202
1203/****************************************************************************/
1204
1205void RF24::stopListening(const uint8_t* txAddress)
1206{
1207 memcpy(pipe0_writing_address, txAddress, addr_width);
1208 stopListening();
1209 write_register(TX_ADDR, pipe0_writing_address, addr_width);
1210}
1211
1212/****************************************************************************/
1213
1215{
1216 ce(LOW); // Guarantee CE is low on powerDown
1217 config_reg = static_cast<uint8_t>(config_reg & ~_BV(PWR_UP));
1218 write_register(CONFIG, config_reg);
1219}
1220
1221/****************************************************************************/
1222
1223//Power up now. Radio will not power down unless instructed by MCU for config changes etc.
1225{
1226 // if not powered up then power up and wait for the radio to initialize
1227 if (!(config_reg & _BV(PWR_UP))) {
1228 config_reg |= _BV(PWR_UP);
1229 write_register(CONFIG, config_reg);
1230
1231 // For nRF24L01+ to go from power down mode to TX or RX mode it must first pass through stand-by mode.
1232 // There must be a delay of Tpd2stby (see Table 16.) after the nRF24L01+ leaves power down mode before
1233 // the CEis set high. - Tpd2stby can be up to 5ms per the 1.0 datasheet
1235 }
1236}
1237
1238/******************************************************************/
1239#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1240
1241void RF24::errNotify()
1242{
1243 #if defined(RF24_DEBUG) || defined(RF24_LINUX)
1244 printf_P(PSTR("RF24 HARDWARE FAIL: Radio not responding, verify pin connections, wiring, etc.\r\n"));
1245 #endif
1246 #if defined(FAILURE_HANDLING)
1247 failureDetected = 1;
1248 #else
1249 delay(5000);
1250 #endif
1251}
1252
1253/******************************************************************/
1254
1255int8_t RF24::errHandler(bool* doRecovery)
1256{
1257
1258 //Wait until complete or failed
1259 uint32_t timer = millis();
1260
1261 while (!(update() & (RF24_TX_DS | RF24_TX_DF))) {
1262 if (millis() - timer > 95) {
1263 #if defined(FAILURE_HANDLING)
1264 flush_rx();
1265 flush_tx();
1266 if (doRecovery) {
1267 *doRecovery = false;
1269 ce(LOW);
1270 return -1;
1271 }
1272 else {
1273 #endif
1274 errNotify();
1275 #if defined(FAILURE_HANDLING)
1276 }
1277 return 0;
1278 #else
1279 delay(100);
1280 #endif
1281 }
1282 }
1283 return 0;
1284}
1285
1286/******************************************************************/
1287
1288void RF24::errHandler()
1289{
1290
1291 #if defined(FAILURE_HANDLING)
1292 flush_tx();
1293 flush_rx();
1294 if (!failureFlushed) {
1295 failureFlushed = true;
1297 }
1298 else {
1299 #endif
1300 errNotify();
1301 #if defined(FAILURE_HANDLING)
1302 failureFlushed = false;
1303 }
1304 ce(LOW);
1305 #endif
1306}
1307
1308#endif
1309
1310/******************************************************************/
1311
1312//Similar to the previous write, clears the interrupt flags
1313bool RF24::write(const void* buf, uint8_t len, const bool multicast)
1314{
1315
1316 //Start Writing
1317#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1318 bool doRecovery = true;
1319 do {
1320#endif
1321 startFastWrite(buf, len, multicast);
1322#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1323 } while (errHandler(&doRecovery) < 0);
1324#endif
1325
1326 ce(LOW);
1327
1328 write_register(STATUS, RF24_IRQ_ALL);
1329
1330 //Max retries exceeded
1331 if (status & RF24_TX_DF) {
1332 flush_tx(); // Only going to be 1 packet in the FIFO at a time using this method, so just flush
1333 return 0;
1334 }
1335 //TX OK 1 or 0
1336 return 1;
1337}
1338
1339/****************************************************************************/
1340
1341bool RF24::write(const void* buf, uint8_t len)
1342{
1343 return write(buf, len, 0);
1344}
1345
1346/****************************************************************************/
1347
1348//For general use, the interrupt flags are not important to clear
1349bool RF24::writeBlocking(const void* buf, uint8_t len, uint32_t timeout)
1350{
1351 //Block until the FIFO is NOT full.
1352 //Keep track of the MAX retries and set auto-retry if seeing failures
1353 //This way the FIFO will fill up and allow blocking until packets go through
1354 //The radio will auto-clear everything in the FIFO as long as CE remains high
1355#if defined(FAILURE_HANDLING)
1356 bool timeoutInvoked = false;
1357#endif
1358
1359 uint32_t timer = millis(); // Get the time that the payload transmission started
1360
1361 while (update() & _BV(TX_FULL)) { // Blocking only if FIFO is full. This will loop and block until TX is successful or timeout
1362
1363 if (status & RF24_TX_DF) { // If MAX Retries have been reached
1364 reUseTX(); // Set re-transmit and clear the MAX_RT interrupt flag
1365 if (millis() - timer > timeout) {
1366#if defined(FAILURE_HANDLING)
1367 failureFlushed = false;
1368#endif
1369 return 0; // If this payload has exceeded the user-defined timeout, exit and return 0
1370 }
1371 }
1372#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1373 if (millis() - timer > (timeout + 95)) {
1374 errHandler();
1375 #if defined(FAILURE_HANDLING)
1376 timeoutInvoked = true;
1377 if (!failureFlushed) {
1378 #endif
1379 return 0;
1380 #if defined(FAILURE_HANDLING)
1381 }
1382 #endif
1383 }
1384#endif
1385 }
1386
1387 //Start Writing
1388 startFastWrite(buf, len, 0); // Write the payload if a buffer is clear
1389#if defined(FAILURE_HANDLING)
1390 if (!timeoutInvoked) {
1391 failureFlushed = false;
1392 }
1393#endif
1394 return 1; // Return 1 to indicate successful transmission
1395}
1396
1397/****************************************************************************/
1398
1400{
1401 ce(LOW);
1402 write_register(STATUS, RF24_TX_DF); //Clear max retry flag
1403 read_register(REUSE_TX_PL, (uint8_t*)nullptr, 0);
1404 IF_RF24_DEBUG(printf_P("[Reusing payload in TX FIFO]"););
1405 ce(HIGH); //Re-Transfer packet
1406}
1407
1408/****************************************************************************/
1409
1410bool RF24::writeFast(const void* buf, uint8_t len, const bool multicast)
1411{
1412 //Block until the FIFO is NOT full.
1413 //Keep track of the MAX retries and set auto-retry if seeing failures
1414 //Return 0 so the user can control the retries and set a timer or failure counter if required
1415 //The radio will auto-clear everything in the FIFO as long as CE remains high
1416
1417#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1418 uint32_t timer = millis();
1419 bool timeoutInvoked = false;
1420#endif
1421
1422 //Blocking only if FIFO is full. This will loop and block until TX is successful or fail
1423 while (update() & _BV(TX_FULL)) {
1424 if (status & RF24_TX_DF) {
1425#if defined(FAILURE_HANDLING)
1426 failureFlushed = false;
1427#endif
1428 return 0; //Return 0. The previous payload has not been retransmitted
1429 // From the user perspective, if you get a 0, call txStandBy()
1430 }
1431#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1432 if (millis() - timer > 95) {
1433 timeoutInvoked = true;
1434 errHandler();
1435 #if defined(FAILURE_HANDLING)
1436 if (!failureFlushed) {
1437 #endif
1438 return 0;
1439 #if defined(FAILURE_HANDLING)
1440 }
1441 #endif
1442 }
1443#endif
1444 }
1445 startFastWrite(buf, len, multicast); // Start Writing
1446#if defined(FAILURE_HANDLING)
1447 if (!timeoutInvoked) {
1448 failureFlushed = false;
1449 }
1450#endif
1451 return 1;
1452}
1453
1454bool RF24::writeFast(const void* buf, uint8_t len)
1455{
1456 return writeFast(buf, len, 0);
1457}
1458
1459/****************************************************************************/
1460
1461//Per the documentation, we want to set PTX Mode when not listening. Then all we do is write data and set CE high
1462//In this mode, if we can keep the FIFO buffers loaded, packets will transmit immediately (no 130us delay)
1463//Otherwise we enter Standby-II mode, which is still faster than standby mode
1464//Also, we remove the need to keep writing the config register over and over and delaying for 150 us each time if sending a stream of data
1465
1466void RF24::startFastWrite(const void* buf, uint8_t len, const bool multicast, bool startTx)
1467{ //TMRh20
1468
1469 write_payload(buf, len, multicast ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD);
1470 if (startTx) {
1471 ce(HIGH);
1472 }
1473}
1474
1475/****************************************************************************/
1476
1477//Added the original startWrite back in so users can still use interrupts, ack payloads, etc
1478//Allows the library to pass all tests
1479bool RF24::startWrite(const void* buf, uint8_t len, const bool multicast)
1480{
1481
1482 // Send the payload
1483 write_payload(buf, len, multicast ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD);
1484 ce(HIGH);
1485#if !defined(F_CPU) || F_CPU > 20000000
1487#endif
1488#ifdef ARDUINO_ARCH_STM32
1489 if (F_CPU > 20000000) {
1491 }
1492#endif
1493 ce(LOW);
1494 return !(status & _BV(TX_FULL));
1495}
1496
1497/****************************************************************************/
1498
1500{
1502}
1503
1504/****************************************************************************/
1505
1507{
1508 uint8_t state = (read_register(FIFO_STATUS) >> (4 * about_tx)) & 3;
1509 return static_cast<rf24_fifo_state_e>(state);
1510}
1511
1512/****************************************************************************/
1513
1514bool RF24::isFifo(bool about_tx, bool check_empty)
1515{
1516 return static_cast<bool>(static_cast<uint8_t>(isFifo(about_tx)) & _BV(!check_empty));
1517}
1518
1519/****************************************************************************/
1520
1522{
1523
1524#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1525 uint32_t timeout = millis();
1526#endif
1527 while (!(read_register(FIFO_STATUS) & _BV(TX_EMPTY))) {
1528 if (status & RF24_TX_DF) {
1529 write_register(STATUS, RF24_TX_DF);
1530 ce(LOW);
1531 flush_tx(); //Non blocking, flush the data
1532#if defined(FAILURE_HANDLING)
1533 failureFlushed = false;
1534#endif
1535 return 0;
1536 }
1537#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1538 if (millis() - timeout > 95) {
1539 errHandler();
1540 return 0;
1541 }
1542#endif
1543 }
1544
1545 ce(LOW); //Set STANDBY-I mode
1546#if defined(FAILURE_HANDLING)
1547 failureFlushed = false;
1548#endif
1549 return 1;
1550}
1551
1552/****************************************************************************/
1553
1554bool RF24::txStandBy(uint32_t timeout, bool startTx)
1555{
1556
1557 if (startTx) {
1558 stopListening();
1559 ce(HIGH);
1560 }
1561 uint32_t start = millis();
1562
1563 while (!(read_register(FIFO_STATUS) & _BV(TX_EMPTY))) {
1564 if (status & RF24_TX_DF) {
1565 write_register(STATUS, RF24_TX_DF);
1566 ce(LOW); // Set re-transmit
1567 ce(HIGH);
1568 if (millis() - start >= timeout) {
1569 ce(LOW);
1570 flush_tx();
1571#if defined(FAILURE_HANDLING)
1572 failureFlushed = false;
1573#endif
1574 return 0;
1575 }
1576 }
1577#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
1578 if (millis() - start > timeout + 95) {
1579 errHandler();
1580 return 0;
1581 }
1582#endif
1583 }
1584
1585 ce(LOW); //Set STANDBY-I mode
1586#if defined(FAILURE_HANDLING)
1587 failureFlushed = false;
1588#endif
1589 return 1;
1590}
1591
1592/****************************************************************************/
1593
1594void RF24::maskIRQ(bool tx, bool fail, bool rx)
1595{
1596 /* clear the interrupt flags */
1597 config_reg = static_cast<uint8_t>(config_reg & ~(1 << MASK_MAX_RT | 1 << MASK_TX_DS | 1 << MASK_RX_DR));
1598 /* set the specified interrupt flags */
1599 config_reg = static_cast<uint8_t>(config_reg | fail << MASK_MAX_RT | tx << MASK_TX_DS | rx << MASK_RX_DR);
1600 write_register(CONFIG, config_reg);
1601}
1602
1603/****************************************************************************/
1604
1606{
1607 uint8_t result = read_register(R_RX_PL_WID);
1608
1609 if (result > 32 || !result) {
1610 flush_rx();
1611 return 0;
1612 }
1613 return result;
1614}
1615
1616/****************************************************************************/
1617
1619{
1620 return (read_register(FIFO_STATUS) & 1) == 0;
1621}
1622
1623/****************************************************************************/
1624
1625bool RF24::available(uint8_t* pipe_num)
1626{
1627 if (available()) { // if RX FIFO is not empty
1628 *pipe_num = (update() >> RX_P_NO) & 0x07;
1629 return 1;
1630 }
1631 return 0;
1632}
1633
1634/****************************************************************************/
1635
1636void RF24::read(void* buf, uint8_t len)
1637{
1638
1639 // Fetch the payload
1640 read_payload(buf, len);
1641
1642 //Clear the only applicable interrupt flags
1643 write_register(STATUS, RF24_RX_DR);
1644}
1645
1646/****************************************************************************/
1647
1648void RF24::whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready)
1649{
1650 // Read the status & reset the status in one easy call
1651 // Or is that such a good idea?
1652 write_register(STATUS, RF24_IRQ_ALL);
1653
1654 // Report to the user what happened
1655 tx_ok = status & RF24_TX_DS;
1656 tx_fail = status & RF24_TX_DF;
1657 rx_ready = status & RF24_RX_DR;
1658}
1659
1660/****************************************************************************/
1661
1662uint8_t RF24::clearStatusFlags(uint8_t flags)
1663{
1664 write_register(STATUS, flags & RF24_IRQ_ALL);
1665 return status;
1666}
1667
1668/****************************************************************************/
1669
1670void RF24::setStatusFlags(uint8_t flags)
1671{
1672 // flip the `flags` to translate from "human understanding"
1673 config_reg = (config_reg & ~RF24_IRQ_ALL) | (~flags & RF24_IRQ_ALL);
1674 write_register(CONFIG, config_reg);
1675}
1676
1677/****************************************************************************/
1678
1680{
1681 return status;
1682}
1683
1684/****************************************************************************/
1685
1687{
1688 read_register(NOP, (uint8_t*)nullptr, 0);
1689 return status;
1690}
1691
1692/****************************************************************************/
1693
1694void RF24::openWritingPipe(uint64_t value)
1695{
1696 // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
1697 // expects it LSB first too, so we're good.
1698
1699 write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width);
1700 write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width);
1701 memcpy(pipe0_writing_address, &value, addr_width);
1702}
1703
1704/****************************************************************************/
1705
1706void RF24::openWritingPipe(const uint8_t* address)
1707{
1708 // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
1709 // expects it LSB first too, so we're good.
1710 write_register(RX_ADDR_P0, address, addr_width);
1711 write_register(TX_ADDR, address, addr_width);
1712 memcpy(pipe0_writing_address, address, addr_width);
1713}
1714
1715/****************************************************************************/
1716
1719
1720void RF24::openReadingPipe(uint8_t child, uint64_t address)
1721{
1722 // If this is pipe 0, cache the address. This is needed because
1723 // openWritingPipe() will overwrite the pipe 0 address, so
1724 // startListening() will have to restore it.
1725 if (child == 0) {
1726 memcpy(pipe0_reading_address, &address, addr_width);
1727 _is_p0_rx = true;
1728 }
1729
1730 if (child <= 5) {
1731 // For pipes 2-5, only write the LSB
1732 if (child > 1) {
1733 write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);
1734 }
1735 // avoid overwriting the TX address on pipe 0 while still in TX mode.
1736 // NOTE, the cached RX address on pipe 0 is written when startListening() is called.
1737 else if (static_cast<bool>(config_reg & _BV(PRIM_RX)) || child != 0) {
1738 write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), addr_width);
1739 }
1740
1741 // Note it would be more efficient to set all of the bits for all open
1742 // pipes at once. However, I thought it would make the calling code
1743 // more simple to do it this way.
1744 write_register(EN_RXADDR, static_cast<uint8_t>(read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child]))));
1745 }
1746}
1747
1748/****************************************************************************/
1749
1750void RF24::setAddressWidth(uint8_t a_width)
1751{
1752 a_width = static_cast<uint8_t>(a_width - 2);
1753 if (a_width) {
1754 write_register(SETUP_AW, static_cast<uint8_t>(a_width % 4));
1755 addr_width = static_cast<uint8_t>((a_width % 4) + 2);
1756 }
1757 else {
1758 write_register(SETUP_AW, static_cast<uint8_t>(0));
1759 addr_width = static_cast<uint8_t>(2);
1760 }
1761}
1762
1763/****************************************************************************/
1764
1765void RF24::openReadingPipe(uint8_t child, const uint8_t* address)
1766{
1767 // If this is pipe 0, cache the address. This is needed because
1768 // openWritingPipe() will overwrite the pipe 0 address, so
1769 // startListening() will have to restore it.
1770 if (child == 0) {
1771 memcpy(pipe0_reading_address, address, addr_width);
1772 _is_p0_rx = true;
1773 }
1774 if (child <= 5) {
1775 // For pipes 2-5, only write the LSB
1776 if (child > 1) {
1777 write_register(pgm_read_byte(&child_pipe[child]), address, 1);
1778 }
1779 // avoid overwriting the TX address on pipe 0 while still in TX mode.
1780 // NOTE, the cached RX address on pipe 0 is written when startListening() is called.
1781 else if (static_cast<bool>(config_reg & _BV(PRIM_RX)) || child != 0) {
1782 write_register(pgm_read_byte(&child_pipe[child]), address, addr_width);
1783 }
1784
1785 // Note it would be more efficient to set all of the bits for all open
1786 // pipes at once. However, I thought it would make the calling code
1787 // more simple to do it this way.
1788 write_register(EN_RXADDR, static_cast<uint8_t>(read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child]))));
1789 }
1790}
1791
1792/****************************************************************************/
1793
1794void RF24::closeReadingPipe(uint8_t pipe)
1795{
1796 write_register(EN_RXADDR, static_cast<uint8_t>(read_register(EN_RXADDR) & ~_BV(pgm_read_byte(&child_pipe_enable[pipe]))));
1797 if (!pipe) {
1798 // keep track of pipe 0's RX state to avoid null vs 0 in addr cache
1799 _is_p0_rx = false;
1800 }
1801}
1802
1803/****************************************************************************/
1804
1805void RF24::toggle_features(void)
1806{
1808#if defined(RF24_SPI_PTR)
1809 status = _spi->transfer(ACTIVATE);
1810 _spi->transfer(0x73);
1811#else
1812 status = _SPI.transfer(ACTIVATE);
1813 _SPI.transfer(0x73);
1814#endif
1816}
1817
1818/****************************************************************************/
1819
1821{
1822 // Enable dynamic payload throughout the system
1823
1824 //toggle_features();
1825 write_register(FEATURE, read_register(FEATURE) | _BV(EN_DPL));
1826
1827 IF_RF24_DEBUG(printf_P("FEATURE=%i\r\n", read_register(FEATURE)));
1828
1829 // Enable dynamic payload on all pipes
1830 //
1831 // Not sure the use case of only having dynamic payload on certain
1832 // pipes, so the library does not support it.
1833 write_register(DYNPD, read_register(DYNPD) | _BV(DPL_P5) | _BV(DPL_P4) | _BV(DPL_P3) | _BV(DPL_P2) | _BV(DPL_P1) | _BV(DPL_P0));
1834
1836}
1837
1838/****************************************************************************/
1839
1841{
1842 // Disables dynamic payload throughout the system. Also disables Ack Payloads
1843
1844 //toggle_features();
1845 write_register(FEATURE, 0);
1846
1847 IF_RF24_DEBUG(printf_P("FEATURE=%i\r\n", read_register(FEATURE)));
1848
1849 // Disable dynamic payload on all pipes
1850 //
1851 // Not sure the use case of only having dynamic payload on certain
1852 // pipes, so the library does not support it.
1853 write_register(DYNPD, 0);
1854
1856 ack_payloads_enabled = false;
1857}
1858
1859/****************************************************************************/
1860
1862{
1863 // enable ack payloads and dynamic payload features
1864
1865 if (!ack_payloads_enabled) {
1866 write_register(FEATURE, read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL));
1867
1868 IF_RF24_DEBUG(printf_P("FEATURE=%i\r\n", read_register(FEATURE)));
1869
1870 // Enable dynamic payload on pipes 0 & 1
1871 write_register(DYNPD, read_register(DYNPD) | _BV(DPL_P1) | _BV(DPL_P0));
1873 ack_payloads_enabled = true;
1874 }
1875}
1876
1877/****************************************************************************/
1878
1880{
1881 // disable ack payloads (leave dynamic payload features as is)
1883 write_register(FEATURE, static_cast<uint8_t>(read_register(FEATURE) & ~_BV(EN_ACK_PAY)));
1884
1885 IF_RF24_DEBUG(printf_P("FEATURE=%i\r\n", read_register(FEATURE)));
1886
1887 ack_payloads_enabled = false;
1888 }
1889}
1890
1891/****************************************************************************/
1892
1894{
1895 //
1896 // enable dynamic ack features
1897 //
1898 //toggle_features();
1899 write_register(FEATURE, read_register(FEATURE) | _BV(EN_DYN_ACK));
1900
1901 IF_RF24_DEBUG(printf_P("FEATURE=%i\r\n", read_register(FEATURE)));
1902}
1903
1904/****************************************************************************/
1905
1906bool RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
1907{
1909 const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
1910
1911 write_register(W_ACK_PAYLOAD | (pipe & 0x07), current, rf24_min(len, static_cast<uint8_t>(32)));
1912 return !(status & _BV(TX_FULL));
1913 }
1914 return 0;
1915}
1916
1917/****************************************************************************/
1918
1920{
1921 return available();
1922}
1923
1924/****************************************************************************/
1925
1927{
1928 return _is_p_variant;
1929}
1930
1931/****************************************************************************/
1932
1933void RF24::setAutoAck(bool enable)
1934{
1935 if (enable) {
1936 write_register(EN_AA, 0x3F);
1937 }
1938 else {
1939 write_register(EN_AA, 0);
1940 // accommodate ACK payloads feature
1943 }
1944 }
1945}
1946
1947/****************************************************************************/
1948
1949void RF24::setAutoAck(uint8_t pipe, bool enable)
1950{
1951 if (pipe < 6) {
1952 uint8_t en_aa = read_register(EN_AA);
1953 if (enable) {
1954 en_aa |= static_cast<uint8_t>(_BV(pipe));
1955 }
1956 else {
1957 en_aa = static_cast<uint8_t>(en_aa & ~_BV(pipe));
1958 if (ack_payloads_enabled && !pipe) {
1960 }
1961 }
1962 write_register(EN_AA, en_aa);
1963 }
1964}
1965
1966/****************************************************************************/
1967
1969{
1970 return (read_register(CD) & 1);
1971}
1972
1973/****************************************************************************/
1974
1976{
1977 return (read_register(RPD) & 1);
1978}
1979
1980/****************************************************************************/
1981
1982void RF24::setPALevel(uint8_t level, bool lnaEnable)
1983{
1984 uint8_t setup = read_register(RF_SETUP) & static_cast<uint8_t>(0xF8);
1985 setup |= _pa_level_reg_value(level, lnaEnable);
1986 write_register(RF_SETUP, setup);
1987}
1988
1989/****************************************************************************/
1990
1991uint8_t RF24::getPALevel(void)
1992{
1993 return (read_register(RF_SETUP) & (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH))) >> 1;
1994}
1995
1996/****************************************************************************/
1997
1998uint8_t RF24::getARC(void)
1999{
2000 return read_register(OBSERVE_TX) & 0x0F;
2001}
2002
2003/****************************************************************************/
2004
2006{
2007 bool result = false;
2008 uint8_t setup = read_register(RF_SETUP);
2009
2010 // HIGH and LOW '00' is 1Mbs - our default
2011 setup = static_cast<uint8_t>(setup & ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)));
2012 setup |= _data_rate_reg_value(speed);
2013
2014 write_register(RF_SETUP, setup);
2015
2016 // Verify our result
2017 if (read_register(RF_SETUP) == setup) {
2018 result = true;
2019 }
2020 return result;
2021}
2022
2023/****************************************************************************/
2024
2026{
2027 rf24_datarate_e result;
2028 uint8_t dr = read_register(RF_SETUP) & (_BV(RF_DR_LOW) | _BV(RF_DR_HIGH));
2029
2030 // switch uses RAM (evil!)
2031 // Order matters in our case below
2032 if (dr == _BV(RF_DR_LOW)) {
2033 // '10' = 250KBPS
2034 result = RF24_250KBPS;
2035 }
2036 else if (dr == _BV(RF_DR_HIGH)) {
2037 // '01' = 2MBPS
2038 result = RF24_2MBPS;
2039 }
2040 else {
2041 // '00' = 1MBPS
2042 result = RF24_1MBPS;
2043 }
2044 return result;
2045}
2046
2047/****************************************************************************/
2048
2050{
2051 config_reg = static_cast<uint8_t>(config_reg & ~(_BV(CRCO) | _BV(EN_CRC)));
2052
2053 // switch uses RAM (evil!)
2054 if (length == RF24_CRC_DISABLED) {
2055 // Do nothing, we turned it off above.
2056 }
2057 else if (length == RF24_CRC_8) {
2058 config_reg |= _BV(EN_CRC);
2059 }
2060 else {
2061 config_reg |= _BV(EN_CRC);
2062 config_reg |= _BV(CRCO);
2063 }
2064 write_register(CONFIG, config_reg);
2065}
2066
2067/****************************************************************************/
2068
2070{
2072 uint8_t AA = read_register(EN_AA);
2073 config_reg = read_register(CONFIG);
2074
2075 if (config_reg & _BV(EN_CRC) || AA) {
2076 if (config_reg & _BV(CRCO)) {
2077 result = RF24_CRC_16;
2078 }
2079 else {
2080 result = RF24_CRC_8;
2081 }
2082 }
2083
2084 return result;
2085}
2086
2087/****************************************************************************/
2088
2090{
2091 config_reg = static_cast<uint8_t>(config_reg & ~_BV(EN_CRC));
2092 write_register(CONFIG, config_reg);
2093}
2094
2095/****************************************************************************/
2096void RF24::setRetries(uint8_t delay, uint8_t count)
2097{
2098 write_register(SETUP_RETR, static_cast<uint8_t>(rf24_min(15, delay) << ARD | rf24_min(15, count)));
2099}
2100
2101/****************************************************************************/
2102void RF24::startConstCarrier(rf24_pa_dbm_e level, uint8_t channel)
2103{
2104 stopListening();
2105 write_register(RF_SETUP, read_register(RF_SETUP) | _BV(CONT_WAVE) | _BV(PLL_LOCK));
2106 if (isPVariant()) {
2107 setAutoAck(0);
2108 setRetries(0, 0);
2109 uint8_t dummy_buf[32];
2110 for (uint8_t i = 0; i < 32; ++i)
2111 dummy_buf[i] = 0xFF;
2112
2113 // use write_register() instead of openWritingPipe() to bypass
2114 // truncation of the address with the current RF24::addr_width value
2115 write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&dummy_buf), 5);
2116 flush_tx(); // so we can write to top level
2117
2118 // use write_register() instead of write_payload() to bypass
2119 // truncation of the payload with the current RF24::payload_size value
2120 write_register(W_TX_PAYLOAD, reinterpret_cast<const uint8_t*>(&dummy_buf), 32);
2121
2122 disableCRC();
2123 }
2124 setPALevel(level);
2125 setChannel(channel);
2126 IF_RF24_DEBUG(printf_P(PSTR("RF_SETUP=%02x\r\n"), read_register(RF_SETUP)));
2127 ce(HIGH);
2128 if (isPVariant()) {
2129 delay(1); // datasheet says 1 ms is ok in this instance
2130 reUseTX(); // CE gets toggled here
2131 }
2132}
2133
2134/****************************************************************************/
2135
2137{
2138 /*
2139 * A note from the datasheet:
2140 * Do not use REUSE_TX_PL together with CONT_WAVE=1. When both these
2141 * registers are set the chip does not react when setting CE low. If
2142 * however, both registers are set PWR_UP = 0 will turn TX mode off.
2143 */
2144 powerDown(); // per datasheet recommendation (just to be safe)
2145 write_register(RF_SETUP, static_cast<uint8_t>(read_register(RF_SETUP) & ~_BV(CONT_WAVE) & ~_BV(PLL_LOCK)));
2146 ce(LOW);
2147 flush_tx();
2148 if (isPVariant()) {
2149 // restore the cached TX address
2150 write_register(TX_ADDR, pipe0_writing_address, addr_width);
2151 }
2152}
2153
2154/****************************************************************************/
2155
2156void RF24::toggleAllPipes(bool isEnabled)
2157{
2158 write_register(EN_RXADDR, static_cast<uint8_t>(isEnabled ? 0x3F : 0));
2159}
2160
2161/****************************************************************************/
2162
2163uint8_t RF24::_data_rate_reg_value(rf24_datarate_e speed)
2164{
2165#if !defined(F_CPU) || F_CPU > 20000000
2166 txDelay = 280;
2167#else //16Mhz Arduino
2168 txDelay = 85;
2169#endif
2170 if (speed == RF24_250KBPS) {
2171#if !defined(F_CPU) || F_CPU > 20000000
2172 txDelay = 505;
2173#else //16Mhz Arduino
2174 txDelay = 155;
2175#endif
2176 // Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0
2177 // Making it '10'.
2178 return static_cast<uint8_t>(_BV(RF_DR_LOW));
2179 }
2180 else if (speed == RF24_2MBPS) {
2181#if !defined(F_CPU) || F_CPU > 20000000
2182 txDelay = 240;
2183#else // 16Mhz Arduino
2184 txDelay = 65;
2185#endif
2186 // Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1
2187 // Making it '01'
2188 return static_cast<uint8_t>(_BV(RF_DR_HIGH));
2189 }
2190 // HIGH and LOW '00' is 1Mbs - our default
2191 return static_cast<uint8_t>(0);
2192}
2193
2194/****************************************************************************/
2195
2196uint8_t RF24::_pa_level_reg_value(uint8_t level, bool lnaEnable)
2197{
2198 // If invalid level, go to max PA
2199 // Else set level as requested
2200 // + lnaEnable (1 or 0) to support the SI24R1 chip extra bit
2201 return static_cast<uint8_t>(((level > RF24_PA_MAX ? static_cast<uint8_t>(RF24_PA_MAX) : level) << 1) + lnaEnable);
2202}
2203
2204/****************************************************************************/
2205
2206void RF24::setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable)
2207{
2208 uint8_t setup = _data_rate_reg_value(speed);
2209 setup |= _pa_level_reg_value(level, lnaEnable);
2210 write_register(RF_SETUP, setup);
2211}
static const PROGMEM char rf24_datarate_e_str_1[]
Definition RF24.cpp:651
static const PROGMEM char rf24_feature_e_str_on[]
Definition RF24.cpp:683
static const PROGMEM char rf24_feature_e_str_open[]
Definition RF24.cpp:685
static const PROGMEM char rf24_feature_e_str_closed[]
Definition RF24.cpp:686
static const PROGMEM char *const rf24_datarate_e_str_P[]
Definition RF24.cpp:653
static const PROGMEM char rf24_pa_dbm_e_str_2[]
Definition RF24.cpp:674
static const PROGMEM char rf24_model_e_str_0[]
Definition RF24.cpp:658
static const PROGMEM char rf24_pa_dbm_e_str_0[]
Definition RF24.cpp:672
static const PROGMEM char *const rf24_crclength_e_str_P[]
Definition RF24.cpp:667
static const PROGMEM char *const rf24_feature_e_str_P[]
Definition RF24.cpp:687
static const PROGMEM char rf24_datarate_e_str_0[]
Definition RF24.cpp:650
static const PROGMEM char rf24_crclength_e_str_2[]
Definition RF24.cpp:666
static const PROGMEM char *const rf24_pa_dbm_e_str_P[]
Definition RF24.cpp:676
static const PROGMEM char rf24_feature_e_str_allowed[]
Definition RF24.cpp:684
static const PROGMEM uint8_t child_pipe[]
Definition RF24.cpp:1717
static const PROGMEM char rf24_model_e_str_1[]
Definition RF24.cpp:659
static const PROGMEM uint8_t child_pipe_enable[]
Definition RF24.cpp:1167
static const PROGMEM char rf24_crclength_e_str_1[]
Definition RF24.cpp:665
static const PROGMEM char rf24_crclength_e_str_0[]
Definition RF24.cpp:664
static const PROGMEM char *const rf24_model_e_str_P[]
Definition RF24.cpp:660
static const PROGMEM char rf24_datarate_e_str_2[]
Definition RF24.cpp:652
static const PROGMEM char rf24_pa_dbm_e_str_1[]
Definition RF24.cpp:673
static const PROGMEM char rf24_pa_dbm_e_str_3[]
Definition RF24.cpp:675
#define RF24_POWERUP_DELAY
Definition RF24_config.h:35
#define sprintf_P
Definition RF24_config.h:66
#define RF24_SPI_SPEED
The default SPI speed (in Hz).
Definition RF24_config.h:44
#define rf24_min(a, b)
Definition RF24_config.h:40
#define FAILURE_HANDLING
Definition RF24_config.h:23
#define rf24_max(a, b)
Definition RF24_config.h:39
void disableAckPayload(void)
Definition RF24.cpp:1879
RF24(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin, uint32_t _spi_speed=RF24_SPI_SPEED)
Definition RF24.cpp:562
uint16_t sprintfPrettyDetails(char *debugging_information)
Definition RF24.cpp:838
bool begin(void)
Definition RF24.cpp:975
uint8_t getPayloadSize(void)
Definition RF24.cpp:641
bool available(void)
Definition RF24.cpp:1618
bool txStandBy()
Definition RF24.cpp:1521
void endTransaction()
Definition RF24.cpp:137
bool failureDetected
Definition RF24.h:1454
void startListening(void)
Definition RF24.cpp:1146
bool isAckPayloadAvailable(void)
Definition RF24.cpp:1919
void ce(bool level)
Definition RF24.cpp:105
void printPrettyDetails(void)
Definition RF24.cpp:741
void setPayloadSize(uint8_t size)
Definition RF24.cpp:628
bool isValid()
Definition RF24.cpp:1139
bool writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
Definition RF24.cpp:1906
void stopConstCarrier(void)
Definition RF24.cpp:2136
rf24_fifo_state_e isFifo(bool about_tx)
Definition RF24.cpp:1506
bool dynamic_payloads_enabled
Definition RF24.h:206
void enableDynamicPayloads(void)
Definition RF24.cpp:1820
bool writeFast(const void *buf, uint8_t len)
Definition RF24.cpp:1454
void disableDynamicPayloads(void)
Definition RF24.cpp:1840
void setRetries(uint8_t delay, uint8_t count)
Definition RF24.cpp:2096
bool write(const void *buf, uint8_t len)
Definition RF24.cpp:1341
uint8_t getARC(void)
Definition RF24.cpp:1998
uint8_t flush_rx(void)
Definition RF24.cpp:471
void beginTransaction()
Definition RF24.cpp:119
void powerUp(void)
Definition RF24.cpp:1224
void setChannel(uint8_t channel)
Definition RF24.cpp:615
void disableCRC(void)
Definition RF24.cpp:2089
void enableDynamicAck()
Definition RF24.cpp:1893
bool isPVariant(void)
Definition RF24.cpp:1926
uint8_t getDynamicPayloadSize(void)
Definition RF24.cpp:1605
bool ack_payloads_enabled
Definition RF24.h:202
uint8_t getChannel(void)
Definition RF24.cpp:621
void stopListening(void)
Definition RF24.cpp:1170
rf24_datarate_e getDataRate(void)
Definition RF24.cpp:2025
bool testRPD(void)
Definition RF24.cpp:1975
uint16_t failureRecoveryAttempts
Definition RF24.h:1455
void setCRCLength(rf24_crclength_e length)
Definition RF24.cpp:2049
void read(void *buf, uint8_t len)
Definition RF24.cpp:1636
uint32_t txDelay
Definition RF24.h:1833
void closeReadingPipe(uint8_t pipe)
Definition RF24.cpp:1794
void read_register(uint8_t reg, uint8_t *buf, uint8_t len)
Definition RF24.cpp:151
void openReadingPipe(uint8_t number, const uint8_t *address)
Definition RF24.cpp:1765
void powerDown(void)
Definition RF24.cpp:1214
void toggleAllPipes(bool isEnabled)
Open or close all data pipes.
Definition RF24.cpp:2156
uint8_t addr_width
Definition RF24.h:204
void setPALevel(uint8_t level, bool lnaEnable=1)
Definition RF24.cpp:1982
rf24_crclength_e getCRCLength(void)
Definition RF24.cpp:2069
void encodeRadioDetails(uint8_t *encoded_status)
Definition RF24.cpp:921
void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready)
Definition RF24.cpp:1594
void enableAckPayload(void)
Definition RF24.cpp:1861
bool isChipConnected()
Definition RF24.cpp:1132
void startConstCarrier(rf24_pa_dbm_e level, uint8_t channel)
Definition RF24.cpp:2102
void startFastWrite(const void *buf, uint8_t len, const bool multicast, bool startTx=1)
Definition RF24.cpp:1466
uint32_t csDelay
Definition RF24.h:1843
bool testCarrier(void)
Definition RF24.cpp:1968
bool rxFifoFull()
Definition RF24.cpp:1499
void setAddressWidth(uint8_t a_width)
Definition RF24.cpp:1750
void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable=true)
configure the RF_SETUP register in 1 transaction
Definition RF24.cpp:2206
uint8_t flush_tx(void)
Definition RF24.cpp:480
bool startWrite(const void *buf, uint8_t len, const bool multicast)
Definition RF24.cpp:1479
void printDetails(void)
Definition RF24.cpp:695
void printStatus(uint8_t flags)
Definition RF24.cpp:490
bool writeBlocking(const void *buf, uint8_t len, uint32_t timeout)
Definition RF24.cpp:1349
void reUseTX()
Definition RF24.cpp:1399
bool setDataRate(rf24_datarate_e speed)
Definition RF24.cpp:2005
void setAutoAck(bool enable)
Definition RF24.cpp:1933
void openWritingPipe(const uint8_t *address)
Definition RF24.cpp:1706
uint8_t getPALevel(void)
Definition RF24.cpp:1991
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
Definition RF24.cpp:1648
rf24_crclength_e
Definition RF24.h:103
@ RF24_CRC_16
Definition RF24.h:109
@ RF24_CRC_DISABLED
Definition RF24.h:105
@ RF24_CRC_8
Definition RF24.h:107
rf24_datarate_e
Definition RF24.h:82
@ RF24_2MBPS
Definition RF24.h:86
@ RF24_250KBPS
Definition RF24.h:88
@ RF24_1MBPS
Definition RF24.h:84
rf24_pa_dbm_e
Definition RF24.h:37
@ RF24_PA_MAX
Definition RF24.h:65
#define delay(millisec)
uint16_t rf24_gpio_pin_t
#define pinMode(pin, direction)
#define _BV(x)
#define HIGH
#define OUTPUT
#define printf_P
#define PROGMEM
#define PRIPSTR
#define delayMicroseconds(usec)
#define PSTR(x)
#define _SPI
#define LOW
#define digitalWrite(pin, value)
#define IF_RF24_DEBUG(x)
#define millis()
#define pgm_read_byte(p)
uint8_t clearStatusFlags(uint8_t flags=RF24_IRQ_ALL)
Definition RF24.cpp:1662
uint8_t getStatusFlags()
Definition RF24.cpp:1679
uint8_t update()
Definition RF24.cpp:1686
void setStatusFlags(uint8_t flags=RF24_IRQ_NONE)
Definition RF24.cpp:1670
@ RF24_TX_DS
Represents an event where TX Data Sent successfully.
Definition RF24.h:148
@ RF24_TX_DF
Represents an event where TX Data Failed to send.
Definition RF24.h:146
@ RF24_RX_DR
Represents an event where RX Data is Ready to RF24::read().
Definition RF24.h:150
@ RF24_IRQ_ALL
Equivalent to RF24_RX_DR | RF24_TX_DS | RF24_TX_DF.
Definition RF24.h:152
rf24_fifo_state_e
Definition RF24.h:121
constexpr uint8_t FEATURE
Definition nRF24L01.h:59
constexpr uint8_t RPD
Definition nRF24L01.h:128
constexpr uint8_t RF_SETUP
Definition nRF24L01.h:40
constexpr uint8_t RX_ADDR_P2
Definition nRF24L01.h:46
constexpr uint8_t R_RX_PL_WID
Definition nRF24L01.h:115
constexpr uint8_t EN_CRC
Definition nRF24L01.h:65
constexpr uint8_t DPL_P3
Definition nRF24L01.h:102
constexpr uint8_t MASK_MAX_RT
Definition nRF24L01.h:64
constexpr uint8_t EN_DYN_ACK
Definition nRF24L01.h:108
constexpr uint8_t CONT_WAVE
Definition nRF24L01.h:85
constexpr uint8_t RX_ADDR_P4
Definition nRF24L01.h:48
constexpr uint8_t MASK_TX_DS
Definition nRF24L01.h:63
constexpr uint8_t DYNPD
Definition nRF24L01.h:58
constexpr uint8_t TX_FULL
Definition nRF24L01.h:92
constexpr uint8_t EN_AA
Definition nRF24L01.h:35
constexpr uint8_t MASK_RX_DR
Definition nRF24L01.h:62
constexpr uint8_t RX_P_NO
Definition nRF24L01.h:91
constexpr uint8_t W_TX_PAYLOAD_NO_ACK
Definition nRF24L01.h:129
constexpr uint8_t OBSERVE_TX
Definition nRF24L01.h:42
constexpr uint8_t ARC_CNT
Definition nRF24L01.h:94
constexpr uint8_t EN_DPL
Definition nRF24L01.h:106
constexpr uint8_t DPL_P1
Definition nRF24L01.h:104
constexpr uint8_t RX_ADDR_P3
Definition nRF24L01.h:47
constexpr uint8_t DPL_P5
Definition nRF24L01.h:100
constexpr uint8_t RX_ADDR_P0
Definition nRF24L01.h:44
constexpr uint8_t RX_ADDR_P1
Definition nRF24L01.h:45
constexpr uint8_t EN_ACK_PAY
Definition nRF24L01.h:107
constexpr uint8_t RF_DR_LOW
Definition nRF24L01.h:132
constexpr uint8_t PRIM_RX
Definition nRF24L01.h:68
constexpr uint8_t RX_FULL
Definition nRF24L01.h:98
constexpr uint8_t DPL_P0
Definition nRF24L01.h:105
constexpr uint8_t ENAA_P0
Definition nRF24L01.h:74
constexpr uint8_t FIFO_STATUS
Definition nRF24L01.h:57
constexpr uint8_t ACTIVATE
Definition nRF24L01.h:114
constexpr uint8_t RF_DR_HIGH
Definition nRF24L01.h:133
constexpr uint8_t RF_CH
Definition nRF24L01.h:39
constexpr uint8_t ERX_P3
Definition nRF24L01.h:77
constexpr uint8_t ERX_P4
Definition nRF24L01.h:76
constexpr uint8_t DPL_P4
Definition nRF24L01.h:101
constexpr uint8_t ENAA_P5
Definition nRF24L01.h:69
constexpr uint8_t STATUS
Definition nRF24L01.h:41
constexpr uint8_t CONFIG
Definition nRF24L01.h:34
constexpr uint8_t NOP
Definition nRF24L01.h:122
constexpr uint8_t ENAA_P4
Definition nRF24L01.h:70
constexpr uint8_t R_RX_PAYLOAD
Definition nRF24L01.h:116
constexpr uint8_t EN_RXADDR
Definition nRF24L01.h:36
constexpr uint8_t ERX_P0
Definition nRF24L01.h:80
constexpr uint8_t ENAA_P2
Definition nRF24L01.h:72
constexpr uint8_t REUSE_TX_PL
Definition nRF24L01.h:121
constexpr uint8_t RX_ADDR_P5
Definition nRF24L01.h:49
constexpr uint8_t RF_PWR_HIGH
Definition nRF24L01.h:135
constexpr uint8_t SETUP_AW
Definition nRF24L01.h:37
constexpr uint8_t CRCO
Definition nRF24L01.h:66
constexpr uint8_t DPL_P2
Definition nRF24L01.h:103
constexpr uint8_t CD
Definition nRF24L01.h:43
constexpr uint8_t PWR_UP
Definition nRF24L01.h:67
constexpr uint8_t ENAA_P1
Definition nRF24L01.h:73
constexpr uint8_t W_TX_PAYLOAD
Definition nRF24L01.h:117
constexpr uint8_t PLL_LOCK
Definition nRF24L01.h:84
constexpr uint8_t SETUP_RETR
Definition nRF24L01.h:38
constexpr uint8_t TX_EMPTY
Definition nRF24L01.h:97
constexpr uint8_t ERX_P5
Definition nRF24L01.h:75
constexpr uint8_t TX_ADDR
Definition nRF24L01.h:50
constexpr uint8_t FLUSH_TX
Definition nRF24L01.h:119
constexpr uint8_t ARD
Definition nRF24L01.h:82
constexpr uint8_t W_ACK_PAYLOAD
Definition nRF24L01.h:118
constexpr uint8_t RF_PWR_LOW
Definition nRF24L01.h:134
constexpr uint8_t ERX_P1
Definition nRF24L01.h:79
constexpr uint8_t FLUSH_RX
Definition nRF24L01.h:120
constexpr uint8_t ENAA_P3
Definition nRF24L01.h:71
constexpr uint8_t W_REGISTER
Definition nRF24L01.h:112
constexpr uint8_t PLOS_CNT
Definition nRF24L01.h:93
constexpr uint8_t RX_PW_P0
Definition nRF24L01.h:51
constexpr uint8_t ERX_P2
Definition nRF24L01.h:78