Skip to content

Commit

Permalink
10bit addressing
Browse files Browse the repository at this point in the history
Uses 4 least significant bits in the CTL header byte to upgrade from 8bit to 10bit addressing, thus extending from 256 to 1024 possible addresses.
  • Loading branch information
LowPowerLab committed May 1, 2019
1 parent f072131 commit 3ace1ac
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 56 deletions.
49 changes: 20 additions & 29 deletions RFM69.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
uint8_t RFM69::DATA[RF69_MAX_DATA_LEN];
uint8_t RFM69::_mode; // current transceiver state
uint8_t RFM69::DATALEN;
uint8_t RFM69::SENDERID;
uint8_t RFM69::TARGETID; // should match _address
uint16_t RFM69::SENDERID;
uint16_t RFM69::TARGETID; // should match _address
uint8_t RFM69::PAYLOADLEN;
uint8_t RFM69::ACK_REQUESTED;
uint8_t RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
Expand All @@ -56,7 +56,7 @@ RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW)
#endif
}

bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID)
{
_interruptNum = digitalPinToInterrupt(_interruptPin);
if (_interruptNum == NOT_AN_INTERRUPT) return false;
Expand Down Expand Up @@ -206,10 +206,10 @@ void RFM69::sleep() {
}

//set this node's address
void RFM69::setAddress(uint8_t addr)
void RFM69::setAddress(uint16_t addr)
{
_address = addr;
writeReg(REG_NODEADRS, _address);
writeReg(REG_NODEADRS, _address); //unused in packet mode
}

//set this node's network id
Expand Down Expand Up @@ -242,7 +242,7 @@ bool RFM69::canSend()
return false;
}

