diff --git a/RF24.cpp b/RF24.cpp index 6b14a16..bb16c95 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -235,55 +235,39 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len) uint8_t RF24::flush_rx(void) { - uint8_t status; - #if defined (__arm__) - status = SPI.transfer(csn_pin, FLUSH_RX ); - #else - csn(LOW); - status = SPI.transfer( FLUSH_RX ); - csn(HIGH); - - #endif - - return status; + return spiTrans( FLUSH_TX ); } /****************************************************************************/ uint8_t RF24::flush_tx(void) { - uint8_t status; + return spiTrans( FLUSH_TX ); +} +/****************************************************************************/ + +uint8_t RF24::spiTrans(uint8_t cmd){ + + uint8_t status; #if defined (__arm__) - status = SPI.transfer(csn_pin, FLUSH_TX ); + status = SPI.transfer(csn_pin, cmd ); #else csn(LOW); - status = SPI.transfer( FLUSH_TX ); + status = SPI.transfer( cmd ); csn(HIGH); - #endif - return status; + } /****************************************************************************/ uint8_t RF24::get_status(void) { - uint8_t status; - - #if defined (__arm__) - status = SPI.transfer(csn_pin, NOP ); - #else - - csn(LOW); - status = SPI.transfer( NOP ); - csn(HIGH); - #endif - - return status; + return spiTrans(NOP); } /****************************************************************************/ @@ -347,7 +331,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty) RF24::RF24(uint8_t _cepin, uint8_t _cspin): ce_pin(_cepin), csn_pin(_cspin), wide_band(false), p_variant(false), - payload_size(32), ack_payload_available(false), dynamic_payloads_enabled(false), + payload_size(32), dynamic_payloads_enabled(false), pipe0_reading_address(0) { } @@ -367,8 +351,7 @@ void RF24::setChannel(uint8_t channel) void RF24::setPayloadSize(uint8_t size) { - const uint8_t max_payload_size = 32; - payload_size = min(size,max_payload_size); + payload_size = min(size,32); } /****************************************************************************/ @@ -380,7 +363,7 @@ uint8_t RF24::getPayloadSize(void) /****************************************************************************/ - +#if !defined (MINIMAL) static const char rf24_datarate_e_str_0[] PROGMEM = "1MBPS"; static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS"; @@ -414,7 +397,7 @@ static const char * const rf24_pa_dbm_e_str_P[] PROGMEM = { rf24_pa_dbm_e_str_2, rf24_pa_dbm_e_str_3, }; -#if !defined (MINIMAL) + void RF24::printDetails(void) { print_status(get_status()); @@ -479,8 +462,8 @@ void RF24::begin(void) // sizes must never be used. See documentation for a more complete explanation. setRetries(5,15); - // Restore our default PA level - setPALevel( RF24_PA_MAX ) ; + // Reset value is MAX + //setPALevel( RF24_PA_MAX ) ; // Determine if this is a p or non-p RF24 module and then // reset our data rate back to default value. This works @@ -498,8 +481,8 @@ void RF24::begin(void) // Initialize CRC and request 2-byte (16bit) CRC setCRCLength( RF24_CRC_16 ) ; - // Disable dynamic payloads, to match dynamic_payloads_enabled setting - write_register(DYNPD,0); + // Disable dynamic payloads, to match dynamic_payloads_enabled setting - Reset value is 0 + //write_register(DYNPD,0); // Reset current status // Notice reset and flush is the last thing we do @@ -532,8 +515,9 @@ void RF24::startListening(void) write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); // Restore the pipe0 adddress, if exists - if (pipe0_reading_address) + if (pipe0_reading_address){ write_register(RX_ADDR_P0, reinterpret_cast(&pipe0_reading_address), 5); + } // Flush buffers flush_rx(); @@ -636,16 +620,11 @@ bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout ) return 1; //Return 1 to indicate successful transmission } +/****************************************************************************/ void RF24::reUseTX(){ write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag - #if defined (__arm__) - SPI.transfer(csn_pin, REUSE_TX_PL ); - #else - csn(LOW); - SPI.transfer( REUSE_TX_PL ); - csn(HIGH); - #endif + spiTrans( REUSE_TX_PL ); ce(LOW); //Re-Transfer packet ce(HIGH); } @@ -659,11 +638,11 @@ bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast ) //Return 0 so the user can control the retrys and set a timer or failure counter if required //The radio will auto-clear everything in the FIFO as long as CE remains high - while ( (read_register(FIFO_STATUS) & _BV(FIFO_FULL))){ //Blocking only if FIFO is full. This will loop and block until TX is successful if( get_status() & _BV(MAX_RT)){ - reUseTX(); //Set re-transmit + //reUseTX(); //Set re-transmit + write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag return 0; //Return 0. The previous payload has been retransmitted //From the user perspective, if you get a 0, just keep trying to send the same payload } @@ -689,7 +668,7 @@ bool RF24::writeFast( const void* buf, uint8_t len ){ void RF24::startFastWrite( const void* buf, uint8_t len, const bool multicast){ //TMRh20 //write_payload( buf,len); - write_payload( buf, len,multicast?static_cast(W_TX_PAYLOAD_NO_ACK):static_cast(W_TX_PAYLOAD) ) ; + write_payload( buf, len,multicast ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ; ce(HIGH); } @@ -702,7 +681,8 @@ void RF24::startWrite( const void* buf, uint8_t len, const bool multicast ){ // Send the payload //write_payload( buf, len ); - write_payload( buf, len,multicast?static_cast(W_TX_PAYLOAD_NO_ACK):static_cast(W_TX_PAYLOAD) ) ; + write_payload( buf, len,multicast? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ; + ce(HIGH); ce(LOW); @@ -836,8 +816,9 @@ void RF24::openWritingPipe(uint64_t value) write_register(RX_ADDR_P0, reinterpret_cast(&value), 5); write_register(TX_ADDR, reinterpret_cast(&value), 5); - const uint8_t max_payload_size = 32; - write_register(RX_PW_P0,min(payload_size,max_payload_size)); + //const uint8_t max_payload_size = 32; + //write_register(RX_PW_P0,min(payload_size,max_payload_size)); + write_register(RX_PW_P0,payload_size); } /****************************************************************************/ @@ -901,15 +882,10 @@ void RF24::toggle_features(void) void RF24::enableDynamicPayloads(void) { // Enable dynamic payload throughout the system - write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) ); - // If it didn't work, the features are not enabled - if ( ! read_register(FEATURE) ) - { - // So enable them and try again toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) ); - } + IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); @@ -930,15 +906,8 @@ void RF24::enableAckPayload(void) // enable ack payload and dynamic payload features // - write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) ); - - // If it didn't work, the features are not enabled - if ( ! read_register(FEATURE) ) - { - // So enable them and try again toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) ); - } IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); @@ -955,15 +924,8 @@ void RF24::enableDynamicAck(void){ // // enable dynamic ack features // - - write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) ); - - // If it didn't work, the features are not enabled - if ( ! read_register(FEATURE) & _BV(EN_DYN_ACK) ){ - // So enable them and try again toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) ); - } IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); @@ -976,8 +938,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len) { const uint8_t* current = reinterpret_cast(buf); - const uint8_t max_payload_size = 32; - uint8_t data_len = min(len,max_payload_size); + uint8_t data_len = min(len,32); #if defined (__arm__) SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE); @@ -1002,10 +963,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len) bool RF24::isAckPayloadAvailable(void) { - if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){ - return 1; - } - return 0; + return ! read_register(FIFO_STATUS) & _BV(RX_EMPTY); } /****************************************************************************/ diff --git a/RF24.h b/RF24.h index e2e0b77..b1f8a37 100644 --- a/RF24.h +++ b/RF24.h @@ -50,9 +50,7 @@ private: bool wide_band; /* 2Mbs data rate in use? */ bool p_variant; /* False for RF24L01 and true for RF24L01P */ uint8_t payload_size; /**< Fixed size of payloads */ - bool ack_payload_available; /**< Whether there is an ack payload waiting */ bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */ - uint8_t ack_payload_length; /**< Dynamic size of pending ack payload. */ uint64_t pipe0_reading_address; /**< Last address set on pipe 0 for reading. */ public: @@ -907,6 +905,8 @@ private: * are enabled. See the datasheet for details. */ void toggle_features(void); + + uint8_t spiTrans(uint8_t cmd); /**@}*/ }; @@ -1040,7 +1040,7 @@ private: */ /** - * @example scanner.pde + * @example scanner.ino * * Example to detect interference on the various channels available. * This is a good diagnostic tool to check whether you're picking a diff --git a/examples/GettingStarted/GettingStarted.ino b/examples/GettingStarted/GettingStarted.ino index 8b0353c..ff871be 100644 --- a/examples/GettingStarted/GettingStarted.ino +++ b/examples/GettingStarted/GettingStarted.ino @@ -61,12 +61,11 @@ void loop(void){ radio.stopListening(); // First, stop listening so we can talk. - unsigned long time; - printf("Now sending ",time); + + printf("Now sending \n\r"); - time = micros(); // Take the time, and send it. This will block until complete - boolean ok = radio.write( &time, sizeof(unsigned long) ); // Send the current time - if (!ok){ printf("failed.\n\r"); } + unsigned long time = micros(); // Take the time, and send it. This will block until complete + if (!radio.write( &time, sizeof(unsigned long) )){ printf("failed.\n\r"); } radio.startListening(); // Now, continue listening diff --git a/examples/GettingStarted_CallResponse/GettingStarted_CallResponse.ino b/examples/GettingStarted_CallResponse/GettingStarted_CallResponse.ino index 5f09ad2..8b2d0d5 100644 --- a/examples/GettingStarted_CallResponse/GettingStarted_CallResponse.ino +++ b/examples/GettingStarted_CallResponse/GettingStarted_CallResponse.ino @@ -1,7 +1,7 @@ /* - // March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork - // Parts derived from examples by J. Coliz - + March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork + Parts derived from examples by J. Coliz +*/ /** * Example for efficient call-response using ack-payloads * diff --git a/examples/Transfer/printf.h b/examples/Transfer/printf.h index b2efd56..b40dd38 100644 --- a/examples/Transfer/printf.h +++ b/examples/Transfer/printf.h @@ -1,11 +1,11 @@ /* Copyright (C) 2011 J. Coliz - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ - + /** * @file printf.h * @@ -18,12 +18,12 @@ #ifdef ARDUINO -int serial_putc( char c, FILE * ) +int serial_putc( char c, FILE * ) { Serial.write( c ); return c; -} +} void printf_begin(void) { diff --git a/examples/pingpair_ack/pingpair_ack.ino b/examples/pingpair_ack/pingpair_ack.ino index 2d3ecf7..4cd3fe3 100644 --- a/examples/pingpair_ack/pingpair_ack.ino +++ b/examples/pingpair_ack/pingpair_ack.ino @@ -1,7 +1,7 @@ /* // March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork // Parts derived from examples by J. Coliz - +*/ /** * Example for efficient call-response using ack-payloads * diff --git a/examples/pingpair_dyn/pingpair_dyn.ino b/examples/pingpair_dyn/pingpair_dyn.ino index 16fc01b..e1b1319 100644 --- a/examples/pingpair_dyn/pingpair_dyn.ino +++ b/examples/pingpair_dyn/pingpair_dyn.ino @@ -15,7 +15,7 @@ #include #include "nRF24L01.h" #include "RF24.h" -//#include "printf.h" +#include "printf.h" // // Hardware configuration @@ -23,7 +23,7 @@ // Set up nRF24L01 radio on SPI bus plus pins 9 & 10 -RF24 radio(9,10); +RF24 radio(7,8); // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // Leave open to be the 'ping' transmitter @@ -74,7 +74,7 @@ void setup(void) // set up the role pin pinMode(role_pin, INPUT); - digitalWrite(role_pin,LOW); + digitalWrite(role_pin,HIGH); delay(20); // Just to get a solid reading on the role pin // read the address pin, establish our role @@ -88,7 +88,7 @@ void setup(void) // Serial.begin(57600); - //printf_begin(); + printf_begin(); printf("\n\rRF24/examples/pingpair_dyn/\n\r"); printf("ROLE: %s\n\r",role_friendly_name[role]); diff --git a/examples/pingpair_irq/pingpair_irq.ino b/examples/pingpair_irq/pingpair_irq.ino index de00b62..fb3f429 100644 --- a/examples/pingpair_irq/pingpair_irq.ino +++ b/examples/pingpair_irq/pingpair_irq.ino @@ -67,13 +67,15 @@ void setup(){ radio.enableAckPayload(); // We will be using the Ack Payload feature, so please enable it // Open pipes to other node for communication if ( role == role_sender ) { // This simple sketch opens a pipe on a single address for these two nodes to - radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it. + radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it. + radio.openReadingPipe(1,address[1]); }else{ + radio.openWritingPipe(address[1]); radio.openReadingPipe(1,address[0]); radio.startListening(); } radio.printDetails(); // Dump the configuration of the rf unit for debugging - radio.powerUp(); // Power up the radio + delay(50); attachInterrupt(0, check_radio, FALLING); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver } @@ -87,20 +89,12 @@ void loop() { if (role == role_sender) { // Sender role. Repeatedly send the current time unsigned long time = millis(); // Take the time, and send it. printf("Now sending %lu\n\r",time); - radio.startWrite( &time, sizeof(unsigned long) ); + radio.startWrite( &time, sizeof(unsigned long) ,0); delay(2000); // Try again soon } - if(role == role_receiver){ // Receiver does nothing but sleep between payloads - if(!radio.available() && gotMsg == 1 ){ // Display the uptime in millis(). (Counter does not increment during sleep) - gotMsg = 0; - set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Put the Arduino to sleep between payloads - sleep_enable(); - sleep_mode(); // Arduino goes to sleep here - //ZZZ... sleeping - sleep_disable(); // This happens when the interrupt wakes Arduino up - } + if(role == role_receiver){ // Receiver does nothing except in IRQ } } @@ -141,4 +135,3 @@ void check_radio(void) // Receiver role: Does not } } } - diff --git a/examples/pingpair_sleepy/pingpair_sleepy.ino b/examples/pingpair_sleepy/pingpair_sleepy.ino index ebc024e..8a8bc4e 100644 --- a/examples/pingpair_sleepy/pingpair_sleepy.ino +++ b/examples/pingpair_sleepy/pingpair_sleepy.ino @@ -71,7 +71,7 @@ void setup(){ // set up the role pin pinMode(role_pin, INPUT); - digitalWrite(role_pin,LOW); + digitalWrite(role_pin,HIGH); delay(20); // Just to get a solid reading on the role pin // read the address pin, establish our role @@ -113,7 +113,7 @@ void setup(){ radio.startListening(); // Dump the configuration of the rf unit for debugging - radio.printDetails(); + //radio.printDetails(); } void loop(){ @@ -123,8 +123,9 @@ void loop(){ radio.powerUp(); // Power up the radio after sleeping radio.stopListening(); // First, stop listening so we can talk. - printf("Now sending... "); - unsigned long time = millis(); // Take the time, and send it. + unsigned long time = millis(); // Take the time, and send it. + printf("Now sending... %lu \n\r",time); + radio.write( &time, sizeof(unsigned long) ); radio.startListening(); // Now, continue listening @@ -177,7 +178,7 @@ void loop(){ radio.startListening(); // Now, resume listening so we catch the next packets. }else{ Serial.println("Sleeping"); - delay(200); // Delay so the serial data can print out + delay(50); // Delay so the serial data can print out do_sleep(); } @@ -199,7 +200,6 @@ void setup_watchdog(uint8_t prescalar){ uint8_t wdtcsr = prescalar & 7; if ( prescalar & 8 ) wdtcsr |= _BV(WDP3); - Serial.println(wdtcsr); MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU @@ -223,4 +223,3 @@ void do_sleep(void) detachInterrupt(0); WDTCSR &= ~_BV(WDIE); } - diff --git a/examples/scanner/scanner.pde b/examples/scanner/scanner.ino similarity index 94% rename from examples/scanner/scanner.pde rename to examples/scanner/scanner.ino index 1a43d72..e4aca24 100644 --- a/examples/scanner/scanner.pde +++ b/examples/scanner/scanner.ino @@ -102,12 +102,14 @@ void loop(void) // Listen for a little radio.startListening(); - delayMicroseconds(128); - radio.stopListening(); + delayMicroseconds(225); + // Did we get a carrier? - if ( radio.testCarrier() ) + if ( radio.testCarrier() ){ ++values[i]; + } + radio.stopListening(); } } @@ -121,4 +123,4 @@ void loop(void) printf("\n\r"); } -// vim:ai:cin:sts=2 sw=2 ft=cpp +// vim:ai:cin:sts=2 sw=2 ft=cpp \ No newline at end of file diff --git a/tests/pingpair_blocking/pingpair_blocking.pde b/tests/pingpair_blocking/pingpair_blocking.pde index 1501d37..004b421 100644 --- a/tests/pingpair_blocking/pingpair_blocking.pde +++ b/tests/pingpair_blocking/pingpair_blocking.pde @@ -27,11 +27,11 @@ public: RF24Test(int a, int b): RF24(a,b) {} // Set up nRF24L01 radio on SPI bus plus pins 8 & 9 -RF24Test radio(8,9); +RF24Test radio(48,49); // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // Leave open to be the 'ping' transmitter -const int role_pin = 7; +const int role_pin = 5; // // Topology @@ -230,28 +230,28 @@ void loop(void) // Dump the payloads until we've gotten everything unsigned long got_time; bool done = false; - while (!done) + while (radio.available()) { // Fetch the payload, and see if this was the last one. - done = radio.read( &got_time, sizeof(unsigned long) ); - - // Spew it - printf("Got payload %lu...",got_time); - + radio.read( &got_time, sizeof(unsigned long) ); + } // Delay just a little bit to let the other unit // make the transition to receiver - delay(20); - } + //delay(20); + //} // First, stop listening so we can talk radio.stopListening(); + + // Spew it + printf("Got payload %lu...",got_time); // Send the final one back. radio.write( &got_time, sizeof(unsigned long) ); - printf("Sent response.\n\r"); // Now, resume listening so we catch the next packets. radio.startListening(); + printf("Sent response.\n\r"); } } @@ -270,4 +270,4 @@ void loop(void) printf("FAIL\n\r\n\r"); } } -// vim:cin:ai:sts=2 sw=2 ft=cpp +// vim:cin:ai:sts=2 sw=2 ft=cpp \ No newline at end of file diff --git a/tests/pingpair_test/pingpair_test.pde b/tests/pingpair_test/pingpair_test.pde index 6acbf51..7685ca8 100644 --- a/tests/pingpair_test/pingpair_test.pde +++ b/tests/pingpair_test/pingpair_test.pde @@ -29,11 +29,11 @@ // Set up nRF24L01 radio on SPI bus plus pins 8 & 9 -RF24 radio(8,9); +RF24 radio(7,8); // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // Leave open to be the 'ping' transmitter -const short role_pin = 7; +const short role_pin = 5; // // Topology @@ -254,7 +254,7 @@ void setup(void) // attachInterrupt(0, check_radio, FALLING); - + delay(50); if ( role == role_receiver ) printf("\n\r+OK "); } @@ -294,7 +294,7 @@ void loop(void) // Send it. This will block until complete printf("\n\rNow sending length %i...",next_payload_size); - radio.startWrite( send_payload, next_payload_size ); + radio.startWrite( send_payload, next_payload_size,0 ); // Update size for next time. next_payload_size += payload_size_increments_by; @@ -374,10 +374,9 @@ void check_radio(void) prbuf_in += sprintf(prbuf_in,"Ack Payload:Failed\n\r"); } - // Transmitter can power down for now, because - // the transmission is done. - if ( ( tx || fail ) && ( role == role_sender ) ) - radio.powerDown(); + // Not powering down since radio is in standby mode + //if ( ( tx || fail ) && ( role == role_sender ) ) + //radio.powerDown(); // Did we receive a message? if ( rx ) @@ -387,7 +386,7 @@ void check_radio(void) { radio.read(&message_count,sizeof(message_count)); prbuf_in += sprintf(prbuf_in,"Ack:%lu ",message_count); - + // is this ack what we were expecting? to account // for failures, we simply want to make sure we get a // DIFFERENT ack every time. @@ -411,10 +410,11 @@ void check_radio(void) size_t len = max_payload_size; memset(receive_payload,0,max_payload_size); - if ( configuration == '3' ) + if ( configuration == '3' ){ len = next_payload_size; - else + }else{ len = radio.getDynamicPayloadSize(); + } radio.read( receive_payload, len ); @@ -427,9 +427,9 @@ void check_radio(void) // Add an ack packet for the next time around. // Here we will report back how many bytes we got this time. radio.writeAckPayload( pipe_number, &len, sizeof(len) ); + ++message_count; } + } } - -// vim:ai:cin:sts=2 sw=2 ft=cpp