From 4a6f77ad479126be722ad43d4e2db1f737d15e61 Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Fri, 2 Aug 2019 16:38:00 -0400 Subject: [PATCH] A few optimizations --- RFM69.cpp | 17 ++++++++++------- RFM69.h | 2 +- RFM69_ATC.cpp | 8 +++----- RFM69_ATC.h | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/RFM69.cpp b/RFM69.cpp index f9998da..2efbcea 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -37,7 +37,6 @@ uint8_t RFM69::ACK_REQUESTED; uint8_t RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request int16_t RFM69::RSSI; // most accurate RSSI during reception (closest to the reception) volatile bool RFM69::_haveData; -RFM69* RFM69::selfPointer; RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW) { @@ -76,7 +75,7 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) /* 0x08 */ { REG_FRFMID, (uint8_t) (freqBand==RF69_315MHZ ? RF_FRFMID_315 : (freqBand==RF69_433MHZ ? RF_FRFMID_433 : (freqBand==RF69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915))) }, /* 0x09 */ { REG_FRFLSB, (uint8_t) (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 + // looks like PA1 and PA2 are not implemented on RFM69W/CW, hence the max output power is 13dBm // +17dBm and +20dBm are possible on RFM69HW // +13dBm formula: Pout = -18 + OutputPower (with PA0 or PA1**) // +17dBm formula: Pout = -14 + OutputPower (with PA1 and PA2)** @@ -95,6 +94,8 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) /* 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 + //* 0x31 */ { REG_SYNCVALUE3, 0xAA }, + //* 0x31 */ { REG_SYNCVALUE4, 0xBB }, /* 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 @@ -113,7 +114,7 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) _settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); #endif - unsigned long start = millis(); + uint32_t start = millis(); uint8_t timeout = 50; do writeReg(REG_SYNCVALUE1, 0xAA); while (readReg(REG_SYNCVALUE1) != 0xaa && millis()-start < timeout); start = millis(); @@ -134,9 +135,9 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) return false; attachInterrupt(_interruptNum, RFM69::isr0, RISING); - selfPointer = this; _address = nodeID; #if defined(RF69_LISTENMODE_ENABLE) + selfPointer = this; _freqBand = freqBand; _networkID = networkID; #endif @@ -300,7 +301,7 @@ void RFM69::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize { setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady - writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent" + //writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent" if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN; // control byte @@ -328,8 +329,8 @@ void RFM69::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize // no need to wait for transmit mode to be ready since its handled by the radio setMode(RF69_MODE_TX); uint32_t txStart = millis(); - while (digitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish - //while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady + //while (digitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish + while ((readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT) == 0x00); // wait for PacketSent setMode(RF69_MODE_STANDBY); } @@ -347,6 +348,7 @@ void RFM69::interruptHandler() { uint8_t CTLbyte = SPI.transfer(0); TARGETID |= (uint16_t(CTLbyte) & 0x0C) << 6; //10 bit address (most significant 2 bits stored in bits(2,3) of CTL byte SENDERID |= (uint16_t(CTLbyte) & 0x03) << 8; //10 bit address (most sifnigicant 2 bits stored in bits(0,1) of CTL byte + if(!(_promiscuousMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this libraries extra fields { @@ -855,6 +857,7 @@ void RFM69::rcCalibration() // ListenMode specific functions //============================================================================= #if defined(RF69_LISTENMODE_ENABLE) +RFM69* RFM69::selfPointer=0; volatile uint16_t RFM69::RF69_LISTEN_BURST_REMAINING_MS = 0; //============================================================================= diff --git a/RFM69.h b/RFM69.h index 8289deb..b970d88 100644 --- a/RFM69.h +++ b/RFM69.h @@ -224,7 +224,6 @@ class RFM69 { static volatile bool _haveData; virtual void sendFrame(uint16_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false); - static RFM69* selfPointer; uint8_t _slaveSelectPin; uint8_t _interruptPin; uint8_t _interruptNum; @@ -247,6 +246,7 @@ class RFM69 { virtual void unselect(); #if defined(RF69_LISTENMODE_ENABLE) + static RFM69* selfPointer; //============================================================================= // ListenMode specific declarations //============================================================================= diff --git a/RFM69_ATC.cpp b/RFM69_ATC.cpp index 0f25e30..b240fc1 100644 --- a/RFM69_ATC.cpp +++ b/RFM69_ATC.cpp @@ -92,7 +92,7 @@ void RFM69_ATC::sendFrame(uint16_t toAddress, const void* buffer, uint8_t buffer void RFM69_ATC::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK, bool sendRSSI, int16_t lastRSSI) { setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady - writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent" + //writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent" bufferSize += (sendACK && sendRSSI)?1:0; // if sending ACK_RSSI then increase data size by 1 if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN; @@ -127,8 +127,8 @@ void RFM69_ATC::sendFrame(uint16_t toAddress, const void* buffer, uint8_t buffer // no need to wait for transmit mode to be ready since its handled by the radio setMode(RF69_MODE_TX); uint32_t txStart = millis(); - while (digitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish - //while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady + //while (digitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish + while ((readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT) == 0x00); // wait for PacketSent setMode(RF69_MODE_STANDBY); } @@ -151,7 +151,6 @@ void RFM69_ATC::interruptHook(uint8_t CTLbyte) { // } else { if (_ackRSSI < _targetRSSI && _transmitLevel < 31) { - //_transmitLevel++; _transmitLevel += _transmitLevelStep; if (_transmitLevel > 31) _transmitLevel = 31; } @@ -175,7 +174,6 @@ bool RFM69_ATC::sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bu while (millis() - sentTime < retryWaitTime) if (ACKReceived(toAddress)) return true; if (_transmitLevel < 31) { - //_transmitLevel++; _transmitLevel += _transmitLevelStep; if (_transmitLevel > 31) _transmitLevel = 31; } diff --git a/RFM69_ATC.h b/RFM69_ATC.h index a28fa52..de0a6af 100644 --- a/RFM69_ATC.h +++ b/RFM69_ATC.h @@ -45,9 +45,9 @@ class RFM69_ATC: public RFM69 { //void setHighPower(bool onOFF=true, uint8_t PA_ctl=0x60); //have to call it after initialize for RFM69HW //void setPowerLevel(uint8_t level); // reduce/increase transmit power level bool sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=RFM69_ACK_TIMEOUT); - void enableAutoPower(int16_t targetRSSI=-90); // TWS: New method to enable/disable auto Power control + void enableAutoPower(int16_t targetRSSI=-90); // TWS: New method to enable/disable auto Power control void setMode(uint8_t mode); // TWS: moved from protected to try to build block()/unblock() wrapper - + int16_t getAckRSSI(void); // TWS: New method to retrieve the ack'd RSSI (if any) uint8_t setLNA(uint8_t newReg); // TWS: function to control LNA reg for power testing purposes int16_t _targetRSSI; // if non-zero then this is the desired end point RSSI for our transmission