From d23a4c95b93b15b8f4827e0004b0d8f140ac9ada Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Sat, 13 Jul 2013 23:49:26 -0400 Subject: [PATCH] bug fixes & performance improvements --- RFM69.cpp | 112 +++++++++++++++++++++++++++-------------------- RFM69.h | 30 +++++++------ RFM69registers.h | 8 ++++ 3 files changed, 89 insertions(+), 61 deletions(-) diff --git a/RFM69.cpp b/RFM69.cpp index 9d49dc7..e74f888 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -1,28 +1,39 @@ +// ********************************************************************************** // Driver definition for HopeRF RFM69W/RFM69HW, Semtech SX1231/1231H +// ********************************************************************************** // Creative Commons Attrib Share-Alike License -// If you want to use/extend this library please abide with the license terms: http://creativecommons.org/licenses/by-sa/3.0/ +// You are free to use/extend this library but please abide with the CCSA license: +// http://creativecommons.org/licenses/by-sa/3.0/ // 2013-06-14 (C) felix@lowpowerlab.com - +// ********************************************************************************** #include #include #include +volatile byte RFM69::DATA[MAX_DATA_LEN]; +volatile byte RFM69::_mode; // current transceiver state +volatile byte RFM69::DATALEN; +volatile byte RFM69::SENDERID; +volatile byte RFM69::TARGETID; //should match _address +volatile byte RFM69::PAYLOADLEN; +volatile byte RFM69::ACK_REQUESTED; +volatile byte RFM69::ACK_RECEIVED; /// Should be polled immediately after sending a packet with ACK request RFM69* RFM69::selfPointer; bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID) { const byte CONFIG[][2] = { - /* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY}, - /* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00}, //gaussian, bt=1.0 - /* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, //default is 4.8 KBPS + /* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY }, + /* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, //gaussian, bt=1.0 + /* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, //default:4.8 KBPS /* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_55555}, - /* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, //default is 5khz + /* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, //default:5khz, (FDEV + BitRate/2 <= 500Khz) /* 0x06 */ { REG_FDEVLSB, RF_FDEVLSB_50000}, - /* 0x07 */ { REG_FRFMSB, (freqBand==RF69_315MHZ ? RF_FRFMSB_315 : (freqBand==RF69_433MHZ ? RF_FRFMSB_433 : (freqBand==RF69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915)))}, - /* 0x08 */ { REG_FRFMID, (freqBand==RF69_315MHZ ? RF_FRFMID_315 : (freqBand==RF69_433MHZ ? RF_FRFMID_433 : (freqBand==RF69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915)))}, - /* 0x09 */ { REG_FRFLSB, (freqBand==RF69_315MHZ ? RF_FRFLSB_315 : (freqBand==RF69_433MHZ ? RF_FRFLSB_433 : (freqBand==RF69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915)))}, + /* 0x07 */ { REG_FRFMSB, (freqBand==RF69_315MHZ ? RF_FRFMSB_315 : (freqBand==RF69_433MHZ ? RF_FRFMSB_433 : (freqBand==RF69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915))) }, + /* 0x08 */ { REG_FRFMID, (freqBand==RF69_315MHZ ? RF_FRFMID_315 : (freqBand==RF69_433MHZ ? RF_FRFMID_433 : (freqBand==RF69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915))) }, + /* 0x09 */ { REG_FRFLSB, (freqBand==RF69_315MHZ ? RF_FRFLSB_315 : (freqBand==RF69_433MHZ ? RF_FRFLSB_433 : (freqBand==RF69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915))) }, // looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm // +17dBm and +20dBm are possible on RFM69HW @@ -30,23 +41,26 @@ bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID) // +17dBm formula: Pout=-14+OutputPower (with PA1 and PA2)** // +20dBm formula: Pout=-11+OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet) ///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111}, - /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2}, - /* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01}, //DIO0 is the only IRQ we're using - /* 0x29 */ { REG_RSSITHRESH, 220}, // must be set to dBm = (-Sensitivity / 2) - default is 0xE4=228 so -114dBm + ///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, //over current protection (default is 95mA) + + // RXBW defaults are {RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4khz) + /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, //(BitRate < 2 * RxBw) + /* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, //DIO0 is the only IRQ we're using + /* 0x29 */ { REG_RSSITHRESH, 220 }, //must be set to dBm = (-Sensitivity / 2) - default is 0xE4=228 so -114dBm ///* 0x2d */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA - /* 0x2e */ { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0}, - /* 0x2f */ { REG_SYNCVALUE1, 0x2D}, //attempt to make this compatible with sync1 byte of RFM12B lib - /* 0x30 */ { REG_SYNCVALUE2, networkID}, //NETWORK ID + /* 0x2e */ { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 }, + /* 0x2f */ { REG_SYNCVALUE1, 0x2D }, //attempt to make this compatible with sync1 byte of RFM12B lib + /* 0x30 */ { REG_SYNCVALUE2, networkID }, //NETWORK ID /* 0x37 */ { REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF }, /* 0x38 */ { REG_PAYLOADLENGTH, 66 }, //in variable length mode: the max frame size, not used in TX - //* 0x39 */ { REG_NODEADRS, nodeID}, //turned off because we're not using address filtering + //* 0x39 */ { REG_NODEADRS, nodeID }, //turned off because we're not using address filtering + /* 0x3C */ { REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE }, //TX on FIFO not empty /* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, //RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent) /* 0x6F */ { REG_TESTDAGC, RF_DAGC_CONTINUOUS }, // run DAGC continuously in RX mode {255, 0} }; - + pinMode(_slaveSelectPin, OUTPUT); - unselect(); SPI.setDataMode(SPI_MODE0); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV2); //max speed, except on Due which can run at system clock speed @@ -100,7 +114,7 @@ void RFM69::setMode(byte newMode) } // we are using packet mode, so this check is not really needed - // but waiting for mode ready is necessary when going from to sleep because the FIFO may not be immediately available from previous mode + // but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode while (_mode == RF69_MODE_SLEEP && (readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady _mode = newMode; @@ -112,7 +126,7 @@ void RFM69::sleep() { void RFM69::setAddress(byte addr) { - _address = addr; //& 0x7F; //make sure MSB is reserved for ACK flagging + _address = addr; writeReg(REG_NODEADRS, _address); } @@ -135,20 +149,21 @@ bool RFM69::canSend() void RFM69::send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK) { + writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks while (!canSend()) receiveDone(); sendFrame(toAddress, buffer, bufferSize, requestACK, false); } -//to dramatically increase the chance of getting a packet across, call this function instead of send +// to increase the chance of getting a packet across, call this function instead of send // and it handles all the ACK requesting/retrying for you :) // The only twist is that you have to manually listen to ACK requests on the other side and send back the ACKs // The reason for the semi-automaton is that the lib is ingterrupt driven and // requires user action to read the received data and decide what to do with it -// rountrips usually take only 5-8ms at 50kbps -bool RFM69::sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte attempts, byte retryWaitTime) { +// replies usually take only 5-8ms at 50kbps@915Mhz +bool RFM69::sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries, byte retryWaitTime) { long sentTime; byte howLong=0; - for (byte i=0; i MAX_DATA_LEN) bufferSize = MAX_DATA_LEN; //write to FIFO @@ -208,68 +221,73 @@ void RFM69::sendFrame(byte toAddress, const void* buffer, byte bufferSize, bool /* no need to wait for transmit mode to be ready since its handled by the radio */ setMode(RF69_MODE_TX); - // wait for IRQ when finished transmitting while (digitalRead(_interruptPin) == 0); //wait for DIO0 to turn HIGH signalling transmission finish + //while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // Wait for ModeReady setMode(RF69_MODE_STANDBY); } void RFM69::interruptHandler() { - //Serial.print("MODE:");Serial.print(readReg(REG_OPMODE), BIN); - //Serial.print("\n IRQ1:");Serial.print(readReg(REG_IRQFLAGS1), BIN); - //Serial.print(" IRQ2:");Serial.print(readReg(REG_IRQFLAGS2), BIN); + //pinMode(4, OUTPUT); + //digitalWrite(4, 1); if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) { - //Serial.print("\nRDY"); setMode(RF69_MODE_STANDBY); select(); SPI.transfer(REG_FIFO & 0x7f); PAYLOADLEN = SPI.transfer(0); + DATALEN = PAYLOADLEN - 3; PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; //precaution TARGETID = SPI.transfer(0); if(!(_promiscuousMode || TARGETID==_address || TARGETID==0)) //match this node's address, or broadcast addr 0x0 or anything in promiscuous mode { PAYLOADLEN = 0; unselect(); + //digitalWrite(4, 0); return; } - SENDERID = SPI.transfer(0); + SENDERID = SPI.transfer(0); byte CTLbyte = SPI.transfer(0); ACK_RECEIVED = CTLbyte & 0x80; //extract ACK-requested flag ACK_REQUESTED = CTLbyte & 0x40; //extract ACK-received flag - //Serial.print(" *** "); - DATALEN = PAYLOADLEN - 3; + for (byte i= 0; i < DATALEN; i++) { DATA[i] = SPI.transfer(0); - //Serial.print((char)DATA[i]); } - //Serial.println(" ***"); - //Serial.println(DATALEN); unselect(); setMode(RF69_MODE_RX); } - //else Serial.println("UNK"); + //digitalWrite(4, 0); } void RFM69::isr0() { selfPointer->interruptHandler(); } void RFM69::receiveBegin() { - DATALEN = SENDERID = TARGETID = PAYLOADLEN = ACK_REQUESTED = ACK_RECEIVED = 0; - writeReg(REG_DIOMAPPING1, (readReg(REG_DIOMAPPING1) & 0x3F) | RF_DIOMAPPING1_DIO0_01); //set DIO0 to "PAYLOADREADY" in receive mode + DATALEN = 0; + SENDERID = 0; + TARGETID = 0; + PAYLOADLEN = 0; + ACK_REQUESTED = 0; + ACK_RECEIVED = 0; + if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) + writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks + writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); //set DIO0 to "PAYLOADREADY" in receive mode setMode(RF69_MODE_RX); } bool RFM69::receiveDone() { - noInterrupts(); //will be re-enabled in the setMode() method +// ATOMIC_BLOCK(ATOMIC_FORCEON) +// { + noInterrupts(); //re-enabled in unselect() via setMode() if (_mode == RF69_MODE_RX && PAYLOADLEN>0) { setMode(RF69_MODE_STANDBY); return true; } - receiveBegin(); return false; +//} } // To enable encryption: radio.encrypt("ABCDEFGHIJKLMNOP"); @@ -388,4 +406,4 @@ void RFM69::readAllRegs() // writeReg(REG_TEMP1, RF_TEMP1_MEAS_START); // while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)); // return readReg(REG_TEMP2); -// } +// } \ No newline at end of file diff --git a/RFM69.h b/RFM69.h index e1b016d..20b6bce 100644 --- a/RFM69.h +++ b/RFM69.h @@ -1,11 +1,13 @@ +// ********************************************************************************** // Driver definition for HopeRF RFM69W/RFM69HW, Semtech SX1231/1231H +// ********************************************************************************** // Creative Commons Attrib Share-Alike License -// If you want to use/extend this library please abide with the license terms: http://creativecommons.org/licenses/by-sa/3.0/ +// You are free to use/extend this library but please abide with the CCSA license: +// http://creativecommons.org/licenses/by-sa/3.0/ // 2013-06-14 (C) felix@lowpowerlab.com - +// ********************************************************************************** #ifndef RFM69_h #define RFM69_h - #include //assumes Arduino IDE v1.0 or greater #define 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) @@ -29,13 +31,14 @@ class RFM69 { public: - volatile byte DATA[MAX_DATA_LEN]; // recv/xmit buf, including hdr & crc bytes - volatile byte DATALEN; - volatile byte SENDERID; - volatile byte TARGETID; //should match _address - volatile byte PAYLOADLEN; - volatile byte ACK_REQUESTED; - volatile byte ACK_RECEIVED; /// Should be polled immediately after sending a packet with ACK request + static volatile byte DATA[MAX_DATA_LEN]; // recv/xmit buf, including hdr & crc bytes + static volatile byte DATALEN; + static volatile byte SENDERID; + static volatile byte TARGETID; //should match _address + static volatile byte PAYLOADLEN; + static volatile byte ACK_REQUESTED; + static volatile byte ACK_RECEIVED; /// Should be polled immediately after sending a packet with ACK request + static volatile byte _mode; //should be protected? RFM69(byte slaveSelectPin=SPI_CS, byte interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false) { _slaveSelectPin = slaveSelectPin; @@ -50,7 +53,7 @@ class RFM69 { void setAddress(byte addr); bool canSend(); void send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK=false); - bool sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte attempts=3, byte retryWaitTime=15); + bool sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries=2, byte retryWaitTime=15); bool receiveDone(); bool ACKReceived(byte fromNodeID); void sendACK(const void* buffer = "", uint8_t bufferSize=0); @@ -66,7 +69,7 @@ class RFM69 { byte readReg(byte addr); void writeReg(byte addr, byte val); void readAllRegs(); - + protected: static void isr0(); void interruptHandler(); @@ -75,10 +78,9 @@ class RFM69 { static RFM69* selfPointer; byte _slaveSelectPin; byte _interruptPin; - byte _mode; byte _address; - byte _powerLevel; bool _promiscuousMode; + byte _powerLevel; bool _isRFM69HW; void receiveBegin(); diff --git a/RFM69registers.h b/RFM69registers.h index 7fb82bc..962f422 100644 --- a/RFM69registers.h +++ b/RFM69registers.h @@ -1,3 +1,11 @@ +// ********************************************************************************** +// Registers used in driver definition for HopeRF RFM69W/RFM69HW, Semtech SX1231/1231H +// ********************************************************************************** +// Creative Commons Attrib Share-Alike License +// You are free to use/extend this library but please abide with the CCSA license: +// http://creativecommons.org/licenses/by-sa/3.0/ +// 2013-06-14 (C) felix@lowpowerlab.com +// ********************************************************************************** //************************************************** // RFM69/SX1231 Internal registers addresses