From b1c6d0e9f1a1bece8203a0bdd3e62fa720b022df Mon Sep 17 00:00:00 2001 From: dhineshkumarmcci Date: Fri, 10 Jul 2020 21:38:44 +0530 Subject: [PATCH] Fix #259: added code snippet to apply better Calibration value --- .../CatenaStm32L0_CalibrateSystemClock.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib/stm32/stm32l0/CatenaStm32L0_CalibrateSystemClock.cpp b/src/lib/stm32/stm32l0/CatenaStm32L0_CalibrateSystemClock.cpp index d8affa0..72341f7 100644 --- a/src/lib/stm32/stm32l0/CatenaStm32L0_CalibrateSystemClock.cpp +++ b/src/lib/stm32/stm32l0/CatenaStm32L0_CalibrateSystemClock.cpp @@ -253,6 +253,25 @@ uint32_t CatenaStm32L0::CalibrateSystemClock(void) } while ((Calib != CalibNew) && (! fHaveSeenLow || !fHaveSeenHigh)); + // + // Check which calibration value is better, + // with lower error in PPM + // + if (fHaveSeenLow && fHaveSeenHigh) + { + uint32_t mSecondHighDiff; + uint32_t mSecondLowDiff; + + mSecondHighDiff = (mSecondHigh - 1000); + mSecondLowDiff = (1000 - mSecondLow); + + if (mSecondHighDiff > mSecondLowDiff) + fHaveSeenHigh = false; + + else if (mSecondHighDiff <= mSecondLowDiff) + fHaveSeenLow = false; + } + // // We are going to take higher calibration value first and // it allows us not to call LMIC_setClockError().