Skip to content

Commit f3b3fb1

Browse files
committed
Fix: txStandBy hang. Updated Transfer example
- txStandBy was hanging if the last of the data timed out. Now clears the MAX_RT flag if timed out - Finished updating example file Transfer.ino for demonstrating half-rate transfers using the FIFO buffers
1 parent 4d8da2f commit f3b3fb1

File tree

3 files changed

+73
-78
lines changed

3 files changed

+73
-78
lines changed

RF24.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ bool RF24::writeFast( const void* buf, uint8_t len )
524524
}
525525

526526
}
527-
//Start Writing
527+
//Start Writing
528528
startWrite(buf,len);
529529

530530
return 1;
@@ -553,6 +553,10 @@ bool RF24::txStandBy(){
553553
ce(LOW);
554554
return 1;
555555
}
556+
if( get_status() & _BV(MAX_RT)){
557+
write_register(STATUS,_BV(MAX_RT) );
558+
return 0;
559+
}
556560
return 0;
557561
}
558562

RF24.h

+22-10
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class RF24
270270
void stopListening(void);
271271

272272
/**
273-
* Optimization: Improved performance and reliability
273+
* @note Optimization: Improved performance and reliability
274274
* Write to the open writing pipe
275275
*
276276
* Be sure to call openWritingPipe() first to set the destination
@@ -293,16 +293,17 @@ class RF24
293293
bool write( const void* buf, uint8_t len );
294294

295295
/**
296-
* Optimization: New Command
296+
* @note Optimization: New Command
297297
* Write to the open writing pipe filling up the FIFO buffers
298298
*
299299
* Be sure to call openWritingPipe() first to set the destination
300300
* of where to write to.
301301
*
302302
* This will not block until the 3 FIFO buffers are filled with data.
303-
* Once the FIFOs are full, writeFast will simply return 0. From a user
304-
* perspective, just keep trying to send the same data. The library will
305-
* keep auto retrying the previous payload using the built in functionality.
303+
* Once the FIFOs are full, writeFast will simply wait for success or
304+
* timeout, and return 1 or 0 respectively. From a user perspective, just
305+
* keep trying to send the same data. The library will keep auto retrying
306+
* the current payload using the built in functionality.
306307
*
307308
* The maximum size of data written is the fixed payload size, see
308309
* getPayloadSize(). However, you can write less, and the remainder
@@ -317,7 +318,7 @@ class RF24
317318
bool writeFast( const void* buf, uint8_t len );
318319

319320
/**
320-
* Optimization: New Command
321+
* @note Optimization: New Command
321322
* Write to the open writing pipe
322323
*
323324
* Be sure to call openWritingPipe() first to set the destination
@@ -344,7 +345,7 @@ class RF24
344345
bool writeBlocking( const void* buf, uint8_t len );
345346

346347
/**
347-
* Optimization: New Command
348+
* @note Optimization: New Command
348349
* This function should be called as soon as transmission is finished to
349350
* drop the radio back to STANDBY-I mode. If not issued, the radio will
350351
* remain in STANDBY-II mode which, per the data sheet, is not a recommended
@@ -781,8 +782,8 @@ class RF24
781782
};
782783

783784
/**
784-
* @example GettingStarted.pde
785-
*
785+
* @example GettingStarted.ino
786+
* Updated: TMRh20
786787
* This is an example which corresponds to my "Getting Started" blog post:
787788
* <a style="text-align:center" href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>.
788789
*
@@ -793,6 +794,17 @@ class RF24
793794
* node can then see how long the whole cycle took.
794795
*/
795796

797+
/**
798+
* @example Transfer.ino
799+
* Updated: TMRh20
800+
* This example demonstrates half-rate transfer using the FIFO buffers
801+
*
802+
* It is an example of how to use the RF24 class. Write this sketch to two
803+
* different nodes. Put one of the nodes into 'transmit' mode by connecting
804+
* with the serial monitor and sending a 'T'. The data transfer will begin,
805+
* with the receiver displaying the payload count. (32Byte Payloads)
806+
*/
807+
796808
/**
797809
* @example nordic_fob.pde
798810
*
@@ -812,7 +824,7 @@ class RF24
812824
*/
813825

814826
/**
815-
* @example pingpair.pde
827+
* @example pingpair.ino
816828
*
817829
* This is an example of how to use the RF24 class. Write this sketch to two
818830
* different nodes, connect the role_pin to ground on one. The ping node sends

examples/Transfer/Transfer.ino

+46-67
Original file line numberDiff line numberDiff line change
@@ -19,105 +19,92 @@ TMRh20 2014
1919
#include "RF24.h"
2020
#include "printf.h"
2121

22-
// Hardware configuration
23-
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
22+
/************* USER Configuration *****************************/
23+
// Hardware configuration
24+
RF24 radio(48,49); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
25+
boolean RADIO_NO = 0; //SET THIS TO 0 or 1 for 1st or 2nd radio
2426

25-
RF24 radio(48,49);
27+
/***************************************************************/
2628

27-
// Topology
28-
// Radio pipe addresses for the 2 nodes to communicate.
29-
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
29+
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
3030

31-
// Role management
32-
// Set up role. This sketch uses the same software for all the nodes
33-
// in this system. Doing so greatly simplifies testing.
31+
byte data[32]; //Data buffer for testing data transfer speeds
3432

