Skip to content

Commit

Permalink
Added FSK RSSI measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
jgromes committed Oct 31, 2019
1 parent b5d8c2b commit a3d49a4
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 39 deletions.
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ setDataShaping KEYWORD2
setOOK KEYWORD2
setDataShapingOOK KEYWORD2
setCRC KEYWORD2
setRSSIConfig KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down Expand Up @@ -96,3 +97,5 @@ ERR_INVALID_SYNC_WORD LITERAL1
ERR_INVALID_DATA_SHAPING LITERAL1
ERR_INVALID_MODULATION LITERAL1
ERR_MEMORY_ALLOCATION_FAILED LITERAL1
ERR_INVALID_NUM_SAMPLES LITERAL1
ERR_INVALID_RSSI_OFFSET LITERAL1
10 changes: 10 additions & 0 deletions src/TypeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@
*/
#define ERR_INVALID_MODULATION -26

/*!
\brief The supplied number of RSSI samples is invalid.
*/
#define ERR_INVALID_NUM_SAMPLES -27

/*!
\brief The supplied RSSI offset is invalid.
*/
#define ERR_INVALID_RSSI_OFFSET -28

/*!
\}
*/
Expand Down
28 changes: 15 additions & 13 deletions src/modules/SX1272.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,24 @@ 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;
}

int8_t lastPacketRSSI = -139 + _mod->SPIgetRegValue(SX127X_REG_PKT_RSSI_VALUE);
return(lastPacketRSSI);

// 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;
} else {
// just read the value for FSK
return((float)_mod->SPIgetRegValue(SX127X_REG_RSSI_VALUE) / 2.0);
}

return(lastPacketRSSI);
}

int16_t SX1272::setCRC(bool enableCRC) {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/SX1272.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 21 additions & 19 deletions src/modules/SX1278.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,29 +411,31 @@ 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;

// 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);
}

int8_t lastPacketRSSI;
// 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;
}

// 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);
}
return(lastPacketRSSI);

// 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;
} else {
// just read the value for FSK
return((float)_mod->SPIgetRegValue(SX127X_REG_RSSI_VALUE) / 2.0);
}

return(lastPacketRSSI);
}

int16_t SX1278::setCRC(bool enableCRC) {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/SX1278.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 28 additions & 1 deletion src/modules/SX127x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ int16_t SX127x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
int16_t SX127x::readData(uint8_t* data, size_t len) {
int16_t modem = getActiveModem();
size_t length = len;

// put module to standby
standby();

Expand Down Expand Up @@ -893,6 +893,33 @@ 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::config() {
// turn off frequency hopping
int16_t state = _mod->SPIsetRegValue(SX127X_REG_HOP_PERIOD, SX127X_HOP_PERIOD_OFF);
Expand Down
12 changes: 12 additions & 0 deletions src/modules/SX127x.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,18 @@ 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);

#ifdef RADIOLIB_DEBUG
void regDump();
#endif
Expand Down

0 comments on commit a3d49a4

Please sign in to comment.