void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
void RFM69::send(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
{
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
uint32_t now = millis();
Expand All @@ -256,27 +256,22 @@ void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool
// The reason for the semi-automaton is that the lib is interrupt driven and
// requires user action to read the received data and decide what to do with it
// replies usually take only 5..8ms at 50kbps@915MHz
bool RFM69::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime) {
bool RFM69::sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime) {
uint32_t sentTime;
for (uint8_t i = 0; i <= retries; i++)
{
send(toAddress, buffer, bufferSize, true);
sentTime = millis();
while (millis() - sentTime < retryWaitTime)
{
if (ACKReceived(toAddress))
{
//Serial.print(" ~ms:"); Serial.print(millis() - sentTime);
return true;
}
if (ACKReceived(toAddress)) return true;
}
//Serial.print(" RETRY#"); Serial.println(i + 1);
}
return false;
}

// should be polled immediately after sending a packet with ACK request
bool RFM69::ACKReceived(uint8_t fromNodeID) {
bool RFM69::ACKReceived(uint16_t fromNodeID) {
if (receiveDone())
return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
return false;
Expand All @@ -290,7 +285,7 @@ bool RFM69::ACKRequested() {
// should be called immediately after reception in case sender wants ACK
void RFM69::sendACK(const void* buffer, uint8_t bufferSize) {
ACK_REQUESTED = 0; // TWS added to make sure we don't end up in a timing race and infinite loop sending Acks
uint8_t sender = SENDERID;
uint16_t sender = SENDERID;
int16_t _RSSI = RSSI; // save payload received RSSI value
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
uint32_t now = millis();
Expand All @@ -301,7 +296,7 @@ void RFM69::sendACK(const void* buffer, uint8_t bufferSize) {
}

// internal function
void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK)
void RFM69::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK)
{
setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
Expand All @@ -315,12 +310,15 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
else if (requestACK)
CTLbyte = RFM69_CTL_REQACK;

if (toAddress > 0xFF) CTLbyte |= (toAddress & 0x300) >> 6; //assign last 2 bits of address if > 255
if (_address > 0xFF) CTLbyte |= (_address & 0x300) >> 8; //assign last 2 bits of address if > 255

// write to FIFO
select();
SPI.transfer(REG_FIFO | 0x80);
SPI.transfer(bufferSize + 3);
SPI.transfer(toAddress);
SPI.transfer(_address);
SPI.transfer((uint8_t)toAddress);
SPI.transfer((uint8_t)_address);
SPI.transfer(CTLbyte);

for (uint8_t i = 0; i < bufferSize; i++)
Expand All @@ -337,46 +335,39 @@ 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() {
//pinMode(4, OUTPUT);
//digitalWrite(4, 1);
if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY))
{
//RSSI = readRSSI();
setMode(RF69_MODE_STANDBY);
select();
SPI.transfer(REG_FIFO & 0x7F);
PAYLOADLEN = SPI.transfer(0);
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
TARGETID = SPI.transfer(0);
SENDERID = SPI.transfer(0);
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
{
PAYLOADLEN = 0;
unselect();
receiveBegin();
//digitalWrite(4, 0);
return;
}

DATALEN = PAYLOADLEN - 3;
SENDERID = SPI.transfer(0);
uint8_t CTLbyte = SPI.transfer(0);

ACK_RECEIVED = CTLbyte & RFM69_CTL_SENDACK; // extract ACK-received flag
ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag

interruptHook(CTLbyte); // TWS: hook to derived class interrupt function

for (uint8_t i = 0; i < DATALEN; i++)
{
DATA[i] = SPI.transfer(0);
}
if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
unselect();
setMode(RF69_MODE_RX);
}
RSSI = readRSSI();
//digitalWrite(4, 0);
}

// internal function
Expand Down
26 changes: 15 additions & 11 deletions RFM69.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@

#define null 0
#define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
#define RF69_BROADCAST_ADDR 255
#define RF69_BROADCAST_ADDR 0
#define RF69_CSMA_LIMIT_MS 1000
#define RF69_TX_LIMIT_MS 1000
#define RF69_FSTEP 61.03515625 // == FXOSC / 2^19 = 32MHz / 2^19 (p13 in datasheet)
Expand All @@ -152,7 +152,11 @@
#define RFM69_CTL_SENDACK 0x80
#define RFM69_CTL_REQACK 0x40

//#define RF69_LISTENMODE_ENABLE //comment this line out to compile sketches without the ListenMode (saves ~2k)
//Native hardware ListenMode is experimental
//It was determined to be buggy and unreliable, see https://lowpowerlab.com/forum/low-power-techniques/ultra-low-power-listening-mode-for-battery-nodes/msg20261/#msg20261
//uncomment to try ListenMode, adds ~1K to compiled size
//FYI - 10bit addressing is not supported in ListenMode
//#define RF69_LISTENMODE_ENABLE

#if defined(RF69_LISTENMODE_ENABLE)
// By default, receive for 256uS in listen mode and idle for ~1s
Expand All @@ -164,8 +168,8 @@ class RFM69 {
public:
static uint8_t DATA[RF69_MAX_DATA_LEN]; // recv/xmit buf, including header & crc bytes
static uint8_t DATALEN;
static uint8_t SENDERID;
static uint8_t TARGETID; // should match _address
static uint16_t SENDERID;
static uint16_t TARGETID; // should match _address
static uint8_t PAYLOADLEN;
static uint8_t ACK_REQUESTED;
static uint8_t ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
Expand All @@ -177,14 +181,14 @@ class RFM69 {

RFM69(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false);

bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
void setAddress(uint8_t addr);
bool initialize(uint8_t freqBand, uint16_t ID, uint8_t networkID=1);
void setAddress(uint16_t addr);
void setNetwork(uint8_t networkID);
bool canSend();
virtual void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
virtual bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
virtual void send(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
virtual bool sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
virtual bool receiveDone();
bool ACKReceived(uint8_t fromNodeID);
bool ACKReceived(uint16_t fromNodeID);
bool ACKRequested();
virtual void sendACK(const void* buffer = "", uint8_t bufferSize=0);
uint32_t getFrequency();
Expand All @@ -210,13 +214,13 @@ class RFM69 {
void interruptHandler();
virtual void interruptHook(uint8_t CTLbyte) {};
static volatile bool _haveData;
virtual void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false);
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;
uint8_t _address;
uint16_t _address;
bool _promiscuousMode;
uint8_t _powerLevel;
bool _isRFM69HW;
Expand Down
25 changes: 14 additions & 11 deletions RFM69_ATC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ volatile uint8_t RFM69_ATC::ACK_RSSI_REQUESTED; // new type of flag on ACK_REQU
//=============================================================================
// initialize() - some extra initialization before calling base class
//=============================================================================
bool RFM69_ATC::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID) {
bool RFM69_ATC::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) {
_targetRSSI = 0; // TomWS1: default to disabled
_ackRSSI = 0; // TomWS1: no existing response at init time
ACK_RSSI_REQUESTED = 0; // TomWS1: init to none
Expand Down Expand Up @@ -66,7 +66,7 @@ void RFM69_ATC::setMode(uint8_t newMode) {
// should be called immediately after reception in case sender wants ACK
void RFM69_ATC::sendACK(const void* buffer, uint8_t bufferSize) {
ACK_REQUESTED = 0; // TomWS1 added to make sure we don't end up in a timing race and infinite loop sending Acks
uint8_t sender = SENDERID;
uint16_t sender = SENDERID;
int16_t _RSSI = RSSI; // save payload received RSSI value
bool sendRSSI = ACK_RSSI_REQUESTED;
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
Expand All @@ -81,14 +81,14 @@ void RFM69_ATC::sendACK(const void* buffer, uint8_t bufferSize) {
// sendFrame() - the basic version is used to match the RFM69 prototype so we can extend it
//=============================================================================
// this sendFrame is generally called by the internal RFM69 functions. Simply transfer to our modified version.
void RFM69_ATC::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK) {
void RFM69_ATC::sendFrame(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK) {
sendFrame(toAddress, buffer, bufferSize, requestACK, sendACK, false, 0); // default sendFrame
}

//=============================================================================
// sendFrame() - the new one with additional parameters. This packages recv'd RSSI with the packet, if required.
//=============================================================================
void RFM69_ATC::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK, bool sendRSSI, int16_t lastRSSI) {
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"
Expand All @@ -100,21 +100,24 @@ void RFM69_ATC::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferS
select();
SPI.transfer(REG_FIFO | 0x80);
SPI.transfer(bufferSize + 3);
SPI.transfer(toAddress);
SPI.transfer(_address);
SPI.transfer(toAddress); //lower 8bits
SPI.transfer(_address); //lower 8bits

// control byte
// CTL (control byte)
uint8_t CTLbyte=0x0;
if (toAddress > 0xFF) CTLbyte |= (toAddress & 0x300) >> 6; //assign last 2 bits of address if > 255
if (_address > 0xFF) CTLbyte |= (_address & 0x300) >> 8; //assign last 2 bits of address if > 255
if (sendACK) { // TomWS1: adding logic to return ACK_RSSI if requested
SPI.transfer(RFM69_CTL_SENDACK | (sendRSSI?RFM69_CTL_RESERVE1:0)); // TomWS1 TODO: Replace with EXT1
SPI.transfer(CTLbyte | RFM69_CTL_SENDACK | (sendRSSI?RFM69_CTL_RESERVE1:0)); // TomWS1 TODO: Replace with EXT1
if (sendRSSI) {
SPI.transfer(abs(lastRSSI)); //RSSI dBm is negative expected between [-100 .. -20], convert to positive and pass along as single extra header byte
bufferSize -=1; // account for the extra ACK-RSSI 'data' byte
}
}
else if (requestACK) { // TODO: add logic to request ackRSSI with ACK - this is when both ends of a transmission would dial power down. May not work well for gateways in multi node networks
SPI.transfer(_targetRSSI ? RFM69_CTL_REQACK | RFM69_CTL_RESERVE1 : RFM69_CTL_REQACK);
SPI.transfer(CTLbyte | (_targetRSSI ? RFM69_CTL_REQACK | RFM69_CTL_RESERVE1 : RFM69_CTL_REQACK));
}
else SPI.transfer(0x00);
else SPI.transfer(CTLbyte);

for (uint8_t i = 0; i < bufferSize; i++)
SPI.transfer(((uint8_t*) buffer)[i]);
Expand Down Expand Up @@ -156,7 +159,7 @@ void RFM69_ATC::interruptHook(uint8_t CTLbyte) {
//=============================================================================
// sendWithRetry() - overrides the base to allow increasing power when repeated ACK requests fail
//=============================================================================
bool RFM69_ATC::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime) {
bool RFM69_ATC::sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime) {
uint32_t sentTime;
for (uint8_t i = 0; i <= retries; i++)
{
Expand Down
8 changes: 4 additions & 4 deletions RFM69_ATC.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class RFM69_ATC: public RFM69 {
RFM69(slaveSelectPin, interruptPin, isRFM69HW) {
}

bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
bool initialize(uint8_t freqBand, uint16_t ID, uint8_t networkID=1);
void sendACK(const void* buffer = "", uint8_t bufferSize=0);
//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(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
bool sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
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

Expand All @@ -55,8 +55,8 @@ class RFM69_ATC: public RFM69 {

protected:
void interruptHook(uint8_t CTLbyte);
void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false); // Need this one to match the RFM69 prototype.
void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK, bool sendACK, bool sendRSSI, int16_t lastRSSI);
void sendFrame(uint16_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false); // Need this one to match the RFM69 prototype.
void sendFrame(uint16_t toAddress, const void* buffer, uint8_t size, bool requestACK, bool sendACK, bool sendRSSI, int16_t lastRSSI);
void receiveBegin();
//void setHighPowerRegs(bool onOff);

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=RFM69_LowPowerLab
version=1.2.0
version=1.3.0
author=LowPowerLab <lowpowerlab.com>
maintainer=Felix Rusu <felix@lowpowerlab.com>
sentence=Simple Arduino library for RFM69/SX1231h based radio module transceivers
Expand Down

0 comments on commit 3ace1ac

Please sign in to comment.