Skip to content

Commit 01711be

Browse files
tekka007henrikekblad
authored andcommitted
Update RFM95 driver (#709)
1 parent dd9dff7 commit 01711be

File tree

4 files changed

+370
-290
lines changed

4 files changed

+370
-290
lines changed

core/MyTransportRFM95.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,26 @@ void transportPowerDown(void)
6767
(void)RFM95_sleep();
6868
}
6969

70-
int16_t transportGetSignalStrength(void)
70+
// experimental
71+
// **********************************************
72+
int16_t transportGetReceivingSignalStrength(void)
7173
{
72-
return RFM95_getRSSI();
74+
return RFM95_getReceivingRSSI();
7375
}
76+
int16_t transportGetSendingSignalStrength(void)
77+
{
78+
return RFM95_getSendingRSSI();
79+
}
80+
int8_t transportGetReceivingSNR(void)
81+
{
82+
return RFM95_getReceivingSNR();
83+
}
84+
int8_t transportGetSendingSNR(void)
85+
{
86+
return RFM95_getSendingSNR();
87+
}
88+
uint8_t transportGetTxPower(void)
89+
{
90+
return RFM95_getTxPowerPercent();
91+
}
92+
// **********************************************

drivers/RFM95/RFM95.cpp

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
#include "RFM95.h"
2626

2727
#if defined(LINUX_ARCH_RASPBERRYPI)
28-
uint8_t spi_rxbuff[32 + 1]; //SPI receive buffer (payload max 32 bytes, MYS protocol limitation)
29-
uint8_t spi_txbuff[32 +
30-
1]; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command, MYS protocol limitation)
28+
// SPI RX and TX buffers (max packet len + 1 byte for the command)
29+
uint8_t spi_rxbuff[RFM95_MAX_PACKET_LEN + 1];
30+
uint8_t spi_txbuff[RFM95_MAX_PACKET_LEN + 1];
3131
#endif
3232

33+
volatile rfm95_internal_t RFM95; //!< internal variables
34+
3335
LOCAL void RFM95_csn(const bool level)
3436
{
3537
hwDigitalWrite(MY_RFM95_SPI_CS, level);
@@ -110,10 +112,6 @@ LOCAL uint8_t RFM95_RAW_writeByteRegister(const uint8_t address, uint8_t value)
110112
#define RFM95_burstReadReg(__reg, __buf, __len) RFM95_spiMultiByteTransfer( __reg & RFM95_READ_REGISTER, (uint8_t*)__buf, __len, true )
111113
#define RFM95_burstWriteReg(__reg, __buf, __len) RFM95_spiMultiByteTransfer( __reg | RFM95_WRITE_REGISTER, (uint8_t*)__buf, __len, false )
112114

113-
114-
// check RegPktRssiValue (0x1A) vs. RegRssiValue (0x1B)
115-
116-
117115
LOCAL bool RFM95_initialise(const float frequency)
118116
{
119117
RFM95_DEBUG(PSTR("RFM95:INIT\n"));
@@ -134,7 +132,7 @@ LOCAL bool RFM95_initialise(const float frequency)
134132
RFM95.txSequenceNumber = 0; // initialise TX sequence counter
135133
RFM95.powerLevel = 0;
136134
RFM95.ATCenabled = false;
137-
RFM95.ATCtargetRSSI = RFM95_TARGET_RSSI + RFM95_RSSI_OFFSET;
135+
RFM95.ATCtargetRSSI = RFM95_RSSItoInternal(RFM95_TARGET_RSSI);
138136

139137
// SPI init
140138
hwDigitalWrite(MY_RFM95_SPI_CS, HIGH);
@@ -143,8 +141,7 @@ LOCAL bool RFM95_initialise(const float frequency)
143141

144142
// Set LoRa mode (during sleep mode)
145143
RFM95_writeReg(RFM95_REG_01_OP_MODE, RFM95_MODE_SLEEP | RFM95_LONG_RANGE_MODE);
146-
delay(10); // Wait for sleep mode to take over from say, CAD
147-
// Check we are in sleep mode, with LORA set
144+
delay(10); // Wait for sleep mode to take over
148145
if (RFM95_readReg(RFM95_REG_01_OP_MODE) != (RFM95_MODE_SLEEP | RFM95_LONG_RANGE_MODE)) {
149146
return false; // No device present?
150147
}
@@ -167,8 +164,14 @@ LOCAL bool RFM95_initialise(const float frequency)
167164
#if defined (SPI_HAS_TRANSACTION) && !defined (ESP8266)
168165
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN));
169166
#endif
170-
attachInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN), RFM95_interruptHandler, RISING);
171167

