Skip to content

Commit b3e2dc9

Browse files
committed
[XXX_F10XRB] enhance RTC API
[XXX_F10XRB] enhance RTC API
2 parents 76649d8 + 7f6c0b8 commit b3e2dc9

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F1/stm32f1xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122

123123
#if !defined (LSE_STARTUP_TIMEOUT)
124-
#define LSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for LSE start up, in ms */
124+
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
125125
#endif /* HSE_STARTUP_TIMEOUT */
126126

127127

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/device.h

Lines changed: 1 addition & 0 deletions
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_STM32F1/TARGET_NUCLEO_F103RB/device.h

Lines changed: 1 addition & 0 deletions
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_STM32F1/rtc_api.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,42 @@ void rtc_init(void)
4646

4747
RtcHandle.Instance = RTC;
4848

49-
// Enable Power clock
50-
__HAL_RCC_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-
49+
#if !DEVICE_RTC_LSI
5950
// Enable LSE Oscillator
6051
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
6152
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
6253
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) {
54+
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
55+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
6456
// Connect LSE to RTC
6557
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
6658
} else {
67-
// Enable LSI clock
68-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
69-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
70-
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
71-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
72-
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
73-
error("RTC error: LSI clock initialization failed.");
74-
}
75-
// Connect LSI to RTC
76-
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
59+
error("Cannot initialize RTC with LSE\n");
7760
}
61+
#else
62+
// Enable Power clock
63+
__HAL_RCC_PWR_CLK_ENABLE();
64+
65+
// Enable access to Backup domain
66+
HAL_PWR_EnableBkUpAccess();
7867

68+
// Reset Backup domain
69+
__HAL_RCC_BACKUPRESET_FORCE();
70+
__HAL_RCC_BACKUPRESET_RELEASE();
71+
72+
// Enable LSI clock
73+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
74+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
75+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
76+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
77+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
78+
error("Cannot initialize RTC with LSI\n");
79+
}
80+
// Connect LSI to RTC
81+
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
82+
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
83+
#endif
84+
7985
// Enable RTC
8086
__HAL_RCC_RTC_ENABLE();
8187

@@ -88,6 +94,7 @@ void rtc_init(void)
8894

8995
void rtc_free(void)
9096
{
97+
#if DEVICE_RTC_LSI
9198
// Enable Power clock
9299
__PWR_CLK_ENABLE();
93100

@@ -100,6 +107,7 @@ void rtc_free(void)
100107

101108
// Disable access to Backup domain
102109
HAL_PWR_DisableBkUpAccess();
110+
#endif
103111

104112
// Disable LSI and LSE clocks
105113
RCC_OscInitTypeDef RCC_OscInitStruct;

0 commit comments

Comments
 (0)