@@ -40,58 +40,59 @@ void rtc_init(void)
40
40
RCC_OscInitTypeDef RCC_OscInitStruct ;
41
41
uint32_t rtc_freq = 0 ;
42
42
43
- RtcHandle .Instance = RTC ;
43
+ RtcHandle .Instance = RTC ;
44
44
45
- // Enable Power clock
46
- __PWR_CLK_ENABLE ();
45
+ // Enable Power clock
46
+ __PWR_CLK_ENABLE ();
47
47
48
- // Enable access to Backup domain
49
- HAL_PWR_EnableBkUpAccess ();
48
+ // Enable access to Backup domain
49
+ HAL_PWR_EnableBkUpAccess ();
50
50
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 ();
54
54
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 ;
55
+ // Enable LSE Oscillator
56
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
67
57
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." );
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 ;
72
78
}
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
- }
79
79
80
- // Check if RTC is already initialized
81
- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
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 ();
82
84
83
- // Enable RTC
84
- __HAL_RCC_RTC_ENABLE ();
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 ;
85
91
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 ;
92
+ if (HAL_RTC_Init (& RtcHandle ) != HAL_OK ) {
93
+ error ("RTC error: RTC initialization failed." );
94
+ }
92
95
93
- if (HAL_RTC_Init (& RtcHandle ) != HAL_OK ) {
94
- error ("RTC error: RTC initialization failed." );
95
96
}
96
97
}
97
98
0 commit comments