@@ -40,59 +40,58 @@ void rtc_init(void)
4040 RCC_OscInitTypeDef RCC_OscInitStruct ;
4141 uint32_t rtc_freq = 0 ;
4242
43- RtcHandle .Instance = RTC ;
43+ RtcHandle .Instance = RTC ;
4444
45- // Enable Power clock
46- __PWR_CLK_ENABLE ();
45+ // Enable Power clock
46+ __PWR_CLK_ENABLE ();
4747
48- // Enable access to Backup domain
49- HAL_PWR_EnableBkUpAccess ();
48+ // Enable access to Backup domain
49+ HAL_PWR_EnableBkUpAccess ();
5050
51- // Reset Backup domain
52- __HAL_RCC_BACKUPRESET_FORCE ();
53- __HAL_RCC_BACKUPRESET_RELEASE ();
51+ // Reset Backup domain
52+ __HAL_RCC_BACKUPRESET_FORCE ();
53+ __HAL_RCC_BACKUPRESET_RELEASE ();
5454
55- // Enable LSE Oscillator
56- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
55+ // Enable LSE Oscillator
56+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
57+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
58+ RCC_OscInitStruct .LSEState = RCC_LSE_ON ; // External 32.768 kHz clock on OSC_IN/OSC_OUT
59+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) {
60+ // Connect LSE to RTC
61+ __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSE );
62+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
63+ rtc_freq = LSE_VALUE ;
64+ } else {
65+ // Enable LSI clock
66+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
5767 RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
58- RCC_OscInitStruct .LSEState = RCC_LSE_ON ; // External 32.768 kHz clock on OSC_IN/OSC_OUT
59- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) {
60- // Connect LSE to RTC
61- __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSE );
62- __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
63- rtc_freq = LSE_VALUE ;
64- } else {
65- // Enable LSI clock
66- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
67- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
68- RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
69- RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
70- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
71- error ("RTC error: LSI clock initialization failed." );
72- }
73- // Connect LSI to RTC
74- __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSI );
75- __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
76- // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
77- rtc_freq = LSI_VALUE ;
68+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
69+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
70+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
71+ error ("RTC error: LSI clock initialization failed." );
7872 }
73+ // Connect LSI to RTC
74+ __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSI );
75+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
76+ // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
77+ rtc_freq = LSI_VALUE ;
78+ }
7979
80- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) { // RTC initialization and status register (RTC_ISR), cold start (with no backup domain power) RTC reset value
81-
82- // Enable RTC
83- __HAL_RCC_RTC_ENABLE ();
80+ // Check if RTC is already initialized
81+ if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
8482
85- RtcHandle .Init .HourFormat = RTC_HOURFORMAT_24 ;
86- RtcHandle .Init .AsynchPrediv = 127 ;
87- RtcHandle .Init .SynchPrediv = (rtc_freq / 128 ) - 1 ;
88- RtcHandle .Init .OutPut = RTC_OUTPUT_DISABLE ;
89- RtcHandle .Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
90- RtcHandle .Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
83+ // Enable RTC
84+ __HAL_RCC_RTC_ENABLE ();
9185
92- if (HAL_RTC_Init (& RtcHandle ) != HAL_OK ) {
93- error ("RTC error: RTC initialization failed." );
94- }
86+ RtcHandle .Init .HourFormat = RTC_HOURFORMAT_24 ;
87+ RtcHandle .Init .AsynchPrediv = 127 ;
88+ RtcHandle .Init .SynchPrediv = (rtc_freq / 128 ) - 1 ;
89+ RtcHandle .Init .OutPut = RTC_OUTPUT_DISABLE ;
90+ RtcHandle .Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
91+ RtcHandle .Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
9592
93+ if (HAL_RTC_Init (& RtcHandle ) != HAL_OK ) {
94+ error ("RTC error: RTC initialization failed." );
9695 }
9796}
9897
0 commit comments