Skip to content

Commit 2c1f04d

Browse files
committed
Add HSE support to STM32 RTC
1 parent 0c6753b commit 2c1f04d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

targets/TARGET_STM/rtc_api.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ void rtc_init(void)
6262
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
6363
}
6464
#endif /* DUAL_CORE */
65-
#if MBED_CONF_TARGET_LSE_AVAILABLE
65+
#if RTC_FROM_HSE
66+
#define RTC_HSE_DIV (HSE_VALUE / RTC_CLOCK)
67+
#if RTC_HSE_DIV > 31
68+
#error "HSE value too high for RTC"
69+
#endif
70+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
71+
PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16);
72+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
73+
error("PeriphClkInitStruct RTC failed with HSE\n");
74+
}
75+
#elif MBED_CONF_TARGET_LSE_AVAILABLE
6676
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
6777
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
6878
#if MBED_CONF_TARGET_LSE_BYPASS

targets/TARGET_STM/rtc_api_hal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
extern "C" {
3939
#endif
4040

41-
#if MBED_CONF_TARGET_LSE_AVAILABLE
41+
#if RTC_FROM_HSE
42+
#define RTC_CLOCK 1000000U
43+
#elif MBED_CONF_TARGET_LSE_AVAILABLE
4244
#define RTC_CLOCK LSE_VALUE
4345
#else
4446
#define RTC_CLOCK LSI_VALUE
@@ -47,7 +49,11 @@ extern "C" {
4749
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
4850
/* PREDIV_A : 7-bit asynchronous prescaler */
4951
/* PREDIV_A is set to set LPTICKER frequency to RTC_CLOCK/4 */
52+
#if RTC_FROM_HSE
53+
#define PREDIV_A_VALUE 124
54+
#else
5055
#define PREDIV_A_VALUE 3
56+
#endif
5157

5258
/** Read RTC counter with sub second precision
5359
*

0 commit comments

Comments
 (0)