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