Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2011 13:24
Show Gist options
  • Save anonymous/1521127 to your computer and use it in GitHub Desktop.
Save anonymous/1521127 to your computer and use it in GitHub Desktop.
Functional Tester for Nanode RFX
/**********************************************************************************************
* Utility to test the Nanode RF and it's various components and functions
*
* Ian Chilton <ian@ichilton.co.uk>
* 21/12/2011
*
* Currently requires Arduino 0022 or 0023. Arduino 1.0 compatibility will come soon.
*
* Requires the following libraries in your libraries folder:
*
* Ports - http://jeelabs.org/pub/snapshots/Ports.zip
* RF12 - http://jeelabs.org/pub/snapshots/RF12.zip
* EtherCard - http://jeelabs.org/pub/snapshots/EtherCard.zip
* NanodeMAC - https://github.com/thiseldo/NanodeMAC
* MCP7941x - https://github.com/ichilton/mcp7941x_arduino
*
**********************************************************************************************/
#include <avr/pgmspace.h>
#include <Wire.h>
#include <NanodeMAC.h>
#include <EtherCard.h>
#include <MCP7941x.h>
#include <Ports.h>
#include <RF12.h>
#include <SD.h>
// I2C Addresses:
#define MCP7941x_EEPROM_I2C_ADDR 0x57
#define MCP7941x_RTC_I2C_ADDR 0x6f
// Memory location of the mac address:
// (starts at 0xF0 but first 2 bytes are empty on the MCP79411)
#define MAC_LOCATION 0xF2
// LED's:
#define LED_GREEN 5
#define LED_RED 6
// Create new instance of the MCP7941x class:
MCP7941x rtc = MCP7941x();
// Payload for RFM12B
static unsigned long payload;
// Ethernet Buffer:
byte Ethernet::buffer[500];
// Array for the mac address:
static byte mymac[6] = { 0,0,0,0,0,0 };
char menuText[] PROGMEM =
"Please choose a command:" "\n"
"\n"
"a. Read MAC Address from 11AA02E48" "\n"
"b. Read MAC Address from MCP7941x" "\n"
"c. Clear MAC Address in MCP7941x" "\n"
"d. Copy MAC Address from 11AA02E48 and write into MCP7941x" "\n"
"e. Flash LED's" "\n"
"f. Test Ethernet DHCP" "\n"
"g. Set RTC via NTP (NOT YET AVAILABLE)" "\n"
"h. Display RTC Time" "\n"
"i. RFM12B - Sender" "\n"
"j. RFM12B - Receiver" "\n"
"k. Test 23K256 Write (NOT YET AVAILABLE)" "\n"
"l. Test 23K256 Read (NOT YET AVAILABLE)" "\n"
"m. Test SD Write (NOT YET AVAILABLE)" "\n"
"n. Test SD Read (NOT YET AVAILABLE)" "\n"
"z. Auto Test Sequence (NOT YET AVAILABLE)" "\n";
void flashLEDS()
{
Serial.println("Will flash LED's 5 times");
Serial.println();
for( int i=0; i < 5; i++ )
{
Serial.println("RED");
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_RED, LOW);
delay(500);
Serial.println("GREEN");
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
delay(500);
}
}
// Function to print out the mac address:
void displayMacAddress(byte *mac_address)
{
// Print the mac address:
for( int i=0; i<6; i++ )
{
if (mac_address[i] < 10)
{
Serial.print(0);
}
Serial.print( mac_address[i], HEX );
Serial.print( i < 5 ? ":" : "" );
}
Serial.println();
}
// Display an IP Address:
void displayIPAddress( uint8_t *ip_address )
{
for( int i = 0; i < 4; i++ )
{
Serial.print( ip_address[i], DEC );
if( i < 3 )
Serial.print( "." );
}
}
void clearMAC(uint8_t *mac_address)
{
for ( int i=0; i<6; i++ )
{
mac_address[i] = 0xFF;
}
}
void getMAC(byte *mac_address)
{
Serial.println("Read MAC Address from 11AA02E48");
Serial.println();
NanodeMAC mac( mac_address );
}
// Function to read the mac address from the MCP7941x:
void getMacAddress(byte *mac_address)
{
Serial.println("Read MAC Address from MCP7941x");
Serial.println();
Wire.beginTransmission(MCP7941x_EEPROM_I2C_ADDR);
Wire.send(MAC_LOCATION);
Wire.endTransmission();
Wire.requestFrom(MCP7941x_EEPROM_I2C_ADDR, 6);
for( int i=0; i<6; i++ )
{
mac_address[i] = Wire.receive();
}
}
// Unlock the unique id area and write in the mac address:
void writeMacAddress(byte *mac_address)
{
Serial.println("Unlocking MCP7941x");
Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
Wire.send(0x09);
Wire.send(0x55);
Wire.endTransmission();
Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
Wire.send(0x09);
Wire.send(0xAA);
Wire.endTransmission();
Serial.println("Writing MAC Address to MCP7941x");
Wire.beginTransmission(MCP7941x_EEPROM_I2C_ADDR);
Wire.send(0xF2);
for( int i=0; i<6; i++ )
{
Wire.send(mac_address[i]);
}
Wire.endTransmission();
Serial.println("Write Complete");
}
void writeBlankMacAddress(byte *mac_address)
{
clearMAC(mac_address);
writeMacAddress(mac_address);
}
void copyMacAddress(byte *mac_address)
{
// Get MAC Address from 11AA02E48:
getMAC(mac_address);
// Check if a mac address has been returned:
if ((mymac[0] != 0x00 && mymac[0] != 0xff) ||
(mymac[1] != 0x00 && mymac[1] != 0xff) ||
(mymac[2] != 0x00 && mymac[2] != 0xff) ||
(mymac[3] != 0x00 && mymac[3] != 0xff) ||
(mymac[4] != 0x00 && mymac[4] != 0xff) ||
(mymac[5] != 0x00 && mymac[5] != 0xff))
{
Serial.print("MAC address from 11AA02E48: ");
displayMacAddress(mymac);
// Attempt to write mac address to MCP7941x:
writeMacAddress(mymac);
}
else
{
Serial.println("No MAC address available from 11AA02E48");
}
}
void rfm12b_rx()
{
Serial.println("Initialising RFM12B..");
// Initialize RFM12B as an 868Mhz module and Node 1 + Group 1:
rf12_initialize(1, RF12_868MHZ, 1);
Serial.println("Going into receive mode - Green LED will flash on receive.");
Serial.println("Press reset button to return to menu");
while(1)
{
// If a packet is received, if it's valid and if it's of the same size as our payload:
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof payload)
{
// Copy the received data into payload:
memcpy(&payload, (byte*) rf12_data, sizeof(payload));
// Flash LED:
digitalWrite(LED_GREEN, LOW);
delay(50);
digitalWrite(LED_GREEN, HIGH);
// Print it out:
Serial.print("Received: ");
Serial.println(payload);
}
}
}
void rfm12b_tx()
{
Serial.println("Initialising RFM12B..");
// Initialize RFM12B as an 868Mhz module and Node 2 + Group 1:
rf12_initialize(2, RF12_868MHZ, 1);
Serial.println("Going into transmit mode - Green LED will flash on transmit.");
Serial.println("Press reset button to return to menu");
// Reset counter:
payload = 0;
while (1)
{
// Wait until we can send:
while(!rf12_canSend())
rf12_recvDone();
// Increment data:
payload++;
// LED ON:
digitalWrite(LED_GREEN, LOW);
Serial.print("Sending ");
Serial.println(payload);
// Send Data:
rf12_sendStart(1, &payload, sizeof payload);
// Delay so LED flash can be seen:
delay(50);
// LED OFF:
digitalWrite(LED_GREEN, HIGH);
// Sleep for 1s:
delay(1000);
}
}
void displayDateTime(
byte second,
byte minute,
byte hour,
byte dayOfWeek,
byte dayOfMonth,
byte month,
byte year)
{
if (hour < 10)
{
Serial.print("0");
}
Serial.print(hour, DEC);
Serial.print(":");
if (minute < 10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
if (dayOfMonth < 10)
{
Serial.print("0");
}
Serial.print(dayOfMonth, DEC);
Serial.print("/");
if (month < 10)
{
Serial.print("0");
}
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" (");
switch(dayOfWeek)
{
case 1:
Serial.print("Sunday");
break;
case 2:
Serial.print("Monday");
break;
case 3:
Serial.print("Tuesday");
break;
case 4:
Serial.print("Wednesday");
break;
case 5:
Serial.print("Thursday");
break;
case 6:
Serial.print("Friday");
break;
case 7:
Serial.print("Saturday");
break;
}
Serial.println(")");
}
void showRTC()
{
Serial.println("Will display the time over 5 seconds and it should be intcrementing..");
Serial.println();
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
for( int i=0; i<5; i++)
{
// Get the date/time from the RTC:
rtc.getDateTime(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// Display the Date/Time on the serial line:
displayDateTime(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
// Sleep for 1s:
delay(1000);
}
}
void initialiseEthernet(byte *mac_address)
{
Serial.println(ether.begin(sizeof Ethernet::buffer, mac_address), DEC);
}
void testEthernetDHCP(byte *mac_address)
{
// Get the mac address from the MCP7941x and store in mymac:
getMacAddress(mac_address);
// If the mac address is empty:
if (mac_address[0] == 0xff)
{
Serial.println("MAC Address in MCP7941x is empty.");
Serial.println();
getMAC(mac_address);
displayMacAddress(mac_address);
}
// If there is no MCP7941x chip fitted:
else if (mac_address[0] == 0x00 &&
mac_address[1] == 0x00 &&
mac_address[2] == 0x00 &&
mac_address[3] == 0x00 &&
mac_address[4] == 0x00 &&
mac_address[5] == 0x00)
{
Serial.println("No MCP7941x Installed");
getMAC(mac_address);
displayMacAddress(mac_address);
}
else
{
Serial.print("MAC Address from MCP7941x: ");
displayMacAddress(mac_address);
}
Serial.println();
// If we have a mac address, try and initialise the ethernet and get DHCP:
if ((mac_address[0] != 0x00 && mac_address[0] != 0xff) ||
(mac_address[1] != 0x00 && mac_address[1] != 0xff) ||
(mac_address[2] != 0x00 && mac_address[2] != 0xff) ||
(mac_address[3] != 0x00 && mac_address[3] != 0xff) ||
(mac_address[4] != 0x00 && mac_address[4] != 0xff) ||
(mac_address[5] != 0x00 && mac_address[5] != 0xff))
{
if (ether.begin(sizeof Ethernet::buffer, mac_address) == 0)
Serial.println( "Failed to access Ethernet controller");
Serial.println("Setting up DHCP");
if (!ether.dhcpSetup())
Serial.println( "DHCP failed");
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}
}
// showString - thanks to Jeelabs rf12demo:
static void showString (PGM_P s) {
for (;;) {
char c = pgm_read_byte(s++);
if (c == 0)
break;
if (c == '\n')
Serial.print('\r');
Serial.print(c);
}
}
void displayMenuPrompt()
{
Serial.println();
Serial.print("> ");
}
void menuSelection (char i)
{
switch (i)
{
// Read MAC Address from 11AA02E48:
case 'a':
clearMAC(mymac);
getMAC(mymac);
displayMacAddress(mymac);
displayMenuPrompt();
break;
// Read MAC Address from MCP7941x:
case 'b':
clearMAC(mymac);
getMacAddress(mymac);
displayMacAddress(mymac);
displayMenuPrompt();
break;
// Clear MAC Address in MCP7941x:
case 'c':
writeBlankMacAddress(mymac);
displayMenuPrompt();
break;
// Clear MAC Address in MCP7941x:
case 'd':
copyMacAddress(mymac);
displayMenuPrompt();
break;
// Flash LED's:
case 'e':
flashLEDS();
displayMenuPrompt();
break;
// Initialise Ethernet:
case 'f':
testEthernetDHCP(mymac);
displayMenuPrompt();
break;
// Show RTC:
case 'h':
showRTC();
displayMenuPrompt();
break;
// RFM12B Sender:
case 'i':
rfm12b_tx();
displayMenuPrompt();
break;
// RFM12B Receive:
case 'j':
rfm12b_rx();
displayMenuPrompt();
break;
default:
Serial.println ("Invalid Option");
displayMenuPrompt();
break;
}
}
void setup()
{
Wire.begin();
Serial.begin(57600);
SD.begin(4) ; //SD Card is on Digital 4
Serial.println("Welcome to Nanode RF");
Serial.println();
// LED Pins:
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
// Start RTC:
rtc.enableClock();
// Show Menu:
showString(menuText);
displayMenuPrompt();
}
void loop()
{
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
// Received input:
if (Serial.available())
{
char c;
// Read input:
c = Serial.read();
// Print input for user:
Serial.println(c);
Serial.println();
// Process menu selection:
menuSelection(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment