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