From e362a80d45815677bdc716608a28e57a832fd472 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sun, 22 Oct 2023 01:30:36 +0200 Subject: [PATCH] Fixed truncation error in AT1846S setFrequency() Fixed error in compuation of values for AT1846S frequency registers causing the output frequency to have an effective resolution of 1kHz instead of 62.5Hz. --- platform/drivers/baseband/AT1846S.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/platform/drivers/baseband/AT1846S.h b/platform/drivers/baseband/AT1846S.h index 8709457b..a1a46b57 100644 --- a/platform/drivers/baseband/AT1846S.h +++ b/platform/drivers/baseband/AT1846S.h @@ -101,8 +101,12 @@ public: */ void setFrequency(const freq_t freq) { - // The value to be written in registers is given by: 0.0016*freqency - uint32_t val = (freq/1000)*16; + // AT1846S datasheet specifies a frequency step of 1/16th of kHz per bit. + // Computation of register value is done using 64 bit to avoid overflows, + // result is then truncated to 32 bits to fit it into the registers. + uint64_t val = ((uint64_t) freq * 16) / 1000; + val &= 0xFFFFFFFF; + uint16_t fHi = (val >> 16) & 0xFFFF; uint16_t fLo = val & 0xFFFF;