Skip to content

Commit 4159906

Browse files
mrschusterfpistm
authored andcommitted
fix: function TIMER_IF_Convert_Tick2ms() returned wrong value
The order of the computation is important: first multiply by 1000, then divide, then add 1. It was: compute (1000/256+1) as int, which is 4, and then use the 4 as factor. Hence, the conversion of ms was quite off the real value. Also rename MS_TO_TICK, since it does not do that. Fixes #40.
1 parent f00cbc1 commit 4159906

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/BSP/timer_if.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,9 @@ const UTIL_SYSTIM_Driver_s UTIL_SYSTIMDriver =
114114
* (1 tick is 3.9ms (when APREDIV = 0x7F)
115115
* for other RTC clock freq, the formula is ck_apre = RTC_clock / (prediv_A +1)
116116
*/
117-
#define MS_TO_TICK \
117+
#define CK_APRE \
118118
(uint32_t)(LL_RCC_GetRTCClockFreq() / (LL_RTC_GetAsynchPrescaler(hrtc->Instance) + 1))
119119

120-
/* Give one more (to adjust to x3.9 factor) */
121-
#define TICK_TO_MS ((1000/MS_TO_TICK) + 1)
122-
123120
/* USER CODE BEGIN PD */
124121

125122
/* USER CODE END PD */
@@ -237,7 +234,7 @@ UTIL_TIMER_Status_t TIMER_IF_StartTimer(uint32_t timeout)
237234
TIMER_IF_DBG_PRINTF("Start timer: time=%d, alarm=%d\n\r", GetTimerTicks(), timeout);
238235

239236
/* Program ALARM B on timeout ticks converted in ms (one more for uncertainty, mask is 31 */
240-
uint64_t subSeconds64 = ((uint64_t)((uint64_t)timeout * (uint64_t)(1000))) / MS_TO_TICK + 1;
237+
uint64_t subSeconds64 = ((uint64_t)((uint64_t)timeout * (uint64_t)(1000))) / CK_APRE + 1;
241238
RTC_StartAlarm64(RTC_ALARM_B, 0, 0, 0, 0, subSeconds64, RTC_HOURFORMAT12_PM, 31UL);
242239

243240
/* USER CODE BEGIN TIMER_IF_StartTimer_Last */
@@ -335,7 +332,7 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec)
335332
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */
336333

337334
/* USER CODE END TIMER_IF_Convert_ms2Tick */
338-
ret = ((uint32_t)(((uint64_t)timeMilliSec * MS_TO_TICK) / 1000));
335+
ret = ((uint32_t)(((uint64_t)timeMilliSec * CK_APRE) / 1000));
339336
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */
340337

341338
/* USER CODE END TIMER_IF_Convert_ms2Tick_Last */
@@ -348,7 +345,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
348345
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */
349346

350347
/* USER CODE END TIMER_IF_Convert_Tick2ms */
351-
ret = tick * TICK_TO_MS;
348+
ret = (uint32_t)((uint64_t)tick * 1000 / CK_APRE + 1);
352349
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */
353350

354351
/* USER CODE END TIMER_IF_Convert_Tick2ms_Last */
@@ -395,8 +392,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds)
395392

396393
ticks = (((uint64_t) timerValueMSB) << 32) + timerValueLsb;
397394

398-
seconds = ticks / MS_TO_TICK;
399-
*mSeconds = (ticks * 1000) / MS_TO_TICK;
395+
seconds = ticks / CK_APRE;
396+
*mSeconds = (ticks * 1000) / CK_APRE;
400397
/* USER CODE BEGIN TIMER_IF_GetTime_Last */
401398

402399
/* USER CODE END TIMER_IF_GetTime_Last */

0 commit comments

Comments
 (0)