@@ -46,36 +46,42 @@ void rtc_init(void)
46
46
47
47
RtcHandle .Instance = RTC ;
48
48
49
- // Enable Power clock
50
- __HAL_RCC_PWR_CLK_ENABLE ();
51
-
52
- // Enable access to Backup domain
53
- HAL_PWR_EnableBkUpAccess ();
54
-
55
- // Reset Backup domain
56
- __HAL_RCC_BACKUPRESET_FORCE ();
57
- __HAL_RCC_BACKUPRESET_RELEASE ();
58
-
49
+ #if !DEVICE_RTC_LSI
59
50
// Enable LSE Oscillator
60
51
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
61
52
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
62
53
RCC_OscInitStruct .LSEState = RCC_LSE_ON ; // External 32.768 kHz clock on OSC_IN/OSC_OUT
63
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) {
54
+ RCC_OscInitStruct .LSIState = RCC_LSI_OFF ;
55
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) { // Check if LSE has started correctly
64
56
// Connect LSE to RTC
65
57
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
66
58
} else {
67
- // Enable LSI clock
68
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
69
- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
70
- RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
71
- RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
72
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
73
- error ("RTC error: LSI clock initialization failed." );
74
- }
75
- // Connect LSI to RTC
76
- __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
59
+ error ("Cannot initialize RTC with LSE\n" );
77
60
}
61
+ #else
62
+ // Enable Power clock
63
+ __HAL_RCC_PWR_CLK_ENABLE ();
64
+
65
+ // Enable access to Backup domain
66
+ HAL_PWR_EnableBkUpAccess ();
78
67
68
+ // Reset Backup domain
69
+ __HAL_RCC_BACKUPRESET_FORCE ();
70
+ __HAL_RCC_BACKUPRESET_RELEASE ();
71
+
72
+ // Enable LSI clock
73
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
74
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
75
+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
76
+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
77
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
78
+ error ("Cannot initialize RTC with LSI\n" );
79
+ }
80
+ // Connect LSI to RTC
81
+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
82
+ // This value is LSI typical value. To be measured precisely using a timer input capture for example.
83
+ #endif
84
+
79
85
// Enable RTC
80
86
__HAL_RCC_RTC_ENABLE ();
81
87
@@ -88,6 +94,7 @@ void rtc_init(void)
88
94
89
95
void rtc_free (void )
90
96
{
97
+ #if DEVICE_RTC_LSI
91
98
// Enable Power clock
92
99
__PWR_CLK_ENABLE ();
93
100
@@ -100,6 +107,7 @@ void rtc_free(void)
100
107
101
108
// Disable access to Backup domain
102
109
HAL_PWR_DisableBkUpAccess ();
110
+ #endif
103
111
104
112
// Disable LSI and LSE clocks
105
113
RCC_OscInitTypeDef RCC_OscInitStruct ;
0 commit comments