Skip to content

Commit c24eb5b

Browse files
authored
Merge pull request #5862 from jeromecoutant/PR_LPT_ISSUE
STM32 LPT optimisation
2 parents bb8ab66 + 9fb865a commit c24eb5b

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

targets/TARGET_STM/lp_ticker.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,8 @@ void lp_ticker_init(void)
292292

293293
uint32_t lp_ticker_read(void)
294294
{
295-
uint32_t usecs = 0;
296-
time_t time = 0;
297-
298-
do {
299-
time = rtc_read();
300-
usecs = rtc_read_subseconds();
301-
} while (time != rtc_read());
302-
303-
return (time * 1000000) + usecs;
295+
uint32_t usecs = rtc_read_us();
296+
return usecs;
304297
}
305298

306299
void lp_ticker_set_interrupt(timestamp_t timestamp)

targets/TARGET_STM/rtc_api.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
static RTC_HandleTypeDef RtcHandle;
3737

3838
#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
39+
40+
#define GET_TICK_PERIOD(VALUE) (2048 * 1000000 / VALUE) /* 1s / SynchPrediv value * 2^11 (value to get the maximum precision value with no u32 overflow) */
41+
3942
static void (*irq_handler)(void);
4043
static void RTC_IRQHandler(void);
44+
static uint32_t lp_TickPeriod_us = GET_TICK_PERIOD(4095); /* default SynchPrediv value = 4095 */
4145
#endif /* DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM */
4246

4347
void rtc_init(void)
@@ -123,6 +127,10 @@ void rtc_init(void)
123127
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
124128
#endif /* TARGET_STM32F1 */
125129

130+
#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
131+
lp_TickPeriod_us = GET_TICK_PERIOD(RtcHandle.Init.SynchPrediv);
132+
#endif
133+
126134
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
127135
error("RTC initialization failed");
128136
}
@@ -309,9 +317,27 @@ static void RTC_IRQHandler(void)
309317
}
310318
}
311319

312-
uint32_t rtc_read_subseconds(void)
320+
uint32_t rtc_read_us(void)
313321
{
314-
return 1000000.f * ((double)((RTC->PRER & RTC_PRER_PREDIV_S) - RTC->SSR) / ((RTC->PRER & RTC_PRER_PREDIV_S) + 1));
322+
RTC_TimeTypeDef timeStruct = {0};
323+
RTC_DateTypeDef dateStruct = {0};
324+
325+
RtcHandle.Instance = RTC;
326+
HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
327+
328+
/* Reading RTC current time locks the values in calendar shadow registers until Current date is read
329+
to ensure consistency between the time and date values */
330+
HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
331+
332+
if (timeStruct.SubSeconds > timeStruct.SecondFraction) {
333+
/* SS can be larger than PREDIV_S only after a shift operation. In that case, the correct
334+
time/date is one second less than as indicated by RTC_TR/RTC_DR. */
335+
timeStruct.Seconds -= 1;
336+
}
337+
uint32_t RTCTime = timeStruct.Seconds + timeStruct.Minutes * 60 + timeStruct.Hours * 60 * 60;
338+
uint32_t Time_us = ((timeStruct.SecondFraction - timeStruct.SubSeconds) * lp_TickPeriod_us) >> 11;
339+
340+
return (RTCTime * 1000000) + Time_us ;
315341
}
316342

317343
void rtc_set_wake_up_timer(uint32_t delta)

targets/TARGET_STM/rtc_api_hal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ extern "C" {
5151
#define RTC_CLOCK LSI_VALUE
5252
#endif
5353

54-
/** Read the subsecond register.
54+
/** Read RTC time with subsecond precision.
5555
*
56-
* @return The remaining time as microseconds (0-999999)
56+
* @return Time is microsecond
5757
*/
58-
uint32_t rtc_read_subseconds(void);
58+
uint32_t rtc_read_us(void);
5959

6060
/** Program a wake up timer event in delta microseconds.
6161
*

0 commit comments

Comments
 (0)