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
RTC_HandleTypeDef RtcHandle ;
39
41
@@ -42,8 +44,10 @@ void rtc_init(void)
42
44
RCC_OscInitTypeDef RCC_OscInitStruct ;
43
45
uint32_t rtc_freq = 0 ;
44
46
47
+ #if DEVICE_RTC_LSI
45
48
if (rtc_inited ) return ;
46
49
rtc_inited = 1 ;
50
+ #endif
47
51
48
52
RtcHandle .Instance = RTC ;
49
53
@@ -57,33 +61,35 @@ void rtc_init(void)
57
61
__HAL_RCC_BACKUPRESET_FORCE ();
58
62
__HAL_RCC_BACKUPRESET_RELEASE ();
59
63
64
+ #if !DEVICE_RTC_LSI
60
65
// Enable LSE Oscillator
61
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
66
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
62
67
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
63
68
RCC_OscInitStruct .LSEState = RCC_LSE_ON ; // External 32.768 kHz clock on OSC_IN/OSC_OUT
64
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) {
69
+ RCC_OscInitStruct .LSIState = RCC_LSI_OFF ;
70
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) { // Check if LSE has started correctly
65
71
// Connect LSE to RTC
66
72
__HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSE );
67
73
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
68
74
rtc_freq = LSE_VALUE ;
69
75
} else {
70
- // Enable LSI clock
71
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
72
- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
73
- RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
74
- RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
75
- if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
76
- error ("RTC error: LSI clock initialization failed." );
77
- }
78
- // Connect LSI to RTC
79
- __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSI );
80
- __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
81
- // This value is LSI typical value. To be measured precisely using a timer input capture for example.
82
- rtc_freq = 40000 ;
76
+ error ("Cannot initialize RTC with LSE\n" );
83
77
}
84
-
85
- // Check if RTC is already initialized
86
- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
78
+ #else
79
+ // Enable LSI clock
80
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
81
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
82
+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
83
+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
84
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
85
+ error ("Cannot initialize RTC with LSI\n" );
86
+ }
87
+ // Connect LSI to RTC
88
+ __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSI );
89
+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
90
+ // This value is LSI typical value. To be measured precisely using a timer input capture for example.
91
+ rtc_freq = 40000 ;
92
+ #endif
87
93
88
94
// Enable RTC
89
95
__HAL_RCC_RTC_ENABLE ();
@@ -129,12 +135,22 @@ void rtc_free(void)
129
135
RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
130
136
HAL_RCC_OscConfig (& RCC_OscInitStruct );
131
137
138
+ #if DEVICE_RTC_LSI
132
139
rtc_inited = 0 ;
140
+ #endif
133
141
}
134
142
135
143
int rtc_isenabled (void )
136
144
{
137
- return rtc_inited ;
145
+ #if DEVICE_RTC_LSI
146
+ return rtc_inited ;
147
+ #else
148
+ if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
149
+ return 1 ;
150
+ } else {
151
+ return 0 ;
152
+ }
153
+ #endif
138
154
}
139
155
140
156
/*
0 commit comments