analogdevicesinc / msdk

Software Development Kit for Analog Device's MAX-series microcontrollers
Apache License 2.0
60 stars 75 forks source link

fix(BLE): PalTimer Calibration #951

Closed BrentK-ADI closed 3 months ago

BrentK-ADI commented 3 months ago

Updated the code for calibrating the PalTimer against the 32MHz clock. Original implementation only added adjustment on 100ms (PAL_TMR_CALIB_TIME) boundaries. In cases where the timer time was in between 100ms intervals, the timer could be severely early or late depending on how large usecDiff is. ` The math could be accomplished by simply running(expUsec * palTimerCb.usecDiff) / PAL_TMR_CALIB_TIME;` however there is not enough objective evidence for the max values of expUsec and usecDiff to show the multiplication won't overflow 32-bits. The math could be done in 64-bits, however the modulo method was used to keep this within a 32-bit scope.

EricB-ADI commented 3 months ago

Yeah this is good, I am wondering why you cast the uint32_t to an int. It seems intentional, but not obvious as to why?

BrentK-ADI commented 3 months ago

usecDiff can be negative, and the with the implicit conversion between the signed and unsigned types, I was getting incorrect values.

Looking at it though, int should probably be int32_t for consistency. Let me push up a change using the stdint type instead.

BrentK-ADI commented 3 months ago

Pushed up the wrong branch and made a big mess. 6f5ab8b should be good now.