From cb1cadbf953e18e7648bfcf0d46be76511573df5 Mon Sep 17 00:00:00 2001 From: acekrystal Date: Mon, 29 Aug 2016 00:49:29 +0930 Subject: [PATCH 1/2] piGateway now support Odroid (C1+) Beside the option -DRASPBERRY added -DODROIDC1 option for the MakeFile. rdm69.h and .cpp file will take different actions based on these options. Added ODROIDC1 RST_PIN and defined this as output with pulldown. Added ODROIDC1 CLK_PIN and defined this as output with pulldown. Changed ODROIDC1 SPI_CS to pinmode OUTPUT and added pulldown. In order to work you still need a 2.2K resistor between CLK pin and Gnd. cleaning Odroid support commit --- piGateway/Makefile | 16 +++++++++-- piGateway/networkconfig.h | 4 +-- piGateway/rfm69.cpp | 58 ++++++++++++++++++++++++++++----------- piGateway/rfm69.h | 12 ++++++++ 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/piGateway/Makefile b/piGateway/Makefile index 5ef4328..3e1527e 100644 --- a/piGateway/Makefile +++ b/piGateway/Makefile @@ -1,3 +1,13 @@ +# +# Makefile for piGateway +# +# Supported devices: +# Arduino use -DARDUINO +# Raspberry Pi use -DRASPBERRY +# Odroid C1(+) use -DODROIDC1 +# +# make sure only your device is defined in the g++ lines. + install : Gatewayd cp Gatewayd /usr/local/bin cp GatewaydService /etc/init.d/Gatewayd @@ -13,11 +23,11 @@ uninstall: rm /usr/local/bin/Gatewayd Gatewayd : Gateway.c rfm69.cpp rfm69.h rfm69registers.h networkconfig.h - g++ Gateway.c rfm69.cpp -o Gatewayd -lwiringPi -lmosquitto -DRASPBERRY -DDAEMON + g++ Gateway.c rfm69.cpp -o Gatewayd -lwiringPi -lmosquitto -DODROIDC1 -DDAEMON Gateway : Gateway.c rfm69.cpp rfm69.h rfm69registers.h networkconfig.h - g++ Gateway.c rfm69.cpp -o Gateway -lwiringPi -lmosquitto -DRASPBERRY + g++ Gateway.c rfm69.cpp -o Gateway -lwiringPi -lmosquitto -DODROIDC1 -DDEBUG SenderReceiver : SenderReceiver.c rfm69.cpp rfm69.h rfm69registers.h networkconfig.h - g++ SenderReceiver.c rfm69.cpp -o SenderReceiver -lwiringPi -DRASPBERRY + g++ SenderReceiver.c rfm69.cpp -o SenderReceiver -lwiringPi -DODROIDC1 diff --git a/piGateway/networkconfig.h b/piGateway/networkconfig.h index a3d6f4d..f073967 100644 --- a/piGateway/networkconfig.h +++ b/piGateway/networkconfig.h @@ -9,14 +9,14 @@ File: Gateway.c This file hold the network configuration */ -#define NWC_NETWORK_ID 101 +#define NWC_NETWORK_ID 100 #define NWC_NODE_ID 1 // Frequency should be one of RF69_433MHZ RF69_868MHZ RF69_915MHZ #define NWC_FREQUENCY RF69_433MHZ // Set to 0 to disable encryption #define NWC_KEY_LENGTH 16 // Must contain 16 characters -#define NWC_KEY "xxxxxxxxxxxxxxxx" +#define NWC_KEY "sampleEncryptKey" // Set to true is the RFM69 is high power, false otherwise #define NWC_RFM69H true // Set to true if you want to listen to all messages on the network, even if for different nodes diff --git a/piGateway/rfm69.cpp b/piGateway/rfm69.cpp index 3e00e81..145a8fe 100644 --- a/piGateway/rfm69.cpp +++ b/piGateway/rfm69.cpp @@ -31,7 +31,8 @@ // ********************************************************************************** #include "rfm69.h" #include "rfm69registers.h" -#ifdef RASPBERRY + +#ifdef RASPBERRY // If RASPBERRY is defined in the make file. #include #include #include @@ -41,6 +42,16 @@ #include //usleep #define MICROSLEEP_LENGTH 15 +#elif ODROIDC1 // If ODROIDC1 is defined in the make file. +#include +#include +#include +#include +#include + +#include //usleep +#define MICROSLEEP_LENGTH 15 + #else #include #endif @@ -102,8 +113,15 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID) {255, 0} }; -#ifdef RASPBERRY - // Initialize SPI device 0 +#if defined(RASPBERRY) || defined(ODROIDC1) + wiringPiSetup(); + pinMode (RF69_RST_PIN, OUTPUT); + digitalWrite (RF69_RST_PIN, LOW); + pinMode (RF69_CLK_PIN, INPUT); + pullUpDnControl (RF69_CLK_PIN, PUD_OFF); + pinMode (RF69_SPI_CS, OUTPUT); + digitalWrite (RF69_SPI_CS, LOW); +// Initialize SPI device 0 if(wiringPiSPISetup(SPI_DEVICE, SPI_SPEED) < 0) { fprintf(stderr, "Unable to open SPI device\n\r"); exit(1); @@ -132,7 +150,7 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID) while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && millis()-start < timeout); // wait for ModeReady if (millis()-start >= timeout) return false; -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) // Attach the Interupt wiringPiSetup(); wiringPiISR(6, INT_EDGE_RISING, RFM69::isr0); @@ -378,7 +396,7 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, else if (requestACK) CTLbyte = RFM69_CTL_REQACK; -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) unsigned char thedata[63]; uint8_t i; for(i = 0; i < 63; i++) thedata[i] = 0; @@ -418,7 +436,7 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, // internal function - interrupt gets called when a packet is received void RFM69::interruptHandler() { -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) unsigned char thedata[67]; char i; for(i = 0; i < 67; i++) thedata[i] = 0; @@ -431,7 +449,7 @@ void RFM69::interruptHandler() { { //RSSI = readRSSI(); setMode(RF69_MODE_STANDBY); -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) thedata[0] = REG_FIFO & 0x7F; thedata[1] = 0; // PAYLOADLEN thedata[2] = 0; // TargetID @@ -457,7 +475,7 @@ void RFM69::interruptHandler() { //digitalWrite(4, 0); return; } -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) DATALEN = PAYLOADLEN - 3; thedata[0] = REG_FIFO & 0x77; thedata[1] = 0; //SENDERID @@ -531,7 +549,9 @@ void RFM69::receiveBegin() { bool RFM69::receiveDone() { //ATOMIC_BLOCK(ATOMIC_FORCEON) //{ -#ifndef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) + // one or both defined, do nothing. +#else noInterrupts(); // re-enabled in unselect() via setMode() or via receiveBegin() #endif if (_mode == RF69_MODE_RX && PAYLOADLEN > 0) @@ -541,7 +561,9 @@ bool RFM69::receiveDone() { } else if (_mode == RF69_MODE_RX) // already in RX no payload yet { -#ifndef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) + // one or both defined, do nothing. +#else // neither defined interrupts(); // explicitly re-enable interrupts #endif return false; @@ -555,7 +577,7 @@ bool RFM69::receiveDone() { // To disable encryption: radio.encrypt(null) or radio.encrypt(0) // KEY HAS TO BE 16 bytes !!! void RFM69::encrypt(const char* key) { -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) unsigned char thedata[17]; char i; @@ -601,7 +623,7 @@ int16_t RFM69::readRSSI(bool forceTrigger) { uint8_t RFM69::readReg(uint8_t addr) { -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) unsigned char thedata[2]; thedata[0] = addr & 0x7F; thedata[1] = 0; @@ -622,7 +644,7 @@ uint8_t RFM69::readReg(uint8_t addr) void RFM69::writeReg(uint8_t addr, uint8_t value) { -#if RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) //printf("%x %x\n", addr, value); unsigned char thedata[2]; thedata[0] = addr | 0x80; @@ -641,7 +663,9 @@ void RFM69::writeReg(uint8_t addr, uint8_t value) // select the RFM69 transceiver (save SPI settings, set CS low) void RFM69::select() { // printf(" diable Int "); -#ifndef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) + // one or both defined, do nothing. +#else // if neither defined noInterrupts(); // save current SPI settings _SPCR = SPCR; @@ -656,7 +680,9 @@ void RFM69::select() { // unselect the RFM69 transceiver (set CS high, restore SPI settings) void RFM69::unselect() { -#ifndef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) + // one or both defined, do nothing. +#else // neither defined digitalWrite(_slaveSelectPin, HIGH); // restore SPI settings to what they were before talking to RFM69 SPCR = _SPCR; @@ -699,7 +725,7 @@ void RFM69::setCS(uint8_t newSPISlaveSelect) { // Serial.print all the RFM69 register values void RFM69::readAllRegs() { -#ifdef RASPBERRY +#if defined(RASPBERRY) || defined(ODROIDC1) char thedata[2]; int i; thedata[1] = 0; diff --git a/piGateway/rfm69.h b/piGateway/rfm69.h index 2c35a61..9310ebc 100644 --- a/piGateway/rfm69.h +++ b/piGateway/rfm69.h @@ -40,6 +40,18 @@ #define RF69_IRQ_PIN 6 #define RF69_IRQ_NUM 0 +#define SPI_SPEED 500000 +#define SPI_DEVICE 0 +#elif ODROIDC1 // If ODROIDC1 is defined in the Makefile instead of RASPBERRY +#include + +#define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc) + +#define RF69_SPI_CS 10 // SS is the SPI slave select pin, for instance D10 on atmega328, use the WiringPi numbering. +#define RF69_IRQ_PIN 6 // The interrupt pin on the OdroidC1, use WirintPi numbering. +#define RF69_IRQ_NUM 0 +#define RF69_RST_PIN 5 // The reset pin should be pulled down by default, pull high for resetting +#define RF69_CLK_PIN 14 // The SPI CLK pin. Used for setting the pulldown and output mode. #define SPI_SPEED 500000 #define SPI_DEVICE 0 #else From c11a45d17bf8f7d068d4658a7921694cb7abf80e Mon Sep 17 00:00:00 2001 From: AcE Krystal Date: Mon, 17 Oct 2016 13:56:58 +0200 Subject: [PATCH 2/2] Update rfm69.cpp There should not be a else here. --- piGateway/rfm69.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/piGateway/rfm69.cpp b/piGateway/rfm69.cpp index 145a8fe..e400f2e 100644 --- a/piGateway/rfm69.cpp +++ b/piGateway/rfm69.cpp @@ -550,8 +550,7 @@ bool RFM69::receiveDone() { //ATOMIC_BLOCK(ATOMIC_FORCEON) //{ #if defined(RASPBERRY) || defined(ODROIDC1) - // one or both defined, do nothing. -#else + // one or both define noInterrupts(); // re-enabled in unselect() via setMode() or via receiveBegin() #endif if (_mode == RF69_MODE_RX && PAYLOADLEN > 0)