From 4d1161b689753c76e1c661fc2c4d52b349d0e556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bora=20=C3=96zgen?= Date: Tue, 9 Feb 2021 14:04:48 +0100 Subject: [PATCH] Add HSE support to STM32 RTC --- targets/TARGET_STM/rtc_api.c | 13 ++++++++++++- targets/TARGET_STM/rtc_api_hal.h | 8 +++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c index a3309c261d..698088b557 100644 --- a/targets/TARGET_STM/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -62,7 +62,18 @@ void rtc_init(void) while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) { } #endif /* DUAL_CORE */ -#if MBED_CONF_TARGET_LSE_AVAILABLE +#if RTC_FROM_HSE +#define RTC_HSE_DIV (HSE_VALUE / RTC_CLOCK) +#if RTC_HSE_DIV > 31 +#error "HSE value too high for RTC" +#endif + (void)RCC_OscInitStruct; + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; + PeriphClkInitStruct.RTCClockSelection = (RCC_RTCCLKSOURCE_HSE_DIVX | RTC_HSE_DIV << 16); + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { + error("PeriphClkInitStruct RTC failed with HSE\n"); + } +#elif MBED_CONF_TARGET_LSE_AVAILABLE RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; #if MBED_CONF_TARGET_LSE_BYPASS diff --git a/targets/TARGET_STM/rtc_api_hal.h b/targets/TARGET_STM/rtc_api_hal.h index 086e64dc98..eaf57fccd9 100644 --- a/targets/TARGET_STM/rtc_api_hal.h +++ b/targets/TARGET_STM/rtc_api_hal.h @@ -38,7 +38,9 @@ extern "C" { #endif -#if MBED_CONF_TARGET_LSE_AVAILABLE +#if RTC_FROM_HSE +#define RTC_CLOCK 1000000U +#elif MBED_CONF_TARGET_LSE_AVAILABLE #define RTC_CLOCK LSE_VALUE #else #define RTC_CLOCK LSI_VALUE @@ -47,7 +49,11 @@ extern "C" { #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM /* PREDIV_A : 7-bit asynchronous prescaler */ /* PREDIV_A is set to set LPTICKER frequency to RTC_CLOCK/4 */ +#if RTC_FROM_HSE +#define PREDIV_A_VALUE 124 +#else #define PREDIV_A_VALUE 3 +#endif /** Read RTC counter with sub second precision *