[SX127x] Sync with LoRaLib 8.0.5

This commit is contained in:
jgromes 2019-05-19 16:15:50 +02:00
parent bf85e16880
commit 975e6e116c
4 changed files with 97 additions and 100 deletions

View file

@ -1,7 +1,7 @@
#include "SX1272.h"
SX1272::SX1272(Module* mod) : SX127x(mod) {
}
int16_t SX1272::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint8_t currentLimit, uint16_t preambleLength, uint8_t gain) {
@ -10,51 +10,51 @@ int16_t SX1272::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync
if(state != ERR_NONE) {
return(state);
}
// configure settings not accessible by API
state = config();
if(state != ERR_NONE) {
return(state);
}
// mitigation of receiver spurious response
// see SX1272/73 Errata, section 2.2 for details
state = _mod->SPIsetRegValue(0x31, 0b10000000, 7, 7);
if(state != ERR_NONE) {
return(state);
}
// configure publicly accessible settings
state = setFrequency(freq);
if(state != ERR_NONE) {
return(state);
}
state = setBandwidth(bw);
if(state != ERR_NONE) {
return(state);
}
state = setSpreadingFactor(sf);
if(state != ERR_NONE) {
return(state);
}
state = setCodingRate(cr);
if(state != ERR_NONE) {
return(state);
}
state = setOutputPower(power);
if(state != ERR_NONE) {
return(state);
}
state = setGain(gain);
if(state != ERR_NONE) {
return(state);
}
return(state);
}
@ -64,24 +64,24 @@ int16_t SX1272::beginFSK(float freq, float br, float rxBw, float freqDev, int8_t
if(state != ERR_NONE) {
return(state);
}
// configure settings not accessible by API
state = configFSK();
if(state != ERR_NONE) {
return(state);
}
// configure publicly accessible settings
state = setFrequency(freq);
if(state != ERR_NONE) {
return(state);
}
state = setOutputPower(power);
if(state != ERR_NONE) {
return(state);
}
return(state);
}
@ -90,7 +90,7 @@ int16_t SX1272::setFrequency(float freq) {
if((freq < 860.0) || (freq > 1020.0)) {
return(ERR_INVALID_FREQUENCY);
}
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == ERR_NONE) {
@ -106,8 +106,8 @@ int16_t SX1272::setBandwidth(float bw) {
}
uint8_t newBandwidth;
// check alowed bandwidth values
// check allowed bandwidth values
if(abs(bw - 125.0) <= 0.001) {
newBandwidth = SX1272_BW_125_00_KHZ;
} else if(abs(bw - 250.0) <= 0.001) {
@ -117,7 +117,7 @@ int16_t SX1272::setBandwidth(float bw) {
} else {
return(ERR_INVALID_BANDWIDTH);
}
// set bandwidth and if successful, save the new setting
int16_t state = SX1272::setBandwidthRaw(newBandwidth);
if(state == ERR_NONE) {
@ -133,7 +133,7 @@ int16_t SX1272::setSpreadingFactor(uint8_t sf) {
}
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
@ -160,7 +160,7 @@ int16_t SX1272::setSpreadingFactor(uint8_t sf) {
default:
return(ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1272::setSpreadingFactorRaw(newSpreadingFactor);
if(state == ERR_NONE) {
@ -176,7 +176,7 @@ int16_t SX1272::setCodingRate(uint8_t cr) {
}
uint8_t newCodingRate;
// check allowed coding rate values
switch(cr) {
case 5:
@ -194,7 +194,7 @@ int16_t SX1272::setCodingRate(uint8_t cr) {
default:
return(ERR_INVALID_CODING_RATE);
}
// set coding rate and if successful, save the new setting
int16_t state = SX1272::setCodingRateRaw(newCodingRate);
if(state == ERR_NONE) {
@ -208,10 +208,10 @@ int16_t SX1272::setOutputPower(int8_t power) {
if(!(((power >= -1) && (power <= 17)) || (power == 20))) {
return(ERR_INVALID_OUTPUT_POWER);
}
// set mode to standby
int16_t state = SX127x::standby();
// set output power
if(power < 2) {
// power is less than 2 dBm, enable PA0 on RFIO
@ -242,10 +242,10 @@ int16_t SX1272::setGain(uint8_t gain) {
if(gain > 6) {
return(ERR_INVALID_GAIN);
}
// set mode to standby
int16_t state = SX127x::standby();
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
@ -262,15 +262,15 @@ int16_t SX1272::setDataShaping(float sh) {
if(getActiveModem() != SX127X_FSK_OOK) {
return(ERR_WRONG_MODEM);
}
// check modulation
if(!SX127x::_ook) {
return(ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
if(abs(sh - 0.0) <= 0.001) {
state |= _mod->SPIsetRegValue(SX127X_REG_OP_MODE, SX1272_NO_SHAPING, 4, 3);
@ -291,15 +291,15 @@ int16_t SX1272::setDataShapingOOK(uint8_t sh) {
if(getActiveModem() != SX127X_FSK_OOK) {
return(ERR_WRONG_MODEM);
}
// check modulation
if(!SX127x::_ook) {
return(ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
switch(sh) {
case 0:
@ -315,7 +315,7 @@ int16_t SX1272::setDataShapingOOK(uint8_t sh) {
state = ERR_INVALID_DATA_SHAPING;
break;
}
return(state);
}
@ -324,16 +324,16 @@ int8_t SX1272::getRSSI() {
if(getActiveModem() != SX127X_LORA) {
return(0);
}
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);
}
@ -358,7 +358,7 @@ int16_t SX1272::setCRC(bool enableCRC) {
int16_t SX1272::setBandwidthRaw(uint8_t newBandwidth) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, newBandwidth, 7, 6);
return(state);
@ -367,7 +367,7 @@ int16_t SX1272::setBandwidthRaw(uint8_t newBandwidth) {
int16_t SX1272::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
// set mode to standby
int16_t state = SX127x::standby();
// write registers
if(newSpreadingFactor == SX127X_SF_6) {
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, SX1272_HEADER_IMPL_MODE | SX1272_RX_CRC_MODE_ON, 2, 1);
@ -386,7 +386,7 @@ int16_t SX1272::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
int16_t SX1272::setCodingRateRaw(uint8_t newCodingRate) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, newCodingRate, 5, 3);
return(state);
@ -398,8 +398,8 @@ int16_t SX1272::config() {
if(state != ERR_NONE) {
return(state);
}
// calculate symbol length and set low datarate optimization, if needed
// calculate symbol length and set low data rate optimization, if needed
uint16_t base = 1;
float symbolLength = (float)(base << _sf) / (float)_bw;
if(symbolLength >= 16.0) {
@ -416,9 +416,9 @@ int16_t SX1272::configFSK() {
if(state != ERR_NONE) {
return(state);
}
// set fast PLL hop
state = _mod->SPIsetRegValue(SX1272_REG_PLL_HOP, SX127X_FAST_HOP_ON, 7, 7);
return(state);
}

View file

@ -1,7 +1,7 @@
#include "SX1278.h"
SX1278::SX1278(Module* mod) : SX127x(mod) {
}
int16_t SX1278::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint8_t currentLimit, uint16_t preambleLength, uint8_t gain) {
@ -10,44 +10,44 @@ int16_t SX1278::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync
if(state != ERR_NONE) {
return(state);
}
// configure settings not accessible by API
state = config();
if(state != ERR_NONE) {
return(state);
}
// configure publicly accessible settings
state = setFrequency(freq);
if(state != ERR_NONE) {
return(state);
}
state = setBandwidth(bw);
if(state != ERR_NONE) {
return(state);
}
state = setSpreadingFactor(sf);
if(state != ERR_NONE) {
return(state);
}
state = setCodingRate(cr);
if(state != ERR_NONE) {
return(state);
}
state = setOutputPower(power);
if(state != ERR_NONE) {
return(state);
}
state = setGain(gain);
if(state != ERR_NONE) {
return(state);
}
return(state);
}
@ -57,24 +57,24 @@ int16_t SX1278::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t
if(state != ERR_NONE) {
return(state);
}
// configure settings not accessible by API
state = configFSK();
if(state != ERR_NONE) {
return(state);
}
// configure publicly accessible settings
state = setFrequency(freq);
if(state != ERR_NONE) {
return(state);
}
state = setOutputPower(power);
if(state != ERR_NONE) {
return(state);
}
return(state);
}
@ -83,7 +83,7 @@ int16_t SX1278::setFrequency(float freq) {
if((freq < 137.0) || (freq > 525.0)) {
return(ERR_INVALID_FREQUENCY);
}
// SX1276/77/78 Errata fixes
if(getActiveModem() == SX127X_LORA) {
// sensitivity optimization for 500kHz bandwidth
@ -97,9 +97,9 @@ int16_t SX1278::setFrequency(float freq) {
_mod->SPIwriteRegister(0x3a, 0x7F);
}
}
// mitigation of receiver spurious response
// see SX1276/77/78 Errata, section 2.3 for details
// see SX1276/77/78 Errata, section 2.3 for details
if(abs(_bw - 7.8) <= 0.001) {
_mod->SPIsetRegValue(0x31, 0b0000000, 7, 7);
_mod->SPIsetRegValue(0x2F, 0x48);
@ -146,7 +146,7 @@ int16_t SX1278::setFrequency(float freq) {
_mod->SPIsetRegValue(0x31, 0b1000000, 7, 7);
}
}
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == ERR_NONE) {
@ -162,8 +162,8 @@ int16_t SX1278::setBandwidth(float bw) {
}
uint8_t newBandwidth;
// check alowed bandwidth values
// check allowed bandwidth values
if(abs(bw - 7.8) <= 0.001) {
newBandwidth = SX1278_BW_7_80_KHZ;
} else if(abs(bw - 10.4) <= 0.001) {
@ -187,7 +187,7 @@ int16_t SX1278::setBandwidth(float bw) {
} else {
return(ERR_INVALID_BANDWIDTH);
}
// set bandwidth and if successful, save the new setting
int16_t state = SX1278::setBandwidthRaw(newBandwidth);
if(state == ERR_NONE) {
@ -203,7 +203,7 @@ int16_t SX1278::setSpreadingFactor(uint8_t sf) {
}
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
@ -230,7 +230,7 @@ int16_t SX1278::setSpreadingFactor(uint8_t sf) {
default:
return(ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor);
if(state == ERR_NONE) {
@ -246,7 +246,7 @@ int16_t SX1278::setCodingRate(uint8_t cr) {
}
uint8_t newCodingRate;
// check allowed coding rate values
switch(cr) {
case 5:
@ -264,7 +264,7 @@ int16_t SX1278::setCodingRate(uint8_t cr) {
default:
return(ERR_INVALID_CODING_RATE);
}
// set coding rate and if successful, save the new setting
int16_t state = SX1278::setCodingRateRaw(newCodingRate);
if(state == ERR_NONE) {
@ -278,10 +278,10 @@ int16_t SX1278::setOutputPower(int8_t power) {
if(!(((power >= -3) && (power <= 17)) || (power == 20))) {
return(ERR_INVALID_OUTPUT_POWER);
}
// set mode to standby
int16_t state = SX127x::standby();
// set output power
if(power < 2) {
// power is less than 2 dBm, enable PA on RFO
@ -312,10 +312,10 @@ int16_t SX1278::setGain(uint8_t gain) {
if(gain > 6) {
return(ERR_INVALID_GAIN);
}
// set mode to standby
int16_t state = SX127x::standby();
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
@ -332,15 +332,15 @@ int16_t SX1278::setDataShaping(float sh) {
if(getActiveModem() != SX127X_FSK_OOK) {
return(ERR_WRONG_MODEM);
}
// check modulation
if(SX127x::_ook) {
return(ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
if(abs(sh - 0.0) <= 0.001) {
state |= _mod->SPIsetRegValue(SX127X_REG_PA_RAMP, SX1278_NO_SHAPING, 6, 5);
@ -361,15 +361,15 @@ int16_t SX1278::setDataShapingOOK(uint8_t sh) {
if(getActiveModem() != SX127X_FSK_OOK) {
return(ERR_WRONG_MODEM);
}
// check modulation
if(!SX127x::_ook) {
return(ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
switch(sh) {
case 0:
@ -384,7 +384,7 @@ int16_t SX1278::setDataShapingOOK(uint8_t sh) {
default:
return(ERR_INVALID_DATA_SHAPING);
}
return(state);
}
@ -395,21 +395,21 @@ int8_t SX1278::getRSSI() {
}
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);
}
@ -434,7 +434,7 @@ int16_t SX1278::setCRC(bool enableCRC) {
int16_t SX1278::setBandwidthRaw(uint8_t newBandwidth) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, newBandwidth, 7, 4);
return(state);
@ -443,7 +443,7 @@ int16_t SX1278::setBandwidthRaw(uint8_t newBandwidth) {
int16_t SX1278::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
// set mode to standby
int16_t state = SX127x::standby();
// write registers
if(newSpreadingFactor == SX127X_SF_6) {
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, SX1278_HEADER_IMPL_MODE, 0, 0);
@ -462,7 +462,7 @@ int16_t SX1278::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
int16_t SX1278::setCodingRateRaw(uint8_t newCodingRate) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(SX127X_REG_MODEM_CONFIG_1, newCodingRate, 3, 1);
return(state);
@ -474,8 +474,8 @@ int16_t SX1278::config() {
if(state != ERR_NONE) {
return(state);
}
// calculate symbol length and set low datarate optimization, if needed
// calculate symbol length and set low data rate optimization, if needed
uint16_t base = 1;
float symbolLength = (float)(base << _sf) / (float)_bw;
if(symbolLength >= 16.0) {
@ -492,9 +492,9 @@ int16_t SX1278::configFSK() {
if(state != ERR_NONE) {
return(state);
}
// set fast PLL hop
state = _mod->SPIsetRegValue(SX1278_REG_PLL_HOP, SX127X_FAST_HOP_ON, 7, 7);
return(state);
}

View file

@ -469,7 +469,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;
size_t length = len;
if(modem == SX127X_LORA) {
// check integrity CRC
if(_mod->SPIgetRegValue(SX127X_REG_IRQ_FLAGS, 5, 5) == SX127X_CLEAR_IRQ_FLAG_PAYLOAD_CRC_ERROR) {
@ -480,7 +480,6 @@ int16_t SX127x::readData(uint8_t* data, size_t len) {
}
// get packet length
length = len;
if(_sf != 6) {
length = _mod->SPIgetRegValue(SX127X_REG_RX_NB_BYTES);
}
@ -506,9 +505,7 @@ int16_t SX127x::readData(uint8_t* data, size_t len) {
_mod->SPIreadRegisterBurst(SX127X_REG_FIFO, length, data);
// add terminating null
if(len == 0) {
data[length] = 0;
}
data[length] = 0;
// clear interrupt flags
clearIRQFlags();
@ -652,7 +649,7 @@ int16_t SX127x::setBitRate(float br) {
return(ERR_WRONG_MODEM);
}
// check allowed bitrate
// check allowed bit rate
if(_ook) {
if((br < 1.2) || (br > 32.768)) {
return(ERR_INVALID_BIT_RATE);
@ -674,7 +671,7 @@ int16_t SX127x::setBitRate(float br) {
state = _mod->SPIsetRegValue(SX127X_REG_BITRATE_MSB, (bitRate & 0xFF00) >> 8, 7, 0);
state |= _mod->SPIsetRegValue(SX127X_REG_BITRATE_LSB, bitRate & 0x00FF, 7, 0);
// TODO: fractional part of bit rate setting (not in OOK)
// TODO fractional part of bit rate setting (not in OOK)
if(state == ERR_NONE) {
SX127x::_br = br;
}
@ -723,7 +720,7 @@ int16_t SX127x::setRxBandwidth(float rxBw) {
return(state);
}
// calculate exponent and mantisa values
// calculate exponent and mantissa values
for(uint8_t e = 7; e >= 1; e--) {
for(int8_t m = 2; m >= 0; m--) {
float point = (SX127X_CRYSTAL_FREQ * 1000000.0)/(((4 * m) + 16) * ((uint32_t)1 << (e + 2)));
@ -955,7 +952,7 @@ int16_t SX127x::setActiveModem(uint8_t modem) {
// set mode to SLEEP
int16_t state = setMode(SX127X_SLEEP);
// set LoRa mode
// set modem
state |= _mod->SPIsetRegValue(SX127X_REG_OP_MODE, modem, 7, 7, 5);
// set mode to STANDBY

View file

@ -615,7 +615,7 @@ class SX127x: public PhysicalLayer {
/*!
\brief Sets the %LoRa module to sleep to save power. %Module will not be able to transmit or receive any data while in sleep mode.
%Module will wake up autmatically when methods like transmit or receive are called.
%Module will wake up automatically when methods like transmit or receive are called.
\returns \ref status_codes
*/
@ -736,7 +736,7 @@ class SX127x: public PhysicalLayer {
/*!
\brief Gets frequency error of the latest received packet.
\param autoCorrect When set to true, frequency will be autmatically corrected.
\param autoCorrect When set to true, frequency will be automatically corrected.
\returns Frequency error in Hz.
*/
@ -784,7 +784,7 @@ class SX127x: public PhysicalLayer {
int16_t setRxBandwidth(float rxBw);
/*!
\brief Sets FSK sync word. Allowed sync words are up to 8 bytes long and can not conatain null bytes. Only available in FSK mode.
\brief Sets FSK sync word. Allowed sync words are up to 8 bytes long and can not contain null bytes. Only available in FSK mode.
\param syncWord Sync word array.