|
36 | 36 | static RTC_HandleTypeDef RtcHandle;
|
37 | 37 |
|
38 | 38 | #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 | + |
39 | 42 | static void (*irq_handler)(void);
|
40 | 43 | static void RTC_IRQHandler(void);
|
| 44 | +static uint32_t lp_TickPeriod_us = GET_TICK_PERIOD(4095); /* default SynchPrediv value = 4095 */ |
41 | 45 | #endif /* DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM */
|
42 | 46 |
|
43 | 47 | void rtc_init(void)
|
@@ -123,6 +127,10 @@ void rtc_init(void)
|
123 | 127 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
124 | 128 | #endif /* TARGET_STM32F1 */
|
125 | 129 |
|
| 130 | +#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM |
| 131 | + lp_TickPeriod_us = GET_TICK_PERIOD(RtcHandle.Init.SynchPrediv); |
| 132 | +#endif |
| 133 | + |
126 | 134 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
|
127 | 135 | error("RTC initialization failed");
|
128 | 136 | }
|
@@ -309,9 +317,27 @@ static void RTC_IRQHandler(void)
|
309 | 317 | }
|
310 | 318 | }
|
311 | 319 |
|
312 |
| -uint32_t rtc_read_subseconds(void) |
| 320 | +uint32_t rtc_read_us(void) |
313 | 321 | {
|
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 ; |
315 | 341 | }
|
316 | 342 |
|
317 | 343 | void rtc_set_wake_up_timer(uint32_t delta)
|
|
0 commit comments