Skip to content

Commit 53fd30a

Browse files
committed
Merge pull request ARMmbed#1641 from dbestm/dev_xxx_F0xx_rtc
[XXX_F0XX] enhance RTC API
2 parents 7b82f27 + fe0ea5c commit 53fd30a

File tree

9 files changed

+52
-27
lines changed

9 files changed

+52
-27
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_conf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
#endif /* LSE_VALUE */
148148

149149
#if !defined (LSE_STARTUP_TIMEOUT)
150-
#define LSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for LSE start up, in ms */
150+
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
151151
#endif /* HSE_STARTUP_TIMEOUT */
152152

153153

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 0
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 0
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 1
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 1
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 0
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 0
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/device.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
51+
#define DEVICE_RTC_LSI 0
5152

5253
#define DEVICE_PWMOUT 1
5354

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/rtc_api.c

+44-26
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,60 @@
3333

3434
#include "mbed_error.h"
3535

36+
#if DEVICE_RTC_LSI
3637
static int rtc_inited = 0;
38+
#endif
3739

3840
static RTC_HandleTypeDef RtcHandle;
3941

4042
void rtc_init(void) {
4143
RCC_OscInitTypeDef RCC_OscInitStruct;
4244
uint32_t rtc_freq = 0;
4345

46+
#if DEVICE_RTC_LSI
4447
if (rtc_inited) return;
4548
rtc_inited = 1;
49+
#endif
4650

4751
RtcHandle.Instance = RTC;
4852

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
5954
// Enable LSE Oscillator
6055
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
6156
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
6257
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
6460
// Connect LSE to RTC
6561
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
6662
rtc_freq = LSE_VALUE;
6763
} 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");
8065
}
66+
#else
67+
// Enable Power clock
68+
__PWR_CLK_ENABLE();
69+
70+
// Enable access to Backup domain
71+
HAL_PWR_EnableBkUpAccess();
8172

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
8490

8591
// Enable RTC
8692
__HAL_RCC_RTC_ENABLE();
@@ -98,6 +104,7 @@ void rtc_init(void) {
98104
}
99105

100106
void rtc_free(void) {
107+
#if DEVICE_RTC_LSI
101108
// Enable Power clock
102109
__PWR_CLK_ENABLE();
103110

@@ -110,6 +117,7 @@ void rtc_free(void) {
110117

111118
// Disable access to Backup domain
112119
HAL_PWR_DisableBkUpAccess();
120+
#endif
113121

114122
// Disable LSI and LSE clocks
115123
RCC_OscInitTypeDef RCC_OscInitStruct;
@@ -119,11 +127,21 @@ void rtc_free(void) {
119127
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
120128
HAL_RCC_OscConfig(&RCC_OscInitStruct);
121129

130+
#if DEVICE_RTC_LSI
122131
rtc_inited = 0;
132+
#endif
123133
}
124134

125135
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
127145
}
128146

129147
/*

0 commit comments

Comments
 (0)