Commit ea987810 authored by TMRh20's avatar TMRh20

RPi - Example updates, minor bmc driver change

- Modified spi transfernb hopefully for the last time, re #9
- Updated RPi examples to minimize CPU usage
- Updated transfer example for Arduino and RPi to exit TX mode after 4ms
per manufacturer. Improves multicast streaming.
- Bit of a cleanup on RPi examples
parent b8d97f53
...@@ -621,28 +621,17 @@ void __bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len, uint8_t dela ...@@ -621,28 +621,17 @@ void __bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len, uint8_t dela
{ {
bcm2835_peri_write_nb(fifo, tbuf[TXCnt]); bcm2835_peri_write_nb(fifo, tbuf[TXCnt]);
TXCnt++; TXCnt++;
} }
//Rx fifo not empty, so get the next received bytes //Rx fifo not empty, so get the next received bytes
while(! (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD) ){} while(! (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_DONE) ){}
while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD))&&( RXCnt < len )) if(TXCnt == len){ bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA); if(delay){ delayMicroseconds(20);}}
{ while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD))&&( RXCnt < len )){
rbuf[RXCnt] = bcm2835_peri_read_nb(fifo); rbuf[RXCnt] = bcm2835_peri_read_nb(fifo);
RXCnt++; RXCnt++;
} }
if(delay){ delayMicroseconds(10); }
} }
delayMicroseconds(5);
// Wait for DONE to be set
while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)){
}
// Set TA = 0, and also set the barrier
bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
if(delay){ delayMicroseconds(20);}
} }
......
...@@ -42,7 +42,6 @@ using namespace std; ...@@ -42,7 +42,6 @@ using namespace std;
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ); RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
// Radio pipe addresses for the 2 nodes to communicate. // Radio pipe addresses for the 2 nodes to communicate.
const uint8_t pipes[][6] = {"1Node","2Node"}; const uint8_t pipes[][6] = {"1Node","2Node"};
//const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; //const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
...@@ -61,7 +60,6 @@ int main(int argc, char** argv){ ...@@ -61,7 +60,6 @@ int main(int argc, char** argv){
// optionally, increase the delay between retries & # of retries // optionally, increase the delay between retries & # of retries
radio.setRetries(15,15); radio.setRetries(15,15);
// Dump the configuration of the rf unit for debugging // Dump the configuration of the rf unit for debugging
radio.printDetails(); radio.printDetails();
...@@ -121,8 +119,6 @@ int main(int argc, char** argv){ ...@@ -121,8 +119,6 @@ int main(int argc, char** argv){
unsigned long started_waiting_at = millis(); unsigned long started_waiting_at = millis();
bool timeout = false; bool timeout = false;
while ( ! radio.available() && ! timeout ) { while ( ! radio.available() && ! timeout ) {
// by bcatalin » Thu Feb 14, 2013 11:26 am
//delay(5); //add a small delay to let radio.available to check payload
if (millis() - started_waiting_at > 200 ) if (millis() - started_waiting_at > 200 )
timeout = true; timeout = true;
} }
...@@ -144,7 +140,7 @@ int main(int argc, char** argv){ ...@@ -144,7 +140,7 @@ int main(int argc, char** argv){
} }
// Try again 1s later // Try again 1s later
// delay(1000); // delay(1000);
sleep(1); sleep(1);
...@@ -156,6 +152,7 @@ int main(int argc, char** argv){ ...@@ -156,6 +152,7 @@ int main(int argc, char** argv){
if ( role == role_pong_back ) if ( role == role_pong_back )
{ {
// if there is data ready // if there is data ready
//printf("Check available...\n"); //printf("Check available...\n");
...@@ -169,8 +166,7 @@ int main(int argc, char** argv){ ...@@ -169,8 +166,7 @@ int main(int argc, char** argv){
radio.read( &got_time, sizeof(unsigned long) ); radio.read( &got_time, sizeof(unsigned long) );
radio.stopListening(); radio.stopListening();
// Seem to need a delay, or the RPi is too quick
radio.write( &got_time, sizeof(unsigned long) ); radio.write( &got_time, sizeof(unsigned long) );
// Now, resume listening so we catch the next packets. // Now, resume listening so we catch the next packets.
...@@ -179,6 +175,8 @@ int main(int argc, char** argv){ ...@@ -179,6 +175,8 @@ int main(int argc, char** argv){
// Spew it // Spew it
printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time); printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
delay(925); //Delay after payload responded to, minimize RPi CPU time
} }
} }
......
...@@ -28,12 +28,12 @@ using namespace std; ...@@ -28,12 +28,12 @@ using namespace std;
// CE Pin, CSN Pin, SPI Speed // CE Pin, CSN Pin, SPI Speed
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 1Mhz // Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 1Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_1MHZ); //RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS1, BCM2835_SPI_SPEED_1MHZ);
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ); //RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ); RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
...@@ -48,14 +48,12 @@ uint8_t counter = 1; // ...@@ -48,14 +48,12 @@ uint8_t counter = 1; //
int main(int argc, char** argv){ int main(int argc, char** argv){
printf("RF24/examples/gettingstarted_call_response\n"); printf("RPi/RF24/examples/gettingstarted_call_response\n");
radio.begin(); radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled radio.setAutoAck(1); // Ensure autoACK is enabled
radio.enableAckPayload(); // Allow optional ack payloads radio.enableAckPayload(); // Allow optional ack payloads
radio.setRetries(1,15); // Smallest time between retries, max no. of retries radio.setRetries(1,15); // Smallest time between retries, max no. of retries
radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed
//radio.powerUp();
radio.printDetails(); // Dump the configuration of the rf unit for debugging radio.printDetails(); // Dump the configuration of the rf unit for debugging
...@@ -76,9 +74,8 @@ int main(int argc, char** argv){ ...@@ -76,9 +74,8 @@ int main(int argc, char** argv){
} }
} }
/***********************************/ /***********************************/
// This simple sketch opens two pipes for these two nodes to communicate // This opens two pipes for these two nodes to communicate
// back and forth. // back and forth.
if ( role == role_ping_out ) { if ( role == role_ping_out ) {
radio.openWritingPipe(addresses[0]); radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]); radio.openReadingPipe(1,addresses[1]);
...@@ -96,7 +93,7 @@ while (1){ ...@@ -96,7 +93,7 @@ while (1){
if (role == role_ping_out){ // Radio is in ping mode if (role == role_ping_out){ // Radio is in ping mode
uint8_t gotByte; // Initialize a variable for the incoming response uint8_t gotByte; // Initialize a variable for the incoming response
radio.stopListening(); // First, stop listening so we can talk. radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending %d as payload. ",counter); // Use a simple byte counter as payload printf("Now sending %d as payload. ",counter); // Use a simple byte counter as payload
...@@ -122,16 +119,16 @@ while (1){ ...@@ -122,16 +119,16 @@ while (1){
/****************** Pong Back Role ***************************/ /****************** Pong Back Role ***************************/
if ( role == role_pong_back ) { if ( role == role_pong_back ) {
uint8_t pipeNo, gotByte; // Declare variables for the pipe and the byte received uint8_t pipeNo, gotByte; // Declare variables for the pipe and the byte received
while( radio.available(&pipeNo)){ // Read all available payloads if( radio.available(&pipeNo)){ // Read all available payloads
radio.flush_tx(); // Clear any unused ACK payloads radio.flush_tx(); // Clear any unused ACK payloads
radio.read( &gotByte, 1 ); radio.read( &gotByte, 1 );
// Since this is a call-response. Respond directly with an ack payload. // Since this is a call-response. Respond directly with an ack payload.
// Ack payloads are much more efficient than switching to transmit mode to respond to a call // Ack payloads are much more efficient than switching to transmit mode to respond to a call
radio.writeAckPayload(pipeNo,&gotByte, 1 ); // This can be commented out to send empty payloads. radio.writeAckPayload(pipeNo,&gotByte, 1 ); // This can be commented out to send empty payloads.
printf("Sent response %d \n\r", gotByte); printf("Sent response %d \n\r", gotByte);
delay(900); //Delay after a response to minimize CPU usage on RPi
//Expects a payload every second
} }
} }
......
...@@ -56,9 +56,9 @@ int main(int argc, char** argv){ ...@@ -56,9 +56,9 @@ int main(int argc, char** argv){
radio.begin(); // Setup and configure rf radio radio.begin(); // Setup and configure rf radio
radio.setChannel(1); radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_2MBPS); radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(1); // Ensure autoACK is enabled radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); radio.setCRCLength(RF24_CRC_8);
radio.printDetails(); radio.printDetails();
/********* Role chooser ***********/ /********* Role chooser ***********/
...@@ -90,7 +90,7 @@ int main(int argc, char** argv){ ...@@ -90,7 +90,7 @@ int main(int argc, char** argv){
for(int i=0; i<32; i++){ for(int i=0; i<32; i++){
data[i] = rand() % 255; //Load the buffer with random data data[i] = rand() % 255; //Load the buffer with random data
} }
// forever loop // forever loop
...@@ -100,16 +100,24 @@ int main(int argc, char** argv){ ...@@ -100,16 +100,24 @@ int main(int argc, char** argv){
sleep(2); sleep(2);
printf("Initiating Basic Data Transfer\n\r"); printf("Initiating Basic Data Transfer\n\r");
long int cycles = 10000; //Change this to a higher or lower number. long int cycles = 10000; //Change this to a higher or lower number.
// unsigned long pauseTime = millis(); //Uncomment if autoAck == 1 ( NOACK )
startTime = millis(); startTime = millis();
for(int i=0; i<cycles; i++){ //Loop through a number of cycles for(int i=0; i<cycles; i++){ //Loop through a number of cycles
data[0] = i; //Change the first byte of the payload for identification data[0] = i; //Change the first byte of the payload for identification
if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
counter++; //Keep count of failed payloads counter++; //Keep count of failed payloads
} }
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
/* if(millis() - pauseTime > 3){ // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
pauseTime = millis();
radio.txStandBy(); // This gives the PLL time to sync back up
}
*/
}
stopTime = millis(); stopTime = millis();
if(!radio.txStandBy()){ counter+=3; } if(!radio.txStandBy()){ counter+=3; }
......
...@@ -13,16 +13,13 @@ TMRh20 2014 ...@@ -13,16 +13,13 @@ TMRh20 2014
*/ */
#include <SPI.h> #include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h" #include "RF24.h"
#include "printf.h" #include "printf.h"
/************* USER Configuration *****************************/ /************* USER Configuration *****************************/
// Hardware configuration // Hardware configuration
RF24 radio(48,49); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8 RF24 radio(7,8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
boolean RADIO_NO = 0; //SET THIS TO 0 or 1 for 1st or 2nd radio
/***************************************************************/ /***************************************************************/
...@@ -46,7 +43,6 @@ void setup(void) { ...@@ -46,7 +43,6 @@ void setup(void) {
radio.setAutoAck(1); // Ensure autoACK is enabled radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); radio.setCRCLength(RF24_CRC_8);
//radio.disableCRC();
radio.openWritingPipe(pipes[0]); radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]); radio.openReadingPipe(1,pipes[1]);
...@@ -77,12 +73,21 @@ void loop(void){ ...@@ -77,12 +73,21 @@ void loop(void){
unsigned long cycles = 10000; //Change this to a higher or lower number. unsigned long cycles = 10000; //Change this to a higher or lower number.
startTime = millis(); startTime = millis();
unsigned long pauseTime = millis();
for(int i=0; i<cycles; i++){ //Loop through a number of cycles for(int i=0; i<cycles; i++){ //Loop through a number of cycles
data[0] = i; //Change the first byte of the payload for identification data[0] = i; //Change the first byte of the payload for identification
if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers if(!radio.writeFast(&data,32)){ //Write to the FIFO buffers
counter++; //Keep count of failed payloads counter++; //Keep count of failed payloads
} }
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
// if(millis() - pauseTime > 3){
// pauseTime = millis();
// radio.txStandBy(); // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
// //delayMicroseconds(130); // This gives the PLL time to sync back up
// }
} }
stopTime = millis(); stopTime = millis();
...@@ -93,8 +98,8 @@ void loop(void){ ...@@ -93,8 +98,8 @@ void loop(void){
float numBytes = cycles*32; float numBytes = cycles*32;
float rate = numBytes / (stopTime - startTime); float rate = numBytes / (stopTime - startTime);
printf("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r"); Serial.print("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r");
printf("%d of ",counter); Serial.print(cycles); printf(" Packets Failed to Send\n\r"); Serial.print(counter); Serial.print(" of "); Serial.print(cycles); printf(" Packets Failed to Send\n\r");
counter = 0; counter = 0;
} }
...@@ -107,12 +112,11 @@ if(role == RX){ ...@@ -107,12 +112,11 @@ if(role == RX){
counter++; counter++;
} }
if(millis() - rxTimer > 1000){ if(millis() - rxTimer > 1000){
rxTimer = millis(); rxTimer = millis();
printf("Rate: "); float numBytes = (counter*32)/1000.0;
float numBytes = counter*32; Serial.print("Rate: ");
Serial.print(numBytes/1000); Serial.print(numBytes);
printf(" KB/s\n\r"); printf("KB/s \n Payload Count: %d \n\r", counter);
printf("Payload Count: %d \n\r", counter);
counter = 0; counter = 0;
} }
} }
...@@ -141,4 +145,3 @@ if(role == RX){ ...@@ -141,4 +145,3 @@ if(role == RX){
} }
} }
} }
// vim:cin:ai:sts=2 sw=2 ft=cpp
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment