Transport update (#722)
* Major transport rework * Adjust signal report calculations * Minor driver optimizations * Implement sanity check in legacy RFM69 driver * Additional RFM69 rework * RFM95 encryption * Polishing
This commit is contained in:
+189
-97
@@ -6,7 +6,7 @@
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2016 Sensnology AB
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
@@ -24,6 +24,22 @@
|
||||
|
||||
#include "RFM95.h"
|
||||
|
||||
// debug
|
||||
#if defined(MY_DEBUG_VERBOSE_RFM95)
|
||||
#define RFM95_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< Debug print
|
||||
#else
|
||||
#define RFM95_DEBUG(x,...) //!< DEBUG null
|
||||
#endif
|
||||
|
||||
#if defined (SREG) // To identify AVR vs EP8266
|
||||
uint8_t _SREG; // Used to save and restore the SREG values in SPI transactions
|
||||
#endif
|
||||
|
||||
#if defined (SPCR) && defined (SPSR)
|
||||
uint8_t _SPCR; //!< _SPCR
|
||||
uint8_t _SPSR; //!< _SPSR
|
||||
#endif
|
||||
|
||||
#if defined(LINUX_SPI_BCM)
|
||||
// SPI RX and TX buffers (max packet len + 1 byte for the command)
|
||||
uint8_t spi_rxbuff[RFM95_MAX_PACKET_LEN + 1];
|
||||
@@ -34,7 +50,7 @@ volatile rfm95_internal_t RFM95; //!< internal variables
|
||||
|
||||
LOCAL void RFM95_csn(const bool level)
|
||||
{
|
||||
hwDigitalWrite(MY_RFM95_SPI_CS, level);
|
||||
hwDigitalWrite(MY_RFM95_CS_PIN, level);
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t len,
|
||||
@@ -42,8 +58,8 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
|
||||
{
|
||||
uint8_t status;
|
||||
uint8_t* current = buf;
|
||||
#if !defined(MY_SOFTSPI)
|
||||
_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_MAX_SPEED, MY_RFM95_SPI_DATA_ORDER,
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_SPEED, MY_RFM95_SPI_DATA_ORDER,
|
||||
MY_RFM95_SPI_DATA_MODE));
|
||||
#endif
|
||||
RFM95_csn(LOW);
|
||||
@@ -88,7 +104,7 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
|
||||
}
|
||||
#endif
|
||||
RFM95_csn(HIGH);
|
||||
#if !defined(MY_SOFTSPI)
|
||||
#if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION)
|
||||
_SPI.endTransaction();
|
||||
#endif
|
||||
return status;
|
||||
@@ -116,6 +132,10 @@ LOCAL bool RFM95_initialise(const float frequency)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT\n"));
|
||||
// reset radio module if rst pin defined
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
hwPinMode(MY_RFM95_POWER_PIN, OUTPUT);
|
||||
#endif
|
||||
RFM95_powerUp();
|
||||
#if defined(MY_RFM95_RST_PIN)
|
||||
hwPinMode(MY_RFM95_RST_PIN, OUTPUT);
|
||||
hwDigitalWrite(MY_RFM95_RST_PIN, HIGH);
|
||||
@@ -124,19 +144,25 @@ LOCAL bool RFM95_initialise(const float frequency)
|
||||
hwDigitalWrite(MY_RFM95_RST_PIN, LOW);
|
||||
// wait until chip ready
|
||||
delay(5);
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT:PIN,CS=%d,IQP=%d,IQN=%d,RST=%d\n"), MY_RFM95_CS_PIN, MY_RFM95_IRQ_PIN,
|
||||
MY_RFM95_IRQ_NUM,MY_RFM95_RST_PIN);
|
||||
#else
|
||||
RFM95_DEBUG(PSTR("RFM95:INIT:PIN,CS=%d,IQP=%d,IQN=%d\n"), MY_RFM95_CS_PIN, MY_RFM95_IRQ_PIN,
|
||||
MY_RFM95_IRQ_NUM);
|
||||
#endif
|
||||
|
||||
// set variables
|
||||
RFM95.address = RFM95_BROADCAST_ADDRESS;
|
||||
RFM95.rxBufferValid = false;
|
||||
RFM95.ackReceived = false;
|
||||
RFM95.dataReceived = false;
|
||||
RFM95.txSequenceNumber = 0; // initialise TX sequence counter
|
||||
RFM95.powerLevel = 0;
|
||||
RFM95.ATCenabled = false;
|
||||
RFM95.ATCtargetRSSI = RFM95_RSSItoInternal(RFM95_TARGET_RSSI);
|
||||
|
||||
// SPI init
|
||||
hwDigitalWrite(MY_RFM95_SPI_CS, HIGH);
|
||||
hwPinMode(MY_RFM95_SPI_CS, OUTPUT);
|
||||
hwDigitalWrite(MY_RFM95_CS_PIN, HIGH);
|
||||
hwPinMode(MY_RFM95_CS_PIN, OUTPUT);
|
||||
_SPI.begin();
|
||||
|
||||
// Set LoRa mode (during sleep mode)
|
||||
@@ -156,13 +182,11 @@ LOCAL bool RFM95_initialise(const float frequency)
|
||||
RFM95_setModemRegisters(&configuration);
|
||||
RFM95_setPreambleLength(RFM95_PREAMBLE_LENGTH);
|
||||
RFM95_setFrequency(frequency);
|
||||
// set power level
|
||||
RFM95_setTxPower(MY_RFM95_TX_POWER);
|
||||
|
||||
(void)RFM95_setTxPowerLevel(MY_RFM95_TX_POWER_DBM);
|
||||
// IRQ
|
||||
hwPinMode(MY_RFM95_IRQ_PIN, INPUT);
|
||||
#if defined (SPI_HAS_TRANSACTION) && !defined (ESP8266)
|
||||
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN));
|
||||
#if defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(MY_SOFTSPI)
|
||||
_SPI.usingInterrupt(MY_RFM95_IRQ_NUM);
|
||||
#endif
|
||||
|
||||
if (!RFM95_sanityCheck()) {
|
||||
@@ -194,20 +218,22 @@ LOCAL void RFM95_interruptHandler(void)
|
||||
// Reset the fifo read ptr to the beginning of the packet
|
||||
RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_readReg(RFM95_REG_10_FIFO_RX_CURRENT_ADDR));
|
||||
RFM95_burstReadReg(RFM95_REG_00_FIFO, RFM95.currentPacket.data, bufLen);
|
||||
RFM95.currentPacket.RSSI = RFM95_readReg(
|
||||
RFM95_REG_1A_PKT_RSSI_VALUE); // RSSI of latest packet received
|
||||
RFM95.currentPacket.RSSI = static_cast<rfm95_RSSI_t>(RFM95_readReg(
|
||||
RFM95_REG_1A_PKT_RSSI_VALUE)); // RSSI of latest packet received
|
||||
RFM95.currentPacket.SNR = static_cast<rfm95_SNR_t>(RFM95_readReg(RFM95_REG_19_PKT_SNR_VALUE));
|
||||
RFM95.currentPacket.payloadLen = bufLen - RFM95_HEADER_LEN;
|
||||
// Message for us
|
||||
if ((RFM95.currentPacket.header.version >= RFM95_MIN_PACKET_HEADER_VERSION) &&
|
||||
(RFM95_PROMISCUOUS || RFM95.currentPacket.header.recipient == RFM95.address ||
|
||||
RFM95.currentPacket.header.recipient == RFM95_BROADCAST_ADDRESS)) {
|
||||
RFM95.rxBufferValid = true;
|
||||
RFM95.ackReceived = RFM95_getACKReceived(RFM95.currentPacket.header.controlFlags) &&
|
||||
!RFM95_getACKRequested(RFM95.currentPacket.header.controlFlags);
|
||||
RFM95.dataReceived = !RFM95.ackReceived;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_TX && (irqFlags & RFM95_TX_DONE) ) {
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY); // change eventually to RX
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_CAD && (irqFlags & RFM95_CAD_DONE) ) {
|
||||
RFM95.cad = irqFlags & RFM95_CAD_DETECTED;
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
@@ -219,59 +245,67 @@ LOCAL void RFM95_interruptHandler(void)
|
||||
|
||||
LOCAL bool RFM95_available(void)
|
||||
{
|
||||
if (RFM95.radioMode == RFM95_RADIO_MODE_TX) {
|
||||
if (RFM95.dataReceived) {
|
||||
// data received - we are still in STDBY from IRQ handler
|
||||
return true;
|
||||
} else if (RFM95.radioMode == RFM95_RADIO_MODE_TX) {
|
||||
return false;
|
||||
} else if (RFM95.radioMode != RFM95_RADIO_MODE_RX) {
|
||||
// we are not in RX, and no data received
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
}
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
return RFM95.rxBufferValid;
|
||||
return false;
|
||||
}
|
||||
|
||||
LOCAL void RFM95_clearRxBuffer(void)
|
||||
LOCAL uint8_t RFM95_recv(uint8_t* buf, const uint8_t maxBufSize)
|
||||
{
|
||||
noInterrupts();
|
||||
RFM95.rxBufferValid = false;
|
||||
interrupts();
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_recv(uint8_t* buf)
|
||||
{
|
||||
if (!RFM95_available()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// atomic
|
||||
#ifdef SREG
|
||||
_SREG = SREG;
|
||||
#endif
|
||||
|
||||
noInterrupts();
|
||||
const uint8_t payloadLen = RFM95.currentPacket.payloadLen;
|
||||
|
||||
const uint8_t payloadLen = min(RFM95.currentPacket.payloadLen, maxBufSize);
|
||||
const uint8_t sender = RFM95.currentPacket.header.sender;
|
||||
const rfm95_sequenceNumber_t sequenceNumber = RFM95.currentPacket.header.sequenceNumber;
|
||||
const uint8_t controlFlags = RFM95.currentPacket.header.controlFlags;
|
||||
const rfm95_RSSI_t RSSI = RFM95.currentPacket.RSSI; // of incoming packet
|
||||
const rfm95_controlFlags_t controlFlags = RFM95.currentPacket.header.controlFlags;
|
||||
const rfm95_RSSI_t RSSI = RFM95.currentPacket.RSSI;
|
||||
const rfm95_SNR_t SNR = RFM95.currentPacket.SNR;
|
||||
if (buf != NULL) {
|
||||
(void)memcpy((void*)buf, (void*)RFM95.currentPacket.payload, payloadLen);
|
||||
RFM95.rxBufferValid = false;
|
||||
RFM95.dataReceived = false;
|
||||
}
|
||||
interrupts();
|
||||
#ifdef SREG
|
||||
SREG = _SREG; // restore interrupts
|
||||
#endif
|
||||
|
||||
interrupts(); // explicitly re-enable interrupts
|
||||
|
||||
// ACK handling
|
||||
if (RFM95_getACKRequested(controlFlags)) {
|
||||
if (RFM95_getACKRequested(controlFlags) && !RFM95_getACKReceived(controlFlags)) {
|
||||
RFM95_sendACK(sender, sequenceNumber, RSSI, SNR);
|
||||
}
|
||||
|
||||
return payloadLen;
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_send(rfm95_packet_t &packet)
|
||||
LOCAL bool RFM95_sendFrame(rfm95_packet_t &packet, const bool increaseSequenceCounter)
|
||||
{
|
||||
const uint8_t finalLen = packet.payloadLen + RFM95_HEADER_LEN;
|
||||
// Make sure we dont interrupt an outgoing message
|
||||
(void)RFM95_waitPacketSent();
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY); //==> not needed
|
||||
// Check channel activity
|
||||
if (!RFM95_waitCAD()) {
|
||||
return false;
|
||||
}
|
||||
packet.header.sequenceNumber = RFM95.txSequenceNumber++;
|
||||
if (increaseSequenceCounter) {
|
||||
// increase sequence counter, overflow is ok
|
||||
RFM95.txSequenceNumber++;
|
||||
}
|
||||
packet.header.sequenceNumber = RFM95.txSequenceNumber;
|
||||
// Position at the beginning of the TX FIFO
|
||||
RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_TX_FIFO_ADDR);
|
||||
// write packet
|
||||
@@ -279,58 +313,66 @@ LOCAL bool RFM95_send(rfm95_packet_t &packet)
|
||||
// total payload length
|
||||
RFM95_writeReg(RFM95_REG_22_PAYLOAD_LENGTH, finalLen);
|
||||
// send message, if sent, irq fires and radio returns to standby
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_TX);
|
||||
return true;
|
||||
return RFM95_setRadioMode(RFM95_RADIO_MODE_TX);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t len,
|
||||
const rfm95_flag_t flags)
|
||||
LOCAL bool RFM95_send(const uint8_t recipient, uint8_t* data, const uint8_t len,
|
||||
const rfm95_controlFlags_t flags, const bool increaseSequenceCounter)
|
||||
{
|
||||
rfm95_packet_t packet;
|
||||
packet.header.version = RFM95_PACKET_HEADER_VERSION;
|
||||
packet.header.sender = RFM95.address;
|
||||
packet.header.recipient = recipient;
|
||||
packet.header.controlFlags = 0x00;
|
||||
packet.payloadLen = min(len, (uint8_t)RFM95_MAX_PAYLOAD_LEN);
|
||||
packet.header.controlFlags = flags;
|
||||
(void)memcpy(&packet.payload, data, packet.payloadLen);
|
||||
return RFM95_send(packet);
|
||||
return RFM95_sendFrame(packet, increaseSequenceCounter);
|
||||
}
|
||||
|
||||
LOCAL void RFM95_setFrequency(const float centre)
|
||||
LOCAL void RFM95_setFrequency(const uint32_t frequencyHz)
|
||||
{
|
||||
const uint32_t freq = (centre * 1000000.0) / RFM95_FSTEP;
|
||||
RFM95_writeReg(RFM95_REG_06_FRF_MSB, (freq >> 16) & 0xff);
|
||||
RFM95_writeReg(RFM95_REG_07_FRF_MID, (freq >> 8) & 0xff);
|
||||
RFM95_writeReg(RFM95_REG_08_FRF_LSB, freq & 0xff);
|
||||
const uint32_t freqReg = (uint32_t)(frequencyHz / RFM95_FSTEP);
|
||||
RFM95_writeReg(RFM95_REG_06_FRF_MSB, (freqReg >> 16) & 0xff);
|
||||
RFM95_writeReg(RFM95_REG_07_FRF_MID, (freqReg >> 8) & 0xff);
|
||||
RFM95_writeReg(RFM95_REG_08_FRF_LSB, freqReg & 0xff);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_setTxPower(uint8_t powerLevel)
|
||||
LOCAL bool RFM95_setTxPowerLevel(rfm95_powerLevel_t newPowerLevel)
|
||||
{
|
||||
// RFM95/96/97/98 does not have RFO pins connected to anything. Only PA_BOOST
|
||||
powerLevel = max((uint8_t)RFM95_MIN_POWER_LEVEL_DBM, powerLevel);
|
||||
powerLevel = min((uint8_t)RFM95_MAX_POWER_LEVEL_DBM, powerLevel);
|
||||
if (powerLevel != RFM95.powerLevel) {
|
||||
RFM95.powerLevel = powerLevel;
|
||||
newPowerLevel = max((uint8_t)RFM95_MIN_POWER_LEVEL_DBM, newPowerLevel);
|
||||
newPowerLevel = min((uint8_t)RFM95_MAX_POWER_LEVEL_DBM, newPowerLevel);
|
||||
if (newPowerLevel != RFM95.powerLevel) {
|
||||
RFM95.powerLevel = newPowerLevel;
|
||||
uint8_t val;
|
||||
if (powerLevel > 20) {
|
||||
if (newPowerLevel > 20) {
|
||||
// enable DAC, adds 3dBm
|
||||
// The documentation is pretty confusing on this topic: PaSelect says the max power is 20dBm,
|
||||
// but OutputPower claims it would be 17dBm. Measurements show 20dBm is correct
|
||||
RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_ENABLE);
|
||||
val = powerLevel - 8;
|
||||
val = newPowerLevel - 8;
|
||||
} else {
|
||||
RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_DISABLE);
|
||||
val = powerLevel - 5;
|
||||
val = newPowerLevel - 5;
|
||||
}
|
||||
RFM95_writeReg(RFM95_REG_09_PA_CONFIG, RFM95_PA_SELECT | val);
|
||||
RFM95_DEBUG(PSTR("RFM95:PTX:LEVEL=%d\n"), powerLevel);
|
||||
RFM95_DEBUG(PSTR("RFM95:PTX:LEVEL=%d\n"), newPowerLevel);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
#if defined(MY_RFM95_TCXO)
|
||||
LOCAL void RFM95_enableTCXO(void)
|
||||
{
|
||||
while ((RFM95_readReg(RFM95_REG_4B_TCXO) & RFM95_TCXO_TCXO_INPUT_ON) != RFM95_TCXO_TCXO_INPUT_ON) {
|
||||
RFM95_sleep();
|
||||
RFM95_writeReg(RFM95_REG_4B_TCXO, (RFM95_readReg(RFM95_REG_4B_TCXO) | RFM95_TCXO_TCXO_INPUT_ON));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Sets registers from a canned modem configuration structure
|
||||
LOCAL void RFM95_setModemRegisters(const rfm95_modemConfig_t* config)
|
||||
{
|
||||
@@ -370,6 +412,8 @@ LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode)
|
||||
regMode = RFM95_MODE_CAD;
|
||||
RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x80); // Interrupt on CadDone, DIO0
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_RX) {
|
||||
RFM95.dataReceived = false;
|
||||
RFM95.ackReceived = false;
|
||||
regMode = RFM95_MODE_RXCONTINUOUS;
|
||||
RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x00); // Interrupt on RxDone, DIO0
|
||||
} else if (newRadioMode == RFM95_RADIO_MODE_TX) {
|
||||
@@ -384,54 +428,80 @@ LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode)
|
||||
return true;
|
||||
}
|
||||
|
||||
LOCAL void RFM95_powerUp(void)
|
||||
{
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
hwDigitalWrite(MY_RFM95_POWER_PIN, HIGH);
|
||||
delay(RFM95_POWERUP_DELAY_MS);
|
||||
#endif
|
||||
}
|
||||
LOCAL void RFM95_powerDown(void)
|
||||
{
|
||||
#if defined(MY_RFM95_POWER_PIN)
|
||||
hwDigitalWrite(RFM69_POWER_PIN, LOW);
|
||||
#endif
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sleep(void)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:RSL\n")); // put radio to sleep
|
||||
return RFM95_setRadioMode(RFM95_RADIO_MODE_SLEEP);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_standBy(void)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:RSB\n")); // put radio to standby
|
||||
return RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
|
||||
}
|
||||
|
||||
|
||||
// should be called immediately after reception in case sender wants ACK
|
||||
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
|
||||
const rfm95_RSSI_t RSSI, const rfm95_RSSI_t SNR)
|
||||
const rfm95_RSSI_t RSSI, const rfm95_SNR_t SNR)
|
||||
{
|
||||
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK to=%d,RSSI=%d\n"),recipient,RFM95_internalToRSSI(RSSI));
|
||||
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK,TO=%d,SEQ=%d,RSSI=%d,SNR=%d\n"),recipient,sequenceNumber,
|
||||
RFM95_internalToRSSI(RSSI),RFM95_internalToSNR(SNR));
|
||||
rfm95_ack_t ACK;
|
||||
ACK.sequenceNumber = sequenceNumber;
|
||||
ACK.RSSI = RSSI;
|
||||
ACK.SNR = SNR;
|
||||
rfm95_flag_t flags = 0x00;
|
||||
rfm95_controlFlags_t flags = 0x00;
|
||||
RFM95_setACKReceived(flags, true);
|
||||
RFM95_setACKRSSIReport(flags, true);
|
||||
(void)RFM95_sendFrame(recipient, (uint8_t*)&ACK, sizeof(rfm95_ack_t), flags);
|
||||
(void)RFM95_send(recipient, (uint8_t*)&ACK, sizeof(rfm95_ack_t), flags);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI)
|
||||
{
|
||||
// RFM95 internal format RSSI has an offset
|
||||
if (currentRSSI < (targetRSSI * (1 + RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
|
||||
rfm95_powerLevel_t newPowerLevel = RFM95.powerLevel;
|
||||
if (RFM95_internalToRSSI(currentRSSI) < RFM95_internalToRSSI(targetRSSI *
|
||||
(1 + RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
|
||||
RFM95.powerLevel < RFM95_MAX_POWER_LEVEL_DBM) {
|
||||
// increase transmitter power
|
||||
RFM95.powerLevel++;
|
||||
} else if (currentRSSI > (targetRSSI * (1 - RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
|
||||
newPowerLevel++;
|
||||
} else if (RFM95_internalToRSSI(currentRSSI) > RFM95_internalToRSSI(targetRSSI *
|
||||
(1 - RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
|
||||
RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
|
||||
// decrease transmitter power
|
||||
RFM95.powerLevel--;
|
||||
newPowerLevel--;
|
||||
} else {
|
||||
// nothing to adjust
|
||||
return false;
|
||||
}
|
||||
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%d,tR=%d,TXL=%d\n"), RFM95_internalToRSSI(currentRSSI),
|
||||
RFM95_internalToRSSI(targetRSSI), RFM95.powerLevel);
|
||||
return RFM95_setTxPower(RFM95.powerLevel);;
|
||||
return RFM95_setTxPowerLevel(newPowerLevel);
|
||||
}
|
||||
|
||||
LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
|
||||
const uint8_t bufferSize, const uint8_t retries, const uint32_t retryWaitTime)
|
||||
{
|
||||
for (uint8_t retry = 0; retry < retries; retry++) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:SEND TO=%d,RETRY=%d\n"), recipient, retry);
|
||||
rfm95_flag_t flags = 0x00;
|
||||
for (uint8_t retry = 0; retry <= retries; retry++) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:SEND,TO=%d,SEQ=%d,RETRY=%d\n"), recipient, RFM95.txSequenceNumber,
|
||||
retry);
|
||||
rfm95_controlFlags_t flags = 0x00;
|
||||
RFM95_setACKRequested(flags, (recipient != RFM95_BROADCAST_ADDRESS));
|
||||
(void)RFM95_sendFrame(recipient, (uint8_t*)buffer, bufferSize, flags);
|
||||
(void)RFM95_send(recipient, (uint8_t*)buffer, bufferSize, flags, !retry);
|
||||
(void)RFM95_waitPacketSent();
|
||||
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
if (recipient == RFM95_BROADCAST_ADDRESS) {
|
||||
@@ -439,20 +509,23 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
|
||||
}
|
||||
const uint32_t enterMS = hwMillis();
|
||||
while (hwMillis() - enterMS < retryWaitTime) {
|
||||
if (RFM95.rxBufferValid) {
|
||||
if (RFM95.ackReceived) {
|
||||
const uint8_t sender = RFM95.currentPacket.header.sender;
|
||||
const rfm95_sequenceNumber_t ACKsequenceNumber = RFM95.currentPacket.ACK.sequenceNumber;
|
||||
const rfm95_flag_t flag = RFM95.currentPacket.header.controlFlags;
|
||||
if (sender == recipient && RFM95_getACKReceived(flag) &&
|
||||
(ACKsequenceNumber == RFM95.txSequenceNumber - 1)) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%d,SEQ=%d,RSSI=%d,SNR=%d\n"),sender,ACKsequenceNumber,
|
||||
RFM95_internalToRSSI(RFM95.currentPacket.ACK.RSSI),
|
||||
RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR));
|
||||
RFM95_clearRxBuffer();
|
||||
|
||||
const rfm95_controlFlags_t flag = RFM95.currentPacket.header.controlFlags;
|
||||
const rfm95_RSSI_t RSSI = RFM95.currentPacket.ACK.RSSI;
|
||||
//const rfm95_SNR_t SNR = RFM95.currentPacket.ACK.SNR;
|
||||
RFM95.ackReceived = false;
|
||||
// packet read, back to RX
|
||||
RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
|
||||
if (sender == recipient &&
|
||||
(ACKsequenceNumber == RFM95.txSequenceNumber)) {
|
||||
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%d,SEQ=%d,RSSI=%d\n"),sender,ACKsequenceNumber,
|
||||
RFM95_internalToRSSI(RSSI));
|
||||
//RFM95_clearRxBuffer();
|
||||
// ATC
|
||||
if (RFM95.ATCenabled && RFM95_getACKRSSIReport(flag)) {
|
||||
(void)RFM95_executeATC(RFM95.currentPacket.ACK.RSSI, RFM95.ATCtargetRSSI);
|
||||
(void)RFM95_executeATC(RSSI, RFM95.ATCtargetRSSI);
|
||||
} // ATC
|
||||
return true;
|
||||
} // seq check
|
||||
@@ -460,11 +533,16 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
|
||||
doYield();
|
||||
}
|
||||
RFM95_DEBUG(PSTR("!RFM95:SWR:NACK\n"));
|
||||
if (RFM95.ATCenabled) {
|
||||
// No ACK received, maybe out of reach: increase power level
|
||||
(void)RFM95_setTxPower(RFM95.powerLevel++);
|
||||
const uint32_t enterCSMAMS = hwMillis();
|
||||
const uint16_t randDelayCSMA = enterMS & 100;
|
||||
while (hwMillis() - enterCSMAMS < randDelayCSMA) {
|
||||
doYield();
|
||||
}
|
||||
}
|
||||
if (RFM95.ATCenabled) {
|
||||
// No ACK received, maybe out of reach: increase power level
|
||||
(void)RFM95_setTxPowerLevel(RFM95.powerLevel + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -511,39 +589,53 @@ LOCAL int16_t RFM95_getSendingRSSI(void)
|
||||
return RFM95_internalToRSSI(RFM95.currentPacket.ACK.RSSI);
|
||||
} else {
|
||||
// not possible
|
||||
return RFM95_RSSI_INVALID;
|
||||
return INVALID_RSSI;
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL int8_t RFM95_getSendingSNR(void)
|
||||
LOCAL int16_t RFM95_getSendingSNR(void)
|
||||
{
|
||||
// own SNR, as measured by the recipient - ACK part
|
||||
if (RFM95_getACKRSSIReport(RFM95.currentPacket.header.controlFlags)) {
|
||||
return RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR);
|
||||
return static_cast<int16_t>(RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR));
|
||||
} else {
|
||||
// not possible
|
||||
return RFM95_SNR_INVALID;
|
||||
return INVALID_SNR;
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL int16_t RFM95_getReceivingRSSI(void)
|
||||
{
|
||||
// RSSI from last received packet
|
||||
return RFM95_internalToRSSI(RFM95.currentPacket.RSSI);
|
||||
return static_cast<int16_t>(RFM95_internalToRSSI(RFM95.currentPacket.RSSI));
|
||||
}
|
||||
|
||||
LOCAL int8_t RFM95_getReceivingSNR(void)
|
||||
LOCAL int16_t RFM95_getReceivingSNR(void)
|
||||
{
|
||||
// SNR from last received packet
|
||||
return RFM95_internalToSNR(RFM95.currentPacket.SNR);
|
||||
return static_cast<int16_t>(RFM95_internalToSNR(RFM95.currentPacket.SNR));
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_getTxPowerLevel(void)
|
||||
{
|
||||
return RFM95.powerLevel;
|
||||
}
|
||||
|
||||
LOCAL uint8_t RFM95_getTxPowerPercent(void)
|
||||
{
|
||||
// report TX level in %
|
||||
const uint8_t result = (uint8_t)(100.0f * (RFM95.powerLevel - RFM95_MIN_POWER_LEVEL_DBM) /
|
||||
(RFM95_MAX_POWER_LEVEL_DBM
|
||||
- RFM95_MIN_POWER_LEVEL_DBM));
|
||||
const uint8_t result = static_cast<uint8_t>(100.0f * (RFM95.powerLevel -
|
||||
RFM95_MIN_POWER_LEVEL_DBM) /
|
||||
(RFM95_MAX_POWER_LEVEL_DBM
|
||||
- RFM95_MIN_POWER_LEVEL_DBM));
|
||||
return result;
|
||||
}
|
||||
LOCAL bool RFM95_setTxPowerPercent(const uint8_t newPowerPercent)
|
||||
{
|
||||
const rfm95_powerLevel_t newPowerLevel = static_cast<rfm95_powerLevel_t>
|
||||
(RFM95_MIN_POWER_LEVEL_DBM + (RFM95_MAX_POWER_LEVEL_DBM
|
||||
- RFM95_MIN_POWER_LEVEL_DBM) * (newPowerPercent / 100.0f));
|
||||
RFM95_DEBUG(PSTR("RFM95:SPP:PCT=%d,TX LEVEL=%d\n"), newPowerPercent,newPowerLevel);
|
||||
return RFM95_setTxPowerLevel(newPowerLevel);
|
||||
}
|
||||
|
||||
|
||||
+149
-76
@@ -6,7 +6,7 @@
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2016 Sensnology AB
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
@@ -18,7 +18,7 @@
|
||||
*
|
||||
* Based on Mike McCauley's RFM95 library, Copyright (C) 2014 Mike McCauley <mikem@airspayce.com>
|
||||
* Radiohead http://www.airspayce.com/mikem/arduino/RadioHead/index.html
|
||||
* RFM95 driver refactored and optimized for MySensors, Copyright (C) 2016 Olivier Mauti <olivier@mysensors.org>
|
||||
* RFM95 driver refactored and optimized for MySensors, Copyright (C) 2017 Olivier Mauti <olivier@mysensors.org>
|
||||
*
|
||||
* Changelog:
|
||||
* - ACK with sequenceNumber
|
||||
@@ -41,16 +41,18 @@
|
||||
* - [!] Exclamation mark is prepended in case of error
|
||||
*
|
||||
* |E| SYS | SUB | Message | Comment
|
||||
* |-|-------|-------|-----------------------------------|---------------------------------------------------------------------
|
||||
* |-|-------|-------|-----------------------------------|-----------------------------------------------------------------------------------
|
||||
* | | RFM95 | INIT | | Initialise RFM95 radio
|
||||
* | | RFM95 | INIT | PIN,CS=%d,IQP=%d,IQN=%d[,RST=%d] | Pin configuration: chip select (CS), IRQ pin (IQP), IRQ number (IQN), Reset (RST)
|
||||
* |!| RFM95 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module
|
||||
* | | RFM95 | RCV | SEND ACK | ACK request received, sending ACK back
|
||||
* | | RFM95 | PTC | LEVEL=%d | Set TX power level
|
||||
* | | RFM95 | SAC | SEND ACK TO=%d,RSSI=%d | Send ACK to node (TO), RSSI of received message (RSSI)
|
||||
* | | RFM95 | SAC | SEND ACK,TO=%d,RSSI=%d,SNR=%d | Send ACK to node (TO), RSSI of received message (RSSI), SNR of message (SNR)
|
||||
* | | RFM95 | ATC | ADJ TXL,cR=%d,tR=%d,TXL=%d | Adjust TX level, current RSSI (cR), target RSSI (tR), TX level (TXL)
|
||||
* | | RFM95 | SWR | SEND TO=%d,RETRY=%d | Send message to (TO), NACK retry counter (RETRY)
|
||||
* | | RFM95 | SWR | SEND,TO=%d,RETRY=%d | Send message to (TO), NACK retry counter (RETRY)
|
||||
* | | RFM95 | SWR | ACK FROM=%d,SEQ=%d,RSSI=%d,SNR=%d | ACK received from node (FROM), seq ID (SEQ), (RSSI), (SNR)
|
||||
* |!| RFM95 | SWR | NACK | No ACK received
|
||||
* | | RFM95 | SPP | PCT=%d,TX LEVEL=%d | Set TX level, input TX percent (PCT)
|
||||
*
|
||||
* RFM95 modem configuration
|
||||
*
|
||||
@@ -58,12 +60,14 @@
|
||||
* CR = Error correction code
|
||||
* SF = Spreading factor, chips / symbol
|
||||
*
|
||||
* | CONFIG | REG_1D | REG_1E | REG_26 | BW | CR | SF | Comment
|
||||
* |------------------|--------|--------|--------|-------|-----|------|-----------------------------
|
||||
* | BW125CR45SF128 | 0x72 | 0x74 | 0x04 | 125 | 4/5 | 128 | Default, medium range
|
||||
* | BW500CR45SF128 | 0x92 | 0x74 | 0x04 | 500 | 4/5 | 128 | Fast, short range
|
||||
* | BW31_25CR48SF512 | 0x48 | 0x94 | 0x04 | 31.25 | 4/8 | 512 | Slow, long range
|
||||
* | BW125CR48SF4096 | 0x78 | 0xC4 | 0x0C | 125 | 4/8 | 4096 | Slow, long range
|
||||
* | CONFIG | REG_1D | REG_1E | REG_26 | BW | CR | SF | Comment | air-time (~15 bytes)
|
||||
* |------------------|--------|--------|--------|-------|-----|------|------------------------------------------------
|
||||
* | BW125CR45SF128 | 0x72 | 0x74 | 0x04 | 125 | 4/5 | 128 | Default, medium range | 50ms
|
||||
* | BW500CR45SF128 | 0x92 | 0x74 | 0x04 | 500 | 4/5 | 128 | Fast, short range | 15ms
|
||||
* | BW31_25CR48SF512 | 0x48 | 0x94 | 0x04 | 31.25 | 4/8 | 512 | Slow, long range | 900ms
|
||||
* | BW125CR48SF4096 | 0x78 | 0xC4 | 0x0C | 125 | 4/8 | 4096 | Slow, long range | 1500ms
|
||||
*
|
||||
* See here for air-time calculation: https://docs.google.com/spreadsheets/d/1voGAtQAjC1qBmaVuP1ApNKs1ekgUjavHuVQIXyYSvNc/edit#gid=0
|
||||
*
|
||||
* @brief API declaration for RFM95
|
||||
*
|
||||
@@ -75,32 +79,38 @@
|
||||
#include "RFM95registers.h"
|
||||
|
||||
// default PIN assignments, can be overridden
|
||||
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega88) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
|
||||
#define RFM95_IRQ_PIN (2) //!< RFM95_IRQ_PIN
|
||||
#define RFM95_RST_PIN (9) //!< RFM95_RST_PIN
|
||||
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
|
||||
#define RFM95_IRQ_PIN (2)
|
||||
#define RFM95_RST_PIN (9) //!< RFM95_RST_PIN
|
||||
#elif defined(__AVR_ATmega32U4__)
|
||||
#define RFM95_IRQ_PIN (3) //!< RFM95_IRQ_PIN
|
||||
#define RFM95_RST_PIN (9) //!< RFM95_RST_PIN
|
||||
#elif defined(__arm__)
|
||||
#define RFM95_IRQ_PIN (10) //!< RFM95_IRQ_PIN
|
||||
#define RFM95_RST_PIN (27) //!< RFM95_RST_PIN
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#if defined(__AVR_ATmega32U4__)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (3) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#else
|
||||
#define RFM95_IRQ_PIN (2) //!< RFM95_IRQ_PIN
|
||||
#define RFM95_RST_PIN (9) //!< RFM95_RST_PIN
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#endif
|
||||
#define RFM95_SPI_CS (SS) //!< RFM95_SPI_CS
|
||||
#define DEFAULT_RFM95_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM95_IRQ_PIN) //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM95_IRQ_PIN) //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#warning not implemented yet
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM95_IRQ_PIN) //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#elif defined(LINUX_ARCH_RASPBERRYPI)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (22) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM DEFAULT_RFM95_IRQ_PIN //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#elif defined(ARDUINO_ARCH_STM32F1)
|
||||
#define DEFAULT_RFM95_IRQ_PIN (PA3) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM DEFAULT_RFM95_IRQ_PIN //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#else
|
||||
#define DEFAULT_RFM95_IRQ_PIN (2) //!< DEFAULT_RFM95_IRQ_PIN
|
||||
#define DEFAULT_RFM95_IRQ_NUM DEFAULT_RFM95_IRQ_PIN //!< DEFAULT_RFM95_IRQ_NUM
|
||||
#endif
|
||||
|
||||
#define DEFAULT_RFM95_CS_PIN (SS) //!< DEFAULT_RFM95_CS_PIN
|
||||
|
||||
// SPI settings
|
||||
#if !defined(MY_RFM95_SPI_MAX_SPEED)
|
||||
#define MY_RFM95_SPI_MAX_SPEED (4*1000000ul) //!< Max SPI speed, 4Mhz
|
||||
#endif
|
||||
#define MY_RFM95_SPI_DATA_ORDER MSBFIRST //!< SPI data order
|
||||
#define MY_RFM95_SPI_DATA_MODE SPI_MODE0 //!< SPI mode
|
||||
|
||||
|
||||
#if defined (ARDUINO) && !defined (__arm__) && !defined (_SPI)
|
||||
#include <SPI.h>
|
||||
#if defined(MY_SOFTSPI)
|
||||
@@ -121,21 +131,36 @@ extern HardwareSPI SPI; //!< SPI
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// debug
|
||||
#if defined(MY_DEBUG_VERBOSE_RFM95)
|
||||
#define RFM95_DEBUG(x,...) debug(x, ##__VA_ARGS__) //!< Debug print
|
||||
#else
|
||||
#define RFM95_DEBUG(x,...) //!< Debug null
|
||||
// RFM95 radio configurations: reg_1d, reg_1e, reg_26 (see datasheet)
|
||||
#define RFM95_BW125CR45SF128 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x72,0x74,0x04
|
||||
#define RFM95_BW500CR45SF128 RFM95_BW_500KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x92,0x74,0x04
|
||||
#define RFM95_BW31_25CR48SF512 RFM95_BW_31_25KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_512CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x48,0x94,0x04
|
||||
#define RFM95_BW125CR48SF4096 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_4096CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON | RFM95_LOW_DATA_RATE_OPTIMIZE //!< 0x78,0xc4,0x0C
|
||||
|
||||
#if !defined(RFM95_RETRY_TIMEOUT_MS)
|
||||
// air-time approximation for timeout, 1 hop ~15 bytes payload - adjust if needed
|
||||
// BW125/SF128: 50ms
|
||||
// BW500/SF128: 15ms
|
||||
// BW31.25/SF512: 900ms
|
||||
// BW125/SF4096: 1500ms
|
||||
#define RFM95_RETRY_TIMEOUT_MS (500ul) //!< Timeout for ACK, adjustments needed if modem configuration changed (air time different)
|
||||
#endif
|
||||
|
||||
// Frequency definitions
|
||||
#define RFM95_169MHZ (169000000ul) //!< 169 Mhz
|
||||
#define RFM95_315MHZ (315000000ul) //!< 315 Mhz
|
||||
#define RFM95_434MHZ (433920000ul) //!< 433.92 Mhz
|
||||
#define RFM95_868MHZ (868100000ul) //!< 868.1 Mhz
|
||||
#define RFM95_915MHZ (915000000ul) //!< 915 Mhz
|
||||
|
||||
#define RFM95_RETRIES (5u) //!< Retries in case of failed transmission
|
||||
#define RFM95_FIFO_SIZE (0xFFu) //!< Max number of bytes the LORA Rx/Tx FIFO can hold
|
||||
#define RFM95_RX_FIFO_ADDR (0x00u) //!< RX FIFO addr pointer
|
||||
#define RFM95_TX_FIFO_ADDR (0x80u) //!< TX FIFO addr pointer
|
||||
#define RFM95_MAX_PACKET_LEN (0x40u) //!< This is the maximum number of bytes that can be carried by the LORA
|
||||
#define RFM95_PREAMBLE_LENGTH (8u) //!< Preamble length, default=8
|
||||
#define RFM95_RETRIES (2u) //!< Retries in case of failed transmission
|
||||
#define RFM95_RETRY_TIMEOUT_MS (500ul) //!< Timeout for ACK, adjustments needed if modem configuration changed (air time different)
|
||||
#define RFM95_CAD_TIMEOUT_MS (2*1000ul) //!< channel activity detection timeout
|
||||
#define RFM95_POWERUP_DELAY_MS (100u) //!< Power up delay, allow VCC to settle, transport to become fully operational
|
||||
|
||||
#define RFM95_PACKET_HEADER_VERSION (1u) //!< RFM95 packet header version
|
||||
#define RFM95_MIN_PACKET_HEADER_VERSION (1u) //!< Minimal RFM95 packet header version
|
||||
@@ -147,12 +172,10 @@ extern HardwareSPI SPI; //!< SPI
|
||||
#define RFM95_ATC_TARGET_RANGE_PERCENT (5u) //!< ATC target range +/-%
|
||||
#define RFM95_RSSI_OFFSET (137u) //!< RSSI offset
|
||||
#define RFM95_TARGET_RSSI (-60) //!< RSSI target
|
||||
#define RFM95_RSSI_INVALID (127) //!< RSSI invalid
|
||||
#define RFM95_SNR_INVALID (127) //!< SNR invalid
|
||||
#define RFM95_PROMISCUOUS (false) //!< RFM95 promiscuous mode
|
||||
|
||||
#define RFM95_FXOSC (32*1000000ul) //!< The crystal oscillator frequency of the module
|
||||
#define RFM95_FSTEP (RFM95_FXOSC / 524288ul) //!< The Frequency Synthesizer step = RFM95_FXOSC /
|
||||
#define RFM95_FSTEP (RFM95_FXOSC / 524288.0f) //!< The Frequency Synthesizer step = RFM95_FXOSC /
|
||||
|
||||
// helper macros
|
||||
#define RFM95_getACKRequested(__value) ((bool)bitRead(__value, RFM95_BIT_ACK_REQUESTED)) //!< getACKRequested
|
||||
@@ -165,16 +188,10 @@ extern HardwareSPI SPI; //!< SPI
|
||||
#define RFM95_RSSItoInternal(__value) ((uint8_t)(__value + RFM95_RSSI_OFFSET)) //!< Convert RSSI to internal RSSI
|
||||
#define RFM95_internalToSNR(__value) ((int8_t)(__value / 4)) //!< Convert internal SNR to SNR
|
||||
|
||||
|
||||
// RFM95 radio configurations: reg_1d, reg_1e, reg_26 (see datasheet)
|
||||
#define RFM95_BW125CR45SF128 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x72,0x74,0x04
|
||||
#define RFM95_BW500CR45SF128 RFM95_BW_500KHZ | RFM95_CODING_RATE_4_5, RFM95_SPREADING_FACTOR_128CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x92,0x74,0x04
|
||||
#define RFM95_BW31_25CR48SF512 RFM95_BW_31_25KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_512CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON //!< 0x48,0x94,0x04
|
||||
#define RFM95_BW125CR48SF4096 RFM95_BW_125KHZ | RFM95_CODING_RATE_4_8, RFM95_SPREADING_FACTOR_4096CPS | RFM95_RX_PAYLOAD_CRC_ON, RFM95_AGC_AUTO_ON | RFM95_LOW_DATA_RATE_OPTIMIZE //!< 0x78,0xc4,0x0C
|
||||
|
||||
#define RFM95_MIN_POWER_LEVEL_DBM (5u) //!< min. power level
|
||||
#define RFM95_MAX_POWER_LEVEL_DBM (23u) //!< max. power level
|
||||
|
||||
#define RFM95_MIN_POWER_LEVEL_DBM ((rfm95_powerLevel_t)5u) //!< min. power level
|
||||
#if !defined(RFM95_MAX_POWER_LEVEL_DBM)
|
||||
#define RFM95_MAX_POWER_LEVEL_DBM ((rfm95_powerLevel_t)23u) //!< max. power level
|
||||
#endif
|
||||
/**
|
||||
* @brief Radio modes
|
||||
*/
|
||||
@@ -210,7 +227,11 @@ typedef int8_t rfm95_SNR_t;
|
||||
/**
|
||||
* @brief Control flag data type
|
||||
*/
|
||||
typedef uint8_t rfm95_flag_t;
|
||||
typedef uint8_t rfm95_controlFlags_t;
|
||||
/**
|
||||
* @brief Power level in dBm
|
||||
*/
|
||||
typedef int8_t rfm95_powerLevel_t;
|
||||
/**
|
||||
* @brief RFM95 LoRa header
|
||||
*/
|
||||
@@ -218,7 +239,7 @@ typedef struct {
|
||||
uint8_t version; //!< Header version
|
||||
uint8_t recipient; //!< Payload recipient
|
||||
uint8_t sender; //!< Payload sender
|
||||
rfm95_flag_t controlFlags; //!< Control flags, used for ACK
|
||||
rfm95_controlFlags_t controlFlags; //!< Control flags, used for ACK
|
||||
rfm95_sequenceNumber_t sequenceNumber; //!< Packet sequence number, used for ACK
|
||||
} __attribute__((packed)) rfm95_header_t;
|
||||
|
||||
@@ -262,14 +283,15 @@ typedef struct {
|
||||
uint8_t address; //!< Node address
|
||||
rfm95_packet_t currentPacket; //!< Buffer for current packet
|
||||
rfm95_sequenceNumber_t txSequenceNumber; //!< RFM95_txSequenceNumber
|
||||
uint8_t powerLevel; //!< TX power level dBm
|
||||
uint8_t ATCtargetRSSI; //!< ATC: target RSSI
|
||||
rfm95_powerLevel_t powerLevel; //!< TX power level dBm
|
||||
rfm95_RSSI_t ATCtargetRSSI; //!< ATC: target RSSI
|
||||
// 8 bit
|
||||
rfm95_radioMode_t radioMode : 3; //!< current transceiver state
|
||||
bool cad : 1; //!< RFM95_cad
|
||||
bool rxBufferValid : 1; //!< RX buffer valid
|
||||
bool ATCenabled : 1; //!< ATC enabled
|
||||
uint8_t reserved : 2; //!< reserved
|
||||
bool ackReceived : 1; //!< ACK received
|
||||
bool dataReceived : 1; //!< AData received
|
||||
uint8_t reserved : 1; //!< reserved
|
||||
} rfm95_internal_t;
|
||||
|
||||
#define LOCAL static //!< static
|
||||
@@ -304,25 +326,28 @@ LOCAL bool RFM95_available(void);
|
||||
/**
|
||||
* @brief If a valid message is received, copy it to buf and return length. 0 byte messages are permitted.
|
||||
* @param buf Location to copy the received message
|
||||
* @param maxBufSize Max buffer size
|
||||
* @return Number of bytes
|
||||
*/
|
||||
LOCAL uint8_t RFM95_recv(uint8_t* buf);
|
||||
LOCAL uint8_t RFM95_recv(uint8_t* buf, const uint8_t maxBufSize);
|
||||
/**
|
||||
* @brief RFM95_send
|
||||
* @param packet
|
||||
* @return True if packet sent
|
||||
*/
|
||||
LOCAL bool RFM95_send(rfm95_packet_t &packet);
|
||||
/**
|
||||
* @brief RFM95_sendFrame
|
||||
* @param recipient
|
||||
* @param data
|
||||
* @param len
|
||||
* @param flags
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if packet sent
|
||||
*/
|
||||
LOCAL bool RFM95_send(const uint8_t recipient, uint8_t* data, const uint8_t len,
|
||||
const rfm95_controlFlags_t flags, const bool increaseSequenceCounter = true);
|
||||
/**
|
||||
* @brief RFM95_sendFrame
|
||||
* @param packet
|
||||
* @param increaseSequenceCounter
|
||||
* @return True if frame sent
|
||||
*/
|
||||
LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t len,
|
||||
const rfm95_flag_t flags);
|
||||
LOCAL bool RFM95_sendFrame(rfm95_packet_t &packet, const bool increaseSequenceCounter = true);
|
||||
/**
|
||||
* @brief RFM95_setPreambleLength
|
||||
* @param preambleLength
|
||||
@@ -330,29 +355,62 @@ LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t
|
||||
LOCAL void RFM95_setPreambleLength(const uint16_t preambleLength);
|
||||
/**
|
||||
* @brief Sets the transmitter and receiver centre frequency
|
||||
* @param centre Frequency in MHz (137.0 to 1020.0)
|
||||
* @param frequencyHz Frequency in Hz
|
||||
*/
|
||||
LOCAL void RFM95_setFrequency(const float centre);
|
||||
LOCAL void RFM95_setFrequency(const uint32_t frequencyHz);
|
||||
/**
|
||||
* @brief Sets the transmitter power output level, and configures the transmitter pin
|
||||
* @param powerLevel Transmitter power level in dBm (+5 to +23)
|
||||
* @param newPowerLevel Transmitter power level in dBm (+5 to +23)
|
||||
* @return True power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM95_setTxPower(uint8_t powerLevel);
|
||||
LOCAL bool RFM95_setTxPowerLevel(rfm95_powerLevel_t newPowerLevel);
|
||||
|
||||
/**
|
||||
* @brief Sets the transmitter power output percent.
|
||||
* @param newPowerPercent Transmitter power level in percent
|
||||
* @return True power level adjusted
|
||||
*/
|
||||
LOCAL bool RFM95_setTxPowerPercent(const uint8_t newPowerPercent);
|
||||
|
||||
#if defined(MY_RFM95_TCXO)
|
||||
/**
|
||||
* @brief Enable TCXO mode
|
||||
* Call this immediately after init(), to force your radio to use an external
|
||||
* frequency source, such as a Temperature Compensated Crystal Oscillator (TCXO).
|
||||
* See the comments in the main documentation about the sensitivity of this radio to
|
||||
* clock frequency especially when using narrow bandwidths.
|
||||
* Leaves the module in sleep mode.
|
||||
*/
|
||||
LOCAL void RFM95_enableTCXO(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Sets the radio into low-power sleep mode
|
||||
* @return true if sleep mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM95_sleep(void);
|
||||
/**
|
||||
* @brief Sets the radio into standby mode
|
||||
* @return true if standby mode was successfully entered
|
||||
*/
|
||||
LOCAL bool RFM95_standBy(void);
|
||||
/**
|
||||
* @brief Powerdown radio, if RFM95_POWER_PIN defined
|
||||
*/
|
||||
LOCAL void RFM95_powerDown(void);
|
||||
/**
|
||||
* @brief Powerup radio, if RFM95_POWER_PIN defined
|
||||
*/
|
||||
LOCAL void RFM95_powerUp(void);
|
||||
/**
|
||||
* @brief RFM95_sendACK
|
||||
* @param recipient
|
||||
* @param sequenceNumber
|
||||
* @param RSSI (rfm95_RSSI_t)
|
||||
* @param SNR (rfm95_RSSI_t)
|
||||
* @param SNR (rfm95_SNR_t)
|
||||
*/
|
||||
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
|
||||
const rfm95_RSSI_t RSSI, const rfm95_RSSI_t SNR);
|
||||
const rfm95_RSSI_t RSSI, const rfm95_SNR_t SNR);
|
||||
/**
|
||||
* @brief RFM95_sendWithRetry
|
||||
* @param recipient
|
||||
@@ -386,21 +444,36 @@ LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode);
|
||||
*/
|
||||
LOCAL void RFM95_interruptHandler(void);
|
||||
/**
|
||||
* @brief RFM95_clearRxBuffer
|
||||
*/
|
||||
LOCAL void RFM95_clearRxBuffer(void);
|
||||
/**
|
||||
* @brief RFM95_getSendingRSSI
|
||||
* @return RSSI Signal strength of last packet received
|
||||
*/
|
||||
LOCAL int16_t RFM95_getReceivingRSSI(void);
|
||||
|
||||
/**
|
||||
* @brief RFM95_getSendingRSSI
|
||||
* @return RSSI Signal strength of last packet sent (if ACK and ATC enabled)
|
||||
*/
|
||||
LOCAL int16_t RFM95_getSendingRSSI(void);
|
||||
/**
|
||||
* @brief RFM95_getReceivingSNR
|
||||
* @return SNR
|
||||
*/
|
||||
LOCAL int16_t RFM95_getReceivingSNR(void);
|
||||
/**
|
||||
* @brief RFM95_getSendingSNR
|
||||
* @return SNR of last packet sent (if ACK and ATC enabled)
|
||||
*/
|
||||
LOCAL int16_t RFM95_getSendingSNR(void);
|
||||
/**
|
||||
* @brief Get transmitter power level
|
||||
* @return Transmitter power level in percents
|
||||
*/
|
||||
LOCAL uint8_t RFM95_getTxPowerPercent(void);
|
||||
/**
|
||||
* @brief Get transmitter power level
|
||||
* @return Transmitter power level in dBm
|
||||
*/
|
||||
LOCAL uint8_t RFM95_getTxPowerLevel(void);
|
||||
/**
|
||||
* @brief RFM_executeATC
|
||||
* @param currentRSSI
|
||||
* @param targetRSSI
|
||||
|
||||
@@ -200,6 +200,9 @@
|
||||
#define RFM95_LOW_DATA_RATE_OPTIMIZE 0x08 //!< LOW_DATA_RATE_OPTIMIZE
|
||||
#define RFM95_AGC_AUTO_ON 0x04 //!< AGC_AUTO_ON
|
||||
|
||||
// RFM95_REG_4B_TCXO 0x4b
|
||||
#define RFM95_TCXO_TCXO_INPUT_ON 0x10 //!< TCXO_TCXO_INPUT_ON
|
||||
|
||||
// RFM95_REG_4D_PA_DAC 0x4d
|
||||
#define RFM95_PA_DAC_DISABLE 0x04 //!< PA_DAC_DISABLE
|
||||
#define RFM95_PA_DAC_ENABLE 0x07 //!< PA_DAC_ENABLE
|
||||
|
||||
Reference in New Issue
Block a user