From 2c1c93c3e34347e4b9ffaa7076004cc6274bcb3e Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Tue, 15 Nov 2022 15:01:38 +0100 Subject: [PATCH] Removed cached RF parameters and added getters Signed-off-by: Federico Maggi --- src/modules/CC1101/CC1101.cpp | 34 ++++++++++++++----------- src/modules/CC1101/CC1101.h | 19 ++++++++------ src/modules/RF69/RF69.cpp | 47 +++++++++++++++++++++++++++-------- src/modules/RF69/RF69.h | 31 +++++++++++++++-------- 4 files changed, 87 insertions(+), 44 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index 6cd72ad7..d0a67f6f 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -492,10 +492,6 @@ int16_t CC1101::setRxBandwidth(float rxBw) { // set Rx channel filter bandwidth uint16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG4, (e << 6) | (m << 4), 7, 4); - if(state == RADIOLIB_ERR_NONE) { - _rxBw = rxBw; - } - return(state); } @@ -526,13 +522,28 @@ int16_t CC1101::setFrequencyDeviation(float freqDev) { int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_DEVIATN, (e << 4), 6, 4); state |= SPIsetRegValue(RADIOLIB_CC1101_REG_DEVIATN, m, 2, 0); - if(state == RADIOLIB_ERR_NONE) { - _freqDev = freqDev; + return(state); +} + +int16_t CC1101::getFrequencyDeviation(float *freqDev) { + // if ASK/OOK, deviation makes no sense + if (_modulation == RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK) { + *freqDev = 0.0; + + return(RADIOLIB_ERR_NONE); } + // get exponent and mantissa values from registers + uint8_t e = (uint8_t)SPIgetRegValue(RADIOLIB_CC1101_REG_DEVIATN, 6, 4); + uint8_t m = (uint8_t)SPIgetRegValue(RADIOLIB_CC1101_REG_DEVIATN, 2, 0); - - return(state); + // calculate frequency deviation (pag. 79 of the CC1101 datasheet): + // + // freqDev = (fXosc / 2^17) * (8 + m) * 2^e + // + *freqDev = (1000.0 / (uint32_t(1) << 17)) - (8 + m) * (uint32_t(1) << e); + + return(RADIOLIB_ERR_NONE); } int16_t CC1101::setOutputPower(int8_t power) { @@ -674,17 +685,10 @@ int16_t CC1101::setPreambleLength(uint8_t preambleLength) { } uint16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG1, value, 6, 4); - if(state == RADIOLIB_ERR_NONE) { - _preambleLen = preambleLength; - } return(state); } - return(state); -} - - int16_t CC1101::setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs) { RADIOLIB_CHECK_RANGE(numBroadcastAddrs, 1, 2, RADIOLIB_ERR_INVALID_NUM_BROAD_ADDRS); diff --git a/src/modules/CC1101/CC1101.h b/src/modules/CC1101/CC1101.h index 2c9a9714..8c2100f2 100644 --- a/src/modules/CC1101/CC1101.h +++ b/src/modules/CC1101/CC1101.h @@ -730,6 +730,15 @@ class CC1101: public PhysicalLayer { */ int16_t setFrequencyDeviation(float freqDev) override; + /*! + \brief Gets frequency deviation. + + \param[out] freqDev Pointer to variable where to save the frequency deviation. + + \returns \ref status_codes + */ + int16_t getFrequencyDeviation(float *freqDev); + /*! \brief Sets output power. Allowed values are -30, -20, -15, -10, 0, 5, 7 or 10 dBm. @@ -985,12 +994,6 @@ class CC1101: public PhysicalLayer { float _freq = RADIOLIB_CC1101_DEFAULT_FREQ; float _br = RADIOLIB_CC1101_DEFAULT_BR; - float _freqDev = RADIOLIB_CC1101_DEFAULT_FREQDEV; - float _rxBw = RADIOLIB_CC1101_DEFAULT_RXBW; - int8_t _power = RADIOLIB_CC1101_DEFAULT_POWER; - uint8_t _preambleLen = RADIOLIB_CC1101_DEFAULT_PREAMBLELEN; - //float _freq = 0; - //float _br = 0; uint8_t _rawRSSI = 0; uint8_t _rawLQI = 0; uint8_t _modulation = RADIOLIB_CC1101_MOD_FORMAT_2_FSK; @@ -1003,9 +1006,9 @@ class CC1101: public PhysicalLayer { bool _crcOn = true; bool _directMode = true; - //uint8_t _syncWordLength = 2; - //int8_t _power = 0; uint8_t _syncWordLength = RADIOLIB_CC1101_DEFAULT_SW_LEN; + int8_t _power = RADIOLIB_CC1101_DEFAULT_POWER; + int16_t config(); int16_t transmitDirect(bool sync, uint32_t frf); diff --git a/src/modules/RF69/RF69.cpp b/src/modules/RF69/RF69.cpp index 06ea791b..5187c54c 100644 --- a/src/modules/RF69/RF69.cpp +++ b/src/modules/RF69/RF69.cpp @@ -526,12 +526,26 @@ int16_t RF69::setFrequency(float freq) { setMode(RADIOLIB_RF69_STANDBY); //set carrier frequency + //FRF(23:0) = freq / Fstep = freq * (1 / Fstep) = freq * (2^19 / 32.0) (pag. 17 of datasheet) uint32_t FRF = (freq * (uint32_t(1) << RADIOLIB_RF69_DIV_EXPONENT)) / RADIOLIB_RF69_CRYSTAL_FREQ; _mod->SPIwriteRegister(RADIOLIB_RF69_REG_FRF_MSB, (FRF & 0xFF0000) >> 16); _mod->SPIwriteRegister(RADIOLIB_RF69_REG_FRF_MID, (FRF & 0x00FF00) >> 8); _mod->SPIwriteRegister(RADIOLIB_RF69_REG_FRF_LSB, FRF & 0x0000FF); - _freq = freq; + return(RADIOLIB_ERR_NONE); +} + +int16_t RF69::getFrequency(float *freq) { + uint32_t FRF = 0; + + //FRF(23:0) = [ [FRF_MSB]|[FRF_MID]|[FRF_LSB]] + //FRF(32:0) = [0x00|[FRF_MSB]|[FRF_MID]|[FRF_LSB]] + FRF |= (((uint32_t)(_mod->SPIgetRegValue(RADIOLIB_RF69_REG_FRF_MSB, 7, 0)) << 16) & 0x00FF0000); + FRF |= (((uint32_t)(_mod->SPIgetRegValue(RADIOLIB_RF69_REG_FRF_MID, 7, 0)) << 8) & 0x0000FF00); + FRF |= (((uint32_t)(_mod->SPIgetRegValue(RADIOLIB_RF69_REG_FRF_LSB, 7, 0)) << 0) & 0x000000FF); + + //freq = Fstep * FRF(23:0) = (32.0 / 2^19) * FRF(23:0) (pag. 17 of datasheet) + *freq = FRF * ( RADIOLIB_RF69_CRYSTAL_FREQ / (uint32_t(1) << RADIOLIB_RF69_DIV_EXPONENT) ); return(RADIOLIB_ERR_NONE); } @@ -575,7 +589,7 @@ int16_t RF69::setRxBandwidth(float rxBw) { // set Rx bandwidth state = _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RX_BW, (m << 3) | e, 4, 0); if(state == RADIOLIB_ERR_NONE) { - RF69::_rxBw = rxBw; + _rxBw = rxBw; } return(state); } @@ -602,16 +616,33 @@ int16_t RF69::setFrequencyDeviation(float freqDev) { // set frequency deviation from carrier frequency uint32_t base = 1; - uint32_t fdev = (newFreqDev * (base << 19)) / 32000; + uint32_t fdev = (newFreqDev * (uint32_t(1) << RADIOLIB_RF69_DIV_EXPONENT)) / 32000; int16_t state = _mod->SPIsetRegValue(RADIOLIB_RF69_REG_FDEV_MSB, (fdev & 0xFF00) >> 8, 5, 0); state |= _mod->SPIsetRegValue(RADIOLIB_RF69_REG_FDEV_LSB, fdev & 0x00FF, 7, 0); - if (state == RADIOLIB_ERR_NONE) { - _freqDev = freqDev; - } return(state); } +int16_t RF69::getFrequencyDeviation(float *freqDev) { + if (RF69::_ook) { + *freqDev = 0.0; + + return(RADIOLIB_ERR_NONE); + } + + // get raw value from register + uint32_t fdev = 0; + fdev |= (uint32_t)((_mod->SPIgetRegValue(RADIOLIB_RF69_REG_FDEV_MSB, 5, 0) << 8) & 0x0000FF00); + fdev |= (uint32_t)((_mod->SPIgetRegValue(RADIOLIB_RF69_REG_FDEV_LSB, 7, 0) << 0) & 0x000000FF); + + // calculate frequency deviation from raw value obtained from register + // Fdev = Fstep * Fdev(13:0) (pag. 20 of datasheet) + *freqDev = (fdev * RADIOLIB_RF69_CRYSTAL_FREQ) / + (uint32_t(1) << RADIOLIB_RF69_DIV_EXPONENT); + + return(RADIOLIB_ERR_NONE); +} + int16_t RF69::setOutputPower(int8_t power, bool highPower) { if(highPower) { RADIOLIB_CHECK_RANGE(power, -2, 20, RADIOLIB_ERR_INVALID_OUTPUT_POWER); @@ -686,10 +717,6 @@ int16_t RF69::setPreambleLength(uint8_t preambleLen) { _mod->SPIwriteRegister(RADIOLIB_RF69_REG_PREAMBLE_MSB, 0x00); uint16_t state = _mod->SPIsetRegValue(RADIOLIB_RF69_REG_PREAMBLE_LSB, preLenBytes); - if (state == RADIOLIB_ERR_NONE) { - _preambleLen = preambleLen; - } - return (state); } diff --git a/src/modules/RF69/RF69.h b/src/modules/RF69/RF69.h index de2624f1..777d8d04 100644 --- a/src/modules/RF69/RF69.h +++ b/src/modules/RF69/RF69.h @@ -736,6 +736,15 @@ class RF69: public PhysicalLayer { */ int16_t setFrequency(float freq); + /*! + \brief Gets carrier frequency. + + \param[out] freq Variable to write carrier frequency currently set, in MHz. + + \returns \ref status_codes + */ + int16_t getFrequency(float *freq); + /*! \brief Sets bit rate. Allowed values range from 1.2 to 300.0 kbps. @@ -763,6 +772,15 @@ class RF69: public PhysicalLayer { */ int16_t setFrequencyDeviation(float freqDev) override; + /*! + \brief Gets frequency deviation. + + \param[out] freqDev Where to write the frequency deviation currently set, in kHz. + + \returns \ref status_codes + */ + int16_t getFrequencyDeviation(float *freqDev); + /*! \brief Sets output power. Allowed values range from -18 to 13 dBm for low power modules (RF69C/CW) or -2 to 20 dBm (RF69H/HC/HCW). @@ -1052,28 +1070,19 @@ class RF69: public PhysicalLayer { protected: #endif - //float _freq = 0; - //float _br = 0; - //float _rxBw = 0; - //bool _ook = false; - //int16_t _tempOffset = 0; - //int8_t _power = 0; float _freq = RADIOLIB_RF69_DEFAULT_FREQ; float _br = RADIOLIB_RF69_DEFAULT_BR; - float _freqDev = RADIOLIB_RF69_DEFAULT_FREQDEV; float _rxBw = RADIOLIB_RF69_DEFAULT_RXBW; + bool _ook = false; + int16_t _tempOffset = 0; int8_t _power = RADIOLIB_RF69_DEFAULT_POWER; - uint8_t _preambleLen = RADIOLIB_RF69_DEFAULT_PREAMBLELEN; size_t _packetLength = 0; bool _packetLengthQueried = false; uint8_t _packetLengthConfig = RADIOLIB_RF69_PACKET_FORMAT_VARIABLE; bool _promiscuous = false; - bool _ook = false; - int16_t _tempOffset = 0; - //uint8_t _syncWordLength = 2; uint8_t _syncWordLength = RADIOLIB_RF69_DEFAULT_SW_LEN; bool _bitSync = true;