From 04e24ab9d14c23ac94fbe501b70122732e021569 Mon Sep 17 00:00:00 2001 From: G4lile0 Date: Fri, 24 Mar 2023 20:30:06 +0100 Subject: [PATCH 1/5] [SX1278] New getInstRSSI to get current RSSI --- src/modules/SX127x/SX1278.cpp | 33 +++++++++++++++++++++++++++++++++ src/modules/SX127x/SX1278.h | 9 +++++++++ 2 files changed, 42 insertions(+) diff --git a/src/modules/SX127x/SX1278.cpp b/src/modules/SX127x/SX1278.cpp index fda1b3c2..4bcd3ee7 100644 --- a/src/modules/SX127x/SX1278.cpp +++ b/src/modules/SX127x/SX1278.cpp @@ -421,6 +421,39 @@ float SX1278::getRSSI(bool skipReceive) { } } +float SX1278::getInstRSSI(bool skipReceive) { + if(getActiveModem() == RADIOLIB_SX127X_LORA) { + // for LoRa, get current RSSI + float currentRSSI; + + // RSSI calculation uses different constant for low-frequency and high-frequency ports + if(_freq < 868.0) { + currentRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + } else { + currentRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + } + + return(currentRSSI); + + } else { + // enable listen mode + if(!skipReceive) { + startReceive(); + } + + // read the value for FSK + float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0; + + // set mode back to standby + if(!skipReceive) { + standby(); + } + + // return the value + return(rssi); + } +} + int16_t SX1278::setCRC(bool enable, bool mode) { if(getActiveModem() == RADIOLIB_SX127X_LORA) { // set LoRa CRC diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index fd274572..51096cd5 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -257,6 +257,15 @@ class SX1278: public SX127x { */ float getRSSI(bool skipReceive = false); + /*! + \brief Gets current signal strength indicator of for LoRa modem, or current RSSI level for FSK modem. + + \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode. + + \returns Current packet RSSI for LoRa modem, or FSK modem. + */ + float getInstRSSI(bool skipReceive = false); + /*! \brief Enables/disables CRC check of received packets. From 09ab5f8073f7d9b2ca20492b2aa78fa9c3aa35a1 Mon Sep 17 00:00:00 2001 From: G4lile0 Date: Mon, 27 Mar 2023 20:51:44 +0200 Subject: [PATCH 2/5] [SX127x] & [SX126x] read current RSSI for getRSSI --- src/modules/SX126x/SX126x.cpp | 16 +++++++-- src/modules/SX126x/SX126x.h | 4 ++- src/modules/SX127x/SX1278.cpp | 64 +++++++++++++---------------------- src/modules/SX127x/SX1278.h | 13 ++----- 4 files changed, 42 insertions(+), 55 deletions(-) diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index 2c6c8c15..9a55b236 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1222,10 +1222,20 @@ float SX126x::getDataRate() const { } float SX126x::getRSSI() { + + if (packet) { // get last packet RSSI from packet status - uint32_t packetStatus = getPacketStatus(); - uint8_t rssiPkt = packetStatus & 0xFF; - return(-1.0 * rssiPkt/2.0); + uint32_t packetStatus = getPacketStatus(); + uint8_t rssiPkt = packetStatus & 0xFF; + return(-1.0 * rssiPkt/2.0); + } else { + // get instantaneous RSSI value + uint8_t data[3] = {0, 0, 0}; // RssiInst, Status, RFU + _mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RSSI_INST, data, 3); + + return (float)data[0] / (-2.0); + } + } float SX126x::getSNR() { diff --git a/src/modules/SX126x/SX126x.h b/src/modules/SX126x/SX126x.h index 92b0f2c6..6634ddf7 100644 --- a/src/modules/SX126x/SX126x.h +++ b/src/modules/SX126x/SX126x.h @@ -917,9 +917,11 @@ class SX126x: public PhysicalLayer { /*! \brief Gets RSSI (Recorded Signal Strength Indicator) of the last received packet. + \param packet Set to false to gets current RSSI measurement in LoRa mode. + \returns RSSI of the last received packet in dBm. */ - float getRSSI(); + float getRSSI(bool packet = true); /*! \brief Gets SNR (Signal to Noise Ratio) of the last received packet. Only available for LoRa modem. diff --git a/src/modules/SX127x/SX1278.cpp b/src/modules/SX127x/SX1278.cpp index 4bcd3ee7..e06604bc 100644 --- a/src/modules/SX127x/SX1278.cpp +++ b/src/modules/SX127x/SX1278.cpp @@ -381,60 +381,42 @@ int16_t SX1278::setDataShapingOOK(uint8_t sh) { return(state); } -float SX1278::getRSSI(bool skipReceive) { +float SX1278::getRSSI(bool packet, bool skipReceive) { if(getActiveModem() == RADIOLIB_SX127X_LORA) { - // for LoRa, get RSSI of the last packet - float lastPacketRSSI; + if (packet) { + // for LoRa, get RSSI of the last packet + float lastPacketRSSI; // RSSI calculation uses different constant for low-frequency and high-frequency ports - if(_freq < 868.0) { - lastPacketRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); - } else { - lastPacketRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); - } + if(_freq < 868.0) { + lastPacketRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); + } else { + lastPacketRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); + } // spread-spectrum modulation signal can be received below noise floor // check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value - float lastPacketSNR = SX127x::getSNR(); - if(lastPacketSNR < 0.0) { - lastPacketRSSI += lastPacketSNR; - } + float lastPacketSNR = SX127x::getSNR(); + if(lastPacketSNR < 0.0) { + lastPacketRSSI += lastPacketSNR; + } + return(lastPacketRSSI); - return(lastPacketRSSI); + } else { - } else { - // enable listen mode - if(!skipReceive) { - startReceive(); - } - - // read the value for FSK - float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0; - - // set mode back to standby - if(!skipReceive) { - standby(); - } - - // return the value - return(rssi); - } -} - -float SX1278::getInstRSSI(bool skipReceive) { - if(getActiveModem() == RADIOLIB_SX127X_LORA) { // for LoRa, get current RSSI - float currentRSSI; + float currentRSSI; // RSSI calculation uses different constant for low-frequency and high-frequency ports - if(_freq < 868.0) { - currentRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); - } else { - currentRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + if(_freq < 868.0) { + currentRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + } else { + currentRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + } + return(currentRSSI); } - return(currentRSSI); - + } else { // enable listen mode if(!skipReceive) { diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index 51096cd5..23ff4054 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -251,20 +251,13 @@ class SX1278: public SX127x { /*! \brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem. + \param packet Set to false to gets current RSSI measurement in LoRa mode. + \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode. \returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem. */ - float getRSSI(bool skipReceive = false); - - /*! - \brief Gets current signal strength indicator of for LoRa modem, or current RSSI level for FSK modem. - - \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode. - - \returns Current packet RSSI for LoRa modem, or FSK modem. - */ - float getInstRSSI(bool skipReceive = false); + float getRSSI(bool packet = true, bool skipReceive = false); /*! \brief Enables/disables CRC check of received packets. From 50318a6c602c9b0bf31b8d399b7bef6306708993 Mon Sep 17 00:00:00 2001 From: G4lile0 Date: Mon, 27 Mar 2023 21:21:45 +0200 Subject: [PATCH 3/5] bool packet miss on SX126x::getRSSI --- src/modules/SX126x/SX126x.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index 9a55b236..4ffbc9e0 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1221,7 +1221,7 @@ float SX126x::getDataRate() const { return(_dataRate); } -float SX126x::getRSSI() { +float SX126x::getRSSI(bool packet) { if (packet) { // get last packet RSSI from packet status From ab9cf0d52839bf28bd1108cd3efc9ae367ac05e4 Mon Sep 17 00:00:00 2001 From: jgromes Date: Mon, 27 Mar 2023 23:21:37 +0200 Subject: [PATCH 4/5] [SX126x] Unified getRSSI interface --- src/modules/SX126x/SX126x.cpp | 18 ++++-------------- src/modules/SX126x/SX126x.h | 13 +++---------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index 4ffbc9e0..ea01e088 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1222,20 +1222,17 @@ float SX126x::getDataRate() const { } float SX126x::getRSSI(bool packet) { - - if (packet) { - // get last packet RSSI from packet status + if(packet) { + // get last packet RSSI from packet status uint32_t packetStatus = getPacketStatus(); uint8_t rssiPkt = packetStatus & 0xFF; return(-1.0 * rssiPkt/2.0); } else { - // get instantaneous RSSI value + // get instantaneous RSSI value uint8_t data[3] = {0, 0, 0}; // RssiInst, Status, RFU _mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RSSI_INST, data, 3); - - return (float)data[0] / (-2.0); + return((float)data[0] / (-2.0)); } - } float SX126x::getSNR() { @@ -1337,13 +1334,6 @@ uint32_t SX126x::getTimeOnAir(size_t len) { } } -float SX126x::getRSSIInst() { - uint8_t data[3] = {0, 0, 0}; // RssiInst, Status, RFU - _mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RSSI_INST, data, 3); - - return (float)data[0] / (-2.0); -} - int16_t SX126x::implicitHeader(size_t len) { return(setHeaderType(RADIOLIB_SX126X_LORA_HEADER_IMPLICIT, len)); } diff --git a/src/modules/SX126x/SX126x.h b/src/modules/SX126x/SX126x.h index 6634ddf7..deb437af 100644 --- a/src/modules/SX126x/SX126x.h +++ b/src/modules/SX126x/SX126x.h @@ -915,11 +915,11 @@ class SX126x: public PhysicalLayer { float getDataRate() const; /*! - \brief Gets RSSI (Recorded Signal Strength Indicator) of the last received packet. + \brief GetsRSSI (Recorded Signal Strength Indicator). - \param packet Set to false to gets current RSSI measurement in LoRa mode. + \param packet Whether to read last packet RSSI, or the current value. - \returns RSSI of the last received packet in dBm. + \returns RSSI value in dBm. */ float getRSSI(bool packet = true); @@ -975,13 +975,6 @@ class SX126x: public PhysicalLayer { */ uint32_t getTimeOnAir(size_t len); - /*! - \brief Get instantaneous RSSI value during recption of the packet. Should switch to FSK receive mode for LBT implementation. - - \returns Instantaneous RSSI value in dBm, in steps of 0.5dBm - */ - float getRSSIInst(); - /*! \brief Set implicit header mode for future reception/transmission. From f62f912c877e59ef8f55cf0654edd674f6b31158 Mon Sep 17 00:00:00 2001 From: jgromes Date: Mon, 27 Mar 2023 23:22:03 +0200 Subject: [PATCH 5/5] [SX127x] Unified getRSSI interface --- src/modules/SX127x/SX1272.cpp | 33 ++------------------- src/modules/SX127x/SX1272.h | 10 ++++--- src/modules/SX127x/SX1278.cpp | 55 +++-------------------------------- src/modules/SX127x/SX1278.h | 8 ++--- src/modules/SX127x/SX127x.cpp | 41 ++++++++++++++++++++++++++ src/modules/SX127x/SX127x.h | 11 +++++++ 6 files changed, 68 insertions(+), 90 deletions(-) diff --git a/src/modules/SX127x/SX1272.cpp b/src/modules/SX127x/SX1272.cpp index 771555ca..5c8722e4 100644 --- a/src/modules/SX127x/SX1272.cpp +++ b/src/modules/SX127x/SX1272.cpp @@ -352,37 +352,8 @@ int16_t SX1272::setDataShapingOOK(uint8_t sh) { return(state); } -float SX1272::getRSSI(bool skipReceive) { - if(getActiveModem() == RADIOLIB_SX127X_LORA) { - // RSSI calculation uses different constant for low-frequency and high-frequency ports - float lastPacketRSSI = -139 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); - - // spread-spectrum modulation signal can be received below noise floor - // check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value - float lastPacketSNR = SX127x::getSNR(); - if(lastPacketSNR < 0.0) { - lastPacketRSSI += lastPacketSNR; - } - - return(lastPacketRSSI); - - } else { - // enable listen mode - if(!skipReceive) { - startReceive(); - } - - // read the value for FSK - float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0; - - // set mode back to standby - if(!skipReceive) { - standby(); - } - - // return the value - return(rssi); - } +float SX1272::getRSSI(bool packet, bool skipReceive) { + return(SX127x::getRSSI(packet, skipReceive, -139)); } int16_t SX1272::setCRC(bool enable, bool mode) { diff --git a/src/modules/SX127x/SX1272.h b/src/modules/SX127x/SX1272.h index 8ef723f7..3c74c6da 100644 --- a/src/modules/SX127x/SX1272.h +++ b/src/modules/SX127x/SX1272.h @@ -240,13 +240,15 @@ class SX1272: public SX127x { int16_t setDataShapingOOK(uint8_t sh); /*! - \brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem. + \brief Gets recorded signal strength indicator. - \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode. + \param packet Whether to read last packet RSSI, or the current value. LoRa mode only, ignored for FSK. - \returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem. + \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FSK/OOK mode. + + \returns RSSI value in dBm. */ - float getRSSI(bool skipReceive = false); + float getRSSI(bool packet = true, bool skipReceive = false); /*! \brief Enables/disables CRC check of received packets. diff --git a/src/modules/SX127x/SX1278.cpp b/src/modules/SX127x/SX1278.cpp index e06604bc..0feb05fc 100644 --- a/src/modules/SX127x/SX1278.cpp +++ b/src/modules/SX127x/SX1278.cpp @@ -382,58 +382,11 @@ int16_t SX1278::setDataShapingOOK(uint8_t sh) { } float SX1278::getRSSI(bool packet, bool skipReceive) { - if(getActiveModem() == RADIOLIB_SX127X_LORA) { - - if (packet) { - // for LoRa, get RSSI of the last packet - float lastPacketRSSI; - // RSSI calculation uses different constant for low-frequency and high-frequency ports - if(_freq < 868.0) { - lastPacketRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); - } else { - lastPacketRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); - } - - // spread-spectrum modulation signal can be received below noise floor - // check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value - float lastPacketSNR = SX127x::getSNR(); - if(lastPacketSNR < 0.0) { - lastPacketRSSI += lastPacketSNR; - } - return(lastPacketRSSI); - - } else { - - // for LoRa, get current RSSI - float currentRSSI; - - // RSSI calculation uses different constant for low-frequency and high-frequency ports - if(_freq < 868.0) { - currentRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); - } else { - currentRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); - } - return(currentRSSI); - } - - - } else { - // enable listen mode - if(!skipReceive) { - startReceive(); - } - - // read the value for FSK - float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0; - - // set mode back to standby - if(!skipReceive) { - standby(); - } - - // return the value - return(rssi); + int16_t offset = -157; + if(_freq < 868.0) { + offset = -164; } + return(SX127x::getRSSI(packet, skipReceive, offset)); } int16_t SX1278::setCRC(bool enable, bool mode) { diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index 23ff4054..6db1b516 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -249,13 +249,13 @@ class SX1278: public SX127x { int16_t setDataShapingOOK(uint8_t sh); /*! - \brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem. + \brief Gets recorded signal strength indicator. - \param packet Set to false to gets current RSSI measurement in LoRa mode. + \param packet Whether to read last packet RSSI, or the current value. LoRa mode only, ignored for FSK. - \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode. + \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FSK/OOK mode. - \returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem. + \returns RSSI value in dBm. */ float getRSSI(bool packet = true, bool skipReceive = false); diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index dace7ae0..bf74ca46 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1552,4 +1552,45 @@ int16_t SX127x::setDIOPreambleDetect(bool usePreambleDetect) { return _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_2, (usePreambleDetect) ? RADIOLIB_SX127X_DIO_MAP_PREAMBLE_DETECT : RADIOLIB_SX127X_DIO_MAP_RSSI, 0, 0); } +float SX127x::getRSSI(bool packet, bool skipReceive, int16_t offset) { + if(getActiveModem() == RADIOLIB_SX127X_LORA) { + if(packet) { + // LoRa packet mode, get RSSI of the last packet + float lastPacketRSSI = offset + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE); + + // spread-spectrum modulation signal can be received below noise floor + // check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value + float lastPacketSNR = SX127x::getSNR(); + if(lastPacketSNR < 0.0) { + lastPacketRSSI += lastPacketSNR; + } + return(lastPacketRSSI); + + } else { + // LoRa instant, get current RSSI + float currentRSSI = offset + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE); + return(currentRSSI); + } + + } else { + // for FSK, there is no packet RSSI + + // enable listen mode + if(!skipReceive) { + startReceive(); + } + + // read the value for FSK + float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0; + + // set mode back to standby + if(!skipReceive) { + standby(); + } + + // return the value + return(rssi); + } +} + #endif diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index e40a5de4..c3b8ca7d 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -1243,6 +1243,17 @@ class SX127x: public PhysicalLayer { */ int16_t setDIOPreambleDetect(bool usePreambleDetect); + /*! + \brief Gets recorded signal strength indicator. + + \param packet Whether to read last packet RSSI, or the current value. LoRa mode only, ignored for FSK. + + \param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FSK/OOK mode. + + \returns RSSI value in dBm. + */ + float getRSSI(bool packet, bool skipReceive, int16_t offset); + /*! \brief Sets the RSSI value above which the RSSI interrupt is signaled