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