diff --git a/keywords.txt b/keywords.txt index 1bb6c6f8..02739f5a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -94,6 +94,8 @@ setCrcFiltering KEYWORD2 enableSyncWordFiltering KEYWORD2 disableSyncWordFiltering KEYWORD2 setPromiscuous KEYWORD2 +setRSSIConfig KEYWORD2 +setEncoding KEYWORD2 # RF69-specific setAESKey KEYWORD2 @@ -189,6 +191,9 @@ ERR_INVALID_CURRENT_LIMIT LITERAL1 ERR_INVALID_PREAMBLE_LENGTH LITERAL1 ERR_INVALID_GAIN LITERAL1 ERR_WRONG_MODEM LITERAL1 +ERR_INVALID_NUM_SAMPLES LITERAL1 +ERR_INVALID_RSSI_OFFSET LITERAL1 +ERR_INVALID_ENCODING LITERAL1 ERR_INVALID_BIT_RATE LITERAL1 ERR_INVALID_FREQUENCY_DEVIATION LITERAL1 diff --git a/src/TypeDef.h b/src/TypeDef.h index f1675bcd..4814dd7d 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -263,6 +263,21 @@ */ #define ERR_WRONG_MODEM -20 +/*! + \brief The supplied number of RSSI samples is invalid. +*/ +#define ERR_INVALID_NUM_SAMPLES -21 + +/*! + \brief The supplied RSSI offset is invalid. +*/ +#define ERR_INVALID_RSSI_OFFSET -22 + +/*! + \brief The supplied encoding is invalid. +*/ +#define ERR_INVALID_ENCODING -23 + // RF69-specific status codes /*! diff --git a/src/modules/SX1272.cpp b/src/modules/SX1272.cpp index 86d058d7..1221bb27 100644 --- a/src/modules/SX1272.cpp +++ b/src/modules/SX1272.cpp @@ -342,22 +342,33 @@ int16_t SX1272::setDataShapingOOK(uint8_t sh) { return(state); } -int8_t SX1272::getRSSI() { - // check active modem - if(getActiveModem() != SX127X_LORA) { - return(0); +float SX1272::getRSSI() { + if(getActiveModem() == SX127X_LORA) { + // RSSI calculation uses different constant for low-frequency and high-frequency ports + float lastPacketRSSI = -139 + _mod->SPIgetRegValue(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 + startReceive(); + + // read the value for FSK + float rssi = (float)_mod->SPIgetRegValue(SX127X_REG_RSSI_VALUE_FSK) / -2.0; + + // set mode back to standby + standby(); + + // return the value + return(rssi); } - - int8_t lastPacketRSSI = -139 + _mod->SPIgetRegValue(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); } int16_t SX1272::setCRC(bool enableCRC) { diff --git a/src/modules/SX1272.h b/src/modules/SX1272.h index 95f4068f..26f8ac9f 100644 --- a/src/modules/SX1272.h +++ b/src/modules/SX1272.h @@ -235,11 +235,11 @@ class SX1272: public SX127x { int16_t setDataShapingOOK(uint8_t sh); /*! - \brief Gets recorded signal strength indicator of the latest received packet. + \brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem. - \returns Last packet recorded signal strength indicator (RSSI). + \returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem. */ - int8_t getRSSI(); + float getRSSI(); /*! \brief Enables/disables CRC check of received packets. diff --git a/src/modules/SX1278.cpp b/src/modules/SX1278.cpp index 64f2245a..f56392be 100644 --- a/src/modules/SX1278.cpp +++ b/src/modules/SX1278.cpp @@ -411,29 +411,40 @@ int16_t SX1278::setDataShapingOOK(uint8_t sh) { return(state); } -int8_t SX1278::getRSSI() { - // check active modem - if(getActiveModem() != SX127X_LORA) { - return(0); - } +float SX1278::getRSSI() { + if(getActiveModem() == SX127X_LORA) { + // for LoRa, get RSSI of the last packet + float lastPacketRSSI; - int8_t lastPacketRSSI; + // RSSI calculation uses different constant for low-frequency and high-frequency ports + if(_freq < 868.0) { + lastPacketRSSI = -164 + _mod->SPIgetRegValue(SX127X_REG_PKT_RSSI_VALUE); + } else { + lastPacketRSSI = -157 + _mod->SPIgetRegValue(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); - // RSSI calculation uses different constant for low-frequency and high-frequency ports - if(_freq < 868.0) { - lastPacketRSSI = -164 + _mod->SPIgetRegValue(SX127X_REG_PKT_RSSI_VALUE); } else { - lastPacketRSSI = -157 + _mod->SPIgetRegValue(SX127X_REG_PKT_RSSI_VALUE); - } + // enable listen mode + startReceive(); - // 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; - } + // read the value for FSK + float rssi = (float)_mod->SPIgetRegValue(SX127X_REG_RSSI_VALUE_FSK) / -2.0; - return(lastPacketRSSI); + // set mode back to standby + standby(); + + // return the value + return(rssi); + } } int16_t SX1278::setCRC(bool enableCRC) { diff --git a/src/modules/SX1278.h b/src/modules/SX1278.h index 937b0c58..1715e4b1 100644 --- a/src/modules/SX1278.h +++ b/src/modules/SX1278.h @@ -244,11 +244,11 @@ class SX1278: public SX127x { int16_t setDataShapingOOK(uint8_t sh); /*! - \brief Gets recorded signal strength indicator of the latest received packet. + \brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem. - \returns Last packet recorded signal strength indicator (RSSI). + \returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem. */ - int8_t getRSSI(); + float getRSSI(); /*! \brief Enables/disables CRC check of received packets. diff --git a/src/modules/SX127x.cpp b/src/modules/SX127x.cpp index b3e8603a..7411e998 100644 --- a/src/modules/SX127x.cpp +++ b/src/modules/SX127x.cpp @@ -117,6 +117,18 @@ int16_t SX127x::beginFSK(uint8_t chipVersion, float br, float freqDev, float rxB // enable/disable OOK state = setOOK(enableOOK); + if(state != ERR_NONE) { + return(state); + } + + // set default RSSI measurement config + state = setRSSIConfig(2); + if(state != ERR_NONE) { + return(state); + } + + // set default encoding + state = setEncoding(0); return(state); } @@ -174,6 +186,8 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) { return(ERR_TX_TIMEOUT); } } + } else { + return(ERR_UNKNOWN); } // update data rate @@ -891,6 +905,52 @@ size_t SX127x::getPacketLength(bool update) { return(_packetLength); } +int16_t SX127x::setRSSIConfig(uint8_t smoothingSamples, int8_t offset) { + // check active modem + if(getActiveModem() != SX127X_FSK_OOK) { + return(ERR_WRONG_MODEM); + } + + // set mode to standby + int16_t state = standby(); + if(state != ERR_NONE) { + return(state); + } + + // check provided values + if(!(smoothingSamples <= 7)) { + return(ERR_INVALID_NUM_SAMPLES); + } + + if(!((offset >= -16) && (offset <= 15))) { + return(ERR_INVALID_RSSI_OFFSET); + } + + // set new register values + state = _mod->SPIsetRegValue(SX127X_REG_RSSI_CONFIG, offset, 7, 3); + state |= _mod->SPIsetRegValue(SX127X_REG_RSSI_CONFIG, smoothingSamples, 2, 0); + return(state); +} + +int16_t SX127x::setEncoding(uint8_t encoding) { + // check active modem + if(getActiveModem() != SX127X_FSK_OOK) { + return(ERR_WRONG_MODEM); + } + + // set encoding + switch(encoding) { + case 0: + return(_mod->SPIsetRegValue(SX127X_REG_PACKET_CONFIG_1, SX127X_DC_FREE_NONE, 6, 5)); + case 1: + return(_mod->SPIsetRegValue(SX127X_REG_PACKET_CONFIG_1, SX127X_DC_FREE_MANCHESTER, 6, 5)); + case 2: + return(_mod->SPIsetRegValue(SX127X_REG_PACKET_CONFIG_1, SX127X_DC_FREE_WHITENING, 6, 5)); + default: + return(ERR_INVALID_ENCODING); + } +} + int16_t SX127x::config() { // turn off frequency hopping int16_t state = _mod->SPIsetRegValue(SX127X_REG_HOP_PERIOD, SX127X_HOP_PERIOD_OFF); @@ -963,8 +1023,7 @@ bool SX127x::findChip(uint8_t ver) { sprintf(buffHex, "0x%02X", version); RADIOLIB_DEBUG_PRINT(buffHex); RADIOLIB_DEBUG_PRINT(F(", expected 0x00")); - RADIOLIB_DEBUG_PRINT(ver, HEX); - RADIOLIB_DEBUG_PRINTLN(); + RADIOLIB_DEBUG_PRINTLN(ver, HEX); #endif delay(1000); i++; diff --git a/src/modules/SX127x.h b/src/modules/SX127x.h index 5236e595..05f1e5cf 100644 --- a/src/modules/SX127x.h +++ b/src/modules/SX127x.h @@ -842,6 +842,27 @@ class SX127x: public PhysicalLayer { */ size_t getPacketLength(bool update = true); + /*! + \brief Sets RSSI measurement configuration in FSK mode. + + \param smoothingSamples Number of samples taken to avergae the RSSI result. + numSamples = 2 ^ (1 + smoothingSamples), allowed values are in range 0 (2 samples) - 7 (256 samples) + + \param offset Signed RSSI offset that will be automatically compensated. 1 dB per LSB, defaults to 0, allowed values are in range -16 dB to +15 dB. + + \returns \ref status_codes + */ + int16_t setRSSIConfig(uint8_t smoothingSamples, int8_t offset = 0); + + /*! + \brief Sets transmission encoding. Only available in FSK mode. + + \param encoding Encoding to be used. Set to 0 for NRZ, 1 for Manchester and 2 for whitening. + + \returns \ref status_codes + */ + int16_t setEncoding(uint8_t encoding); + #ifdef RADIOLIB_DEBUG void regDump(); #endif