Skip to content

Commit d2da46d

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)
1 parent 5ade244 commit d2da46d

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ extern "C" {
3939
#define UTIL_TIMER_IRQ_MAP_INIT()
4040
#endif /* UTIL_TIMER_IRQ_MAP_INIT */
4141

42+
/* With RTC clocked by LSE, the APRE freq is 256Hz: 1 tick is 3.9ms (APREDIV = 0x7F) */
43+
#define MS_TO_TICK 256
44+
/* Give one more (to adjust to x3.9 factor) */
45+
#define TICK_TO_MS ((1000/256) + 1)
46+
4247
void UTIL_TIMER_IRQ_MAP_PROCESS(void *data);
4348

4449
/* USER CODE END Includes */

0 commit comments

Comments
 (0)