168+
if (!RFM95_sanityCheck()) {
169+
RFM95_DEBUG(
170+
PSTR("!RFM95:INIT:SANCHK FAIL\n")); // sanity check failed, check wiring or replace module
171+
return false;
172+
}
173+
174+
attachInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN), RFM95_interruptHandler, RISING);
172175
return true;
173176
}
174177

@@ -191,7 +194,8 @@ LOCAL void RFM95_interruptHandler(void)
191194
// Reset the fifo read ptr to the beginning of the packet
192195
RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_readReg(RFM95_REG_10_FIFO_RX_CURRENT_ADDR));
193196
RFM95_burstReadReg(RFM95_REG_00_FIFO, RFM95.currentPacket.data, bufLen);
194-
RFM95.currentPacket.RSSI = RFM95_readReg(RFM95_REG_1A_PKT_RSSI_VALUE);
197+
RFM95.currentPacket.RSSI = RFM95_readReg(
198+
RFM95_REG_1A_PKT_RSSI_VALUE); // RSSI of latest packet received
195199
RFM95.currentPacket.SNR = static_cast<rfm95_SNR_t>(RFM95_readReg(RFM95_REG_19_PKT_SNR_VALUE));
196200
RFM95.currentPacket.payloadLen = bufLen - RFM95_HEADER_LEN;
197201
// Message for us
@@ -244,14 +248,13 @@ LOCAL uint8_t RFM95_recv(uint8_t* buf)
244248
const rfm95_RSSI_t RSSI = RFM95.currentPacket.RSSI; // of incoming packet
245249
const rfm95_SNR_t SNR = RFM95.currentPacket.SNR;
246250
if (buf != NULL) {
247-
memcpy((void*)buf, (void*)RFM95.currentPacket.payload, payloadLen);
251+
(void)memcpy((void*)buf, (void*)RFM95.currentPacket.payload, payloadLen);
248252
RFM95.rxBufferValid = false;
249253
}
250254
interrupts();
251255

252256
// ACK handling
253257
if (RFM95_getACKRequested(controlFlags)) {
254-
RFM95_DEBUG(PSTR("RFM95:RCV:SEND ACK\n"));
255258
RFM95_sendACK(sender, sequenceNumber, RSSI, SNR);
256259
}
257260

@@ -262,9 +265,8 @@ LOCAL bool RFM95_send(rfm95_packet_t &packet)
262265
{
263266
const uint8_t finalLen = packet.payloadLen + RFM95_HEADER_LEN;
264267
// Make sure we dont interrupt an outgoing message
265-
RFM95_waitPacketSent();
268+
(void)RFM95_waitPacketSent();
266269
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
267-
268270
// Check channel activity
269271
if (!RFM95_waitCAD()) {
270272
return false;
@@ -289,9 +291,9 @@ LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t
289291
packet.header.sender = RFM95.address;
290292
packet.header.recipient = recipient;
291293
packet.header.controlFlags = 0x00;
292-
packet.payloadLen = min(len, (const uint8_t)RFM95_MAX_PAYLOAD_LEN);
294+
packet.payloadLen = min(len, (uint8_t)RFM95_MAX_PAYLOAD_LEN);
293295
packet.header.controlFlags = flags;
294-
memcpy(&packet.payload, data, packet.payloadLen);
296+
(void)memcpy(&packet.payload, data, packet.payloadLen);
295297
return RFM95_send(packet);
296298
}
297299

@@ -314,8 +316,7 @@ LOCAL bool RFM95_setTxPower(uint8_t powerLevel)
314316
if (powerLevel > 20) {
315317
// enable DAC, adds 3dBm
316318
// The documentation is pretty confusing on this topic: PaSelect says the max power is 20dBm,
317-
// but OutputPower claims it would be 17dBm.
318-
// My measurements show 20dBm is correct
319+
// but OutputPower claims it would be 17dBm. Measurements show 20dBm is correct
319320
RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_ENABLE);
320321
val = powerLevel - 8;
321322
} else {
@@ -354,17 +355,6 @@ LOCAL uint8_t RFM95_getAddress(void)
354355
return RFM95.address;
355356
}
356357