35-
// The various roles supported by this sketch
36-
typedef enum { role_TX = 1, role_RX } role_e;
37-
38-
// The debug-friendly names of those roles
39-
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
40-
41-
//Set this to determine the default role of the current running sketch
42-
role_e role = role_RX;
43-
44-
//Data buffer for testing data transfer speeds
45-
byte data[32];
46-
47-
//Counter and timer for keeping track transfer info
48-
unsigned long counter, rxTimer;
33+
unsigned long counter, rxTimer; //Counter and timer for keeping track transfer info
4934
float startTime, stopTime;
35+
bool TX=1,RX=0,role=0;
5036

51-
void setup(void)
52-
{
37+
void setup(void) {
5338

5439
Serial.begin(57600);
5540
printf_begin();
56-
printf("\n\rRF24/examples/GettingStarted/\n\r");
57-
printf("ROLE: %s\n\r",role_friendly_name[role]);
58-
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
5941

60-
radio.begin(); // Setup and configure rf radio
42+
radio.begin(); // Setup and configure rf radio
6143
radio.setChannel(1);
6244
radio.setPALevel(RF24_PA_MAX);
63-
radio.setAutoAck(1); // Ensure autoACK is enabled
64-
radio.setRetries(2,3); // Optionally, increase the delay between retries & # of retries
65-
//radio.disableCRC();
45+
radio.setAutoAck(1); // Ensure autoACK is enabled
46+
radio.setRetries(1,1); // Optionally, increase the delay between retries & # of retries
6647
radio.setCRCLength(RF24_CRC_8);
67-
radio.openWritingPipe(pipes[1]);
68-
radio.openReadingPipe(1,pipes[0]);
69-
48+
49+
if(RADIO_NO){ //Lets the two radios choose the correct pipes depending on user selection
50+
radio.openWritingPipe(pipes[0]);
51+
radio.openReadingPipe(1,pipes[1]);
52+
}else{
53+
radio.openWritingPipe(pipes[1]);
54+
radio.openReadingPipe(1,pipes[0]);
55+
}
56+
7057
radio.startListening(); // Start listening
7158
radio.printDetails(); // Dump the configuration of the rf unit for debugging
7259

60+
printf("\n\rRF24/examples/Transfer Rates/\n\r");
61+
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
62+
7363
randomSeed(analogRead(0)); //Seed for random number generation
7464

7565
for(int i=0; i<32; i++){
7666
data[i] = random(255); //Load the buffer with random data
7767
}
78-
radio.powerUp();
68+
radio.powerUp(); //Power up the radio
7969
}
8070

8171
void loop(void){
8272

83-
84-
//TX with pre-blocking writes
85-
if(role == role_TX){
73+
74+
if(role == TX){
8675

8776
delay(1500);
8877

8978
printf("Initiating Basic Data Transfer\n\r");
9079

9180

92-
float cycles = 10000;
81+
float cycles = 1000; //Change this to a higher or lower number.
9382

9483
startTime = millis();
9584

96-
for(int i=0; i<cycles; i++){
97-
if(!radio.writeFast(&data,32)){
98-
counter++;
85+
for(int i=0; i<cycles; i++){ //Loop through a number of cycles
86+
data[0] = i; //Change the first byte of the payload for identification
87+
if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
88+
counter++; //Keep count of failed payloads
9989
}
10090
}
91+
10192
stopTime = millis();
102-
103-
while(!radio.txStandBy()){ }
93+
94+
while(!radio.txStandBy()){ } //This should be called to wait for completion and put the radio in standby mode after transmission
10495

10596
float numBytes = cycles * 32;
10697
float rate = numBytes / (stopTime - startTime) / cycles;
107-
10898

109-
printf("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r");
110-
printf("%d of ",counter); Serial.print(cycles); printf(" Packets Failed to Send\n\r");
111-
counter = 0;
99+
printf("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r");
100+
printf("%d of ",counter); Serial.print(cycles); printf(" Packets Failed to Send\n\r");
101+
counter = 0;
112102

113-
114-
115-
116103
}
117104

118105

119106

120-
if(role == role_RX){
107+
if(role == RX){
121108
while(radio.available()){
122109
radio.read(&data,32);
123110
counter++;
@@ -130,34 +117,26 @@ void loop(void){
130117
printf("Payload Count: %d \n\r", counter);
131118
counter = 0;
132119
}
133-
134120
}
135-
136121
//
137122
// Change roles
138123
//
139124

140125
if ( Serial.available() )
141126
{
142127
char c = toupper(Serial.read());
143-
if ( c == 'T' && role == role_RX )
128+
if ( c == 'T' && role == RX )
144129
{
145130
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
146131
radio.stopListening();
147-
role = role_TX; // Become the primary transmitter (ping out)
148-
//radio.openWritingPipe(pipes[0]);
149-
//radio.openReadingPipe(1,pipes[1]);
132+
role = TX; // Become the primary transmitter (ping out)
150133
}
151-
else if ( c == 'R' && role == role_TX )
134+
else if ( c == 'R' && role == TX )
152135
{
153136
radio.startListening();
154-
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
155-
156-
role = role_RX; // Become the primary receiver (pong back)
157-
158-
//radio.openWritingPipe(pipes[1]);
159-
//radio.openReadingPipe(1,pipes[0]);
137+
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
138+
role = RX; // Become the primary receiver (pong back)
160139
}
161140
}
162141
}
163-
// vim:cin:ai:sts=2 sw=2 ft=cpp
142+
// vim:cin:ai:sts=2 sw=2 ft=cpp

0 commit comments

Comments
 (0)