33
33
34
34
#include "mbed_error.h"
35
35
36
+ #if DEVICE_RTC_LSI
37
+ static int rtc_inited = 0 ;
38
+ #endif
39
+
36
40
static RTC_HandleTypeDef RtcHandle ;
37
41
38
42
void rtc_init (void )
@@ -52,33 +56,35 @@ void rtc_init(void)
52
56
__HAL_RCC_BACKUPRESET_FORCE ();
53
57
__HAL_RCC_BACKUPRESET_RELEASE ();
54
58
59
+ #if !DEVICE_RTC_LSI
55
60
// Enable LSE Oscillator
56
61
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
57
62
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
58
63
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 ) {
64
+ RCC_OscInitStruct .LSIState = RCC_LSI_OFF ;
65
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) == HAL_OK ) { // Check if LSE has started correctly
60
66
// Connect LSE to RTC
61
67
__HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSE );
62
68
__HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSE );
63
69
rtc_freq = LSE_VALUE ;
64
70
} 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 ;
71
+ error ("Cannot initialize RTC with LSE\n" );
78
72
}
79
-
80
- // Check if RTC is already initialized
81
- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
73
+ #else
74
+ // Enable LSI clock
75
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
76
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
77
+ RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
78
+ RCC_OscInitStruct .LSIState = RCC_LSI_ON ;
79
+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
80
+ error ("Cannot initialize RTC with LSI\n" );
81
+ }
82
+ // Connect LSI to RTC
83
+ __HAL_RCC_RTC_CLKPRESCALER (RCC_RTCCLKSOURCE_LSI );
84
+ __HAL_RCC_RTC_CONFIG (RCC_RTCCLKSOURCE_LSI );
85
+ // This value is LSI typical value. To be measured precisely using a timer input capture for example.
86
+ rtc_freq = LSI_VALUE ;
87
+ #endif
82
88
83
89
// Enable RTC
84
90
__HAL_RCC_RTC_ENABLE ();
@@ -117,15 +123,23 @@ void rtc_free(void)
117
123
RCC_OscInitStruct .LSIState = RCC_LSI_OFF ;
118
124
RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
119
125
HAL_RCC_OscConfig (& RCC_OscInitStruct );
126
+
127
+ #if DEVICE_RTC_LSI
128
+ rtc_inited = 0 ;
129
+ #endif
120
130
}
121
131
122
132
int rtc_isenabled (void )
123
133
{
124
- if (RTC -> ISR != 7 ) {
125
- return 1 ;
126
- } else {
127
- return 0 ;
128
- }
134
+ #if DEVICE_RTC_LSI
135
+ return rtc_inited ;
136
+ #else
137
+ if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
138
+ return 1 ;
139
+ } else {
140
+ return 0 ;
141
+ }
142
+ #endif
129
143
}
130
144
131
145
/*
0 commit comments