|
10 | 10 | * 2021-02-05 Meco Man fix the problem of mixing local time and UTC time |
11 | 11 | * 2021-07-05 iysheng implement RTC framework V2.0 |
12 | 12 | * 2025-06-05 RCSN add local time conversion for get timeval and set stamp |
| 13 | + * 0206-02-03 wdfk_prog compute tv_usec from SecondFraction/SubSeconds |
13 | 14 | */ |
14 | 15 |
|
15 | 16 | #include "board.h" |
@@ -85,10 +86,30 @@ static rt_err_t stm32_rtc_get_timeval(struct timeval *tv) |
85 | 86 | #else |
86 | 87 | tv->tv_sec = timegm(&tm_new); |
87 | 88 | #endif |
88 | | -#if defined(SOC_SERIES_STM32H7) |
89 | | - tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0; |
90 | | -#endif |
91 | | - |
| 89 | + tv->tv_usec = 0U; |
| 90 | +/* F1 RTC does not have SSR/PRER */ |
| 91 | +#if defined(RTC_SSR_SS) && defined(RTC_PRER_PREDIV_S) |
| 92 | + /* |
| 93 | + * You can use SubSeconds and SecondFraction (sTime structure fields |
| 94 | + * returned) to convert SubSeconds value in second fraction ratio with |
| 95 | + * time unit following generic formula: |
| 96 | + * Second fraction ratio * time_unit = |
| 97 | + * [(SecondFraction - SubSeconds) / (SecondFraction + 1)] * time_unit |
| 98 | + * This conversion can be performed only if no shift operation is pending |
| 99 | + * (ie. SHFP=0) when PREDIV_S >= SS |
| 100 | + */ |
| 101 | +#if defined(RTC_ISR_SHPF) |
| 102 | + if (READ_BIT(RTC->ISR, RTC_ISR_SHPF) == 0U) |
| 103 | +#endif /* RTC_ISR_SHPF */ |
| 104 | + { |
| 105 | + uint32_t sf = RTC_TimeStruct.SecondFraction; |
| 106 | + uint32_t ss = RTC_TimeStruct.SubSeconds; |
| 107 | + if ((sf != 0U) && (ss <= sf)) |
| 108 | + { |
| 109 | + tv->tv_usec = (uint32_t)(((sf - ss) * 1000000ULL) / (sf + 1U)); |
| 110 | + } |
| 111 | + } |
| 112 | +#endif /* defined(RTC_SSR_SS) && defined(RTC_PRER_PREDIV_S) */ |
92 | 113 | return RT_EOK; |
93 | 114 | } |
94 | 115 |
|
|
0 commit comments