2011-07-13 20:00:51 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-07-13 21:12:05 +00:00
|
|
|
* Full test on single RF pair
|
2011-07-13 20:00:51 +00:00
|
|
|
*
|
2011-07-13 21:12:05 +00:00
|
|
|
* This sketches uses as many RF24 methods as possible in a single test.
|
|
|
|
*
|
|
|
|
* To operate:
|
|
|
|
* Upload this sketch on two nodes, each with IRQ -> pin 2
|
|
|
|
* One node needs pin 7 -> GND, the other NC. That's the receiving node
|
|
|
|
* Monitor the sending node's serial output
|
|
|
|
* Look for "+OK PASS" or "+OK FAIL"
|
2011-07-13 20:00:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include "nRF24L01.h"
|
|
|
|
#include "RF24.h"
|
|
|
|
#include "printf.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Hardware configuration
|
|
|
|
//
|
|
|
|
|
|
|
|
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
|
|
|
|
|
|
|
|
RF24 radio(8,9);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Topology
|
|
|
|
//
|
|
|
|
|
|
|
|
// Single radio pipe address for the 2 nodes to communicate.
|
|
|
|
const uint64_t pipe = 0xE8E8F0F0E1LL;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Role management
|
|
|
|
//
|
|
|
|
// Set up role. This sketch uses the same software for all the nodes in this
|
|
|
|
// system. Doing so greatly simplifies testing. The hardware itself specifies
|
|
|
|
// which node it is.
|
|
|
|
//
|
|
|
|
// This is done through the role_pin
|
|
|
|
//
|
|
|
|
|
|
|
|
// The various roles supported by this sketch
|
|
|
|
typedef enum { role_sender = 1, role_receiver } role_e;
|
|
|
|
|
|
|
|
// The debug-friendly names of those roles
|
|
|
|
const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
|
|
|
|
|
|
|
|
// The role of the current running sketch
|
|
|
|
role_e role;
|
|
|
|
|
|
|
|
// Interrupt handler, check the radio because we got an IRQ
|
|
|
|
void check_radio(void);
|
|
|
|
|
2011-07-13 21:12:05 +00:00
|
|
|
//
|
|
|
|
// Payload
|
|
|
|
//
|
|
|
|
|
|
|
|
const int min_payload_size = 4;
|
|
|
|
const int max_payload_size = 32;
|
|
|
|
const int payload_size_increments_by = 2;
|
|
|
|
int next_payload_size = min_payload_size;
|
|
|
|
|
|
|
|
char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test state
|
|
|
|
//
|
|
|
|
|
|
|
|
bool done; //*< Are we done with the test? */
|
|
|
|
bool passed; //*< Have we passed the test? */
|
|
|
|
const int num_needed = 10; //*< How many success/failures until we're done? */
|
|
|
|
int receives_remaining = num_needed; //*< How many ack packets until we declare victory? */
|
|
|
|
int failures_remaining = num_needed; //*< How many more failed sends until we declare failure? */
|
|
|
|
const int interval = 100; //*< ms to wait between sends */
|
|
|
|
|
|
|
|
void one_ok(void)
|
|
|
|
{
|
|
|
|
// Have we received enough yet?
|
|
|
|
if ( ! --receives_remaining )
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
passed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void one_failed(void)
|
|
|
|
{
|
|
|
|
// Have we failed enough yet?
|
|
|
|
if ( ! --failures_remaining )
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
passed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-13 20:00:51 +00:00
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Role
|
|
|
|
//
|
|
|
|
|
|
|
|
// set up the role pin
|
|
|
|
pinMode(role_pin, INPUT);
|
|
|
|
digitalWrite(role_pin,HIGH);
|
|
|
|
delay(20); // Just to get a solid reading on the role pin
|
|
|
|
|
|
|
|
// read the address pin, establish our role
|
|
|
|
if ( digitalRead(role_pin) )
|
|
|
|
role = role_sender;
|
|
|
|
else
|
|
|
|
role = role_receiver;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Print preamble
|
|
|
|
//
|
|
|
|
|
|
|
|
Serial.begin(57600);
|
|
|
|
printf_begin();
|
2011-07-13 21:12:05 +00:00
|
|
|
printf("\n\rRF24/tests/pingpair_test/\n\r");
|
2011-07-13 20:00:51 +00:00
|
|
|
printf("ROLE: %s\n\r",role_friendly_name[role]);
|
|
|
|
|
2011-07-13 21:12:05 +00:00
|
|
|
//
|
|
|
|
// TODO: Read configuration from serial
|
|
|
|
//
|
|
|
|
// It would be a much better test if this program could accept configuration
|
|
|
|
// from the serial port. Then it would be possible to run the same test under
|
|
|
|
// lots of different circumstances.
|
2011-07-13 22:41:37 +00:00
|
|
|
//
|
|
|
|
// The idea is that we will print "+READY" at this point. The python script
|
|
|
|
// will wait for it, and then send down a configuration script that we
|
|
|
|
// execute here and then run with.
|
|
|
|
//
|
|
|
|
// The test controller will need to configure the receiver first, then go run
|
|
|
|
// the test on the sender.
|
|
|
|
//
|
2011-07-13 21:12:05 +00:00
|
|
|
|
2011-07-13 20:00:51 +00:00
|
|
|
//
|
|
|
|
// Setup and configure rf radio
|
|
|
|
//
|
|
|
|
|
|
|
|
radio.begin();
|
|
|
|
|
|
|
|
// We will be using the Ack Payload feature, so please enable it
|
|
|
|
radio.enableAckPayload();
|
2011-07-13 21:12:05 +00:00
|
|
|
|
|
|
|
// enable dynamic payloads
|
|
|
|
radio.enableDynamicPayloads();
|
2011-07-13 20:00:51 +00:00
|
|
|
|
|
|
|
// Optional: Increase CRC length for improved reliability
|
|
|
|
radio.setCRCLength(RF24_CRC_16);
|
|
|
|
|
2011-07-13 21:12:05 +00:00
|
|
|
// Optional: Decrease data rate for improved reliability
|
|
|
|
radio.setDataRate(RF24_1MBPS);
|
|
|
|
|
2011-07-13 20:00:51 +00:00
|
|
|
// Optional: Pick a high channel
|
2011-07-13 21:12:05 +00:00
|
|
|
radio.setChannel(90);
|
2011-07-13 20:00:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Open pipes to other nodes for communication
|
|
|
|
//
|
|
|
|
|
|
|
|
// This simple sketch opens a single pipe for these two nodes to communicate
|
|
|
|
// back and forth. One listens on it, the other talks to it.
|
|
|
|
|
|
|
|
if ( role == role_sender )
|
|
|
|
{
|
|
|
|
radio.openWritingPipe(pipe);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
radio.openReadingPipe(1,pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Start listening
|
|
|
|
//
|
|
|
|
|
|
|
|
if ( role == role_receiver )
|
|
|
|
radio.startListening();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Dump the configuration of the rf unit for debugging
|
|
|
|
//
|
|
|
|
|
|
|
|
radio.printDetails();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attach interrupt handler to interrupt #0 (using pin 2)
|
|
|
|
// on BOTH the sender and receiver
|
|
|
|
//
|
|
|
|
|
|
|
|
attachInterrupt(0, check_radio, FALLING);
|
2011-07-13 22:41:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Receiver node automatically "passes" the test
|
|
|
|
//
|
|
|
|
if ( role == role_receiver )
|
|
|
|
{
|
|
|
|
done = passed = true;
|
|
|
|
}
|
2011-07-13 20:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t message_count = 0;
|
2011-07-13 21:12:05 +00:00
|
|
|
static uint32_t last_message_count = 0;
|
2011-07-13 20:00:51 +00:00
|
|
|
|
|
|
|
void loop(void)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Sender role. Repeatedly send the current time
|
|
|
|
//
|
|
|
|
|
2011-07-13 22:41:37 +00:00
|
|
|
if (role == role_sender && !done)
|
2011-07-13 20:00:51 +00:00
|
|
|
{
|
2011-07-13 21:12:05 +00:00
|
|
|
// The payload will always be the same, what will change is how much of it we send.
|
|
|
|
static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
|
|
|
|
|
|
|
|
// First, stop listening so we can talk.
|
|
|
|
radio.stopListening();
|
2011-07-13 20:00:51 +00:00
|
|
|
|
2011-07-13 21:12:05 +00:00
|
|
|
// Send it. This will block until complete
|
|
|
|
printf("\n\rNow sending length %i...",next_payload_size);
|
|
|
|
radio.startWrite( send_payload, next_payload_size );
|
|
|
|
|
|
|
|
// Update size for next time.
|
|
|
|
next_payload_size += payload_size_increments_by;
|
|
|
|
if ( next_payload_size > max_payload_size )
|
|
|
|
next_payload_size = min_payload_size;
|
|
|
|
|
2011-07-13 20:00:51 +00:00
|
|
|
// Try again soon
|
2011-07-13 21:12:05 +00:00
|
|
|
delay(interval);
|
2011-07-13 20:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Receiver role: Does nothing! All the work is in IRQ
|
|
|
|
//
|
|
|
|
|
2011-07-13 21:12:05 +00:00
|
|
|
//
|
|
|
|
// Stop the test if we're done and report results
|
|
|
|
//
|
|
|
|
if ( done )
|
|
|
|
{
|
|
|
|
printf("\n\r+OK ");
|
|
|
|
if ( passed )
|
2011-07-13 22:02:03 +00:00
|
|
|
printf("PASS\n\r\n\r");
|
2011-07-13 21:12:05 +00:00
|
|
|
else
|
2011-07-13 22:02:03 +00:00
|
|
|
printf("FAIL\n\r\n\r");
|
2011-07-13 21:12:05 +00:00
|
|
|
}
|
2011-07-13 20:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void check_radio(void)
|
|
|
|
{
|
|
|
|
// What happened?
|
|
|
|
bool tx,fail,rx;
|
|
|
|
radio.whatHappened(tx,fail,rx);
|
|
|
|
|
|
|
|
// Have we successfully transmitted?
|
|
|
|
if ( tx )
|
|
|
|
{
|
|
|
|
if ( role == role_sender )
|
2011-07-13 21:12:05 +00:00
|
|
|
printf("Send:OK ");
|
2011-07-13 20:00:51 +00:00
|
|
|
|
|
|
|
if ( role == role_receiver )
|
|
|
|
printf("Ack Payload:Sent\n\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have we failed to transmit?
|
|
|
|
if ( fail )
|
|
|
|
{
|
|
|
|
if ( role == role_sender )
|
2011-07-13 21:12:05 +00:00
|
|
|
{
|
|
|
|
printf("Send:Failed ");
|
|
|
|
|
|
|
|
// log status of this line
|
|
|
|
one_failed();
|
|
|
|
}
|
2011-07-13 20:00:51 +00:00
|
|
|
|
|
|
|
if ( role == role_receiver )
|
|
|
|
printf("Ack Payload:Failed\n\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transmitter can power down for now, because
|
|
|
|
// the transmission is done.
|
|
|
|
if ( ( tx || fail ) && ( role == role_sender ) )
|
|
|
|
radio.powerDown();
|
|
|
|
|
|
|
|
// Did we receive a message?
|
|
|
|
if ( rx )
|
|
|
|
{
|
|
|
|
// If we're the sender, we've received an ack payload
|
|
|
|
if ( role == role_sender )
|
|
|
|
{
|
|
|
|
radio.read(&message_count,sizeof(message_count));
|
2011-07-13 21:12:05 +00:00
|
|
|
printf("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.
|
|
|
|
if ( message_count != last_message_count )
|
|
|
|
{
|
|
|
|
printf("OK ");
|
|
|
|
one_ok();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("FAILED ");
|
|
|
|
one_failed();
|
|
|
|
}
|
2011-07-13 20:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're the receiver, we've received a time message
|
|
|
|
if ( role == role_receiver )
|
|
|
|
{
|
|
|
|
// Get this payload and dump it
|
2011-07-13 21:12:05 +00:00
|
|
|
size_t len = radio.getDynamicPayloadSize();
|
|
|
|
radio.read( receive_payload, len );
|
|
|
|
|
|
|
|
// Put a zero at the end for easy printing
|
|
|
|
receive_payload[len] = 0;
|
|
|
|
|
|
|
|
// Spew it
|
|
|
|
printf("Got payload size=%i value=%s\n\r",len,receive_payload);
|
|
|
|
|
|
|
|
// Add an ack packet for the next time around.
|
|
|
|
// Here we will report back how many bytes we got this time.
|
|
|
|
radio.writeAckPayload( 1, &len, sizeof(len) );
|
2011-07-13 20:00:51 +00:00
|
|
|
++message_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim:ai:cin:sts=2 sw=2 ft=cpp
|