11 #include "RF24_config.h" 13 #include "digitalWriteFast.h" 20 digitalWriteFast2(csn_pin,mode);
27 digitalWriteFast2(ce_pin,level);
34 digitalWriteFast2(mosi_pin, 1);
36 digitalWriteFast2(mosi_pin, 0);
38 digitalWriteFast2(sck_pin, 1);
40 if(digitalReadFast2(miso_pin) == 1) {
43 digitalWriteFast2(sck_pin, 0);
55 status =
SPI_RW( R_REGISTER | ( REGISTER_MASK & reg ) );
69 SPI_RW( R_REGISTER | ( REGISTER_MASK & reg ) );
70 uint8_t result =
SPI_RW(0xff);
83 status =
SPI_RW( W_REGISTER | ( REGISTER_MASK & reg ) );
99 status =
SPI_RW( W_REGISTER | ( REGISTER_MASK & reg ) );
112 const uint8_t* current =
reinterpret_cast<const uint8_t*
>(buf);
114 uint8_t data_len = min(len,payload_size);
115 uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
120 status =
SPI_RW( W_TX_PAYLOAD );
123 while ( blank_len-- )
135 uint8_t* current =
reinterpret_cast<uint8_t*
>(buf);
137 uint8_t data_len = min(len,payload_size);
138 uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
143 status =
SPI_RW( R_RX_PAYLOAD );
145 *current++ =
SPI_RW(0xff);
146 while ( blank_len-- )
160 status =
SPI_RW( FLUSH_RX );
173 status =
SPI_RW( FLUSH_TX );
196 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"),
198 (status & _BV(RX_DR))?1:0,
199 (status & _BV(TX_DS))?1:0,
200 (status & _BV(MAX_RT))?1:0,
201 ((status >> RX_P_NO) & B111),
202 (status & _BV(TX_FULL))?1:0
210 printf_P(PSTR(
"OBSERVE_TX=%02x: POLS_CNT=%x ARC_CNT=%x\r\n"),
212 (value >> PLOS_CNT) & B1111,
213 (value >> ARC_CNT) & B1111
221 char extra_tab = strlen_P(name) < 8 ?
'\t' : 0;
222 printf_P(PSTR(PRIPSTR
"\t%c ="),name,extra_tab);
225 printf_P(PSTR(
"\r\n"));
232 char extra_tab = strlen_P(name) < 8 ?
'\t' : 0;
233 printf_P(PSTR(PRIPSTR
"\t%c ="),name,extra_tab);
240 printf_P(PSTR(
" 0x"));
241 uint8_t* bufptr = buffer +
sizeof buffer;
242 while( --bufptr >= buffer )
243 printf_P(PSTR(
"%02x"),*bufptr);
246 printf_P(PSTR(
"\r\n"));
251 iBoardRF24::iBoardRF24(uint8_t _cepin, uint8_t _cspin, uint8_t _mosi_pin, uint8_t _miso_pin, uint8_t _sck_pin, uint8_t _irq_pin):
252 ce_pin(_cepin), csn_pin(_cspin), mosi_pin(_mosi_pin), miso_pin(_miso_pin), sck_pin(_sck_pin), irq_pin(_irq_pin),
253 wide_band(true), p_variant(false), payload_size(32), ack_payload_available(false),
254 dynamic_payloads_enabled(false), pipe0_reading_address(0)
265 const uint8_t max_channel = 127;
273 const uint8_t max_payload_size = 32;
274 payload_size = min(size,max_payload_size);
286 static const char rf24_datarate_e_str_0[] PROGMEM =
"1MBPS";
287 static const char rf24_datarate_e_str_1[] PROGMEM =
"2MBPS";
288 static const char rf24_datarate_e_str_2[] PROGMEM =
"250KBPS";
289 static const char *
const rf24_datarate_e_str_P[] PROGMEM = {
290 rf24_datarate_e_str_0,
291 rf24_datarate_e_str_1,
292 rf24_datarate_e_str_2,
294 static const char rf24_model_e_str_0[] PROGMEM =
"nRF24L01";
295 static const char rf24_model_e_str_1[] PROGMEM =
"nRF24L01+";
296 static const char *
const rf24_model_e_str_P[] PROGMEM = {
300 static const char rf24_crclength_e_str_0[] PROGMEM =
"Disabled";
301 static const char rf24_crclength_e_str_1[] PROGMEM =
"8 bits";
302 static const char rf24_crclength_e_str_2[] PROGMEM =
"16 bits" ;
303 static const char *
const rf24_crclength_e_str_P[] PROGMEM = {
304 rf24_crclength_e_str_0,
305 rf24_crclength_e_str_1,
306 rf24_crclength_e_str_2,
308 static const char rf24_pa_dbm_e_str_0[] PROGMEM =
"PA_MIN";
309 static const char rf24_pa_dbm_e_str_1[] PROGMEM =
"PA_LOW";
310 static const char rf24_pa_dbm_e_str_2[] PROGMEM =
"LA_MED";
311 static const char rf24_pa_dbm_e_str_3[] PROGMEM =
"PA_HIGH";
312 static const char *
const rf24_pa_dbm_e_str_P[] PROGMEM = {
335 printf_P(PSTR(
"Data Rate\t = %S\r\n"),pgm_read_word(&rf24_datarate_e_str_P[
getDataRate()]));
336 printf_P(PSTR(
"Model\t\t = %S\r\n"),pgm_read_word(&rf24_model_e_str_P[
isPVariant()]));
337 printf_P(PSTR(
"CRC Length\t = %S\r\n"),pgm_read_word(&rf24_crclength_e_str_P[
getCRCLength()]));
338 printf_P(PSTR(
"PA Power\t = %S\r\n"),pgm_read_word(&rf24_pa_dbm_e_str_P[
getPALevel()]));
346 pinMode(ce_pin,OUTPUT);
347 pinMode(csn_pin,OUTPUT);
349 pinMode(miso_pin, INPUT);
350 pinMode(irq_pin, INPUT);
351 pinMode(mosi_pin, OUTPUT);
352 pinMode(sck_pin, OUTPUT);
416 if (pipe0_reading_address)
417 write_register(RX_ADDR_P0, reinterpret_cast<const uint8_t*>(&pipe0_reading_address), 5);
427 delayMicroseconds(130);
475 uint32_t sent_at = millis();
476 const uint32_t timeout = 500;
480 IF_SERIAL_DEBUG(Serial.print(observe_tx,HEX));
482 while( ! ( status & ( _BV(TX_DS) | _BV(MAX_RT) ) ) && ( millis() - sent_at < timeout ) );
499 IF_SERIAL_DEBUG(Serial.print(result?
"...OK.":
"...Failed"));
502 if ( ack_payload_available )
505 IF_SERIAL_DEBUG(Serial.print(
"[AckPacket]/"));
506 IF_SERIAL_DEBUG(Serial.println(ack_payload_length,DEC));
525 delayMicroseconds(150);
532 delayMicroseconds(15);
566 *pipe_num = ( status >> RX_P_NO ) & 0b111;
579 bool result = ( status & _BV(RX_DR) );
580 if(! result)
return 0;
583 *pipe =( status >> RX_P_NO ) & B111;
586 uint8_t* current =
reinterpret_cast<uint8_t*
>(buf);
590 *pipe==((status >> RX_P_NO) & B111) )
601 if ( status & _BV(TX_DS) )
610 result = ( status & _BV(RX_DR) );
626 write_register(NRF_STATUS,_BV(RX_DR) | _BV(MAX_RT) | _BV(TX_DS) );
637 uint8_t status =
write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
640 tx_ok = status & _BV(TX_DS);
641 tx_fail = status & _BV(MAX_RT);
642 rx_ready = status & _BV(RX_DR);
652 write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5);
655 const uint8_t max_payload_size = 32;
661 static const uint8_t child_pipe[] PROGMEM =
663 RX_ADDR_P0, RX_ADDR_P1, RX_ADDR_P2, RX_ADDR_P3, RX_ADDR_P4, RX_ADDR_P5
665 static const uint8_t child_payload_size[] PROGMEM =
667 RX_PW_P0, RX_PW_P1, RX_PW_P2, RX_PW_P3, RX_PW_P4, RX_PW_P5
669 static const uint8_t child_pipe_enable[] PROGMEM =
671 ERX_P0, ERX_P1, ERX_P2, ERX_P3, ERX_P4, ERX_P5
680 pipe0_reading_address = address;
686 write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 5);
688 write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);
690 write_register(pgm_read_byte(&child_payload_size[child]),payload_size);
724 IF_SERIAL_DEBUG(printf(
"FEATURE=%i\r\n",
read_register(FEATURE)));
732 dynamic_payloads_enabled =
true;
753 IF_SERIAL_DEBUG(printf(
"FEATURE=%i\r\n",
read_register(FEATURE)));
766 const uint8_t* current =
reinterpret_cast<const uint8_t*
>(buf);
769 SPI_RW( W_ACK_PAYLOAD | ( pipe & B111 ) );
770 const uint8_t max_payload_size = 32;
771 uint8_t data_len = min(len,max_payload_size);
782 bool result = ack_payload_available;
783 ack_payload_available =
false;
817 en_aa &= ~_BV(pipe) ;
842 setup &= ~(_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
845 if ( level == RF24_PA_MAX )
847 setup |= (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
849 else if ( level == RF24_PA_HIGH )
851 setup |= _BV(RF_PWR_HIGH) ;
853 else if ( level == RF24_PA_LOW )
855 setup |= _BV(RF_PWR_LOW);
857 else if ( level == RF24_PA_MIN )
861 else if ( level == RF24_PA_ERROR )
864 setup |= (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
875 uint8_t power =
read_register(RF_SETUP) & (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
878 if ( power == (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) )
880 result = RF24_PA_MAX ;
882 else if ( power == _BV(RF_PWR_HIGH) )
884 result = RF24_PA_HIGH ;
886 else if ( power == _BV(RF_PWR_LOW) )
888 result = RF24_PA_LOW ;
892 result = RF24_PA_MIN ;
907 setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ;
908 if( speed == RF24_250KBPS )
913 setup |= _BV( RF_DR_LOW ) ;
919 if ( speed == RF24_2MBPS )
922 setup |= _BV(RF_DR_HIGH);
950 uint8_t dr =
read_register(RF_SETUP) & (_BV(RF_DR_LOW) | _BV(RF_DR_HIGH));
954 if ( dr == _BV(RF_DR_LOW) )
957 result = RF24_250KBPS ;
959 else if ( dr == _BV(RF_DR_HIGH) )
962 result = RF24_2MBPS ;
967 result = RF24_1MBPS ;
976 uint8_t config =
read_register(CONFIG) & ~( _BV(CRCO) | _BV(EN_CRC)) ;
979 if ( length == RF24_CRC_DISABLED )
983 else if ( length == RF24_CRC_8 )
985 config |= _BV(EN_CRC);
989 config |= _BV(EN_CRC);
990 config |= _BV( CRCO );
1000 uint8_t config =
read_register(CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;
1002 if ( config & _BV(EN_CRC ) )
1004 if ( config & _BV(CRCO) )
1005 result = RF24_CRC_16;
1007 result = RF24_CRC_8;
void toggle_features(void)
uint8_t write_register(uint8_t reg, const uint8_t *buf, uint8_t len)
uint8_t readPipe(void *buf, uint8_t *pipe)
void setChannel(uint8_t channel)
uint8_t getPayloadSize(void)
void openWritingPipe(uint64_t address)
bool read(void *buf, uint8_t len)
unsigned char SPI_RW(unsigned char byte)
void writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
void setCRCLength(rf24_crclength_e length)
void setAutoAck(bool enable)
void setPALevel(rf24_pa_dbm_e level)
rf24_crclength_e getCRCLength(void)
void startListening(void)
rf24_pa_dbm_e getPALevel(void)
bool isAckPayloadAvailable(void)
void enableAckPayload(void)
void setRetries(uint8_t delay, uint8_t count)
uint8_t read_register(uint8_t reg, uint8_t *buf, uint8_t len)
iBoardRF24(uint8_t _cepin, uint8_t _cspin, uint8_t _mosi_pin, uint8_t _miso_pin, uint8_t _sck_pin, uint8_t _irq_pin)
void setPayloadSize(uint8_t size)
void print_address_register(const char *name, uint8_t reg, uint8_t qty=1)
void print_observe_tx(uint8_t value)
void openReadingPipe(uint8_t number, uint64_t address)
bool setDataRate(rf24_datarate_e speed)
uint8_t read_payload(void *buf, uint8_t len)
void startWrite(const void *buf, uint8_t len)
bool write(const void *buf, uint8_t len)
uint8_t write_payload(const void *buf, uint8_t len)
uint8_t getDynamicPayloadSize(void)
void print_byte_register(const char *name, uint8_t reg, uint8_t qty=1)
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
void enableDynamicPayloads(void)
rf24_datarate_e getDataRate(void)
void print_status(uint8_t status)