3333
3434#include "mbed_error.h"
3535
36+ #if DEVICE_RTC_LSI
3637static int rtc_inited = 0 ;
38+ #endif
3739
3840static RTC_HandleTypeDef RtcHandle ;
3941
@@ -43,8 +45,10 @@ void rtc_init(void)
4345 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
4446 uint32_t rtc_freq = 0 ;
4547
48+ #if DEVICE_RTC_LSI
4649 if (rtc_inited ) return ;
4750 rtc_inited = 1 ;
51+ #endif
4852
4953 RtcHandle .Instance = RTC ;
5054
@@ -58,6 +62,7 @@ void rtc_init(void)
5862 __HAL_RCC_BACKUPRESET_FORCE ();
5963 __HAL_RCC_BACKUPRESET_RELEASE ();
6064
65+ #if !DEVICE_RTC_LSI
6166 // Enable LSE Oscillator
6267 RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE ;
6368 RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ; // Mandatory, otherwise the PLL is reconfigured!
@@ -69,24 +74,27 @@ void rtc_init(void)
6974 PeriphClkInitStruct .RTCClockSelection = RCC_RTCCLKSOURCE_LSE ;
7075 HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
7176 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" );
8979 }
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
9098
9199 // Check if RTC is already initialized
92100 if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) return ;
@@ -129,12 +137,19 @@ void rtc_free(void)
129137 RCC_OscInitStruct .LSEState = RCC_LSE_OFF ;
130138 HAL_RCC_OscConfig (& RCC_OscInitStruct );
131139
140+ #if DEVICE_RTC_LSI
132141 rtc_inited = 0 ;
142+ #endif
133143}
134144
135145int rtc_isenabled (void )
136146{
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 ) return 1 ;
151+ else return 0 ;
152+ #endif
138153}
139154
140155/*
0 commit comments