BayEOS-Arduino  1.8.0_0.0.4
RF24.cpp
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 
15 void RF24::csn(bool mode)
16 {
17 
18 #if defined (RF24_TINY)
19  if (ce_pin != csn_pin) {
20  digitalWrite(csn_pin,mode);
21  }
22  else {
23  if (mode == HIGH) {
24  PORTB |= (1<<PINB2); // SCK->CSN HIGH
25  delayMicroseconds(100); // allow csn to settle.
26  }
27  else {
28  PORTB &= ~(1<<PINB2); // SCK->CSN LOW
29  delayMicroseconds(11); // allow csn to settle
30  }
31  }
32  // Return, CSN toggle complete
33  return;
34 
35 #elif defined(ARDUINO) && !defined (RF24_SPI_TRANSACTIONS)
36  // Minimum ideal SPI bus speed is 2x data rate
37  // If we assume 2Mbs data rate and 16Mhz clock, a
38  // divider of 4 is the minimum we want.
39  // CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
40 
41  #if !defined (SOFTSPI)
42  _SPI.setBitOrder(MSBFIRST);
43  _SPI.setDataMode(SPI_MODE0);
44  _SPI.setClockDivider(SPI_CLOCK_DIV2);
45  #endif
46 #elif defined (RF24_RPi)
47  if(!mode)
48  _SPI.chipSelect(csn_pin);
49 #endif
50 
51 #if !defined (RF24_LINUX)
52  digitalWrite(csn_pin,mode);
53  delayMicroseconds(5);
54 #endif
55 
56 }
57 
58 /****************************************************************************/
59 
60 void RF24::ce(bool level)
61 {
62  //Allow for 3-pin use on ATTiny
63  if (ce_pin != csn_pin) digitalWrite(ce_pin,level);
64 }
65 
66 /****************************************************************************/
67 
68  inline void RF24::beginTransaction() {
69  #if defined (RF24_SPI_TRANSACTIONS)
70  _SPI.beginTransaction(SPISettings(RF24_SPI_SPEED, MSBFIRST, SPI_MODE0));
71  #endif
72  csn(LOW);
73  }
74 
75 /****************************************************************************/
76 
77  inline void RF24::endTransaction() {
78  csn(HIGH);
79  #if defined (RF24_SPI_TRANSACTIONS)
80  _SPI.endTransaction();
81  #endif
82  }
83 
84 /****************************************************************************/
85 
86 uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
87 {
88  uint8_t status;
89 
90  #if defined (RF24_LINUX)
91  beginTransaction(); //configures the spi settings for RPi, locks mutex and setting csn low
92  uint8_t * prx = spi_rxbuff;
93  uint8_t * ptx = spi_txbuff;
94  uint8_t size = len + 1; // Add register value to transmit buffer
95 
96  *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) );
97 
98  while (len--){ *ptx++ = NOP; } // Dummy operation, just for reading
99 
100  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
101 
102  status = *prx++; // status is 1st byte of receive buffer
103 
104  // decrement before to skip status byte
105  while ( --size ){ *buf++ = *prx++; }
106  endTransaction(); //unlocks mutex and setting csn high
107 
108 #else
109 
111  status = _SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
112  while ( len-- ){
113  *buf++ = _SPI.transfer(0xff);
114  }
115  endTransaction();
116 
117 #endif
118 
119  return status;
120 }
121 
122 /****************************************************************************/
123 
124 uint8_t RF24::read_register(uint8_t reg)
125 {
126  uint8_t result;
127 
128  #if defined (RF24_LINUX)
129 
131 
132  uint8_t * prx = spi_rxbuff;
133  uint8_t * ptx = spi_txbuff;
134  *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) );
135  *ptx++ = NOP ; // Dummy operation, just for reading
136 
137  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
138  result = *++prx; // result is 2nd byte of receive buffer
139 
140  endTransaction();
141  #else
142 
144  _SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
145  result = _SPI.transfer(0xff);
146  endTransaction();
147 
148  #endif
149 
150  return result;
151 }
152 
153 /****************************************************************************/
154 
155 uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
156 {
157  uint8_t status;
158 
159  #if defined (RF24_LINUX)
161  uint8_t * prx = spi_rxbuff;
162  uint8_t * ptx = spi_txbuff;
163  uint8_t size = len + 1; // Add register value to transmit buffer
164 
165  *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
166  while ( len-- )
167  *ptx++ = *buf++;
168 
169  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
170  status = *prx; // status is 1st byte of receive buffer
171  endTransaction();
172  #else
173 
175  status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
176  while ( len-- )
177  _SPI.transfer(*buf++);
178  endTransaction();
179 
180  #endif
181 
182  return status;
183 }
184 
185 /****************************************************************************/
186 
187 uint8_t RF24::write_register(uint8_t reg, uint8_t value)
188 {
189  uint8_t status;
190 
191  IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x,%02x)\r\n"),reg,value));
192 
193  #if defined (RF24_LINUX)
195  uint8_t * prx = spi_rxbuff;
196  uint8_t * ptx = spi_txbuff;
197  *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
198  *ptx = value ;
199 
200  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
201  status = *prx++; // status is 1st byte of receive buffer
202  endTransaction();
203  #else
204 
206  status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
207  _SPI.transfer(value);
208  endTransaction();
209 
210  #endif
211 
212  return status;
213 }
214 
215 /****************************************************************************/
216 
217 uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t writeType)
218 {
219  uint8_t status;
220  const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
221 
222  data_len = rf24_min(data_len, payload_size);
223  uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
224 
225  //printf("[Writing %u bytes %u blanks]",data_len,blank_len);
226  IF_SERIAL_DEBUG( printf("[Writing %u bytes %u blanks]\n",data_len,blank_len); );
227 
228  #if defined (RF24_LINUX)
230  uint8_t * prx = spi_rxbuff;
231  uint8_t * ptx = spi_txbuff;
232  uint8_t size;
233  size = data_len + blank_len + 1 ; // Add register value to transmit buffer
234 
235  *ptx++ = writeType;
236  while ( data_len-- )
237  *ptx++ = *current++;
238  while ( blank_len-- )
239  *ptx++ = 0;
240 
241  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
242  status = *prx; // status is 1st byte of receive buffer
243  endTransaction();
244 
245  #else
246 
248  status = _SPI.transfer( writeType );
249  while ( data_len-- ) {
250  _SPI.transfer(*current++);
251  }
252  while ( blank_len-- ) {
253  _SPI.transfer(0);
254  }
255  endTransaction();
256 
257  #endif
258 
259  return status;
260 }
261 
262 /****************************************************************************/
263 
264 uint8_t RF24::read_payload(void* buf, uint8_t data_len)
265 {
266  uint8_t status;
267  uint8_t* current = reinterpret_cast<uint8_t*>(buf);
268 
269  if(data_len > payload_size) data_len = payload_size;
270  uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
271 
272  //printf("[Reading %u bytes %u blanks]",data_len,blank_len);
273 
274  IF_SERIAL_DEBUG( printf("[Reading %u bytes %u blanks]\n",data_len,blank_len); );
275 
276  #if defined (RF24_LINUX)
278  uint8_t * prx = spi_rxbuff;
279  uint8_t * ptx = spi_txbuff;
280  uint8_t size;
281  size = data_len + blank_len + 1; // Add register value to transmit buffer
282 
283  *ptx++ = R_RX_PAYLOAD;
284  while(--size)
285  *ptx++ = NOP;
286 
287  size = data_len + blank_len + 1; // Size has been lost during while, re affect
288 
289  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
290 
291  status = *prx++; // 1st byte is status
292 
293  while ( --data_len ) // Decrement before to skip 1st status byte
294  *current++ = *prx++;
295 
296  *current = *prx;
297  endTransaction();
298  #else
299 
301  status = _SPI.transfer( R_RX_PAYLOAD );
302  while ( data_len-- ) {
303  *current++ = _SPI.transfer(0xFF);
304  }
305  while ( blank_len-- ) {
306  _SPI.transfer(0xff);
307  }
308  endTransaction();
309 
310  #endif
311 
312  return status;
313 }
314 
315 /****************************************************************************/
316 
317 uint8_t RF24::flush_rx(void)
318 {
319  return spiTrans( FLUSH_RX );
320 }
321 
322 /****************************************************************************/
323 
324 uint8_t RF24::flush_tx(void)
325 {
326  return spiTrans( FLUSH_TX );
327 }
328 
329 /****************************************************************************/
330 
331 uint8_t RF24::spiTrans(uint8_t cmd){
332 
333  uint8_t status;
334 
336  status = _SPI.transfer( cmd );
337  endTransaction();
338 
339  return status;
340 }
341 
342 /****************************************************************************/
343 
344 uint8_t RF24::get_status(void)
345 {
346  return spiTrans(NOP);
347 }
348 
349 /****************************************************************************/
350 #if !defined (MINIMAL)
351 void RF24::print_status(uint8_t status)
352 {
353  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"),
354  status,
355  (status & _BV(RX_DR))?1:0,
356  (status & _BV(TX_DS))?1:0,
357  (status & _BV(MAX_RT))?1:0,
358  ((status >> RX_P_NO) & 0b111),
359  (status & _BV(TX_FULL))?1:0
360  );
361 }
362 
363 /****************************************************************************/
364 
365 void RF24::print_observe_tx(uint8_t value)
366 {
367  printf_P(PSTR("OBSERVE_TX=%02x: POLS_CNT=%x ARC_CNT=%x\r\n"),
368  value,
369  (value >> PLOS_CNT) & 0b1111,
370  (value >> ARC_CNT) & 0b1111
371  );
372 }
373 
374 /****************************************************************************/
375 
376 void RF24::print_byte_register(const char* name, uint8_t reg, uint8_t qty)
377 {
378  //char extra_tab = strlen_P(name) < 8 ? '\t' : 0;
379  //printf_P(PSTR(PRIPSTR"\t%c ="),name,extra_tab);
380  #if defined (RF24_LINUX)
381  printf("%s\t =", name);
382  #else
383  printf_P(PSTR(PRIPSTR"\t ="),name);
384  #endif
385  while (qty--)
386  printf_P(PSTR(" 0x%02x"),read_register(reg++));
387  printf_P(PSTR("\r\n"));
388 }
389 
390 /****************************************************************************/
391 
392 void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)
393 {
394 
395  #if defined (RF24_LINUX)
396  printf("%s\t =",name);
397  #else
398  printf_P(PSTR(PRIPSTR"\t ="),name);
399  #endif
400  while (qty--)
401  {
402  uint8_t buffer[addr_width];
403  read_register(reg++,buffer,sizeof buffer);
404 
405  printf_P(PSTR(" 0x"));
406  uint8_t* bufptr = buffer + sizeof buffer;
407  while( --bufptr >= buffer )
408  printf_P(PSTR("%02x"),*bufptr);
409  }
410 
411  printf_P(PSTR("\r\n"));
412 }
413 #endif
414 /****************************************************************************/
415 
416 RF24::RF24(uint8_t _cepin, uint8_t _cspin):
417  ce_pin(_cepin), csn_pin(_cspin), p_variant(false),
418  payload_size(32), dynamic_payloads_enabled(false), addr_width(5)//,pipe0_reading_address(0)
419 {
420  pipe0_reading_address[0]=0;
421 }
422 
423 /****************************************************************************/
424 
425 #if defined (RF24_LINUX) && !defined (MRAA)//RPi constructor
426 RF24::RF24(uint8_t _cepin, uint8_t _cspin, uint32_t _spi_speed):
427  ce_pin(_cepin),csn_pin(_cspin),spi_speed(_spi_speed),p_variant(false), payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0)
428 {
429  pipe0_reading_address[0]=0;
430 }
431 #endif
432 
433 /****************************************************************************/
434 
435 void RF24::setChannel(uint8_t channel)
436 {
437  const uint8_t max_channel = 125;
438  write_register(RF_CH,rf24_min(channel,max_channel));
439 }
440 
442 {
443 
444  return read_register(RF_CH);
445 }
446 /****************************************************************************/
447 
448 void RF24::setPayloadSize(uint8_t size)
449 {
450  payload_size = rf24_min(size,32);
451 }
452 
453 /****************************************************************************/
454 
455 uint8_t RF24::getPayloadSize(void)
456 {
457  return payload_size;
458 }
459 
460 /****************************************************************************/
461 
462 #if !defined (MINIMAL)
463 
464 static const char rf24_datarate_e_str_0[] PROGMEM = "1MBPS";
465 static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS";
466 static const char rf24_datarate_e_str_2[] PROGMEM = "250KBPS";
467 static const char * const rf24_datarate_e_str_P[] PROGMEM = {
468  rf24_datarate_e_str_0,
469  rf24_datarate_e_str_1,
470  rf24_datarate_e_str_2,
471 };
472 static const char rf24_model_e_str_0[] PROGMEM = "nRF24L01";
473 static const char rf24_model_e_str_1[] PROGMEM = "nRF24L01+";
474 static const char * const rf24_model_e_str_P[] PROGMEM = {
475  rf24_model_e_str_0,
476  rf24_model_e_str_1,
477 };
478 static const char rf24_crclength_e_str_0[] PROGMEM = "Disabled";
479 static const char rf24_crclength_e_str_1[] PROGMEM = "8 bits";
480 static const char rf24_crclength_e_str_2[] PROGMEM = "16 bits" ;
481 static const char * const rf24_crclength_e_str_P[] PROGMEM = {
482  rf24_crclength_e_str_0,
483  rf24_crclength_e_str_1,
484  rf24_crclength_e_str_2,
485 };
486 static const char rf24_pa_dbm_e_str_0[] PROGMEM = "PA_MIN";
487 static const char rf24_pa_dbm_e_str_1[] PROGMEM = "PA_LOW";
488 static const char rf24_pa_dbm_e_str_2[] PROGMEM = "PA_HIGH";
489 static const char rf24_pa_dbm_e_str_3[] PROGMEM = "PA_MAX";
490 static const char * const rf24_pa_dbm_e_str_P[] PROGMEM = {
491  rf24_pa_dbm_e_str_0,
492  rf24_pa_dbm_e_str_1,
493  rf24_pa_dbm_e_str_2,
494  rf24_pa_dbm_e_str_3,
495 };
496 
497 #if defined (RF24_LINUX)
498 static const char rf24_csn_e_str_0[] = "CE0 (PI Hardware Driven)";
499 static const char rf24_csn_e_str_1[] = "CE1 (PI Hardware Driven)";
500 static const char rf24_csn_e_str_2[] = "CE2 (PI Hardware Driven)";
501 static const char rf24_csn_e_str_3[] = "Custom GPIO Software Driven";
502 static const char * const rf24_csn_e_str_P[] = {
503  rf24_csn_e_str_0,
504  rf24_csn_e_str_1,
505  rf24_csn_e_str_2,
506  rf24_csn_e_str_3,
507 };
508 #endif
509 
511 {
512 
513 #if defined (RF24_RPi)
514  printf("================ SPI Configuration ================\n" );
515  if (csn_pin < BCM2835_SPI_CS_NONE ){
516  printf("CSN Pin \t = %s\n",rf24_csn_e_str_P[csn_pin]);
517  }else{
518  printf("CSN Pin \t = Custom GPIO%d%s\n", csn_pin,
519  csn_pin==RPI_V2_GPIO_P1_26 ? " (CE1) Software Driven" : "" );
520  }
521  printf("CE Pin \t = Custom GPIO%d\n", ce_pin );
522  printf("Clock Speed\t = " );
523  switch (spi_speed)
524  {
525  case BCM2835_SPI_SPEED_64MHZ : printf("64 Mhz"); break ;
526  case BCM2835_SPI_SPEED_32MHZ : printf("32 Mhz"); break ;
527  case BCM2835_SPI_SPEED_16MHZ : printf("16 Mhz"); break ;
528  case BCM2835_SPI_SPEED_8MHZ : printf("8 Mhz"); break ;
529  case BCM2835_SPI_SPEED_4MHZ : printf("4 Mhz"); break ;
530  case BCM2835_SPI_SPEED_2MHZ : printf("2 Mhz"); break ;
531  case BCM2835_SPI_SPEED_1MHZ : printf("1 Mhz"); break ;
532  case BCM2835_SPI_SPEED_512KHZ: printf("512 KHz"); break ;
533  case BCM2835_SPI_SPEED_256KHZ: printf("256 KHz"); break ;
534  case BCM2835_SPI_SPEED_128KHZ: printf("128 KHz"); break ;
535  case BCM2835_SPI_SPEED_64KHZ : printf("64 KHz"); break ;
536  case BCM2835_SPI_SPEED_32KHZ : printf("32 KHz"); break ;
537  case BCM2835_SPI_SPEED_16KHZ : printf("16 KHz"); break ;
538  case BCM2835_SPI_SPEED_8KHZ : printf("8 KHz"); break ;
539  default : printf("8 Mhz"); break ;
540  }
541  printf("\n================ NRF Configuration ================\n");
542 
543 #endif //Linux
544 
545  print_status(get_status());
546 
547  print_address_register(PSTR("RX_ADDR_P0-1"),RX_ADDR_P0,2);
548  print_byte_register(PSTR("RX_ADDR_P2-5"),RX_ADDR_P2,4);
549  print_address_register(PSTR("TX_ADDR\t"),TX_ADDR);
550 
551  print_byte_register(PSTR("RX_PW_P0-6"),RX_PW_P0,6);
552  print_byte_register(PSTR("EN_AA\t"),EN_AA);
553  print_byte_register(PSTR("EN_RXADDR"),EN_RXADDR);
554  print_byte_register(PSTR("RF_CH\t"),RF_CH);
555  print_byte_register(PSTR("RF_SETUP"),RF_SETUP);
556  print_byte_register(PSTR("CONFIG\t"),CONFIG);
557  print_byte_register(PSTR("DYNPD/FEATURE"),DYNPD,2);
558 
559  printf_P(PSTR("Data Rate\t = " PRIPSTR "\r\n"),pgm_read_word(&rf24_datarate_e_str_P[getDataRate()]));
560  printf_P(PSTR("Model\t\t = " PRIPSTR "\r\n"),pgm_read_word(&rf24_model_e_str_P[isPVariant()]));
561  printf_P(PSTR("CRC Length\t = " PRIPSTR "\r\n"),pgm_read_word(&rf24_crclength_e_str_P[getCRCLength()]));
562  printf_P(PSTR("PA Power\t = " PRIPSTR "\r\n"), pgm_read_word(&rf24_pa_dbm_e_str_P[getPALevel()]));
563 
564 }
565 
566 #endif
567 /****************************************************************************/
568 
569 bool RF24::begin(void)
570 {
571 
572  uint8_t setup=0;
573 
574  #if defined (RF24_LINUX)
575 
576  SPI();
577 
578  #if defined (MRAA)
579  GPIO();
580  gpio.begin(ce_pin,csn_pin);
581  #endif
582 
583 
584  switch(csn_pin){ //Ensure valid hardware CS pin
585  case 0: break;
586  case 1: break;
587  // Allow BCM2835 enums for RPi
588  case 8: csn_pin = 0; break;
589  case 7: csn_pin = 1; break;
590  default: csn_pin = 0; break;
591  }
592 
593  _SPI.begin(csn_pin);
594 
595  pinMode(ce_pin,OUTPUT);
596  ce(LOW);
597 
598  delay(100);
599 
600  #elif defined(LITTLEWIRE)
601  pinMode(csn_pin,OUTPUT);
602  _SPI.begin();
603  csn(HIGH);
604  #else
605  // Initialize pins
606  if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);
607 
608  #if ! defined(LITTLEWIRE)
609  if (ce_pin != csn_pin)
610  #endif
611  pinMode(csn_pin,OUTPUT);
612 
613  _SPI.begin();
614  ce(LOW);
615  csn(HIGH);
616  #if defined (__ARDUINO_X86__)
617  delay(100);
618  #endif
619  #endif //Linux
620 
621  // Must allow the radio time to settle else configuration bits will not necessarily stick.
622  // This is actually only required following power up but some settling time also appears to
623  // be required after resets too. For full coverage, we'll always assume the worst.
624  // Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
625  // Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
626  // WARNING: Delay is based on P-variant whereby non-P *may* require different timing.
627  delay( 5 ) ;
628 
629  // Reset CONFIG and enable 16-bit CRC.
630  write_register( CONFIG, 0b00001100 ) ;
631 
632  // Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
633  // WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
634  // sizes must never be used. See documentation for a more complete explanation.
635  setRetries(5,15);
636 
637  // Reset value is MAX
638  //setPALevel( RF24_PA_MAX ) ;
639 
640  // check for connected module and if this is a p nRF24l01 variant
641  //
642  if( setDataRate( RF24_250KBPS ) )
643  {
644  p_variant = true ;
645  }
646  setup = read_register(RF_SETUP);
647  /*if( setup == 0b00001110 ) // register default for nRF24L01P
648  {
649  p_variant = true ;
650  }*/
651 
652  // Then set the data rate to the slowest (and most reliable) speed supported by all
653  // hardware.
654  setDataRate( RF24_1MBPS ) ;
655 
656  // Initialize CRC and request 2-byte (16bit) CRC
657  //setCRCLength( RF24_CRC_16 ) ;
658 
659  // Disable dynamic payloads, to match dynamic_payloads_enabled setting - Reset value is 0
660  toggle_features();
661  write_register(FEATURE,0 );
662  write_register(DYNPD,0);
663 
664  // Reset current status
665  // Notice reset and flush is the last thing we do
666  write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
667 
668  // Set up default configuration. Callers can always change it later.
669  // This channel should be universally safe and not bleed over into adjacent
670  // spectrum.
671  setChannel(76);
672 
673  // Flush buffers
674  flush_rx();
675  flush_tx();
676 
677  powerUp(); //Power up by default when begin() is called
678 
679  // Enable PTX, do not write CE high so radio will remain in standby I mode ( 130us max to transition to RX or TX instead of 1500us from powerUp )
680  // PTX should use only 22uA of power
681  write_register(CONFIG, ( read_register(CONFIG) ) & ~_BV(PRIM_RX) );
682 
683  // if setup is 0 or ff then there was no response from module
684  return ( setup != 0 && setup != 0xff );
685 }
686 
687 /****************************************************************************/
688 
690 {
691  #if !defined (RF24_TINY) && ! defined(LITTLEWIRE)
692  powerUp();
693  #endif
694  write_register(CONFIG, read_register(CONFIG) | _BV(PRIM_RX));
695  write_register(NRF_STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
696  ce(HIGH);
697  // Restore the pipe0 adddress, if exists
698  if (pipe0_reading_address[0] > 0){
699  write_register(RX_ADDR_P0, pipe0_reading_address, addr_width);
700  }else{
701  closeReadingPipe(0);
702  }
703 
704  // Flush buffers
705  //flush_rx();
706  if(read_register(FEATURE) & _BV(EN_ACK_PAY)){
707  flush_tx();
708  }
709 
710  // Go!
711  //delayMicroseconds(100);
712 }
713 
714 /****************************************************************************/
715 static const uint8_t child_pipe_enable[] PROGMEM =
716 {
717  ERX_P0, ERX_P1, ERX_P2, ERX_P3, ERX_P4, ERX_P5
718 };
719 
721 {
722  ce(LOW);
723 
724  delayMicroseconds(txRxDelay);
725 
726  if(read_register(FEATURE) & _BV(EN_ACK_PAY)){
727  delayMicroseconds(txRxDelay); //200
728  flush_tx();
729  }
730  //flush_rx();
731  write_register(CONFIG, ( read_register(CONFIG) ) & ~_BV(PRIM_RX) );
732 
733  #if defined (RF24_TINY) || defined (LITTLEWIRE)
734  // for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle
735  if (ce_pin == csn_pin) {
736  powerDown();
737  powerUp();
738  }
739  #endif
740  write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[0]))); // Enable RX on pipe0
741 
742  //delayMicroseconds(100);
743 
744 }
745 
746 /****************************************************************************/
747 
748 void RF24::powerDown(void)
749 {
750  ce(LOW); // Guarantee CE is low on powerDown
751  write_register(CONFIG,read_register(CONFIG) & ~_BV(PWR_UP));
752 }
753 
754 /****************************************************************************/
755 
756 //Power up now. Radio will not power down unless instructed by MCU for config changes etc.
757 void RF24::powerUp(void)
758 {
759  uint8_t cfg = read_register(CONFIG);
760 
761  // if not powered up then power up and wait for the radio to initialize
762  if (!(cfg & _BV(PWR_UP))){
763  write_register(CONFIG,read_register(CONFIG) | _BV(PWR_UP));
764 
765  // For nRF24L01+ to go from power down mode to TX or RX mode it must first pass through stand-by mode.
766  // There must be a delay of Tpd2stby (see Table 16.) after the nRF24L01+ leaves power down mode before
767  // the CEis set high. - Tpd2stby can be up to 5ms per the 1.0 datasheet
768  delay(5);
769  }
770 }
771 
772 /******************************************************************/
773 #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
774 void RF24::errNotify(){
775  #if defined (SERIAL_DEBUG) || defined (RF24_LINUX)
776  printf_P(PSTR("RF24 HARDWARE FAIL: Radio not responding, verify pin connections, wiring, etc.\r\n"));
777  #endif
778  #if defined (FAILURE_HANDLING)
779  failureDetected = 1;
780  #else
781  delay(5000);
782  #endif
783 }
784 #endif
785 /******************************************************************/
786 
787 //Similar to the previous write, clears the interrupt flags
788 bool RF24::write( const void* buf, uint8_t len, const bool multicast )
789 {
790  //Start Writing
791  startFastWrite(buf,len,multicast);
792 
793  //Wait until complete or failed
794  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
795  uint32_t timer = millis();
796  #endif
797 
798  while( ! ( get_status() & ( _BV(TX_DS) | _BV(MAX_RT) ))) {
799  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
800  if(millis() - timer > 85){
801  errNotify();
802  #if defined (FAILURE_HANDLING)
803  return 0;
804  #else
805  delay(100);
806  #endif
807  }
808  #endif
809  }
810 
811  ce(LOW);
812 
813  uint8_t status = write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
814 
815  //Max retries exceeded
816  if( status & _BV(MAX_RT)){
817  flush_tx(); //Only going to be 1 packet int the FIFO at a time using this method, so just flush
818  return 0;
819  }
820  //TX OK 1 or 0
821  return 1;
822 }
823 
824 bool RF24::write( const void* buf, uint8_t len ){
825  return write(buf,len,0);
826 }
827 /****************************************************************************/
828 
829 //For general use, the interrupt flags are not important to clear
830 bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout )
831 {
832  //Block until the FIFO is NOT full.
833  //Keep track of the MAX retries and set auto-retry if seeing failures
834  //This way the FIFO will fill up and allow blocking until packets go through
835  //The radio will auto-clear everything in the FIFO as long as CE remains high
836 
837  uint32_t timer = millis(); //Get the time that the payload transmission started
838 
839  while( ( get_status() & ( _BV(TX_FULL) ))) { //Blocking only if FIFO is full. This will loop and block until TX is successful or timeout
840 
841  if( get_status() & _BV(MAX_RT)){ //If MAX Retries have been reached
842  reUseTX(); //Set re-transmit and clear the MAX_RT interrupt flag
843  if(millis() - timer > timeout){ return 0; } //If this payload has exceeded the user-defined timeout, exit and return 0
844  }
845  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
846  if(millis() - timer > (timeout+85) ){
847  errNotify();
848  #if defined (FAILURE_HANDLING)
849  return 0;
850  #endif
851  }
852  #endif
853 
854  }
855 
856  //Start Writing
857  startFastWrite(buf,len,0); //Write the payload if a buffer is clear
858 
859  return 1; //Return 1 to indicate successful transmission
860 }
861 
862 /****************************************************************************/
863 
865  write_register(NRF_STATUS,_BV(MAX_RT) ); //Clear max retry flag
866  spiTrans( REUSE_TX_PL );
867  ce(LOW); //Re-Transfer packet
868  ce(HIGH);
869 }
870 
871 /****************************************************************************/
872 
873 bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast )
874 {
875  //Block until the FIFO is NOT full.
876  //Keep track of the MAX retries and set auto-retry if seeing failures
877  //Return 0 so the user can control the retrys and set a timer or failure counter if required
878  //The radio will auto-clear everything in the FIFO as long as CE remains high
879 
880  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
881  uint32_t timer = millis();
882  #endif
883 
884  while( ( get_status() & ( _BV(TX_FULL) ))) { //Blocking only if FIFO is full. This will loop and block until TX is successful or fail
885 
886  if( get_status() & _BV(MAX_RT)){
887  //reUseTX(); //Set re-transmit
888  write_register(NRF_STATUS,_BV(MAX_RT) ); //Clear max retry flag
889  return 0; //Return 0. The previous payload has been retransmitted
890  //From the user perspective, if you get a 0, just keep trying to send the same payload
891  }
892  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
893  if(millis() - timer > 85 ){
894  errNotify();
895  #if defined (FAILURE_HANDLING)
896  return 0;
897  #endif
898  }
899  #endif
900  }
901  //Start Writing
902  startFastWrite(buf,len,multicast);
903 
904  return 1;
905 }
906 
907 bool RF24::writeFast( const void* buf, uint8_t len ){
908  return writeFast(buf,len,0);
909 }
910 
911 /****************************************************************************/
912 
913 //Per the documentation, we want to set PTX Mode when not listening. Then all we do is write data and set CE high
914 //In this mode, if we can keep the FIFO buffers loaded, packets will transmit immediately (no 130us delay)
915 //Otherwise we enter Standby-II mode, which is still faster than standby mode
916 //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
917 
918 void RF24::startFastWrite( const void* buf, uint8_t len, const bool multicast, bool startTx){ //TMRh20
919 
920  //write_payload( buf,len);
921  write_payload( buf, len,multicast ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ;
922  if(startTx){
923  ce(HIGH);
924  }
925 
926 }
927 
928 /****************************************************************************/
929 
930 //Added the original startWrite back in so users can still use interrupts, ack payloads, etc
931 //Allows the library to pass all tests
932 void RF24::startWrite( const void* buf, uint8_t len, const bool multicast ){
933 
934  // Send the payload
935 
936  //write_payload( buf, len );
937  write_payload( buf, len,multicast? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ;
938  ce(HIGH);
939  #if defined(CORE_TEENSY) || !defined(ARDUINO) || defined (RF24_BBB) || defined (RF24_DUE)
940  delayMicroseconds(10);
941  #endif
942  ce(LOW);
943 
944 
945 }
946 
947 /****************************************************************************/
948 
950  return read_register(FIFO_STATUS) & _BV(RX_FULL);
951 }
952 /****************************************************************************/
953 
955 
956  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
957  uint32_t timeout = millis();
958  #endif
959  while( ! (read_register(FIFO_STATUS) & _BV(TX_EMPTY)) ){
960  if( get_status() & _BV(MAX_RT)){
961  write_register(NRF_STATUS,_BV(MAX_RT) );
962  ce(LOW);
963  flush_tx(); //Non blocking, flush the data
964  return 0;
965  }
966  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
967  if( millis() - timeout > 85){
968  errNotify();
969  #if defined (FAILURE_HANDLING)
970  return 0;
971  #endif
972  }
973  #endif
974  }
975 
976  ce(LOW); //Set STANDBY-I mode
977  return 1;
978 }
979 
980 /****************************************************************************/
981 
982 bool RF24::txStandBy(uint32_t timeout, bool startTx){
983 
984  if(startTx){
985  stopListening();
986  ce(HIGH);
987  }
988  uint32_t start = millis();
989 
990  while( ! (read_register(FIFO_STATUS) & _BV(TX_EMPTY)) ){
991  if( get_status() & _BV(MAX_RT)){
992  write_register(NRF_STATUS,_BV(MAX_RT) );
993  ce(LOW); //Set re-transmit
994  ce(HIGH);
995  if(millis() - start >= timeout){
996  ce(LOW); flush_tx(); return 0;
997  }
998  }
999  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
1000  if( millis() - start > (timeout+85)){
1001  errNotify();
1002  #if defined (FAILURE_HANDLING)
1003  return 0;
1004  #endif
1005  }
1006  #endif
1007  }
1008 
1009 
1010  ce(LOW); //Set STANDBY-I mode
1011  return 1;
1012 
1013 }
1014 
1015 /****************************************************************************/
1016 
1017 void RF24::maskIRQ(bool tx, bool fail, bool rx){
1018 
1019  uint8_t config = read_register(CONFIG);
1020  /* clear the interrupt flags */
1021  config &= ~(1 << MASK_MAX_RT | 1 << MASK_TX_DS | 1 << MASK_RX_DR);
1022  /* set the specified interrupt flags */
1023  config |= fail << MASK_MAX_RT | tx << MASK_TX_DS | rx << MASK_RX_DR;
1024  write_register(CONFIG, config);
1025 }
1026 
1027 /****************************************************************************/
1028 
1030 {
1031  uint8_t result = 0;
1032 
1033  #if defined (RF24_LINUX)
1034  spi_txbuff[0] = R_RX_PL_WID;
1035  spi_rxbuff[1] = 0xff;
1036  beginTransaction();
1037  _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
1038  result = spi_rxbuff[1];
1039  endTransaction();
1040  #else
1041  beginTransaction();
1042  _SPI.transfer( R_RX_PL_WID );
1043  result = _SPI.transfer(0xff);
1044  endTransaction();
1045  #endif
1046 
1047  if(result > 32) { flush_rx(); delay(2); return 0; }
1048  return result;
1049 }
1050 
1051 /****************************************************************************/
1052 
1054 {
1055  return available(NULL);
1056 }
1057 
1058 /****************************************************************************/
1059 
1060 bool RF24::available(uint8_t* pipe_num)
1061 {
1062  if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){
1063 
1064  // If the caller wants the pipe number, include that
1065  if ( pipe_num ){
1066  uint8_t status = get_status();
1067  *pipe_num = ( status >> RX_P_NO ) & 0b111;
1068  }
1069  return 1;
1070  }
1071 
1072 
1073  return 0;
1074 
1075 
1076 }
1077 
1078 /****************************************************************************/
1079 
1080 void RF24::read( void* buf, uint8_t len ){
1081 
1082  // Fetch the payload
1083  read_payload( buf, len );
1084 
1085  //Clear the two possible interrupt flags with one command
1086  write_register(NRF_STATUS,_BV(RX_DR) | _BV(MAX_RT) | _BV(TX_DS) );
1087 
1088 }
1089 
1090 /****************************************************************************/
1091 
1092 void RF24::whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready)
1093 {
1094  // Read the status & reset the status in one easy call
1095  // Or is that such a good idea?
1096  uint8_t status = write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
1097 
1098  // Report to the user what happened
1099  tx_ok = status & _BV(TX_DS);
1100  tx_fail = status & _BV(MAX_RT);
1101  rx_ready = status & _BV(RX_DR);
1102 }
1103 
1104 /****************************************************************************/
1105 
1106 void RF24::openWritingPipe(uint64_t value)
1107 {
1108  // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
1109  // expects it LSB first too, so we're good.
1110 
1111  write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width);
1112  write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width);
1113 
1114 
1115  //const uint8_t max_payload_size = 32;
1116  //write_register(RX_PW_P0,rf24_min(payload_size,max_payload_size));
1117  write_register(RX_PW_P0,payload_size);
1118 }
1119 
1120 /****************************************************************************/
1121 void RF24::openWritingPipe(const uint8_t *address)
1122 {
1123  // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
1124  // expects it LSB first too, so we're good.
1125 
1126  write_register(RX_ADDR_P0,address, addr_width);
1127  write_register(TX_ADDR, address, addr_width);
1128 
1129  //const uint8_t max_payload_size = 32;
1130  //write_register(RX_PW_P0,rf24_min(payload_size,max_payload_size));
1131  write_register(RX_PW_P0,payload_size);
1132 }
1133 
1134 /****************************************************************************/
1135 static const uint8_t child_pipe[] PROGMEM =
1136 {
1137  RX_ADDR_P0, RX_ADDR_P1, RX_ADDR_P2, RX_ADDR_P3, RX_ADDR_P4, RX_ADDR_P5
1138 };
1139 static const uint8_t child_payload_size[] PROGMEM =
1140 {
1141  RX_PW_P0, RX_PW_P1, RX_PW_P2, RX_PW_P3, RX_PW_P4, RX_PW_P5
1142 };
1143 
1144 
1145 void RF24::openReadingPipe(uint8_t child, uint64_t address)
1146 {
1147  // If this is pipe 0, cache the address. This is needed because
1148  // openWritingPipe() will overwrite the pipe 0 address, so
1149  // startListening() will have to restore it.
1150  if (child == 0){
1151  memcpy(pipe0_reading_address,&address,addr_width);
1152  }
1153 
1154  if (child <= 6)
1155  {
1156  // For pipes 2-5, only write the LSB
1157  if ( child < 2 )
1158  write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), addr_width);
1159  else
1160  write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);
1161 
1162  write_register(pgm_read_byte(&child_payload_size[child]),payload_size);
1163 
1164  // Note it would be more efficient to set all of the bits for all open
1165  // pipes at once. However, I thought it would make the calling code
1166  // more simple to do it this way.
1167  write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child])));
1168  }
1169 }
1170 
1171 /****************************************************************************/
1172 void RF24::setAddressWidth(uint8_t a_width){
1173 
1174  if(a_width -= 2){
1175  write_register(SETUP_AW,a_width%4);
1176  addr_width = (a_width%4) + 2;
1177  }
1178 
1179 }
1180 
1181 /****************************************************************************/
1182 
1183 void RF24::openReadingPipe(uint8_t child, const uint8_t *address)
1184 {
1185  // If this is pipe 0, cache the address. This is needed because
1186  // openWritingPipe() will overwrite the pipe 0 address, so
1187  // startListening() will have to restore it.
1188  if (child == 0){
1189  memcpy(pipe0_reading_address,address,addr_width);
1190  }
1191  if (child <= 6)
1192  {
1193  // For pipes 2-5, only write the LSB
1194  if ( child < 2 ){
1195  write_register(pgm_read_byte(&child_pipe[child]), address, addr_width);
1196  }else{
1197  write_register(pgm_read_byte(&child_pipe[child]), address, 1);
1198  }
1199  write_register(pgm_read_byte(&child_payload_size[child]),payload_size);
1200 
1201  // Note it would be more efficient to set all of the bits for all open
1202  // pipes at once. However, I thought it would make the calling code
1203  // more simple to do it this way.
1204  write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child])));
1205 
1206  }
1207 }
1208 
1209 /****************************************************************************/
1210 
1211 void RF24::closeReadingPipe( uint8_t pipe )
1212 {
1213  write_register(EN_RXADDR,read_register(EN_RXADDR) & ~_BV(pgm_read_byte(&child_pipe_enable[pipe])));
1214 }
1215 
1216 /****************************************************************************/
1217 
1218 void RF24::toggle_features(void)
1219 {
1220  beginTransaction();
1221  _SPI.transfer( ACTIVATE );
1222  _SPI.transfer( 0x73 );
1223  endTransaction();
1224 }
1225 
1226 /****************************************************************************/
1227 
1229 {
1230  // Enable dynamic payload throughout the system
1231 
1232  //toggle_features();
1233  write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );
1234 
1235 
1236  IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
1237 
1238  // Enable dynamic payload on all pipes
1239  //
1240  // Not sure the use case of only having dynamic payload on certain
1241  // pipes, so the library does not support it.
1242  write_register(DYNPD,read_register(DYNPD) | _BV(DPL_P5) | _BV(DPL_P4) | _BV(DPL_P3) | _BV(DPL_P2) | _BV(DPL_P1) | _BV(DPL_P0));
1243 
1244  dynamic_payloads_enabled = true;
1245 }
1246 
1247 /****************************************************************************/
1248 
1250 {
1251  //
1252  // enable ack payload and dynamic payload features
1253  //
1254 
1255  //toggle_features();
1256  write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );
1257 
1258  IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
1259 
1260  //
1261  // Enable dynamic payload on pipes 0 & 1
1262  //
1263 
1264  write_register(DYNPD,read_register(DYNPD) | _BV(DPL_P1) | _BV(DPL_P0));
1265  dynamic_payloads_enabled = true;
1266 }
1267 
1268 /****************************************************************************/
1269 
1271  //
1272  // enable dynamic ack features
1273  //
1274  //toggle_features();
1275  write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) );
1276 
1277  IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
1278 
1279 
1280 }
1281 
1282 /****************************************************************************/
1283 
1284 void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
1285 {
1286  const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
1287 
1288  uint8_t data_len = rf24_min(len,32);
1289 
1290  #if defined (RF24_LINUX)
1291  beginTransaction();
1292  uint8_t * ptx = spi_txbuff;
1293  uint8_t size = data_len + 1 ; // Add register value to transmit buffer
1294  *ptx++ = W_ACK_PAYLOAD | ( pipe & 0b111 );
1295  while ( data_len-- ){
1296  *ptx++ = *current++;
1297  }
1298 
1299  _SPI.transfern( (char *) spi_txbuff, size);
1300  endTransaction();
1301  #else
1302  beginTransaction();
1303  _SPI.transfer(W_ACK_PAYLOAD | ( pipe & 0b111 ) );
1304 
1305  while ( data_len-- )
1306  _SPI.transfer(*current++);
1307  endTransaction();
1308 
1309  #endif
1310 
1311 }
1312 
1313 /****************************************************************************/
1314 
1316 {
1317  return ! (read_register(FIFO_STATUS) & _BV(RX_EMPTY));
1318 }
1319 
1320 /****************************************************************************/
1321 
1323 {
1324  return p_variant ;
1325 }
1326 
1327 /****************************************************************************/
1328 
1329 void RF24::setAutoAck(bool enable)
1330 {
1331  if ( enable )
1332  write_register(EN_AA, 0b111111);
1333  else
1334  write_register(EN_AA, 0);
1335 }
1336 
1337 /****************************************************************************/
1338 
1339 void RF24::setAutoAck( uint8_t pipe, bool enable )
1340 {
1341  if ( pipe <= 6 )
1342  {
1343  uint8_t en_aa = read_register( EN_AA ) ;
1344  if( enable )
1345  {
1346  en_aa |= _BV(pipe) ;
1347  }
1348  else
1349  {
1350  en_aa &= ~_BV(pipe) ;
1351  }
1352  write_register( EN_AA, en_aa ) ;
1353  }
1354 }
1355 
1356 /****************************************************************************/
1357 
1359 {
1360  return ( read_register(CD) & 1 );
1361 }
1362 
1363 /****************************************************************************/
1364 
1365 bool RF24::testRPD(void)
1366 {
1367  return ( read_register(RPD) & 1 ) ;
1368 }
1369 
1370 /****************************************************************************/
1371 
1372 void RF24::setPALevel(uint8_t level)
1373 {
1374 
1375  uint8_t setup = read_register(RF_SETUP) & 0b11111000;
1376 
1377  if(level > 3){ // If invalid level, go to max PA
1378  level = (RF24_PA_MAX << 1) + 1; // +1 to support the SI24R1 chip extra bit
1379  }else{
1380  level = (level << 1) + 1; // Else set level as requested
1381  }
1382 
1383 
1384  write_register( RF_SETUP, setup |= level ) ; // Write it to the chip
1385 }
1386 
1387 /****************************************************************************/
1388 
1389 uint8_t RF24::getPALevel(void)
1390 {
1391 
1392  return (read_register(RF_SETUP) & (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH))) >> 1 ;
1393 }
1394 
1395 /****************************************************************************/
1396 
1398 {
1399  bool result = false;
1400  uint8_t setup = read_register(RF_SETUP) ;
1401 
1402  // HIGH and LOW '00' is 1Mbs - our default
1403  setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ;
1404 
1405  #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__)
1406  txRxDelay=250;
1407  #else //16Mhz Arduino
1408  txRxDelay=85;
1409  #endif
1410  if( speed == RF24_250KBPS )
1411  {
1412  // Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0
1413  // Making it '10'.
1414  setup |= _BV( RF_DR_LOW ) ;
1415  #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__)
1416  txRxDelay=450;
1417  #else //16Mhz Arduino
1418  txRxDelay=155;
1419  #endif
1420  }
1421  else
1422  {
1423  // Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1
1424  // Making it '01'
1425  if ( speed == RF24_2MBPS )
1426  {
1427  setup |= _BV(RF_DR_HIGH);
1428  #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__)
1429  txRxDelay=190;
1430  #else //16Mhz Arduino
1431  txRxDelay=65;
1432  #endif
1433  }
1434  }
1435  write_register(RF_SETUP,setup);
1436 
1437  // Verify our result
1438  if ( read_register(RF_SETUP) == setup )
1439  {
1440  result = true;
1441  }
1442  return result;
1443 }
1444 
1445 /****************************************************************************/
1446 
1448 {
1449  rf24_datarate_e result ;
1450  uint8_t dr = read_register(RF_SETUP) & (_BV(RF_DR_LOW) | _BV(RF_DR_HIGH));
1451 
1452  // switch uses RAM (evil!)
1453  // Order matters in our case below
1454  if ( dr == _BV(RF_DR_LOW) )
1455  {
1456  // '10' = 250KBPS
1457  result = RF24_250KBPS ;
1458  }
1459  else if ( dr == _BV(RF_DR_HIGH) )
1460  {
1461  // '01' = 2MBPS
1462  result = RF24_2MBPS ;
1463  }
1464  else
1465  {
1466  // '00' = 1MBPS
1467  result = RF24_1MBPS ;
1468  }
1469  return result ;
1470 }
1471 
1472 /****************************************************************************/
1473 
1475 {
1476  uint8_t config = read_register(CONFIG) & ~( _BV(CRCO) | _BV(EN_CRC)) ;
1477 
1478  // switch uses RAM (evil!)
1479  if ( length == RF24_CRC_DISABLED )
1480  {
1481  // Do nothing, we turned it off above.
1482  }
1483  else if ( length == RF24_CRC_8 )
1484  {
1485  config |= _BV(EN_CRC);
1486  }
1487  else
1488  {
1489  config |= _BV(EN_CRC);
1490  config |= _BV( CRCO );
1491  }
1492  write_register( CONFIG, config ) ;
1493 }
1494 
1495 /****************************************************************************/
1496 
1498 {
1499  rf24_crclength_e result = RF24_CRC_DISABLED;
1500 
1501  uint8_t config = read_register(CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;
1502  uint8_t AA = read_register(EN_AA);
1503 
1504  if ( config & _BV(EN_CRC ) || AA)
1505  {
1506  if ( config & _BV(CRCO) )
1507  result = RF24_CRC_16;
1508  else
1509  result = RF24_CRC_8;
1510  }
1511 
1512  return result;
1513 }
1514 
1515 /****************************************************************************/
1516 
1517 void RF24::disableCRC( void )
1518 {
1519  uint8_t disable = read_register(CONFIG) & ~_BV(EN_CRC) ;
1520  write_register( CONFIG, disable ) ;
1521 }
1522 
1523 /****************************************************************************/
1524 void RF24::setRetries(uint8_t delay, uint8_t count)
1525 {
1526  write_register(SETUP_RETR,(delay&0xf)<<ARD | (count&0xf)<<ARC);
1527 }
1528 
1529 
1530 //ATTiny support code pulled in from https://github.com/jscrane/RF24
1531 
1532 #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
1533 // see http://gammon.com.au/spi
1534 # define DI 0 // D0, pin 5 Data In
1535 # define DO 1 // D1, pin 6 Data Out (this is *not* MOSI)
1536 # define USCK 2 // D2, pin 7 Universal Serial Interface clock
1537 # define SS 3 // D3, pin 2 Slave Select
1538 #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
1539 // these depend on the core used (check pins_arduino.h)
1540 // this is for jeelabs' one (based on google-code core)
1541 # define DI 4 // PA6
1542 # define DO 5 // PA5
1543 # define USCK 6 // PA4
1544 # define SS 3 // PA7
1545 #elif defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__)
1546 // these depend on the core used (check pins_arduino.h)
1547 // tested with google-code core
1548 # define DI 14 // PB5
1549 # define DO 15 // PB6
1550 # define USCK 16 // PB7
1551 # define SS 13 // PB4
1552 #endif
1553 
1554 #if defined(RF24_TINY)
1555 
1556 void SPIClass::begin() {
1557 
1558  pinMode(USCK, OUTPUT);
1559  pinMode(DO, OUTPUT);
1560  pinMode(DI, INPUT);
1561  USICR = _BV(USIWM0);
1562 
1563 }
1564 
1565 byte SPIClass::transfer(byte b) {
1566 
1567  USIDR = b;
1568  USISR = _BV(USIOIF);
1569  do
1570  USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC);
1571  while ((USISR & _BV(USIOIF)) == 0);
1572  return USIDR;
1573 
1574 }
1575 
1576 void SPIClass::end() {}
1577 void SPIClass::setDataMode(uint8_t mode){}
1578 void SPIClass::setBitOrder(uint8_t bitOrder){}
1579 void SPIClass::setClockDivider(uint8_t rate){}
1580 
1581 
1582 #endif
bool testRPD(void)
Definition: RF24.cpp:1365
void printDetails(void)
Definition: RF24.cpp:510
void setAutoAck(bool enable)
Definition: RF24.cpp:1329
bool failureDetected
Definition: RF24.h:676
bool isAckPayloadAvailable(void)
Definition: RF24.cpp:1315
bool available(void)
Definition: RF24.cpp:1053
void closeReadingPipe(uint8_t pipe)
Definition: RF24.cpp:1211
void startWrite(const void *buf, uint8_t len, const bool multicast)
Definition: RF24.cpp:932
void enableAckPayload(void)
Definition: RF24.cpp:1249
void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready)
Definition: RF24.cpp:1017
uint8_t getPayloadSize(void)
Definition: RF24.cpp:455
void setChannel(uint8_t channel)
Definition: RF24.cpp:435
uint8_t getChannel(void)
Definition: RF24.cpp:441
bool testCarrier(void)
Definition: RF24.cpp:1358
void setRetries(uint8_t delay, uint8_t count)
Definition: RF24.cpp:1524
void reUseTX()
Definition: RF24.cpp:864
bool write(const void *buf, uint8_t len)
Definition: RF24.cpp:824
void startListening(void)
Definition: RF24.cpp:689
rf24_datarate_e
Definition: iBoardRF24.h:33
bool writeBlocking(const void *buf, uint8_t len, uint32_t timeout)
Definition: RF24.cpp:830
bool writeFast(const void *buf, uint8_t len)
Definition: RF24.cpp:907
bool isPVariant(void)
Definition: RF24.cpp:1322
bool rxFifoFull()
Definition: RF24.cpp:949
void enableDynamicPayloads(void)
Definition: RF24.cpp:1228
void disableCRC(void)
Definition: RF24.cpp:1517
rf24_datarate_e getDataRate(void)
Definition: RF24.cpp:1447
void writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
Definition: RF24.cpp:1284
RF24(uint8_t _cepin, uint8_t _cspin)
Definition: RF24.cpp:416
void setPayloadSize(uint8_t size)
Definition: RF24.cpp:448
void startFastWrite(const void *buf, uint8_t len, const bool multicast, bool startTx=1)
Definition: RF24.cpp:918
uint8_t getDynamicPayloadSize(void)
Definition: RF24.cpp:1029
uint8_t flush_tx(void)
Definition: RF24.cpp:324
uint8_t getPALevel(void)
Definition: RF24.cpp:1389
bool setDataRate(rf24_datarate_e speed)
Definition: RF24.cpp:1397
rf24_crclength_e getCRCLength(void)
Definition: RF24.cpp:1497
void beginTransaction()
Definition: RF24.cpp:68
void openWritingPipe(const uint8_t *address)
Definition: RF24.cpp:1121
void setCRCLength(rf24_crclength_e length)
Definition: RF24.cpp:1474
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
Definition: RF24.cpp:1092
void powerUp(void)
Definition: RF24.cpp:757
void read(void *buf, uint8_t len)
Definition: RF24.cpp:1080
void stopListening(void)
Definition: RF24.cpp:720
void powerDown(void)
Definition: RF24.cpp:748
bool begin(void)
Definition: RF24.cpp:569
void enableDynamicAck()
Definition: RF24.cpp:1270
void openReadingPipe(uint8_t number, const uint8_t *address)
Definition: RF24.cpp:1183
void setPALevel(uint8_t level)
Definition: RF24.cpp:1372
void setAddressWidth(uint8_t a_width)
Definition: RF24.cpp:1172
bool txStandBy()
Definition: RF24.cpp:954
rf24_crclength_e
Definition: iBoardRF24.h:40