Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions drivers/counter/Kconfig.stm32_rtc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ config COUNTER_RTC_STM32_CLOCK_LSE
help
Use LSE as RTC clock

config COUNTER_RTC_STM32_CLOCK_HSE
bool "HSE"
depends on SOC_SERIES_STM32F2X || SOC_SERIES_STM32F4X || \
SOC_SERIES_STM32F7X || SOC_SERIES_STM32H7X || \
SOC_SERIES_STM32L0X || SOC_SERIES_STM32L1X
help
Use HSE as RTC clock. An appropriate prescaler must be set.

endchoice #COUNTER_RTC_STM32_CLOCK_SRC

if !SOC_SERIES_STM32F4X
Expand Down Expand Up @@ -75,6 +83,13 @@ config COUNTER_RTC_STM32_LSE_BYPASS
help
Enable LSE bypass

config COUNTER_RTC_STM32_HSE_DIV
int "HSE prescaler"
depends on COUNTER_RTC_STM32_CLOCK_HSE
default 0
Copy link
Contributor

@FRASTM FRASTM Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is not always the default, some stm32 devices like stm32F4 have a prescaler from 2 to 31 on the HSE clock, some others like the stm32wb or stm32L4/L5 have a fixed divider by 32 on the HSE clock

help
HSE division factor for obtaining a 1 MHz RTC clock

config COUNTER_RTC_STM32_BACKUP_DOMAIN_RESET
bool "Do backup domain reset"
default y
Expand Down
19 changes: 19 additions & 0 deletions drivers/counter/counter_ll_stm32_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
#define RTC_EXTI_LINE LL_EXTI_LINE_17
#endif

/* Macro to fill up HSE prescaler values */
#define fn_hse_prescaler(v) LL_RCC_RTC_HSE_DIV_ ## v
#define hse_prescaler(v) fn_hse_prescaler(v)
#define LL_RCC_RTC_HSE_DIV_0 LL_RCC_RTC_NOCLOCK

struct rtc_stm32_config {
struct counter_config_info counter_info;
struct stm32_pclken pclken;
Expand Down Expand Up @@ -310,6 +315,16 @@ static int rtc_stm32_init(const struct device *dev)

LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);

#elif defined(CONFIG_COUNTER_RTC_STM32_CLOCK_HSE)

LL_RCC_SetRTC_HSEPrescaler(hse_prescaler(CONFIG_COUNTER_RTC_STM32_HSE_DIV));

/* Wait until HSE is ready */
while (LL_RCC_HSE_IsReady() != 1) {
}

LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_HSE);

#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */

#if !defined(CONFIG_SOC_SERIES_STM32F4X) && \
Expand Down Expand Up @@ -396,6 +411,10 @@ static const struct rtc_stm32_config rtc_config = {
/* prescaler values for LSI @ 32 KHz */
.AsynchPrescaler = 0x7F,
.SynchPrescaler = 0x00F9,
#elif defined(CONFIG_COUNTER_RTC_STM32_CLOCK_HSE)
/* prescaler values for divided HSE @ 1 MHz */
.AsynchPrescaler = 0x7C,
.SynchPrescaler = 0x1F3F,
#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */
/* prescaler values for LSE @ 32768 Hz */
.AsynchPrescaler = 0x7F,
Expand Down