2018-11-29 11:08:41 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 Workaround GmbH
|
|
|
|
|
* Copyright (c) 2018 Allterco Robotics
|
|
|
|
|
* Copyright (c) 2018 Linaro Limited
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Source file for the STM32 RTC driver
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-24 14:28:48 -05:00
|
|
|
#define DT_DRV_COMPAT st_stm32_rtc
|
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
|
|
|
|
|
#include <zephyr/drivers/clock_control.h>
|
2026-05-05 15:28:28 +02:00
|
|
|
#include <zephyr/drivers/interrupt_controller/intc_exti_stm32.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
|
#include <zephyr/kernel.h>
|
2018-11-29 11:08:41 +01:00
|
|
|
#include <soc.h>
|
2025-10-21 10:16:20 +02:00
|
|
|
#include <stm32_bitops.h>
|
2025-09-22 16:43:56 +02:00
|
|
|
#include <stm32_ll_cortex.h>
|
2020-11-20 18:27:03 +01:00
|
|
|
#include <stm32_ll_exti.h>
|
|
|
|
|
#include <stm32_ll_pwr.h>
|
|
|
|
|
#include <stm32_ll_rcc.h>
|
|
|
|
|
#include <stm32_ll_rtc.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/counter.h>
|
|
|
|
|
#include <zephyr/sys/timeutil.h>
|
2024-01-26 10:05:39 +01:00
|
|
|
#include <zephyr/pm/device.h>
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-06-23 08:26:39 +02:00
|
|
|
#include <stm32_backup_domain.h>
|
|
|
|
|
#include <stm32_hsem.h>
|
2020-06-23 09:48:07 +02:00
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
#if CONFIG_STM32_HAL2
|
|
|
|
|
#define STM32_RTC_HOUR_FORMAT_24HOUR LL_RTC_HOUR_FORMAT_24HOUR
|
|
|
|
|
#define STM32_RTC_HOUR_FORMAT_AMPM LL_RTC_HOUR_FORMAT_AMPM
|
|
|
|
|
#define STM32_RTC_TIME_FORMAT_AM_24H LL_RTC_TIME_FORMAT_AM_24H
|
|
|
|
|
#define STM32_RTC_GET_SECOND LL_RTC_GET_SECOND
|
|
|
|
|
#define STM32_RTC_GET_MINUTE LL_RTC_GET_MINUTE
|
|
|
|
|
#define STM32_RTC_GET_HOUR LL_RTC_GET_HOUR
|
|
|
|
|
#define STM32_RTC_GET_DAY LL_RTC_GET_DAY
|
|
|
|
|
#define STM32_RTC_GET_WEEKDAY LL_RTC_GET_WEEKDAY
|
|
|
|
|
#define STM32_RTC_GET_YEAR LL_RTC_GET_YEAR
|
|
|
|
|
#define STM32_RTC_GET_MONTH LL_RTC_GET_MONTH
|
|
|
|
|
#define STM32_RTC_EnableBypassShadowReg LL_RTC_EnableBypassShadowReg
|
|
|
|
|
#define STM32_RTC_DisableBypassShadowReg LL_RTC_DisableBypassShadowReg
|
|
|
|
|
|
|
|
|
|
/* On HAL2 this macro skips the RTC instance as the first argument,
|
|
|
|
|
* only the potential following arguments are passed.
|
|
|
|
|
*/
|
|
|
|
|
#define STM32_ARG(dev, ...) __VA_ARGS__
|
|
|
|
|
#else /* CONFIG_STM32_HAL2 */
|
|
|
|
|
#define STM32_RTC_HOUR_FORMAT_24HOUR LL_RTC_HOURFORMAT_24HOUR
|
|
|
|
|
#define STM32_RTC_HOUR_FORMAT_AMPM LL_RTC_HOURFORMAT_AMPM
|
|
|
|
|
#define STM32_RTC_TIME_FORMAT_AM_24H LL_RTC_TIME_FORMAT_AM_OR_24
|
|
|
|
|
#define STM32_RTC_GET_SECOND __LL_RTC_GET_SECOND
|
|
|
|
|
#define STM32_RTC_GET_MINUTE __LL_RTC_GET_MINUTE
|
|
|
|
|
#define STM32_RTC_GET_HOUR __LL_RTC_GET_HOUR
|
|
|
|
|
#define STM32_RTC_GET_DAY __LL_RTC_GET_DAY
|
|
|
|
|
#define STM32_RTC_GET_WEEKDAY __LL_RTC_GET_WEEKDAY
|
|
|
|
|
#define STM32_RTC_GET_MONTH __LL_RTC_GET_MONTH
|
|
|
|
|
#define STM32_RTC_GET_YEAR __LL_RTC_GET_YEAR
|
|
|
|
|
#define STM32_RTC_EnableBypassShadowReg LL_RTC_EnableShadowRegBypass
|
|
|
|
|
#define STM32_RTC_DisableBypassShadowReg LL_RTC_DisableShadowRegBypass
|
|
|
|
|
|
|
|
|
|
/* On HAL1 this macro adds the RTC instance as the first argument,
|
|
|
|
|
* with or without a comma depending on number of arguments.
|
|
|
|
|
*/
|
|
|
|
|
#define STM32_ARG(dev, ...) COND_CODE_1(IS_EMPTY(__VA_ARGS__), (dev), (dev, __VA_ARGS__))
|
|
|
|
|
#endif /* CONFIG_STM32_HAL2 */
|
|
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X) || defined(CONFIG_SOC_SERIES_STM32F2X) || \
|
|
|
|
|
(defined(CONFIG_SOC_SERIES_STM32L1X) && !defined(RTC_SUBSECOND_SUPPORT))
|
|
|
|
|
/* subsecond counting is not supported by some STM32L1x MCUs (Cat.1) & by STM32F1x/2x SoC series */
|
|
|
|
|
#define HW_SUBSECOND_SUPPORT 0
|
|
|
|
|
#else
|
|
|
|
|
#define HW_SUBSECOND_SUPPORT 1
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-30 09:07:14 -06:00
|
|
|
/* Seconds from 1970-01-01T00:00:00 to 2000-01-01T00:00:00 */
|
2019-03-25 14:34:12 +01:00
|
|
|
#define T_TIME_OFFSET 946684800
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2026-05-05 15:28:28 +02:00
|
|
|
#if DT_INST_NODE_HAS_PROP(0, alrm_exti_line)
|
|
|
|
|
#define RTC_EXTI_LINE_NUM DT_INST_PROP(0, alrm_exti_line)
|
|
|
|
|
#endif /* DT_INST_NODE_HAS_PROP(0, alrm_exti_line) */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-08-31 20:32:57 +09:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
|
#define COUNTER_NO_DATE
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-08-16 09:43:14 +02:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_LSI
|
|
|
|
|
/* LSI */
|
|
|
|
|
#define RTCCLK_FREQ STM32_LSI_FREQ
|
|
|
|
|
#else
|
|
|
|
|
/* LSE */
|
|
|
|
|
#define RTCCLK_FREQ STM32_LSE_FREQ
|
|
|
|
|
#endif /* DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_LSI */
|
|
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2023-08-16 09:43:14 +02:00
|
|
|
#define RTC_ASYNCPRE BIT_MASK(7)
|
2023-08-16 11:45:55 +02:00
|
|
|
#else /* !CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
/* Get the highest possible clock for the subsecond register */
|
|
|
|
|
#define RTC_ASYNCPRE 1
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2023-08-16 09:43:14 +02:00
|
|
|
#else /* CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
|
#define RTC_ASYNCPRE (RTCCLK_FREQ - 1)
|
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
/* Timeout in microseconds used to wait for flags */
|
|
|
|
|
#define RTC_TIMEOUT 1000
|
|
|
|
|
|
2023-08-16 09:43:14 +02:00
|
|
|
/* Adjust the second sync prescaler to get 1Hz on ck_spre */
|
|
|
|
|
#define RTC_SYNCPRE ((RTCCLK_FREQ / (1 + RTC_ASYNCPRE)) - 1)
|
|
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
typedef uint32_t tick_t;
|
|
|
|
|
#else
|
|
|
|
|
typedef uint64_t tick_t;
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
struct rtc_stm32_config {
|
|
|
|
|
struct counter_config_info counter_info;
|
2025-09-22 16:43:56 +02:00
|
|
|
uint32_t async_prescaler;
|
|
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
|
uint32_t sync_prescaler;
|
|
|
|
|
#endif /* !CONFIG_SOC_SERIES_STM32F1X */
|
2022-09-05 14:57:25 +02:00
|
|
|
const struct stm32_pclken *pclken;
|
2025-05-30 14:25:07 +01:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE
|
|
|
|
|
uint32_t hse_prescaler;
|
|
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct rtc_stm32_data {
|
|
|
|
|
counter_alarm_callback_t callback;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t ticks;
|
2018-11-29 11:08:41 +01:00
|
|
|
void *user_data;
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
bool irq_on_late;
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2018-11-29 11:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline void ll_clear_alarm_flag(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ClearFlag_ALR(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ClearFlag_ALRA(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline uint32_t ll_is_active_alarm(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
return LL_RTC_IsActiveFlag_ALR(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
return LL_RTC_IsActiveFlag_ALRA(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline void ll_enable_interrupt_alarm(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_EnableIT_ALR(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_EnableIT_ALRA(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline void ll_disable_interrupt_alarm(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableIT_ALR(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableIT_ALRA(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline uint32_t ll_isenabled_interrupt_alarm(void)
|
2023-08-16 11:45:55 +02:00
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
return LL_RTC_IsEnabledIT_ALR(STM32_ARG(RTC));
|
2023-08-16 11:45:55 +02:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
return LL_RTC_IsEnabledIT_ALRA(STM32_ARG(RTC));
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline void ll_enable_alarm(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
2025-10-01 14:37:28 +02:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
|
LL_RTC_ALMA_Enable(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
static inline void ll_disable_alarm(void)
|
2022-08-31 20:32:57 +09:00
|
|
|
{
|
2025-10-01 14:37:28 +02:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
|
LL_RTC_ALMA_Disable(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void rtc_stm32_irq_config(const struct device *dev);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
/* When no error occurs, this function disables the RTC write protection and should be balanced
|
|
|
|
|
* with a call to rtc_stm32_exit_init_mode (which enables RTC write protection).
|
|
|
|
|
* In case of error, the write protection is enabled when leaving this function, so nothing more
|
|
|
|
|
* needs to be made.
|
|
|
|
|
*/
|
|
|
|
|
static int rtc_stm32_enter_init_mode(void)
|
|
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
|
/* Wait for RTC to be ready */
|
2025-10-01 14:37:28 +02:00
|
|
|
if (!WAIT_FOR(LL_RTC_IsActiveFlag_RTOF(STM32_ARG(RTC)), RTC_TIMEOUT, NULL)) {
|
2025-09-22 16:43:56 +02:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
|
|
|
|
|
/* Check if the Initialization mode is set */
|
2025-10-01 14:37:28 +02:00
|
|
|
if (LL_RTC_IsActiveFlag_INIT(STM32_ARG(RTC)) == 0U) {
|
2025-09-22 16:43:56 +02:00
|
|
|
/* Set the Initialization mode */
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_EnableInitMode(STM32_ARG(RTC));
|
|
|
|
|
if (!WAIT_FOR(LL_RTC_IsActiveFlag_INIT(STM32_ARG(RTC)), RTC_TIMEOUT, NULL)) {
|
|
|
|
|
LL_RTC_DisableInitMode(STM32_ARG(RTC));
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rtc_stm32_exit_init_mode(void)
|
|
|
|
|
{
|
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
|
|
|
|
|
/* Wait for RTC to be ready */
|
2025-10-01 14:37:28 +02:00
|
|
|
if (!WAIT_FOR(LL_RTC_IsActiveFlag_RTOF(STM32_ARG(RTC)), RTC_TIMEOUT, NULL)) {
|
2025-09-22 16:43:56 +02:00
|
|
|
status = -ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableInitMode(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !defined(CONFIG_COUNTER_RTC_STM32_SAVE_VALUE_BETWEEN_RESETS)
|
|
|
|
|
static int rtc_stm32_wait_for_synchro(void)
|
|
|
|
|
{
|
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
|
|
/* Clear RSF flag */
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ClearFlag_RS(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
if (!WAIT_FOR(LL_RTC_IsActiveFlag_RS(STM32_ARG(RTC)), RTC_TIMEOUT, NULL)) {
|
2025-09-22 16:43:56 +02:00
|
|
|
status = -ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rtc_stm32_deinit(void)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
/* Set Initialization mode */
|
|
|
|
|
ret = rtc_stm32_enter_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to enter RTC init mode");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->CNTL, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->CNTH, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->PRLH, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->PRLL, 0x8000U);
|
|
|
|
|
stm32_reg_write(&RTC->CRH, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->CRL, 0x20U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#else /* CONFIG_SOC_SERIES_STM32F1X */
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->CR, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->TR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#ifdef RTC_WUTR_WUT
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->WUTR, RTC_WUTR_WUT);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* RTC_WUTR_WUT */
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->DR, RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0);
|
|
|
|
|
stm32_reg_write(&RTC->PRER, RTC_PRER_PREDIV_A | 0xFFU);
|
|
|
|
|
stm32_reg_write(&RTC->ALRMAR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#ifdef RTC_CR_ALRBE
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->ALRMBR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* RTC_CR_ALRBE */
|
|
|
|
|
|
|
|
|
|
#if HW_SUBSECOND_SUPPORT
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->CALR, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->SHIFTR, 0U);
|
|
|
|
|
stm32_reg_write(&RTC->ALRMASSR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#ifdef RTC_CR_ALRBE
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->ALRMBSSR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* RTC_CR_ALRBE */
|
|
|
|
|
#endif /* HW_SUBSECOND_SUPPORT */
|
|
|
|
|
|
|
|
|
|
#if defined(RTC_PRIVCFGR_PRIV)
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->PRIVCFGR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* RTC_PRIVCFGR_PRIV */
|
|
|
|
|
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->SECCFGR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
|
|
|
|
|
|
|
|
|
|
/* Reset I(C)SR register and exit initialization mode */
|
|
|
|
|
#ifdef RTC_ICSR_INIT
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->ICSR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#else
|
2025-10-21 10:16:20 +02:00
|
|
|
stm32_reg_write(&RTC->ISR, 0U);
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
|
|
|
|
|
|
/* Exit Initialization mode */
|
|
|
|
|
ret = rtc_stm32_exit_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to exit RTC init mode");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rtc_stm32_wait_for_synchro();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static int rtc_stm32_configure(const struct device *dev)
|
|
|
|
|
{
|
|
|
|
|
const struct rtc_stm32_config *cfg = dev->config;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
/* Set Initialization mode */
|
|
|
|
|
ret = rtc_stm32_enter_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to enter RTC init mode");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_SetAsynchPrescaler(STM32_ARG(RTC, cfg->async_prescaler));
|
2025-09-22 16:43:56 +02:00
|
|
|
LL_RTC_SetOutputSource(BKP, LL_RTC_CALIB_OUTPUT_NONE);
|
|
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_SetHourFormat(STM32_ARG(RTC, STM32_RTC_HOUR_FORMAT_24HOUR));
|
|
|
|
|
LL_RTC_SetAsynchPrescaler(STM32_ARG(RTC, cfg->async_prescaler));
|
|
|
|
|
LL_RTC_SetSynchPrescaler(STM32_ARG(RTC, cfg->sync_prescaler));
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Exit Initialization mode */
|
|
|
|
|
ret = rtc_stm32_exit_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to exit RTC init mode");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_start(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2024-05-07 14:09:36 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32WBAX) || defined(CONFIG_SOC_SERIES_STM32U5X)
|
2023-11-07 14:55:40 +01:00
|
|
|
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
|
|
|
|
const struct rtc_stm32_config *cfg = dev->config;
|
|
|
|
|
|
|
|
|
|
/* Enable RTC bus clock */
|
|
|
|
|
if (clock_control_on(clk, (clock_control_subsys_t) &cfg->pclken[0]) != 0) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_ERR("RTC clock enabling failed");
|
2023-11-07 14:55:40 +01:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_enable_access();
|
2025-12-16 16:26:42 +01:00
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32U3X
|
|
|
|
|
/* STM32U3 series uses LL_RCC_RTC_ClockEnable instead of LL_RCC_EnableRTC */
|
|
|
|
|
LL_RCC_RTC_ClockEnable();
|
|
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_EnableRTC();
|
2025-12-16 16:26:42 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32U3X */
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_disable_access();
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
|
2025-12-16 16:26:42 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBAX || CONFIG_SOC_SERIES_STM32U5X */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_stop(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2024-05-07 14:09:36 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32WBAX) || defined(CONFIG_SOC_SERIES_STM32U5X)
|
2023-11-07 14:55:40 +01:00
|
|
|
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
|
|
|
|
const struct rtc_stm32_config *cfg = dev->config;
|
|
|
|
|
|
2024-05-03 00:17:49 +02:00
|
|
|
/* Disable RTC bus clock */
|
|
|
|
|
if (clock_control_off(clk, (clock_control_subsys_t) &cfg->pclken[0]) != 0) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_ERR("RTC clock disabling failed");
|
2023-11-07 14:55:40 +01:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_enable_access();
|
2025-12-16 16:26:42 +01:00
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32U3X
|
|
|
|
|
/* STM32U3 series uses LL_RCC_RTC_ClockDisable instead of LL_RCC_DisableRTC */
|
|
|
|
|
LL_RCC_RTC_ClockDisable();
|
|
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_DisableRTC();
|
2025-12-16 16:26:42 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32U3X */
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_disable_access();
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
|
2025-12-16 16:26:42 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBAX || CONFIG_SOC_SERIES_STM32U5X */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 08:56:33 +02:00
|
|
|
#if !defined(COUNTER_NO_DATE)
|
2023-08-16 11:45:55 +02:00
|
|
|
tick_t rtc_stm32_read(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
|
|
|
|
struct tm now = { 0 };
|
|
|
|
|
time_t ts;
|
2023-08-16 11:45:55 +02:00
|
|
|
uint32_t rtc_date, rtc_time;
|
|
|
|
|
tick_t ticks;
|
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2025-12-17 20:11:37 -05:00
|
|
|
uint32_t rtc_subsecond;
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2018-11-29 11:08:41 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
2023-08-16 10:12:37 +02:00
|
|
|
do {
|
2025-12-17 20:11:37 -05:00
|
|
|
/* read date, time and subseconds and relaunch if a day increment occurred
|
|
|
|
|
* while doing so as it will result in an erroneous result otherwise
|
|
|
|
|
*/
|
2025-10-01 14:37:28 +02:00
|
|
|
rtc_date = LL_RTC_DATE_Get(STM32_ARG(RTC));
|
2023-08-16 11:45:55 +02:00
|
|
|
do {
|
2025-12-17 20:11:37 -05:00
|
|
|
/* read time and subseconds and relaunch if a second increment occurred
|
|
|
|
|
* while doing so as it will result in an erroneous result otherwise
|
|
|
|
|
*/
|
2025-10-01 14:37:28 +02:00
|
|
|
rtc_time = LL_RTC_TIME_Get(STM32_ARG(RTC));
|
2025-12-17 20:11:37 -05:00
|
|
|
#if CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
do {
|
|
|
|
|
/* read subseconds and relaunch if a second increment occurred
|
|
|
|
|
* while doing so as it will result in an erroneous result otherwise
|
|
|
|
|
*/
|
2025-10-01 14:37:28 +02:00
|
|
|
rtc_subsecond = LL_RTC_TIME_GetSubSecond(STM32_ARG(RTC));
|
|
|
|
|
} while (rtc_subsecond != LL_RTC_TIME_GetSubSecond(STM32_ARG(RTC)));
|
2025-12-17 20:11:37 -05:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2025-10-01 14:37:28 +02:00
|
|
|
} while (rtc_time != LL_RTC_TIME_Get(STM32_ARG(RTC)));
|
|
|
|
|
} while (rtc_date != LL_RTC_DATE_Get(STM32_ARG(RTC)));
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
/* Convert calendar datetime to UNIX timestamp */
|
2019-03-25 14:34:12 +01:00
|
|
|
/* RTC start time: 1st, Jan, 2000 */
|
2020-11-30 09:07:14 -06:00
|
|
|
/* time_t start: 1st, Jan, 1970 */
|
2025-10-01 14:37:28 +02:00
|
|
|
now.tm_year = 100 + bcd2bin(STM32_RTC_GET_YEAR(rtc_date));
|
2019-03-25 14:34:12 +01:00
|
|
|
/* tm_mon allowed values are 0-11 */
|
2025-10-01 14:37:28 +02:00
|
|
|
now.tm_mon = bcd2bin(STM32_RTC_GET_MONTH(rtc_date)) - 1;
|
|
|
|
|
now.tm_mday = bcd2bin(STM32_RTC_GET_DAY(rtc_date));
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
now.tm_hour = bcd2bin(STM32_RTC_GET_HOUR(rtc_time));
|
|
|
|
|
now.tm_min = bcd2bin(STM32_RTC_GET_MINUTE(rtc_time));
|
|
|
|
|
now.tm_sec = bcd2bin(STM32_RTC_GET_SECOND(rtc_time));
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2020-11-30 10:46:56 -06:00
|
|
|
ts = timeutil_timegm(&now);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
/* Return number of seconds since RTC init */
|
|
|
|
|
ts -= T_TIME_OFFSET;
|
|
|
|
|
|
2019-03-15 14:32:18 +01:00
|
|
|
__ASSERT(sizeof(time_t) == 8, "unexpected time_t definition");
|
2023-08-16 11:45:55 +02:00
|
|
|
|
2023-09-26 08:56:26 +02:00
|
|
|
ticks = ts * counter_get_frequency(dev);
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
/* The RTC counts up, except for the subsecond register which counts
|
|
|
|
|
* down starting from the sync prescaler value. Add already counted
|
|
|
|
|
* ticks.
|
|
|
|
|
*/
|
2025-12-17 20:11:37 -05:00
|
|
|
ticks += RTC_SYNCPRE - rtc_subsecond;
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
|
2023-09-26 08:56:33 +02:00
|
|
|
return ticks;
|
|
|
|
|
}
|
|
|
|
|
#else /* defined(COUNTER_NO_DATE) */
|
|
|
|
|
tick_t rtc_stm32_read(const struct device *dev)
|
|
|
|
|
{
|
2025-06-23 08:26:39 +02:00
|
|
|
uint32_t ticks;
|
2023-09-26 08:56:33 +02:00
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
ticks = LL_RTC_TIME_Get(STM32_ARG(RTC));
|
2023-09-26 08:56:33 +02:00
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
return ticks;
|
|
|
|
|
}
|
2023-09-26 08:56:33 +02:00
|
|
|
#endif /* !defined(COUNTER_NO_DATE) */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_get_value(const struct device *dev, uint32_t *ticks)
|
2023-08-16 11:45:55 +02:00
|
|
|
{
|
|
|
|
|
*ticks = (uint32_t)rtc_stm32_read(dev);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
static int rtc_stm32_get_value_64(const struct device *dev, uint64_t *ticks)
|
2020-01-18 15:24:32 +01:00
|
|
|
{
|
|
|
|
|
*ticks = rtc_stm32_read(dev);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
static void rtc_stm32_set_int_pending(void)
|
|
|
|
|
{
|
2026-08-14 23:16:26 -04:00
|
|
|
k_irq_set_pending(DT_INST_IRQN(0));
|
2023-08-16 11:45:55 +02:00
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2020-01-18 15:24:32 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_set_alarm(const struct device *dev, uint8_t chan_id,
|
2018-11-29 11:08:41 +01:00
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
|
{
|
2022-08-31 20:32:57 +09:00
|
|
|
#if !defined(COUNTER_NO_DATE)
|
2018-11-29 11:08:41 +01:00
|
|
|
struct tm alarm_tm;
|
2023-08-16 11:45:55 +02:00
|
|
|
time_t alarm_val_s;
|
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
uint32_t alarm_val_ss;
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
|
|
|
|
uint32_t remain;
|
|
|
|
|
#endif
|
2022-01-18 14:01:39 +01:00
|
|
|
struct rtc_stm32_data *data = dev->data;
|
2025-09-22 16:43:56 +02:00
|
|
|
int ret = 0;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
tick_t now = rtc_stm32_read(dev);
|
|
|
|
|
tick_t ticks = alarm_cfg->ticks;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
if (data->callback != NULL) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_DBG("Alarm busy");
|
2018-11-29 11:08:41 +01:00
|
|
|
return -EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data->callback = alarm_cfg->callback;
|
|
|
|
|
data->user_data = alarm_cfg->user_data;
|
|
|
|
|
|
2022-08-31 20:32:57 +09:00
|
|
|
#if !defined(COUNTER_NO_DATE)
|
2019-05-20 09:46:19 +02:00
|
|
|
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
|
2020-05-14 15:49:24 +02:00
|
|
|
/* Add +1 in order to compensate the partially started tick.
|
|
|
|
|
* Alarm will expire between requested ticks and ticks+1.
|
|
|
|
|
* In case only 1 tick is requested, it will avoid
|
|
|
|
|
* that tick+1 event occurs before alarm setting is finished.
|
|
|
|
|
*/
|
|
|
|
|
ticks += now + 1;
|
2023-08-16 11:45:55 +02:00
|
|
|
alarm_val_s = (time_t)(ticks / counter_get_frequency(dev)) + T_TIME_OFFSET;
|
2021-02-12 14:28:34 +02:00
|
|
|
} else {
|
2023-08-16 11:45:55 +02:00
|
|
|
alarm_val_s = (time_t)(ticks / counter_get_frequency(dev));
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
2025-09-22 16:43:56 +02:00
|
|
|
|
|
|
|
|
gmtime_r(&alarm_val_s, &alarm_tm);
|
|
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
alarm_val_ss = ticks % counter_get_frequency(dev);
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_DBG("Set Alarm: %llu", ticks);
|
|
|
|
|
#else /* !CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
LOG_DBG("Set Alarm: %d", ticks);
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
|
|
|
|
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
|
|
|
|
|
remain = ticks + now + 1;
|
|
|
|
|
} else {
|
|
|
|
|
remain = ticks;
|
|
|
|
|
}
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-08-31 20:32:57 +09:00
|
|
|
/* In F1X, an interrupt occurs when the counter expires,
|
|
|
|
|
* not when the counter matches, so set -1
|
|
|
|
|
*/
|
|
|
|
|
remain--;
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
stm32_backup_domain_enable_access();
|
|
|
|
|
|
2022-08-31 20:32:57 +09:00
|
|
|
#if !defined(COUNTER_NO_DATE)
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
ll_disable_alarm();
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
/* Configure the Alarm registers */
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ALMA_DisableWeekday(STM32_ARG(RTC));
|
|
|
|
|
LL_RTC_ALMA_SetDay(STM32_ARG(RTC, bin2bcd(alarm_tm.tm_mday)));
|
|
|
|
|
LL_RTC_ALMA_ConfigTime(STM32_ARG(RTC, STM32_RTC_TIME_FORMAT_AM_24H,
|
|
|
|
|
bin2bcd(alarm_tm.tm_hour),
|
|
|
|
|
bin2bcd(alarm_tm.tm_min),
|
|
|
|
|
bin2bcd(alarm_tm.tm_sec)));
|
|
|
|
|
LL_RTC_ALMA_SetMask(STM32_ARG(RTC, LL_RTC_ALMA_MASK_NONE));
|
|
|
|
|
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2022-08-31 20:32:57 +09:00
|
|
|
#else
|
2025-09-22 16:43:56 +02:00
|
|
|
/* Set Initialization mode */
|
|
|
|
|
ret = rtc_stm32_enter_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
goto out_disable_bkup_access;
|
|
|
|
|
}
|
2025-07-02 11:57:20 +02:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
/* Set the alarm */
|
|
|
|
|
LL_RTC_ALARM_Set(RTC, remain);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
ret = rtc_stm32_exit_init_mode();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
goto out_disable_bkup_access;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
2025-09-22 16:43:56 +02:00
|
|
|
#if HW_SUBSECOND_SUPPORT
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
/* Care about all bits of the subsecond register */
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ALMA_SetSubSecondMask(STM32_ARG(RTC, 0xF));
|
|
|
|
|
LL_RTC_ALMA_SetSubSecond(STM32_ARG(RTC, RTC_SYNCPRE - alarm_val_ss));
|
2025-09-22 16:43:56 +02:00
|
|
|
#else
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_ALMA_SetSubSecondMask(STM32_ARG(RTC, 0));
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2025-09-22 16:43:56 +02:00
|
|
|
#endif /* HW_SUBSECOND_SUPPORT */
|
2025-10-01 14:37:28 +02:00
|
|
|
ll_enable_alarm();
|
|
|
|
|
ll_clear_alarm_flag();
|
|
|
|
|
ll_enable_interrupt_alarm();
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
#if defined(COUNTER_NO_DATE)
|
|
|
|
|
out_disable_bkup_access:
|
|
|
|
|
#endif
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_disable_access();
|
|
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
/* The reference manual says:
|
|
|
|
|
* "Each change of the RTC_CR register is taken into account after
|
|
|
|
|
* 1 to 2 RTCCLK clock cycles due to clock synchronization."
|
|
|
|
|
* It means we need at least two cycles after programming the CR
|
|
|
|
|
* register. It is confirmed experimentally.
|
|
|
|
|
*
|
|
|
|
|
* It should happen only if one tick alarm is requested and a tick
|
|
|
|
|
* occurs while processing the function. Trigger the irq manually in
|
|
|
|
|
* this case.
|
|
|
|
|
*/
|
|
|
|
|
now = rtc_stm32_read(dev);
|
|
|
|
|
if ((ticks - now < 2) || (now > ticks)) {
|
2026-02-04 21:39:57 +01:00
|
|
|
data->irq_on_late = true;
|
2023-08-16 11:45:55 +02:00
|
|
|
rtc_stm32_set_int_pending();
|
|
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
return ret;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2022-01-18 14:01:39 +01:00
|
|
|
struct rtc_stm32_data *data = dev->data;
|
|
|
|
|
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_enable_access();
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
|
|
|
|
ll_clear_alarm_flag();
|
|
|
|
|
ll_disable_interrupt_alarm();
|
|
|
|
|
ll_disable_alarm();
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_disable_access();
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-01-18 14:01:39 +01:00
|
|
|
data->callback = NULL;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t rtc_stm32_get_pending_int(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2025-10-01 14:37:28 +02:00
|
|
|
return ll_is_active_alarm() != 0;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t rtc_stm32_get_top_value(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
return info->max_top_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_set_top_value(const struct device *dev,
|
2019-03-21 16:31:16 +01:00
|
|
|
const struct counter_top_cfg *cfg)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2019-03-21 16:31:16 +01:00
|
|
|
if ((cfg->ticks != info->max_top_value) ||
|
|
|
|
|
!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
|
2018-11-29 11:08:41 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
void rtc_stm32_isr(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2022-01-18 14:01:39 +01:00
|
|
|
struct rtc_stm32_data *data = dev->data;
|
2018-11-29 11:08:41 +01:00
|
|
|
counter_alarm_callback_t alarm_callback = data->callback;
|
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t now = rtc_stm32_read(dev);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-10-01 14:37:28 +02:00
|
|
|
if (ll_is_active_alarm() != 0
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2025-10-01 14:37:28 +02:00
|
|
|
|| (data->irq_on_late && ll_isenabled_interrupt_alarm())
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
) {
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_enable_access();
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
|
|
|
|
ll_clear_alarm_flag();
|
|
|
|
|
ll_disable_interrupt_alarm();
|
|
|
|
|
ll_disable_alarm();
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2025-07-02 11:57:20 +02:00
|
|
|
stm32_backup_domain_disable_access();
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2026-02-04 21:39:57 +01:00
|
|
|
data->irq_on_late = false;
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
|
if (alarm_callback != NULL) {
|
|
|
|
|
data->callback = NULL;
|
|
|
|
|
alarm_callback(dev, 0, now, data->user_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 15:28:28 +02:00
|
|
|
#if defined(RTC_EXTI_LINE_NUM)
|
|
|
|
|
stm32_exti_clear_pending(RTC_EXTI_LINE_NUM);
|
|
|
|
|
#endif /* defined(RTC_EXTI_LINE_NUM) */
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int rtc_stm32_init(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2022-08-22 10:36:10 +02:00
|
|
|
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
2022-01-18 14:01:39 +01:00
|
|
|
const struct rtc_stm32_config *cfg = dev->config;
|
|
|
|
|
struct rtc_stm32_data *data = dev->data;
|
2025-06-23 08:26:39 +02:00
|
|
|
int ret = -EIO;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-01-18 14:01:39 +01:00
|
|
|
data->callback = NULL;
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-09-05 14:57:25 +02:00
|
|
|
/* Enable RTC bus clock */
|
2023-03-28 08:24:07 +02:00
|
|
|
if (clock_control_on(clk, (clock_control_subsys_t) &cfg->pclken[0]) != 0) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_ERR("clock op failed");
|
2021-02-11 11:49:24 -06:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2022-09-05 14:57:25 +02:00
|
|
|
/* Enable Backup access */
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
|
2025-06-23 08:26:39 +02:00
|
|
|
|
|
|
|
|
stm32_backup_domain_enable_access();
|
2019-05-24 10:25:51 +02:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE
|
|
|
|
|
/* Must be configured before selecting the RTC clock source */
|
|
|
|
|
LL_RCC_SetRTC_HSEPrescaler(cfg->hse_prescaler);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-05 14:57:25 +02:00
|
|
|
/* Enable RTC clock source */
|
|
|
|
|
if (clock_control_configure(clk,
|
2023-03-28 08:24:07 +02:00
|
|
|
(clock_control_subsys_t) &cfg->pclken[1],
|
2022-09-05 14:57:25 +02:00
|
|
|
NULL) != 0) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_ERR("clock configure failed");
|
2025-06-23 08:26:39 +02:00
|
|
|
goto out_disable_bkup_access;
|
2019-06-04 10:52:23 -04:00
|
|
|
}
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2023-11-07 14:55:40 +01:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32WBAX)
|
2025-12-16 16:26:42 +01:00
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32U3X
|
|
|
|
|
/* STM32U3 series uses LL_RCC_RTC_ClockEnable instead of LL_RCC_EnableRTC */
|
|
|
|
|
LL_RCC_RTC_ClockEnable();
|
|
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_EnableRTC();
|
2025-12-16 16:26:42 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32U3X */
|
|
|
|
|
#endif /* !CONFIG_SOC_SERIES_STM32WBAX */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
|
|
|
|
|
|
2021-03-12 11:24:24 +02:00
|
|
|
#if !defined(CONFIG_COUNTER_RTC_STM32_SAVE_VALUE_BETWEEN_RESETS)
|
2025-09-22 16:43:56 +02:00
|
|
|
ret = rtc_stm32_deinit();
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to deinit RTC");
|
2025-06-23 08:26:39 +02:00
|
|
|
goto out_disable_bkup_access;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
2021-03-12 11:24:24 +02:00
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-09-22 16:43:56 +02:00
|
|
|
ret = rtc_stm32_configure(dev);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to init RTC");
|
2025-06-23 08:26:39 +02:00
|
|
|
goto out_disable_bkup_access;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RTC_CR_BYPSHAD
|
2025-10-01 14:37:28 +02:00
|
|
|
LL_RTC_DisableWriteProtection(STM32_ARG(RTC));
|
|
|
|
|
STM32_RTC_EnableBypassShadowReg(STM32_ARG(RTC));
|
|
|
|
|
LL_RTC_EnableWriteProtection(STM32_ARG(RTC));
|
2018-11-29 11:08:41 +01:00
|
|
|
#endif /* RTC_CR_BYPSHAD */
|
|
|
|
|
|
2026-05-05 15:28:28 +02:00
|
|
|
#if defined(RTC_EXTI_LINE_NUM)
|
|
|
|
|
/* Trigger NVIC IRQ on RTC EXTI line rising edge */
|
|
|
|
|
ret = stm32_exti_enable(RTC_EXTI_LINE_NUM,
|
|
|
|
|
STM32_EXTI_TRIG_RISING,
|
|
|
|
|
STM32_EXTI_MODE_IT);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
LOG_ERR("Failed to enable RTC EXTI line");
|
|
|
|
|
goto out_disable_bkup_access;
|
|
|
|
|
}
|
|
|
|
|
#endif /* defined(RTC_EXTI_LINE_NUM) */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2025-06-23 08:26:39 +02:00
|
|
|
out_disable_bkup_access:
|
|
|
|
|
stm32_backup_domain_disable_access();
|
|
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
rtc_stm32_irq_config(dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct rtc_stm32_data rtc_data;
|
|
|
|
|
|
2022-09-05 14:57:25 +02:00
|
|
|
static const struct stm32_pclken rtc_clk[] = STM32_DT_INST_CLOCKS(0);
|
|
|
|
|
|
2025-05-30 14:25:07 +01:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE
|
|
|
|
|
#if STM32_HSE_FREQ % MHZ(1) != 0
|
|
|
|
|
#error RTC clock source HSE frequency should be whole MHz
|
|
|
|
|
#elif STM32_HSE_FREQ < MHZ(16) && defined(LL_RCC_RTC_HSE_DIV_16)
|
|
|
|
|
#define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_16
|
|
|
|
|
#define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 16)
|
|
|
|
|
#elif STM32_HSE_FREQ < MHZ(32) && defined(LL_RCC_RTC_HSE_DIV_32)
|
|
|
|
|
#define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_32
|
|
|
|
|
#define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 32)
|
|
|
|
|
#elif STM32_HSE_FREQ < MHZ(64) && defined(LL_RCC_RTC_HSE_DIV_64)
|
|
|
|
|
#define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_64
|
|
|
|
|
#define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 64)
|
|
|
|
|
#else
|
|
|
|
|
#error RTC does not support HSE frequency
|
|
|
|
|
#endif
|
|
|
|
|
#define RTC_HSE_ASYNC_PRESCALER 125
|
|
|
|
|
#define RTC_HSE_SYNC_PRESCALER (RTC_HSE_FREQUENCY / RTC_HSE_ASYNC_PRESCALER)
|
|
|
|
|
#endif /* DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE */
|
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
static const struct rtc_stm32_config rtc_config = {
|
|
|
|
|
.counter_info = {
|
|
|
|
|
.max_top_value = UINT32_MAX,
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
/* freq = 1Hz for not subsec based driver */
|
2023-08-16 09:43:14 +02:00
|
|
|
.freq = RTCCLK_FREQ / ((RTC_ASYNCPRE + 1) * (RTC_SYNCPRE + 1)),
|
2025-05-30 14:25:07 +01:00
|
|
|
#else /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2023-08-16 11:45:55 +02:00
|
|
|
.freq = RTCCLK_FREQ / (RTC_ASYNCPRE + 1),
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2019-05-17 12:10:55 +02:00
|
|
|
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
|
2018-11-29 11:08:41 +01:00
|
|
|
.channels = 1,
|
|
|
|
|
},
|
2025-05-30 14:25:07 +01:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_LSI || \
|
|
|
|
|
DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_LSE
|
2025-09-22 16:43:56 +02:00
|
|
|
.async_prescaler = DT_INST_PROP_OR(0, async_prescaler, RTC_ASYNCPRE),
|
2022-08-31 20:32:57 +09:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-09-22 16:43:56 +02:00
|
|
|
.sync_prescaler = DT_INST_PROP_OR(0, sync_prescaler, RTC_SYNCPRE),
|
2025-05-30 14:25:07 +01:00
|
|
|
#endif /* !CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
|
#elif DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE
|
2026-07-19 18:55:44 +00:00
|
|
|
.async_prescaler = DT_INST_PROP_OR(0, async_prescaler, RTC_HSE_ASYNC_PRESCALER - 1),
|
2025-05-30 14:25:07 +01:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
2025-09-22 16:43:56 +02:00
|
|
|
.sync_prescaler = DT_INST_PROP_OR(0, hse_prescaler, RTC_HSE_SYNC_PRESCALER - 1),
|
2025-05-30 14:25:07 +01:00
|
|
|
#endif /* !CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
|
#else
|
|
|
|
|
#error Invalid RTC SRC
|
|
|
|
|
#endif
|
2022-09-05 14:57:25 +02:00
|
|
|
.pclken = rtc_clk,
|
2025-05-30 14:25:07 +01:00
|
|
|
#if DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE
|
|
|
|
|
.hse_prescaler = DT_INST_PROP_OR(0, hse_prescaler, RTC_HSE_PRESCALER),
|
|
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-26 10:05:39 +01:00
|
|
|
#ifdef CONFIG_PM_DEVICE
|
|
|
|
|
static int rtc_stm32_pm_action(const struct device *dev,
|
|
|
|
|
enum pm_device_action action)
|
|
|
|
|
{
|
|
|
|
|
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
|
|
|
|
const struct rtc_stm32_config *cfg = dev->config;
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case PM_DEVICE_ACTION_RESUME:
|
|
|
|
|
/* Enable RTC bus clock */
|
|
|
|
|
if (clock_control_on(clk, (clock_control_subsys_t) &cfg->pclken[0]) != 0) {
|
2025-09-22 16:43:56 +02:00
|
|
|
LOG_ERR("clock op failed");
|
2024-01-26 10:05:39 +01:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PM_DEVICE_ACTION_SUSPEND:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2024-11-27 21:21:35 +01:00
|
|
|
static DEVICE_API(counter, rtc_stm32_driver_api) = {
|
2023-09-26 08:56:09 +02:00
|
|
|
.start = rtc_stm32_start,
|
|
|
|
|
.stop = rtc_stm32_stop,
|
|
|
|
|
.get_value = rtc_stm32_get_value,
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|
.get_value_64 = rtc_stm32_get_value_64,
|
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2023-09-26 08:56:09 +02:00
|
|
|
.set_alarm = rtc_stm32_set_alarm,
|
|
|
|
|
.cancel_alarm = rtc_stm32_cancel_alarm,
|
|
|
|
|
.set_top_value = rtc_stm32_set_top_value,
|
|
|
|
|
.get_pending_int = rtc_stm32_get_pending_int,
|
|
|
|
|
.get_top_value = rtc_stm32_get_top_value,
|
2018-11-29 11:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-26 10:05:39 +01:00
|
|
|
PM_DEVICE_DT_INST_DEFINE(0, rtc_stm32_pm_action);
|
|
|
|
|
|
|
|
|
|
DEVICE_DT_INST_DEFINE(0, &rtc_stm32_init, PM_DEVICE_DT_INST_GET(0),
|
2018-11-29 11:08:41 +01:00
|
|
|
&rtc_data, &rtc_config, PRE_KERNEL_1,
|
2021-10-20 15:15:14 -05:00
|
|
|
CONFIG_COUNTER_INIT_PRIORITY, &rtc_stm32_driver_api);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void rtc_stm32_irq_config(const struct device *dev)
|
2018-11-29 11:08:41 +01:00
|
|
|
{
|
2020-03-24 14:28:48 -05:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
|
DT_INST_IRQ(0, priority),
|
2020-12-15 12:58:44 -06:00
|
|
|
rtc_stm32_isr, DEVICE_DT_INST_GET(0), 0);
|
2020-03-24 14:28:48 -05:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|