Skip to content

Commit 8577dc9

Browse files
committed
Merge pull request #1558 from dbestm/dev_l476rg_rtc
l476rg rtc
2 parents 8e70fc7 + ab3cda4 commit 8577dc9

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L4/stm32l4xx_hal_rcc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ typedef struct
159159
* @{
160160
*/
161161
#define RCC_DBP_TIMEOUT_VALUE ((uint32_t)100)
162-
#define RCC_LSE_TIMEOUT_VALUE ((uint32_t)100)
162+
#define RCC_LSE_TIMEOUT_VALUE ((uint32_t)5000)
163163
/**
164164
* @}
165165
*/

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/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_STM32L4/rtc_api.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
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

@@ -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,22 @@ 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

135145
int 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) {
151+
return 1;
152+
} else {
153+
return 0;
154+
}
155+
#endif
138156
}
139157

140158
/*

0 commit comments

Comments
 (0)