Skip to content

Commit cc45012

Browse files
committed
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). 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 <francois.ramu@st.com>
1 parent 46384ab commit cc45012

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/BSP/timer_if.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,12 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec)
318318
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */
319319

320320
/* USER CODE END TIMER_IF_Convert_ms2Tick */
321-
ret = ((uint32_t)((((uint64_t) timeMilliSec) << RTC_N_PREDIV_S) / 1000));
321+
ret = ((uint64_t)timeMilliSec * MS_TO_TICK) / 1000;
322+
322323
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */
323324

324325
/* USER CODE END TIMER_IF_Convert_ms2Tick_Last */
325-
return ret;
326+
return (uint32_t)ret;
326327
}
327328

328329
uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
@@ -331,7 +332,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
331332
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */
332333

333334
/* USER CODE END TIMER_IF_Convert_Tick2ms */
334-
ret = ((uint32_t)((((uint64_t)(tick)) * 1000) >> RTC_N_PREDIV_S));
335+
ret = tick * TICK_TO_MS;
335336
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */
336337

337338
/* USER CODE END TIMER_IF_Convert_Tick2ms_Last */
@@ -384,12 +385,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds)
384385

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

387-
seconds = (uint32_t)(ticks >> RTC_N_PREDIV_S);
388-
389-
ticks = (uint32_t) ticks & RTC_PREDIV_S;
390-
391-
*mSeconds = TIMER_IF_Convert_Tick2ms(ticks);
392-
388+
seconds = ticks / MS_TO_TICK;
389+
*mSeconds = (ticks * 1000) / MS_TO_TICK;
393390
/* USER CODE BEGIN TIMER_IF_GetTime_Last */
394391

395392
/* USER CODE END TIMER_IF_GetTime_Last */

src/BSP/timer_if.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ extern "C" {
3939
#define UTIL_TIMER_IRQ_MAP_INIT()
4040
#endif /* UTIL_TIMER_IRQ_MAP_INIT */
4141

42+
/*
43+
* With RTC (RtcHandle.Instance) clocked by LSE, the APRE freq is 256Hz (default)
44+
* (1 tick is 3.9ms (when APREDIV = 0x7F)
45+
* for other RTC clock freq, the formula is ck_apre = RTC_clock / (prediv_A +1)
46+
*/
47+
#define MS_TO_TICK \
48+
(uint32_t)(LL_RCC_GetRTCClockFreq() / (LL_RTC_GetAsynchPrescaler(RtcHandle.Instance) + 1))
49+
50+
/* Give one more (to adjust to x3.9 factor) */
51+
#define TICK_TO_MS ((1000/MS_TO_TICK) + 1)
52+
4253
void UTIL_TIMER_IRQ_MAP_PROCESS(void *data);
4354

4455
/* USER CODE END Includes */

0 commit comments

Comments
 (0)