Skip to content

Commit

Permalink
fix: function TIMER_IF_Convert_Tick2ms() returned wrong value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrschuster authored and fpistm committed Nov 27, 2024
1 parent f00cbc1 commit 4159906
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/BSP/timer_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,9 @@ const UTIL_SYSTIM_Driver_s UTIL_SYSTIMDriver =
* (1 tick is 3.9ms (when APREDIV = 0x7F)
* for other RTC clock freq, the formula is ck_apre = RTC_clock / (prediv_A +1)
*/
#define MS_TO_TICK \
#define CK_APRE \
(uint32_t)(LL_RCC_GetRTCClockFreq() / (LL_RTC_GetAsynchPrescaler(hrtc->Instance) + 1))

/* Give one more (to adjust to x3.9 factor) */
#define TICK_TO_MS ((1000/MS_TO_TICK) + 1)

/* USER CODE BEGIN PD */

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

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

/* USER CODE BEGIN TIMER_IF_StartTimer_Last */
Expand Down Expand Up @@ -335,7 +332,7 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec)
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */

/* USER CODE END TIMER_IF_Convert_ms2Tick */
ret = ((uint32_t)(((uint64_t)timeMilliSec * MS_TO_TICK) / 1000));
ret = ((uint32_t)(((uint64_t)timeMilliSec * CK_APRE) / 1000));
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */

/* USER CODE END TIMER_IF_Convert_ms2Tick_Last */
Expand All @@ -348,7 +345,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */

/* USER CODE END TIMER_IF_Convert_Tick2ms */
ret = tick * TICK_TO_MS;
ret = (uint32_t)((uint64_t)tick * 1000 / CK_APRE + 1);
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */

/* USER CODE END TIMER_IF_Convert_Tick2ms_Last */
Expand Down Expand Up @@ -395,8 +392,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds)

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

seconds = ticks / MS_TO_TICK;
*mSeconds = (ticks * 1000) / MS_TO_TICK;
seconds = ticks / CK_APRE;
*mSeconds = (ticks * 1000) / CK_APRE;
/* USER CODE BEGIN TIMER_IF_GetTime_Last */

/* USER CODE END TIMER_IF_GetTime_Last */
Expand Down

0 comments on commit 4159906

Please sign in to comment.