Need to change SPI clock rate for RF24 radio modules w/ TMRh20 library

Status
Not open for further replies.

BillG

Member
Hi all,
I'm new to Teensy's but have worked with AVR Arduinos before and have lots of experience with embedded programming.

I'm using a couple of Teensy 3.2's with nRF24L01+ modules from Sparkfun. Arduino 1.6.12, Teensy cpu set to 72MHz, with TMRh20's RF24 library, ver. 1.2.0. I got the RF24 GettingStarted example to work (code below) but I get a considerable number of failed packets, even though the modules are set to HIGH transmit power and they are only a few feet away from each other. I looked at the SPI clock on my scope and it's running at 12MHz. The max for the RF24 chip is 10MHz. I tracked down a setting for the clock speed in RF24_config.h:

// RF modules support 10 Mhz SPI bus speed
const uint32_t RF24_SPI_SPEED = 6000000; //WNG. was 10000000.


It was originally 10MHz. I tested 8MHz and 6MHz, but the actual SPI clock stays at 12MHz. So changing this constant doesn't end up doing anything. I found an earlier post that said

On the Teensy, I run the SPI buss at 4.5MHz using the following code just before radio.begin():
SPI.begin(); // Start the SPI buss
SPI.setClockDivider( SPI_CLOCK_DIV16 ); // Set SPI clock to 4.5MHz, similar to 16M Arduino


but this doesn't do anything either. Looking through RF24.cpp, the beginTransaction() function changes SPI settings anyway, and it's called multiple times throughout the library. Anyone have any idea why changing the config value doesn't work?


//**************************************************************************
// WNG mods - radio(9,10), changed power to HIGH. Get fewer failures on HIGH.

/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/

#include <SPI.h>
#include "RF24.h"

/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 1;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(9,10);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving
bool role = 0; // 0 = pong back , 1 = ping out

void setup() {
Serial.begin(115200);
delay(2000); // WNG - println's below started working after I put in this delay
Serial.println(F("RF24/examples/GettingStarted"));
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));

radio.begin();
//radio.setChannel(100);


// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_HIGH);

// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}

// Start the radio listening for data
radio.startListening();
radio.printDetails(); //WNG
}

void loop() {


/****************** Ping Out Role ***************************/
if (role == 1) {

radio.stopListening(); // First, stop listening so we can talk.

Serial.println(F("Now sending"));

unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
if (!radio.write( &start_time, sizeof(unsigned long) )){
Serial.println(F("failed"));
}

radio.startListening(); // Now, continue listening

unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
boolean timeout = false; // Set up a variable to indicate if a response was received or not

while ( ! radio.available() ){ // While nothing is received
if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}

if ( timeout ){ // Describe the results
Serial.println(F("Failed, response timed out."));
}else{
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
unsigned long end_time = micros();

// Spew it
Serial.print(F("Sent "));
Serial.print(start_time);
Serial.print(F(", Got response "));
Serial.print(got_time);
Serial.print(F(", Round-trip delay "));
Serial.print(end_time-start_time);
Serial.println(F(" microseconds"));
}

// Try again 1s later
delay(1000);
}

/****************** Pong Back Role ***************************/

if ( role == 0 )
{
unsigned long got_time;

if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
}

radio.stopListening(); // First, stop listening so we can talk
radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
Serial.print(F("Sent response "));
Serial.println(got_time);
}
}


/****************** Change Roles via Serial Commands ***************************/

if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == 0 ){
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
role = 1; // Become the primary transmitter (ping out)

}else
if ( c == 'R' && role == 1 ){
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
role = 0; // Become the primary receiver (pong back)
radio.startListening();

}
}

} // Loop



 
It's unlikely that SPI speed is causing losses. The two Sparkfun units that I have seem to work ok at 12MHz. Try reducing the data rate, setDataRate(RF24_250KBPS);
you could boost power to RF24_PA_MAX

(in your posts you could use the CODE tags to retain indentation when posting code)

EDIT1: in RF24.cpp, the ifdef'd code in beginTransaction() is not compiled. You can hack in setting SPI clock, by adding
_SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
after the #endif
(me thinks it is a bug in the RF24 lib https://github.com/TMRh20/RF24). tested on T3.2

EDIT2: or perhaps a more elegant fix to RF24_config.h in TEENSYDUINO #if is to add
#define RF24_SPI_TRANSACTIONS
const uint32_t RF24_SPI_SPEED = 10000000;
 
Last edited:
Thanks, I'll use the code tags from now on. Reducing data rate and increasing power does indeed reduce failures, but I can't run my intended application at 250Kbs; I need more speed. I really don't like running the RF24 chip SCK at higher than its rated maximum. I've been trying various things but no success yet. I'll try your suggestions too.
 
@manitou,
Thanks for this suggestion:
Code:
//perhaps a more elegant fix to RF24_config.h in TEENSYDUINO #if is to add
#define RF24_SPI_TRANSACTIONS
const uint32_t RF24_SPI_SPEED = 10000000;
This works! Now I can get the bus speed I want and there are no more dropped packets.
 
Status
Not open for further replies.
Back
Top