From 0e5e4f1673d555f5a5b8aa70fce062554095cf97 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 25 Jul 2021 20:50:42 +0200 Subject: [PATCH] [Si443x] Fixed rxosr calculation (#199) --- src/modules/Si443x/Si443x.cpp | 36 ++++++++++------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/modules/Si443x/Si443x.cpp b/src/modules/Si443x/Si443x.cpp index 8de98ca2..69111f4a 100644 --- a/src/modules/Si443x/Si443x.cpp +++ b/src/modules/Si443x/Si443x.cpp @@ -677,26 +677,18 @@ int16_t Si443x::updateClockRecovery() { uint8_t manch = _mod->SPIgetRegValue(SI443X_REG_MODULATION_MODE_CONTROL_1, 1, 1) >> 1; // calculate oversampling ratio, NCO offset and clock recovery gain - float rxOsr = ((float)(500 * (1 + 2*bypass))) / (((float)((uint16_t)(1) << decRate)) * _br * ((float)(1 + manch))); + int8_t ndecExp = (int8_t)decRate - 3; + float ndec = 0; + if(ndecExp > 0) { + ndec = (uint16_t)1 << ndecExp; + } else { + ndecExp *= -1; + ndec = 1.0/(float)((uint16_t)1 << ndecExp); + } + float rxOsr = ((float)(500 * (1 + 2*bypass))) / (ndec * _br * ((float)(1 + manch))); uint32_t ncoOff = (_br * (1 + manch) * ((uint32_t)(1) << (20 + decRate))) / (500 * (1 + 2*bypass)); uint16_t crGain = 2 + (((float)(65536.0 * (1 + manch)) * _br) / (rxOsr * (_freqDev / 0.625))); - - // convert oversampling ratio from float to fixed point - uint8_t rxOsr_int = (uint8_t)rxOsr; - uint8_t rxOsr_dec = 0; - float rxOsr_temp = rxOsr; - if((rxOsr_temp - rxOsr_int) >= 0.5) { - rxOsr_dec |= 0x04; - rxOsr_temp -= 0.5; - } - if((rxOsr_temp - rxOsr_int) >= 0.25) { - rxOsr_dec |= 0x02; - rxOsr_temp -= 0.25; - } - if((rxOsr_temp - rxOsr_int) >= 0.125) { - rxOsr_dec |= 0x01; - } - uint16_t rxOsr_fixed = ((uint16_t)rxOsr_int << 3) | ((uint16_t)rxOsr_dec); + uint16_t rxOsr_fixed = (uint16_t)rxOsr; // print that whole mess RADIOLIB_DEBUG_PRINTLN(bypass, HEX); @@ -704,14 +696,6 @@ int16_t Si443x::updateClockRecovery() { RADIOLIB_DEBUG_PRINTLN(manch, HEX); RADIOLIB_DEBUG_PRINT(rxOsr, 3); RADIOLIB_DEBUG_PRINT('\t'); - RADIOLIB_DEBUG_PRINT(rxOsr_int); - RADIOLIB_DEBUG_PRINT('\t'); - RADIOLIB_DEBUG_PRINT(rxOsr_int, HEX); - RADIOLIB_DEBUG_PRINT('\t'); - RADIOLIB_DEBUG_PRINT(rxOsr_dec); - RADIOLIB_DEBUG_PRINT('\t'); - RADIOLIB_DEBUG_PRINT(rxOsr_dec, HEX); - RADIOLIB_DEBUG_PRINT('\t'); RADIOLIB_DEBUG_PRINT(rxOsr_fixed); RADIOLIB_DEBUG_PRINT('\t'); RADIOLIB_DEBUG_PRINTLN(rxOsr_fixed, HEX);