Skip to content

Commit 2479411

Browse files
committed
adjust the tick in MIX mode to 256Hz
In Mix mode, the SSR is counting on a fqce APRE which is RTCCLK freq / 'PREDIV_A + 1) = 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 2dc74e4 commit 2479411

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
@@ -358,11 +358,12 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec)
358358
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */
359359

360360
/* USER CODE END TIMER_IF_Convert_ms2Tick */
361-
ret = ((uint32_t)((((uint64_t) timeMilliSec) << RTC_N_PREDIV_S) / 1000));
361+
ret = ((uint64_t)timeMilliSec * MS_TO_TICK) / 1000;
362+
362363
/* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */
363364

364365
/* USER CODE END TIMER_IF_Convert_ms2Tick_Last */
365-
return ret;
366+
return (uint32_t)ret;
366367
}
367368

368369
uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
@@ -371,7 +372,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick)
371372
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */
372373

373374
/* USER CODE END TIMER_IF_Convert_Tick2ms */
374-
ret = ((uint32_t)((((uint64_t)(tick)) * 1000) >> RTC_N_PREDIV_S));
375+
ret = tick * TICK_TO_MS;
375376
/* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */
376377

377378
/* USER CODE END TIMER_IF_Convert_Tick2ms_Last */
@@ -424,12 +425,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds)
424425

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

427-
seconds = (uint32_t)(ticks >> RTC_N_PREDIV_S);
428-
429-
ticks = (uint32_t) ticks & RTC_PREDIV_S;
430-
431-
*mSeconds = TIMER_IF_Convert_Tick2ms(ticks);
432-
428+
seconds = ticks / MS_TO_TICK;
429+
*mSeconds = (ticks * 1000) / MS_TO_TICK;
433430
/* USER CODE BEGIN TIMER_IF_GetTime_Last */
434431

435432
/* 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)