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