1+ #include "rtconfig.h"
12#include "drivers/rtc.h"
23#include "drivers/rtc_private.h"
34#include "drivers/stm32f2/rtc_calibration.h"
@@ -32,7 +33,7 @@ static RTC_HandleTypeDef RTC_Handler = {
3233
3334typedef uint32_t RtcIntervalTicks ;
3435
35- static const unsigned int LSE_FREQUENCY_HZ = 32768 ;
36+ // static const unsigned int LSE_FREQUENCY_HZ = 32768;
3637#define SECONDS_IN_A_DAY (60 * 60 * 24)
3738#define TICKS_IN_AN_INTERVAL SECONDS_IN_A_DAY
3839
@@ -157,12 +158,47 @@ static void initialize_fast_mode_state(void) {
157158 //save_rtc_time_state(0);
158159}
159160
161+ uint32_t rtc_get_lpcycle ()
162+ {
163+ uint32_t value ;
164+ #ifdef BF0_HCPU
165+ value = HAL_Get_backup (RTC_BACKUP_LPCYCLE_AVE );
166+ if (value == 0 )
167+ value = 1200000 ;
168+ value += BSP_RTC_PPM ; // Calibrate in initial with 8 cycle
169+ HAL_Set_backup (RTC_BACKUP_LPCYCLE , (uint32_t )value );
170+ #else
171+ value = HAL_Get_backup (RTC_BACKUP_LPCYCLE );
172+ #endif
173+ return value ;
174+ }
175+
176+ static uint32_t soft_rc10_backup = 0 ;
177+ static uint16_t rc10_freq_khz ;
178+
179+ void drv_set_soft_rc10_backup (uint32_t backup )
180+ {
181+ soft_rc10_backup = backup ;
182+ /* RC10K freq_khz = 48000 * LXT_LP_CYCLE / v; */
183+ rc10_freq_khz = (uint16_t )(48000UL * LXT_LP_CYCLE / backup );
184+ }
185+
186+ void rtc_rc10_calculate_div (RTC_HandleTypeDef * hdl , uint32_t value )
187+ {
188+ hdl -> Init .DivB = RC10K_SUB_SEC_DIVB ;
189+
190+ // 1 seconds has total 1/(x/(48*8))/256=1.5M/x cycles, times 2^14 for DIVA
191+ uint32_t divider = RTC_Handler .Init .DivB * value ;
192+ value = ((uint64_t )48000000 * LXT_LP_CYCLE * (1 << 14 ) + (divider >> 1 )) / divider ;
193+ hdl -> Init .DivAInt = (uint32_t )(value >> 14 );
194+ hdl -> Init .DivAFrac = (uint32_t )(value & ((1 << 14 ) - 1 ));
195+ }
196+
160197void rtc_init (void ) {
161198 periph_config_acquire_lock ();
162199 rtc_enable_backup_regs ();
163- #ifdef LXT_DISABLE
164- #error "RTC LXT is disabled, but RTC init requires it to be enabled"
165- #endif
200+
201+ #ifndef LXT_DISABLE
166202#ifdef SF32LB52X
167203 HAL_PMU_EnableXTAL32 ();
168204 if (HAL_PMU_LXTReady () != HAL_OK )
@@ -174,11 +210,23 @@ void rtc_init(void) {
174210 WTF ;
175211 }
176212 HAL_RTC_ENABLE_LXT ();
177-
213+ #endif
214+
178215 RTC_Handler .Init .DivAInt = 0x80 ;
179216 RTC_Handler .Init .DivAFrac = 0x0 ;
180217 RTC_Handler .Init .DivB = 0x100 ;
181218 uint32_t wakesrc = RTC_INIT_NORMAL ;
219+
220+ #ifdef LXT_DISABLE
221+ {
222+ uint64_t value = 0 ;
223+ value = rtc_get_lpcycle ();
224+ drv_set_soft_rc10_backup ((uint32_t )value );
225+ if (value )
226+ rtc_rc10_calculate_div (& RTC_Handler , value );
227+ }
228+ #endif
229+
182230 if (HAL_RTC_Init (& RTC_Handler , wakesrc ) != HAL_OK )
183231 {
184232 WTF ;
0 commit comments