357-
358-
LOCAL bool RFM95_isChannelActive(void)
359-
{
360-
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_CAD);
361-
while (RFM95.radioMode == RFM95_RADIO_MODE_CAD) {
362-
doYield();
363-
}
364-
365-
return RFM95.cad;
366-
}
367-
368358
LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode)
369359
{
370360
if (RFM95.radioMode == newRadioMode) {
@@ -403,7 +393,7 @@ LOCAL bool RFM95_sleep(void)
403393
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
404394
const rfm95_RSSI_t RSSI, const rfm95_RSSI_t SNR)
405395
{
406-
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK to=%d,RSSI=%d\n"),recipient,RSSI);
396+
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK to=%d,RSSI=%d\n"),recipient,RFM95_internalToRSSI(RSSI));
407397
rfm95_ack_t ACK;
408398
ACK.sequenceNumber = sequenceNumber;
409399
ACK.RSSI = RSSI;
@@ -416,19 +406,21 @@ LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t s
416406

417407
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI)
418408
{
419-
// allow +-5%
420-
if (currentRSSI < (targetRSSI*1.05) && RFM95.powerLevel < RFM95_MAX_POWER_LEVEL_DBM) {
409+
// RFM95 internal format RSSI has an offset
410+
if (currentRSSI < (targetRSSI * (1 + RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
411+
RFM95.powerLevel < RFM95_MAX_POWER_LEVEL_DBM) {
421412
// increase transmitter power
422413
RFM95.powerLevel++;
423-
} else if (currentRSSI > (targetRSSI*0.95) && RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
414+
} else if (currentRSSI > (targetRSSI * (1 - RFM95_ATC_TARGET_RANGE_PERCENT / 100)) &&
415+
RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
424416
// decrease transmitter power
425417
RFM95.powerLevel--;
426418
} else {
427419
// nothing to adjust
428420
return false;
429421
}
430-
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%d,tR=%d,TXL=%d\n"), currentRSSI - RFM95_RSSI_OFFSET,
431-
targetRSSI - RFM95_RSSI_OFFSET, RFM95.powerLevel);
422+
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%d,tR=%d,TXL=%d\n"), RFM95_internalToRSSI(currentRSSI),
423+
RFM95_internalToRSSI(targetRSSI), RFM95.powerLevel);
432424
return RFM95_setTxPower(RFM95.powerLevel);;
433425
}
434426

@@ -454,7 +446,8 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
454446
if (sender == recipient && RFM95_getACKReceived(flag) &&
455447
(ACKsequenceNumber == RFM95.txSequenceNumber - 1)) {
456448
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%d,SEQ=%d,RSSI=%d,SNR=%d\n"),sender,ACKsequenceNumber,
457-
RFM95.currentPacket.ACK.RSSI-RFM95_RSSI_OFFSET, (int8_t)RFM95.currentPacket.ACK.SNR / 4);
449+
RFM95_internalToRSSI(RFM95.currentPacket.ACK.RSSI),
450+
RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR));
458451
RFM95_clearRxBuffer();
459452

