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
@@ -43,8 +45,10 @@ void rtc_init(void)
43
45
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
44
46
uint32_t rtc_freq = 0 ;
45
47
48
+ #if DEVICE_RTC_LSI
46
49
if (rtc_inited ) return ;
47
50
rtc_inited = 1 ;
51
+ #endif
48
52
49
53
RtcHandle .Instance = RTC ;
50
54
@@ -58,6 +62,7 @@ void rtc_init(void)
58
62
__HAL_RCC_BACKUPRESET_FORCE ();
59
63
__HAL_RCC_BACKUPRESET_RELEASE ();
60
64
65
+ #if !DEVICE_RTC_LSI
61
66
// Enable LSE Oscillator
62
67
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
63
68
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
@@ -69,24 +74,27 @@ void rtc_init(void)
69
74
PeriphClkInitStruct .RTCClockSelection = RCC_RTCCLKSOURCE_LSE ;
70
75
HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
71
76
rtc_freq = LSE_VALUE ;
72
- } else { // LSE didn't start, try with LSI
73
- // Enable LSI clock
74
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
75
- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
76
- RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
77
- RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
78
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
79
- error ("Cannot initialize RTC with LSI\n" );
80
- }
81
- // Connect LSI to RTC
82
- PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_RTC ;
83
- PeriphClkInitStruct .RTCClockSelection = RCC_RTCCLKSOURCE_LSI ;
84
- if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK ) {
85
- error ("Cannot initialize RTC with LSI\n" );
86
- }
87
- // This value is LSI typical value. To be measured precisely using a timer input capture for example.
88
- rtc_freq = 40000 ;
77
+ } else {
78
+ error ("Cannot initialize RTC with LSE\n" );
89
79
}
80
+ #else
81
+ // Enable LSI clock
82
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
83
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
84
+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
85
+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
86
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
87
+ error ("Cannot initialize RTC with LSI\n" );
88
+ }
89
+ // Connect LSI to RTC
90
+ PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_RTC ;
91
+ PeriphClkInitStruct .RTCClockSelection = RCC_RTCCLKSOURCE_LSI ;
92
+ if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK ) {
93
+ error ("Cannot initialize RTC with LSI\n" );
94
+ }
95
+ // This value is LSI typical value. To be measured precisely using a timer input capture for example.
96
+ rtc_freq = 40000 ;
97
+ #endif
90
98
91
99
// Check if RTC is already initialized
92
100
if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
@@ -129,12 +137,22 @@ void rtc_free(void)
129
137
RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
130
138
HAL_RCC_OscConfig (& RCC_OscInitStruct );
131
139
140
+ #if DEVICE_RTC_LSI
132
141
rtc_inited = 0 ;
142
+ #endif
133
143
}
134
144
135
145
int rtc_isenabled (void )
136
146
{
137
- return rtc_inited ;
147
+ #if DEVICE_RTC_LSI
148
+ return rtc_inited ;
149
+ #else
150
+ if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
151
+ return 1 ;
152
+ } else {
153
+ return 0 ;
154
+ }
155
+ #endif
138
156
}
139
157
140
158
/*
0 commit comments