Skip to content

Commit 882f331

Browse files
committed
STM32 LPTICKER : optimize RTC wake up timer init
Division in a while loop is removed
1 parent 5523d53 commit 882f331

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

targets/TARGET_STM/rtc_api.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ uint32_t rtc_read_us(void)
332332

333333
void rtc_set_wake_up_timer(uint32_t delta)
334334
{
335+
#define RTC_CLOCK_US (((uint64_t)RTC_CLOCK << 32 ) / 1000000)
336+
337+
uint32_t WakeUpCounter;
338+
uint32_t WakeUpClock;
339+
335340
/* Ex for Wakeup period resolution with RTCCLK=32768 Hz :
336341
* RTCCLK_DIV2: ~122us < wakeup period < ~4s
337342
* RTCCLK_DIV4: ~244us < wakeup period < ~8s
@@ -340,28 +345,30 @@ void rtc_set_wake_up_timer(uint32_t delta)
340345
* CK_SPRE_16BITS: 1s < wakeup period < (0xFFFF+ 1) x 1 s = 65536 s (18 hours)
341346
* CK_SPRE_17BITS: 18h+1s < wakeup period < (0x1FFFF+ 1) x 1 s = 131072 s (36 hours)
342347
*/
343-
uint32_t WakeUpClock[6] = {RTC_WAKEUPCLOCK_RTCCLK_DIV2, RTC_WAKEUPCLOCK_RTCCLK_DIV4, RTC_WAKEUPCLOCK_RTCCLK_DIV8, RTC_WAKEUPCLOCK_RTCCLK_DIV16, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, RTC_WAKEUPCLOCK_CK_SPRE_17BITS};
344-
uint8_t ClockDiv[4] = {2, 4, 8, 16};
345-
uint32_t WakeUpCounter;
346-
uint8_t DivIndex = 0;
347-
348-
do {
349-
WakeUpCounter = delta / (ClockDiv[DivIndex] * 1000000 / RTC_CLOCK);
350-
DivIndex++;
351-
} while ( (WakeUpCounter > 0xFFFF) && (DivIndex < 4) );
352-
353-
if (WakeUpCounter > 0xFFFF) {
354-
WakeUpCounter = delta / 1000000;
355-
DivIndex++;
348+
if (delta < (0x10000 * 2 / RTC_CLOCK * 1000000) ) { // (0xFFFF + 1) * RTCCLK_DIV2 / RTC_CLOCK * 1s
349+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 1 ;
350+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV2;
351+
} else if (delta < (0x10000 * 4 / RTC_CLOCK * 1000000) ) {
352+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 2 ;
353+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV4;
354+
} else if (delta < (0x10000 * 8 / RTC_CLOCK * 1000000) ) {
355+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 3 ;
356+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV8;
357+
} else if (delta < (0x10000 * 16 / RTC_CLOCK * 1000000) ) {
358+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 4 ;
359+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV16;
360+
} else {
361+
WakeUpCounter = (delta / 1000000) ;
362+
WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
356363
}
357364

358365
irq_handler = (void (*)(void))lp_ticker_irq_handler;
359366
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
360367
NVIC_EnableIRQ(RTC_WKUP_IRQn);
361368

362369
RtcHandle.Instance = RTC;
363-
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex - 1]) != HAL_OK) {
364-
error("rtc_set_wake_up_timer init error (%d)\n", DivIndex);
370+
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, (uint32_t)WakeUpCounter, WakeUpClock) != HAL_OK) {
371+
error("rtc_set_wake_up_timer init error\n");
365372
}
366373
}
367374

0 commit comments

Comments
 (0)