460453
// ATC
@@ -469,24 +462,24 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
469462
RFM95_DEBUG(PSTR("!RFM95:SWR:NACK\n"));
470463
if (RFM95.ATCenabled) {
471464
// No ACK received, maybe out of reach: increase power level
472-
RFM95_setTxPower(RFM95.powerLevel++);
465+
(void)RFM95_setTxPower(RFM95.powerLevel++);
473466
}
474467
}
475468
return false;
476469
}
477470

478-
479471
// Wait until no channel activity detected or timeout
480472
LOCAL bool RFM95_waitCAD(void)
481473
{
474+
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_CAD);
482475
const uint32_t enterMS = hwMillis();
483-
while (RFM95_isChannelActive()) {
476+
while (RFM95.radioMode == RFM95_RADIO_MODE_CAD) {
484477
if (hwMillis() - enterMS > RFM95_CAD_TIMEOUT_MS) {
485478
return false;
486479
}
487480
doYield();
488481
}
489-
return true;
482+
return !RFM95.cad;
490483
}
491484

492485
// Wait for any previous transmission to finish
@@ -498,15 +491,10 @@ LOCAL bool RFM95_waitPacketSent(void)
498491
return true;
499492
}
500493

501-
LOCAL int16_t RFM95_getRSSI(void)
502-
{
503-
return (int16_t)(RFM95.currentPacket.RSSI - RFM95_RSSI_OFFSET);
504-
}
505-
506494
LOCAL void RFM95_ATCmode(const bool OnOff, const int16_t targetRSSI)
507495
{
508496
RFM95.ATCenabled = OnOff;
509-
RFM95.ATCtargetRSSI = (uint8_t)(targetRSSI + RFM95_RSSI_OFFSET);
497+
RFM95.ATCtargetRSSI = RFM95_RSSItoInternal(targetRSSI);
510498
}
511499

512500
LOCAL bool RFM95_sanityCheck(void)
@@ -516,11 +504,46 @@ LOCAL bool RFM95_sanityCheck(void)
516504
}
517505

518506

507+
LOCAL int16_t RFM95_getSendingRSSI(void)
508+
{
509+
// own RSSI, as measured by the recipient - ACK part
510+
if (RFM95_getACKRSSIReport(RFM95.currentPacket.header.controlFlags)) {
511+
return RFM95_internalToRSSI(RFM95.currentPacket.ACK.RSSI);
512+
} else {
513+
// not possible
514+
return RFM95_RSSI_INVALID;
515+
}
516+
}
519517

518+
LOCAL int8_t RFM95_getSendingSNR(void)
519+
{
520+
// own SNR, as measured by the recipient - ACK part
521+
if (RFM95_getACKRSSIReport(RFM95.currentPacket.header.controlFlags)) {
522+
return RFM95_internalToSNR(RFM95.currentPacket.ACK.SNR);
523+
} else {
524+
// not possible
525+
return RFM95_SNR_INVALID;
526+
}
527+
}
520528

529+
LOCAL int16_t RFM95_getReceivingRSSI(void)
530+
{
531+
// RSSI from last received packet
532+
return RFM95_internalToRSSI(RFM95.currentPacket.RSSI);
533+
}
521534

535+
LOCAL int8_t RFM95_getReceivingSNR(void)
536+
{
537+
// SNR from last received packet
538+
return RFM95_internalToSNR(RFM95.currentPacket.SNR);
539+
}
522540

523-
524-
525-
541+
LOCAL uint8_t RFM95_getTxPowerPercent(void)
542+
{
543+
// report TX level in %
544+
const uint8_t result = (uint8_t)(100.0f * (RFM95.powerLevel - RFM95_MIN_POWER_LEVEL_DBM) /
545+
(RFM95_MAX_POWER_LEVEL_DBM
546+
- RFM95_MIN_POWER_LEVEL_DBM));
547+
return result;
548+
}
526549

0 commit comments

Comments
 (0)