Skip to content

Commit 326c69c

Browse files
FRASTMfpistm
authored andcommitted
chore: adjust the tick in MIX mode depending on the rtc clock
In Mix mode, the SSR is counting on a fqce APRE which is RTCCLK freq / 'PREDIV_A + 1). For example for Nucleo_WL55JC, this value is 256Hz when RTC is clocked by LSE. This frqe gives a tick of 3.9ms (= 1/256 * 1000) Signed-off-by: Francois Ramu <[email protected]>
1 parent a44fa46 commit 326c69c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/BSP/timer_if.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ const UTIL_SYSTIM_Driver_s UTIL_SYSTIMDriver =
110110
#define UTIL_TIMER_IRQ_MAP_INIT()
111111
#endif /* UTIL_TIMER_IRQ_MAP_INIT */
112112

113+
/*
114+
* With RTC (RtcHandle.Instance) clocked by LSE, the APRE freq is 256Hz (default)
115+
* (1 tick is 3.9ms (when APREDIV = 0x7F)
116+
* for other RTC clock freq, the formula is ck_apre = RTC_clock / (prediv_A +1)
117+
*/
118+
#define MS_TO_TICK \
119+
(uint32_t)(LL_RCC_GetRTCClockFreq() / (LL_RTC_GetAsynchPrescaler(hrtc->Instance) + 1))
120+
121+
/* Give one more (to adjust to x3.9 factor) */
122+
#define TICK_TO_MS ((1000/MS_TO_TICK) + 1)
123+
113124
/* USER CODE BEGIN PD */
114125

115126
/* USER CODE END PD */
@@ -324,7 +335,7 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec)
324335
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */
325336

326337
/* USER CODE END TIMER_IF_Convert_ms2Tick */
327-
ret = ((uint32_t)((((uint64_t) timeMilliSec) << RTC_N_PREDIV_S) / 1000));
338+
ret = ((uint32_t)(((uint64_t)timeMilliSec * MS_TO_TICK) / 1000));
328339
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */
329340

330341
/* USER CODE END TIMER_IF_Convert_ms2Tick_Last */
@@ -337,7 +348,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
337348
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */
338349

339350
/* USER CODE END TIMER_IF_Convert_Tick2ms */
340-
ret = ((uint32_t)((((uint64_t)(tick)) * 1000) >> RTC_N_PREDIV_S));
351+
ret = tick * TICK_TO_MS;
341352
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */
342353

343354
/* USER CODE END TIMER_IF_Convert_Tick2ms_Last */
@@ -390,12 +401,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds)
390401

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

393-
seconds = (uint32_t)(ticks >> RTC_N_PREDIV_S);
394-
395-
ticks = (uint32_t) ticks & RTC_PREDIV_S;
396-
397-
*mSeconds = TIMER_IF_Convert_Tick2ms(ticks);
398-
404+
seconds = ticks / MS_TO_TICK;
405+
*mSeconds = (ticks * 1000) / MS_TO_TICK;
399406
/* USER CODE BEGIN TIMER_IF_GetTime_Last */
400407

401408
/* USER CODE END TIMER_IF_GetTime_Last */

0 commit comments

Comments
 (0)