Skip to content

Commit 8fd1b68

Browse files
committed
drivers: counter: stm32: Add support for HSE as RTC clock source
Add Kconfig options COUNTER_RTC_STM32_CLOCK_HSE and COUNTER_RTC_STM32_HSE_DIV to allow configuring HSE as the RTC clock source. Signed-off-by: Markus Fuchs <[email protected]>
1 parent 416dd6c commit 8fd1b68

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

drivers/counter/Kconfig.stm32_rtc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ config COUNTER_RTC_STM32_CLOCK_LSE
3030
help
3131
Use LSE as RTC clock
3232

33+
config COUNTER_RTC_STM32_CLOCK_HSE
34+
bool "HSE"
35+
depends on SOC_SERIES_STM32F2X || SOC_SERIES_STM32F4X || \
36+
SOC_SERIES_STM32F7X || SOC_SERIES_STM32H7X || \
37+
SOC_SERIES_STM32L0X || SOC_SERIES_STM32L1X
38+
help
39+
Use HSE as RTC clock. An appropriate prescaler must be set.
40+
3341
endchoice #COUNTER_RTC_STM32_CLOCK_SRC
3442

3543
if !SOC_SERIES_STM32F4X
@@ -75,6 +83,13 @@ config COUNTER_RTC_STM32_LSE_BYPASS
7583
help
7684
Enable LSE bypass
7785

86+
config COUNTER_RTC_STM32_HSE_DIV
87+
int "HSE prescaler"
88+
depends on COUNTER_RTC_STM32_CLOCK_HSE
89+
default 0
90+
help
91+
HSE division factor for obtaining a 1 MHz RTC clock
92+
7893
config COUNTER_RTC_STM32_BACKUP_DOMAIN_RESET
7994
bool "Do backup domain reset"
8095
default y

drivers/counter/counter_ll_stm32_rtc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
5252
#define RTC_EXTI_LINE LL_EXTI_LINE_17
5353
#endif
5454

55+
/* Macro to fill up HSE prescaler values */
56+
#define fn_hse_prescaler(v) LL_RCC_RTC_HSE_DIV_ ## v
57+
#define hse_prescaler(v) fn_hse_prescaler(v)
58+
#define LL_RCC_RTC_HSE_DIV_0 LL_RCC_RTC_NOCLOCK
59+
5560
struct rtc_stm32_config {
5661
struct counter_config_info counter_info;
5762
struct stm32_pclken pclken;
@@ -310,6 +315,16 @@ static int rtc_stm32_init(const struct device *dev)
310315

311316
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
312317

318+
#elif defined(CONFIG_COUNTER_RTC_STM32_CLOCK_HSE)
319+
320+
LL_RCC_SetRTC_HSEPrescaler(hse_prescaler(CONFIG_COUNTER_RTC_STM32_HSE_DIV));
321+
322+
/* Wait until HSE is ready */
323+
while (LL_RCC_HSE_IsReady() != 1) {
324+
}
325+
326+
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_HSE);
327+
313328
#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */
314329

315330
#if !defined(CONFIG_SOC_SERIES_STM32F4X) && \
@@ -396,6 +411,10 @@ static const struct rtc_stm32_config rtc_config = {
396411
/* prescaler values for LSI @ 32 KHz */
397412
.AsynchPrescaler = 0x7F,
398413
.SynchPrescaler = 0x00F9,
414+
#elif defined(CONFIG_COUNTER_RTC_STM32_CLOCK_HSE)
415+
/* prescaler values for divided HSE @ 1 MHz */
416+
.AsynchPrescaler = 0x7C,
417+
.SynchPrescaler = 0x1F3F,
399418
#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */
400419
/* prescaler values for LSE @ 32768 Hz */
401420
.AsynchPrescaler = 0x7F,

0 commit comments

Comments
 (0)