Skip to content

Commit d7dfab5

Browse files
authored
Merge pull request #6370 from jeromecoutant/PR_LPT_RTC_OPTIM
STM32 LPTICKER : optimize RTC wake up timer init
2 parents 7b2ee2d + 882f331 commit d7dfab5

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
@@ -330,6 +330,11 @@ uint32_t rtc_read_us(void)
330330

331331
void rtc_set_wake_up_timer(uint32_t delta)
332332
{
333+
#define RTC_CLOCK_US (((uint64_t)RTC_CLOCK << 32 ) / 1000000)
334+
335+
uint32_t WakeUpCounter;
336+
uint32_t WakeUpClock;
337+
333338
/* Ex for Wakeup period resolution with RTCCLK=32768 Hz :
334339
* RTCCLK_DIV2: ~122us < wakeup period < ~4s
335340
* RTCCLK_DIV4: ~244us < wakeup period < ~8s
@@ -338,28 +343,30 @@ void rtc_set_wake_up_timer(uint32_t delta)
338343
* CK_SPRE_16BITS: 1s < wakeup period < (0xFFFF+ 1) x 1 s = 65536 s (18 hours)
339344
* CK_SPRE_17BITS: 18h+1s < wakeup period < (0x1FFFF+ 1) x 1 s = 131072 s (36 hours)
340345
*/
341-
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};
342-
uint8_t ClockDiv[4] = {2, 4, 8, 16};
343-
uint32_t WakeUpCounter;
344-
uint8_t DivIndex = 0;
345-
346-
do {
347-
WakeUpCounter = delta / (ClockDiv[DivIndex] * 1000000 / RTC_CLOCK);
348-
DivIndex++;
349-
} while ( (WakeUpCounter > 0xFFFF) && (DivIndex < 4) );
350-
351-
if (WakeUpCounter > 0xFFFF) {
352-
WakeUpCounter = delta / 1000000;
353-
DivIndex++;
346+
if (delta < (0x10000 * 2 / RTC_CLOCK * 1000000) ) { // (0xFFFF + 1) * RTCCLK_DIV2 / RTC_CLOCK * 1s
347+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 1 ;
348+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV2;
349+
} else if (delta < (0x10000 * 4 / RTC_CLOCK * 1000000) ) {
350+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 2 ;
351+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV4;
352+
} else if (delta < (0x10000 * 8 / RTC_CLOCK * 1000000) ) {
353+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 3 ;
354+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV8;
355+
} else if (delta < (0x10000 * 16 / RTC_CLOCK * 1000000) ) {
356+
WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 4 ;
357+
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV16;
358+
} else {
359+
WakeUpCounter = (delta / 1000000) ;
360+
WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
354361
}
355362

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

360367
RtcHandle.Instance = RTC;
361-
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex - 1]) != HAL_OK) {
362-
error("rtc_set_wake_up_timer init error (%d)\n", DivIndex);
368+
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, (uint32_t)WakeUpCounter, WakeUpClock) != HAL_OK) {
369+
error("rtc_set_wake_up_timer init error\n");
363370
}
364371
}
365372

0 commit comments

Comments
 (0)