Merge pull request #21 from damadmai/master
Unify indentation (from TAB to two spaces)
This commit is contained in:
commit
957b9952ec
|
|
@ -59,7 +59,7 @@
|
|||
#define BUTTON_PIN 3 //user button on interrupt 1
|
||||
|
||||
RFM69 radio;
|
||||
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI SSD1306 OLED 128x64
|
||||
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI SSD1306 OLED 128x64
|
||||
bool promiscuousMode = true; //set to 'true' to sniff all packets on the same network
|
||||
|
||||
void setup() {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ RFM69 radio;
|
|||
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
|
||||
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
|
||||
|
||||
typedef struct {
|
||||
typedef struct {
|
||||
int nodeId; //store this nodeId
|
||||
unsigned long uptime; //uptime in ms
|
||||
float temp; //temperature maybe?
|
||||
|
|
@ -88,10 +88,10 @@ void loop() {
|
|||
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
|
||||
Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
|
||||
if (promiscuousMode)
|
||||
{
|
||||
{
|
||||
Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
|
||||
}
|
||||
|
||||
|
||||
if (radio.DATALEN != sizeof(Payload))
|
||||
Serial.print("Invalid payload received, not matching Payload struct!");
|
||||
else
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ boolean requestACK = false;
|
|||
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
|
||||
RFM69 radio;
|
||||
|
||||
typedef struct {
|
||||
typedef struct {
|
||||
int nodeId; //store this nodeId
|
||||
unsigned long uptime; //uptime in ms
|
||||
float temp; //temperature maybe?
|
||||
|
|
|
|||
234
RFM69.cpp
234
RFM69.cpp
|
|
@ -33,14 +33,14 @@
|
|||
#include <SPI.h>
|
||||
|
||||
volatile byte RFM69::DATA[RF69_MAX_DATA_LEN];
|
||||
volatile byte RFM69::_mode; // current transceiver state
|
||||
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::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
|
||||
volatile int RFM69::RSSI; //most accurate RSSI during reception (closest to the reception)
|
||||
volatile byte RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
|
||||
volatile int RFM69::RSSI; // most accurate RSSI during reception (closest to the reception)
|
||||
RFM69* RFM69::selfPointer;
|
||||
|
||||
bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID)
|
||||
|
|
@ -48,49 +48,49 @@ 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 }, //no shaping
|
||||
/* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, //default:4.8 KBPS
|
||||
/* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, // no shaping
|
||||
/* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, // default:4.8 KBPS
|
||||
/* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_55555},
|
||||
/* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, //default:5khz, (FDEV + BitRate/2 <= 500Khz)
|
||||
/* 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))) },
|
||||
|
||||
|
||||
// looks like PA1 and PA2 are not implemented on RFM69W, 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)**
|
||||
// +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},
|
||||
///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, //over current protection (default is 95mA)
|
||||
|
||||
///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
|
||||
|
||||
// RXBW defaults are { REG_RXBW, 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)
|
||||
//for BR-19200: //* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
|
||||
/* 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
|
||||
/* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, // (BitRate < 2 * RxBw)
|
||||
//for BR-19200: /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
|
||||
/* 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
|
||||
/* 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
|
||||
/* 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)
|
||||
//for BR-19200: //* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | 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
|
||||
/* 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
|
||||
/* 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)
|
||||
//for BR-19200: /* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | 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
|
||||
/* 0x6F */ { REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0 }, // run DAGC continuously in RX mode, recommended default for AfcLowBetaOn=0
|
||||
{255, 0}
|
||||
};
|
||||
|
||||
pinMode(_slaveSelectPin, OUTPUT);
|
||||
SPI.begin();
|
||||
|
||||
|
||||
do writeReg(REG_SYNCVALUE1, 0xaa); while (readReg(REG_SYNCVALUE1) != 0xaa);
|
||||
do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55);
|
||||
do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55);
|
||||
|
||||
for (byte i = 0; CONFIG[i][0] != 255; i++)
|
||||
writeReg(CONFIG[i][0], CONFIG[i][1]);
|
||||
|
|
@ -99,9 +99,9 @@ bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID)
|
|||
// Disable it during initialization so we always start from a known state.
|
||||
encrypt(0);
|
||||
|
||||
setHighPower(_isRFM69HW); //called regardless if it's a RFM69W or RFM69HW
|
||||
setHighPower(_isRFM69HW); // called regardless if it's a RFM69W or RFM69HW
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
|
||||
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
|
||||
attachInterrupt(_interruptNum, RFM69::isr0, RISING);
|
||||
|
||||
selfPointer = this;
|
||||
|
|
@ -109,17 +109,17 @@ bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID)
|
|||
return true;
|
||||
}
|
||||
|
||||
//return the frequency (in Hz)
|
||||
// return the frequency (in Hz)
|
||||
uint32_t RFM69::getFrequency()
|
||||
{
|
||||
return RF69_FSTEP * (((uint32_t)readReg(REG_FRFMSB)<<16) + ((uint16_t)readReg(REG_FRFMID)<<8) + readReg(REG_FRFLSB));
|
||||
}
|
||||
|
||||
//set the frequency (in Hz)
|
||||
// set the frequency (in Hz)
|
||||
void RFM69::setFrequency(uint32_t freqHz)
|
||||
{
|
||||
//TODO: p38 hopping sequence may need to be followed in some cases
|
||||
freqHz /= RF69_FSTEP; //divide down by FSTEP to get FRF
|
||||
// TODO: p38 hopping sequence may need to be followed in some cases
|
||||
freqHz /= RF69_FSTEP; // divide down by FSTEP to get FRF
|
||||
writeReg(REG_FRFMSB, freqHz >> 16);
|
||||
writeReg(REG_FRFMID, freqHz >> 8);
|
||||
writeReg(REG_FRFLSB, freqHz);
|
||||
|
|
@ -127,34 +127,34 @@ void RFM69::setFrequency(uint32_t freqHz)
|
|||
|
||||
void RFM69::setMode(byte newMode)
|
||||
{
|
||||
if (newMode == _mode) return; //TODO: can remove this?
|
||||
if (newMode == _mode) return; // TODO: can remove this?
|
||||
|
||||
switch (newMode) {
|
||||
case RF69_MODE_TX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
|
||||
switch (newMode) {
|
||||
case RF69_MODE_TX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
|
||||
if (_isRFM69HW) setHighPowerRegs(true);
|
||||
break;
|
||||
case RF69_MODE_RX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
|
||||
break;
|
||||
case RF69_MODE_RX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
|
||||
if (_isRFM69HW) setHighPowerRegs(false);
|
||||
break;
|
||||
case RF69_MODE_SYNTH:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
|
||||
break;
|
||||
case RF69_MODE_STANDBY:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_STANDBY);
|
||||
break;
|
||||
case RF69_MODE_SLEEP:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SLEEP);
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
break;
|
||||
case RF69_MODE_SYNTH:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
|
||||
break;
|
||||
case RF69_MODE_STANDBY:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_STANDBY);
|
||||
break;
|
||||
case RF69_MODE_SLEEP:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SLEEP);
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
// we are using packet mode, so this check is not really needed
|
||||
// we are using packet mode, so this check is not really needed
|
||||
// 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
|
||||
while (_mode == RF69_MODE_SLEEP && (readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
|
||||
|
||||
_mode = newMode;
|
||||
_mode = newMode;
|
||||
}
|
||||
|
||||
void RFM69::sleep() {
|
||||
|
|
@ -164,12 +164,12 @@ void RFM69::sleep() {
|
|||
void RFM69::setAddress(byte addr)
|
||||
{
|
||||
_address = addr;
|
||||
writeReg(REG_NODEADRS, _address);
|
||||
writeReg(REG_NODEADRS, _address);
|
||||
}
|
||||
|
||||
void RFM69::setNetwork(byte networkID)
|
||||
{
|
||||
writeReg(REG_SYNCVALUE2, networkID);
|
||||
writeReg(REG_SYNCVALUE2, networkID);
|
||||
}
|
||||
|
||||
// set output power: 0=min, 31=max
|
||||
|
|
@ -182,7 +182,7 @@ void RFM69::setPowerLevel(byte powerLevel)
|
|||
|
||||
bool RFM69::canSend()
|
||||
{
|
||||
if (_mode == RF69_MODE_RX && PAYLOADLEN == 0 && readRSSI() < CSMA_LIMIT) //if signal stronger than -100dBm is detected assume channel activity
|
||||
if (_mode == RF69_MODE_RX && PAYLOADLEN == 0 && readRSSI() < CSMA_LIMIT) // if signal stronger than -100dBm is detected assume channel activity
|
||||
{
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
return true;
|
||||
|
|
@ -201,12 +201,12 @@ void RFM69::send(byte toAddress, const void* buffer, byte bufferSize, bool reque
|
|||
// 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
|
||||
// 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(byte toAddress, const void* buffer, byte bufferSize, byte retries, byte retryWaitTime) {
|
||||
unsigned long sentTime;
|
||||
for (byte i=0; i<=retries; i++)
|
||||
for (byte i = 0; i <= retries; i++)
|
||||
{
|
||||
send(toAddress, buffer, bufferSize, true);
|
||||
sentTime = millis();
|
||||
|
|
@ -223,59 +223,59 @@ bool RFM69::sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, b
|
|||
return false;
|
||||
}
|
||||
|
||||
/// Should be polled immediately after sending a packet with ACK request
|
||||
// should be polled immediately after sending a packet with ACK request
|
||||
bool RFM69::ACKReceived(byte fromNodeID) {
|
||||
if (receiveDone())
|
||||
return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
|
||||
return false;
|
||||
}
|
||||
|
||||
//check whether an ACK was requested in the last received packet (non-broadcasted packet)
|
||||
// check whether an ACK was requested in the last received packet (non-broadcasted packet)
|
||||
bool RFM69::ACKRequested() {
|
||||
return ACK_REQUESTED && (TARGETID != RF69_BROADCAST_ADDR);
|
||||
}
|
||||
|
||||
/// Should be called immediately after reception in case sender wants ACK
|
||||
// should be called immediately after reception in case sender wants ACK
|
||||
void RFM69::sendACK(const void* buffer, byte bufferSize) {
|
||||
byte sender = SENDERID;
|
||||
int _RSSI = RSSI; //save payload received RSSI value
|
||||
int _RSSI = RSSI; // save payload received RSSI value
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
unsigned long now = millis();
|
||||
while (!canSend() && millis()-now < RF69_CSMA_LIMIT_MS) receiveDone();
|
||||
sendFrame(sender, buffer, bufferSize, false, true);
|
||||
RSSI = _RSSI; //restore payload RSSI
|
||||
RSSI = _RSSI; // restore payload RSSI
|
||||
}
|
||||
|
||||
void RFM69::sendFrame(byte toAddress, const void* buffer, byte 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
|
||||
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"
|
||||
if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
|
||||
|
||||
//write to FIFO
|
||||
select();
|
||||
SPI.transfer(REG_FIFO | 0x80);
|
||||
SPI.transfer(bufferSize + 3);
|
||||
SPI.transfer(toAddress);
|
||||
// write to FIFO
|
||||
select();
|
||||
SPI.transfer(REG_FIFO | 0x80);
|
||||
SPI.transfer(bufferSize + 3);
|
||||
SPI.transfer(toAddress);
|
||||
SPI.transfer(_address);
|
||||
|
||||
//control byte
|
||||
|
||||
// control byte
|
||||
if (sendACK)
|
||||
SPI.transfer(0x80);
|
||||
else if (requestACK)
|
||||
SPI.transfer(0x40);
|
||||
else SPI.transfer(0x00);
|
||||
|
||||
for (byte i = 0; i < bufferSize; i++)
|
||||
SPI.transfer(((byte*)buffer)[i]);
|
||||
unselect();
|
||||
|
||||
/* no need to wait for transmit mode to be ready since its handled by the radio */
|
||||
setMode(RF69_MODE_TX);
|
||||
for (byte i = 0; i < bufferSize; i++)
|
||||
SPI.transfer(((byte*)buffer)[i]);
|
||||
unselect();
|
||||
|
||||
/* no need to wait for transmit mode to be ready since its handled by the radio */
|
||||
setMode(RF69_MODE_TX);
|
||||
unsigned long 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 ModeReady
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
}
|
||||
|
||||
|
|
@ -289,10 +289,10 @@ void RFM69::interruptHandler() {
|
|||
select();
|
||||
SPI.transfer(REG_FIFO & 0x7f);
|
||||
PAYLOADLEN = SPI.transfer(0);
|
||||
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; //precaution
|
||||
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
|
||||
TARGETID = SPI.transfer(0);
|
||||
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
|
||||
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();
|
||||
|
|
@ -304,15 +304,15 @@ void RFM69::interruptHandler() {
|
|||
DATALEN = PAYLOADLEN - 3;
|
||||
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
|
||||
|
||||
for (byte i= 0; i < DATALEN; i++)
|
||||
|
||||
ACK_RECEIVED = CTLbyte & 0x80; // extract ACK-received flag
|
||||
ACK_REQUESTED = CTLbyte & 0x40; // extract ACK-requested flag
|
||||
|
||||
for (byte 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
|
||||
if (DATALEN<RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
|
||||
unselect();
|
||||
setMode(RF69_MODE_RX);
|
||||
}
|
||||
|
|
@ -332,22 +332,22 @@ void RFM69::receiveBegin() {
|
|||
RSSI = 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
|
||||
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
|
||||
setMode(RF69_MODE_RX);
|
||||
}
|
||||
|
||||
bool RFM69::receiveDone() {
|
||||
// ATOMIC_BLOCK(ATOMIC_FORCEON)
|
||||
// {
|
||||
noInterrupts(); //re-enabled in unselect() via setMode() or via receiveBegin()
|
||||
//ATOMIC_BLOCK(ATOMIC_FORCEON)
|
||||
//{
|
||||
noInterrupts(); // re-enabled in unselect() via setMode() or via receiveBegin()
|
||||
if (_mode == RF69_MODE_RX && PAYLOADLEN>0)
|
||||
{
|
||||
setMode(RF69_MODE_STANDBY); //enables interrupts
|
||||
setMode(RF69_MODE_STANDBY); // enables interrupts
|
||||
return true;
|
||||
}
|
||||
else if (_mode == RF69_MODE_RX) //already in RX no payload yet
|
||||
else if (_mode == RF69_MODE_RX) // already in RX no payload yet
|
||||
{
|
||||
interrupts(); //explicitly re-enable interrupts
|
||||
interrupts(); // explicitly re-enable interrupts
|
||||
return false;
|
||||
}
|
||||
receiveBegin();
|
||||
|
|
@ -360,11 +360,11 @@ bool RFM69::receiveDone() {
|
|||
// KEY HAS TO BE 16 bytes !!!
|
||||
void RFM69::encrypt(const char* key) {
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
if (key!=0)
|
||||
if (key != 0)
|
||||
{
|
||||
select();
|
||||
SPI.transfer(REG_AESKEY1 | 0x80);
|
||||
for (byte i = 0; i<16; i++)
|
||||
for (byte i = 0; i < 16; i++)
|
||||
SPI.transfer(key[i]);
|
||||
unselect();
|
||||
}
|
||||
|
|
@ -375,9 +375,9 @@ int RFM69::readRSSI(bool forceTrigger) {
|
|||
int rssi = 0;
|
||||
if (forceTrigger)
|
||||
{
|
||||
//RSSI trigger not needed if DAGC is in continuous mode
|
||||
// RSSI trigger not needed if DAGC is in continuous mode
|
||||
writeReg(REG_RSSICONFIG, RF_RSSI_START);
|
||||
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // Wait for RSSI_Ready
|
||||
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // wait for RSSI_Ready
|
||||
}
|
||||
rssi = -readReg(REG_RSSIVALUE);
|
||||
rssi >>= 1;
|
||||
|
|
@ -401,23 +401,23 @@ void RFM69::writeReg(byte addr, byte value)
|
|||
unselect();
|
||||
}
|
||||
|
||||
/// Select the transceiver
|
||||
// select the transceiver
|
||||
void RFM69::select() {
|
||||
noInterrupts();
|
||||
//save current SPI settings
|
||||
// save current SPI settings
|
||||
_SPCR = SPCR;
|
||||
_SPSR = SPSR;
|
||||
//set RFM69 SPI settings
|
||||
// set RFM69 SPI settings
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV4); //decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV4); // decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
|
||||
digitalWrite(_slaveSelectPin, LOW);
|
||||
}
|
||||
|
||||
/// UNselect the transceiver chip
|
||||
// UNselect the transceiver chip
|
||||
void RFM69::unselect() {
|
||||
digitalWrite(_slaveSelectPin, HIGH);
|
||||
//restore SPI settings to what they were before talking to RFM69
|
||||
// restore SPI settings to what they were before talking to RFM69
|
||||
SPCR = _SPCR;
|
||||
SPSR = _SPSR;
|
||||
interrupts();
|
||||
|
|
@ -426,17 +426,17 @@ void RFM69::unselect() {
|
|||
// ON = disable filtering to capture all frames on network
|
||||
// OFF = enable node+broadcast filtering to capture only frames sent to this/broadcast address
|
||||
void RFM69::promiscuous(bool onOff) {
|
||||
_promiscuousMode=onOff;
|
||||
_promiscuousMode = onOff;
|
||||
//writeReg(REG_PACKETCONFIG1, (readReg(REG_PACKETCONFIG1) & 0xF9) | (onOff ? RF_PACKET1_ADRSFILTERING_OFF : RF_PACKET1_ADRSFILTERING_NODEBROADCAST));
|
||||
}
|
||||
|
||||
void RFM69::setHighPower(bool onOff) {
|
||||
_isRFM69HW = onOff;
|
||||
writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
|
||||
if (_isRFM69HW) //turning ON
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); //enable P1 & P2 amplifier stages
|
||||
if (_isRFM69HW) // turning ON
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); // enable P1 & P2 amplifier stages
|
||||
else
|
||||
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); //enable P0 only
|
||||
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); // enable P0 only
|
||||
}
|
||||
|
||||
void RFM69::setHighPowerRegs(bool onOff) {
|
||||
|
|
@ -449,15 +449,15 @@ void RFM69::setCS(byte newSPISlaveSelect) {
|
|||
pinMode(_slaveSelectPin, OUTPUT);
|
||||
}
|
||||
|
||||
//for debugging
|
||||
// for debugging
|
||||
void RFM69::readAllRegs()
|
||||
{
|
||||
byte regVal;
|
||||
|
||||
|
||||
for (byte regAddr = 1; regAddr <= 0x4F; regAddr++)
|
||||
{
|
||||
{
|
||||
select();
|
||||
SPI.transfer(regAddr & 0x7f); // send address + r/w bit
|
||||
SPI.transfer(regAddr & 0x7f); // send address + r/w bit
|
||||
regVal = SPI.transfer(0);
|
||||
unselect();
|
||||
|
||||
|
|
@ -466,17 +466,17 @@ void RFM69::readAllRegs()
|
|||
Serial.print(regVal,HEX);
|
||||
Serial.print(" - ");
|
||||
Serial.println(regVal,BIN);
|
||||
}
|
||||
}
|
||||
unselect();
|
||||
}
|
||||
|
||||
byte RFM69::readTemperature(byte calFactor) //returns centigrade
|
||||
byte RFM69::readTemperature(byte calFactor) // returns centigrade
|
||||
{
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
|
||||
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
|
||||
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; //'complement'corrects the slope, rising temp = rising val
|
||||
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
|
||||
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement'corrects the slope, rising temp = rising val
|
||||
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
|
||||
|
||||
void RFM69::rcCalibration()
|
||||
{
|
||||
|
|
|
|||
56
RFM69.h
56
RFM69.h
|
|
@ -30,10 +30,10 @@
|
|||
// **********************************************************************************
|
||||
#ifndef RFM69_h
|
||||
#define RFM69_h
|
||||
#include <Arduino.h> //assumes Arduino IDE v1.0 or greater
|
||||
#include <Arduino.h> // assumes Arduino IDE v1.0 or greater
|
||||
|
||||
#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)
|
||||
#define RF69_SPI_CS SS // SS is the SPI slave select pin, for instance D10 on atmega328
|
||||
#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)
|
||||
#define RF69_SPI_CS SS // SS is the SPI slave select pin, for instance D10 on atmega328
|
||||
|
||||
// INT0 on AVRs should be connected to RFM69's DIO0 (ex on Atmega328 it's D2, on Atmega644/1284 it's D2)
|
||||
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega88) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
|
||||
|
|
@ -48,38 +48,38 @@
|
|||
#endif
|
||||
|
||||
|
||||
#define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
|
||||
#define RF69_MODE_SLEEP 0 // XTAL OFF
|
||||
#define RF69_MODE_STANDBY 1 // XTAL ON
|
||||
#define RF69_MODE_SYNTH 2 // PLL ON
|
||||
#define RF69_MODE_RX 3 // RX MODE
|
||||
#define RF69_MODE_TX 4 // TX MODE
|
||||
#define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
|
||||
#define RF69_MODE_SLEEP 0 // XTAL OFF
|
||||
#define RF69_MODE_STANDBY 1 // XTAL ON
|
||||
#define RF69_MODE_SYNTH 2 // PLL ON
|
||||
#define RF69_MODE_RX 3 // RX MODE
|
||||
#define RF69_MODE_TX 4 // TX MODE
|
||||
|
||||
//available frequency bands
|
||||
#define RF69_315MHZ 31 // non trivial values to avoid misconfiguration
|
||||
#define RF69_433MHZ 43
|
||||
#define RF69_868MHZ 86
|
||||
#define RF69_915MHZ 91
|
||||
// available frequency bands
|
||||
#define RF69_315MHZ 31 // non trivial values to avoid misconfiguration
|
||||
#define RF69_433MHZ 43
|
||||
#define RF69_868MHZ 86
|
||||
#define RF69_915MHZ 91
|
||||
|
||||
#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_CSMA_LIMIT_MS 1000
|
||||
#define RF69_TX_LIMIT_MS 1000
|
||||
#define RF69_FSTEP 61.03515625 // == FXOSC/2^19 = 32mhz/2^19 (p13 in DS)
|
||||
#define RF69_TX_LIMIT_MS 1000
|
||||
#define RF69_FSTEP 61.03515625 // == FXOSC/2^19 = 32mhz/2^19 (p13 in DS)
|
||||
|
||||
class RFM69 {
|
||||
public:
|
||||
static volatile byte DATA[RF69_MAX_DATA_LEN]; // recv/xmit buf, including hdr & crc bytes
|
||||
static volatile byte DATA[RF69_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 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 int RSSI; //most accurate RSSI during reception (closest to the reception)
|
||||
static volatile byte _mode; //should be protected?
|
||||
|
||||
static volatile byte ACK_RECEIVED; // Should be polled immediately after sending a packet with ACK request
|
||||
static volatile int RSSI; // most accurate RSSI during reception (closest to the reception)
|
||||
static volatile byte _mode; // should be protected?
|
||||
|
||||
RFM69(byte slaveSelectPin=RF69_SPI_CS, byte interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, byte interruptNum=RF69_IRQ_NUM) {
|
||||
_slaveSelectPin = slaveSelectPin;
|
||||
_interruptPin = interruptPin;
|
||||
|
|
@ -95,7 +95,7 @@ class RFM69 {
|
|||
void setNetwork(byte networkID);
|
||||
bool canSend();
|
||||
void send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK=false);
|
||||
bool sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries=2, byte retryWaitTime=40); //40ms roundtrip req for 61byte packets
|
||||
bool sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries=2, byte retryWaitTime=40); // 40ms roundtrip req for 61byte packets
|
||||
bool receiveDone();
|
||||
bool ACKReceived(byte fromNodeID);
|
||||
bool ACKRequested();
|
||||
|
|
@ -106,11 +106,11 @@ class RFM69 {
|
|||
void setCS(byte newSPISlaveSelect);
|
||||
int readRSSI(bool forceTrigger=false);
|
||||
void promiscuous(bool onOff=true);
|
||||
void setHighPower(bool onOFF=true); //have to call it after initialize for RFM69HW
|
||||
void setPowerLevel(byte level); //reduce/increase transmit power level
|
||||
void setHighPower(bool onOFF=true); // have to call it after initialize for RFM69HW
|
||||
void setPowerLevel(byte level); // reduce/increase transmit power level
|
||||
void sleep();
|
||||
byte readTemperature(byte calFactor=0); //get CMOS temperature (8bit)
|
||||
void rcCalibration(); //calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
|
||||
byte readTemperature(byte calFactor=0); // get CMOS temperature (8bit)
|
||||
void rcCalibration(); // calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
|
||||
|
||||
// allow hacking registers by making these public
|
||||
byte readReg(byte addr);
|
||||
|
|
@ -140,4 +140,4 @@ class RFM69 {
|
|||
void unselect();
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
1727
RFM69registers.h
1727
RFM69registers.h
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue