33
33
34
34
#include "mbed_error.h"
35
35
36
+ #if DEVICE_RTC_LSI
36
37
static int rtc_inited = 0 ;
38
+ #endif
37
39
38
40
static RTC_HandleTypeDef RtcHandle ;
39
41
40
42
void rtc_init (void ) {
41
43
RCC_OscInitTypeDef RCC_OscInitStruct ;
42
44
uint32_t rtc_freq = 0 ;
43
45
46
+ #if DEVICE_RTC_LSI
44
47
if (rtc_inited ) return ;
45
48
rtc_inited = 1 ;
49
+ #endif
46
50
47
51
RtcHandle .Instance = RTC ;
48
52
49
- // Enable Power clock
50
- __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
-
53
+ #if !DEVICE_RTC_LSI
59
54
// Enable LSE Oscillator
60
55
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
61
56
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
62
57
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 ) {
58
+ RCC_OscInitStruct .LSIState = RCC_LSI_OFF ;
59
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) { // Check if LSE has started correctly
64
60
// Connect LSE to RTC
65
61
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
66
62
rtc_freq = LSE_VALUE ;
67
63
} else {
68
- // Enable LSI clock
69
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
70
- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
71
- RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
72
- RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
73
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
74
- error ("RTC error: LSI clock initialization failed." );
75
- }
76
- // Connect LSI to RTC
77
- __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
78
- // This value is LSI typical value. To be measured precisely using a timer input capture for example.
79
- rtc_freq = LSI_VALUE ;
64
+ error ("Cannot initialize RTC with LSE\n" );
80
65
}
66
+ #else
67
+ // Enable Power clock
68
+ __PWR_CLK_ENABLE ();
69
+
70
+ // Enable access to Backup domain
71
+ HAL_PWR_EnableBkUpAccess ();
81
72
82
- // Check if RTC is already initialized
83
- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
73
+ // Reset Backup domain
74
+ __HAL_RCC_BACKUPRESET_FORCE ();
75
+ __HAL_RCC_BACKUPRESET_RELEASE ();
76
+
77
+ // Enable LSI clock
78
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
79
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
80
+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
81
+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
82
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
83
+ error ("Cannot initialize RTC with LSI\n" );
84
+ }
85
+ // Connect LSI to RTC
86
+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
87
+ // This value is LSI typical value. To be measured precisely using a timer input capture for example.
88
+ rtc_freq = LSI_VALUE ;
89
+ #endif
84
90
85
91
// Enable RTC
86
92
__HAL_RCC_RTC_ENABLE ();
@@ -98,6 +104,7 @@ void rtc_init(void) {
98
104
}
99
105
100
106
void rtc_free (void ) {
107
+ #if DEVICE_RTC_LSI
101
108
// Enable Power clock
102
109
__PWR_CLK_ENABLE ();
103
110
@@ -110,6 +117,7 @@ void rtc_free(void) {
110
117
111
118
// Disable access to Backup domain
112
119
HAL_PWR_DisableBkUpAccess ();
120
+ #endif
113
121
114
122
// Disable LSI and LSE clocks
115
123
RCC_OscInitTypeDef RCC_OscInitStruct ;
@@ -119,11 +127,21 @@ void rtc_free(void) {
119
127
RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
120
128
HAL_RCC_OscConfig (& RCC_OscInitStruct );
121
129
130
+ #if DEVICE_RTC_LSI
122
131
rtc_inited = 0 ;
132
+ #endif
123
133
}
124
134
125
135
int rtc_isenabled (void ) {
126
- return rtc_inited ;
136
+ #if DEVICE_RTC_LSI
137
+ return rtc_inited ;
138
+ #else
139
+ if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
140
+ return 1 ;
141
+ } else {
142
+ return 0 ;
143
+ }
144
+ #endif
127
145
}
128
146
129
147
/*
0 commit comments