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 20:28:48 +01: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>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/kernel.h>
|
2018-11-29 11:08:41 +01:00
|
|
|
#include <soc.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
|
|
|
|
2020-06-23 09:48:07 +02:00
|
|
|
#include "stm32_hsem.h"
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
2020-11-30 16:07:14 +01: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
|
|
|
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32L4X)
|
|
|
|
#define RTC_EXTI_LINE LL_EXTI_LINE_18
|
2023-03-17 13:24:58 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32C0X) \
|
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32G0X)
|
2020-07-24 11:01:36 +02:00
|
|
|
#define RTC_EXTI_LINE LL_EXTI_LINE_19
|
2018-11-29 11:08:41 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X) \
|
2020-07-24 12:31:46 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32F0X) \
|
2022-08-31 13:32:57 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32F1X) \
|
2020-06-03 10:10:25 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32F2X) \
|
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32F3X) \
|
2019-07-03 19:07:40 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32F7X) \
|
2019-09-04 10:47:06 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32WBX) \
|
2019-09-07 11:12:02 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32G4X) \
|
2020-02-27 10:25:02 +01:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32L0X) \
|
2020-03-06 14:41:11 +01:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32L1X) \
|
2022-11-27 14:40:13 +01:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32L5X) \
|
2021-04-27 23:53:42 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32H7X) \
|
2023-04-04 12:40:10 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32H5X) \
|
2021-04-27 23:53:42 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32WLX)
|
2018-11-29 11:08:41 +01:00
|
|
|
#define RTC_EXTI_LINE LL_EXTI_LINE_17
|
|
|
|
#endif
|
|
|
|
|
2022-08-31 13:32:57 +02: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 */
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
LL_RTC_InitTypeDef ll_rtc_config;
|
2022-09-05 14:57:25 +02:00
|
|
|
const struct stm32_pclken *pclken;
|
2018-11-29 11:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rtc_stm32_data {
|
|
|
|
counter_alarm_callback_t callback;
|
2020-05-27 18:26:57 +02: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
|
|
|
};
|
|
|
|
|
2022-08-31 13:32:57 +02:00
|
|
|
static inline ErrorStatus ll_func_init_alarm(RTC_TypeDef *rtc, uint32_t format,
|
|
|
|
LL_RTC_AlarmTypeDef *alarmStruct)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
return LL_RTC_ALARM_Init(rtc, format, alarmStruct);
|
|
|
|
#else
|
|
|
|
return LL_RTC_ALMA_Init(rtc, format, alarmStruct);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ll_func_clear_alarm_flag(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
LL_RTC_ClearFlag_ALR(rtc);
|
|
|
|
#else
|
|
|
|
LL_RTC_ClearFlag_ALRA(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t ll_func_is_active_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
return LL_RTC_IsActiveFlag_ALR(rtc);
|
|
|
|
#else
|
|
|
|
return LL_RTC_IsActiveFlag_ALRA(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ll_func_enable_interrupt_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
LL_RTC_EnableIT_ALR(rtc);
|
|
|
|
#else
|
|
|
|
LL_RTC_EnableIT_ALRA(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ll_func_disable_interrupt_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
LL_RTC_DisableIT_ALR(rtc);
|
|
|
|
#else
|
|
|
|
LL_RTC_DisableIT_ALRA(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
static inline uint32_t ll_func_isenabled_interrupt_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
return LL_RTC_IsEnabledIT_ALR(rtc);
|
|
|
|
#else
|
|
|
|
return LL_RTC_IsEnabledIT_ALRA(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
2022-08-31 13:32:57 +02:00
|
|
|
static inline void ll_func_enable_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
ARG_UNUSED(rtc);
|
|
|
|
#else
|
|
|
|
LL_RTC_ALMA_Enable(rtc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ll_func_disable_alarm(RTC_TypeDef *rtc)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F1X)
|
|
|
|
ARG_UNUSED(rtc);
|
|
|
|
#else
|
|
|
|
LL_RTC_ALMA_Disable(rtc);
|
|
|
|
#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
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2023-11-07 14:55:40 +01:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32WBAX)
|
|
|
|
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) {
|
2024-05-03 00:17:49 +02:00
|
|
|
LOG_ERR("RTC clock enabling failed\n");
|
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);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_EnableRTC();
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
|
2023-11-07 14:55:40 +01:00
|
|
|
#endif
|
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
|
|
|
{
|
2023-11-07 14:55:40 +01:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32WBAX)
|
|
|
|
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) {
|
|
|
|
LOG_ERR("RTC clock disabling failed\n");
|
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);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_DisableRTC();
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
|
2023-11-07 14:55:40 +01:00
|
|
|
#endif
|
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
|
|
|
|
uint32_t rtc_subseconds;
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2018-11-29 11:08:41 +01:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2024-03-13 09:32:50 +01:00
|
|
|
/* Enable Backup access */
|
|
|
|
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || \
|
|
|
|
defined(PWR_DBPCR_DBP) || defined(PWR_DBPR_DBP)
|
|
|
|
LL_PWR_EnableBkUpAccess();
|
|
|
|
#endif /* PWR_CR_DBP || PWR_CR1_DBP || PWR_DBPR_DBP */
|
|
|
|
|
2023-08-16 10:12:37 +02:00
|
|
|
/* Read time and date registers. Make sure value of the previous register
|
|
|
|
* hasn't been changed while reading the next one.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
rtc_date = LL_RTC_DATE_Get(RTC);
|
2023-08-16 11:45:55 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
do {
|
|
|
|
rtc_time = LL_RTC_TIME_Get(RTC);
|
|
|
|
rtc_subseconds = LL_RTC_TIME_GetSubSecond(RTC);
|
|
|
|
} while (rtc_time != LL_RTC_TIME_Get(RTC));
|
|
|
|
#else /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2023-08-16 10:12:37 +02:00
|
|
|
rtc_time = LL_RTC_TIME_Get(RTC);
|
2023-08-16 11:45:55 +02:00
|
|
|
#endif
|
2023-08-16 10:12:37 +02:00
|
|
|
|
|
|
|
} while (rtc_date != LL_RTC_DATE_Get(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 16:07:14 +01:00
|
|
|
/* time_t start: 1st, Jan, 1970 */
|
2019-03-25 14:34:12 +01:00
|
|
|
now.tm_year = 100 +
|
|
|
|
__LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_YEAR(rtc_date));
|
|
|
|
/* tm_mon allowed values are 0-11 */
|
|
|
|
now.tm_mon = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_MONTH(rtc_date)) - 1;
|
2018-11-29 11:08:41 +01:00
|
|
|
now.tm_mday = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_DAY(rtc_date));
|
|
|
|
|
|
|
|
now.tm_hour = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_HOUR(rtc_time));
|
|
|
|
now.tm_min = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_MINUTE(rtc_time));
|
|
|
|
now.tm_sec = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_SECOND(rtc_time));
|
|
|
|
|
2020-11-30 17:46:56 +01: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.
|
|
|
|
*/
|
|
|
|
ticks += RTC_SYNCPRE - rtc_subseconds;
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
uint32_t rtc_time, ticks;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2024-03-13 09:32:50 +01:00
|
|
|
/* Enable Backup access */
|
|
|
|
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || \
|
|
|
|
defined(PWR_DBPCR_DBP) || defined(PWR_DBPR_DBP)
|
|
|
|
LL_PWR_EnableBkUpAccess();
|
|
|
|
#endif /* PWR_CR_DBP || PWR_CR1_DBP || PWR_DBPR_DBP */
|
|
|
|
|
2023-09-26 08:56:33 +02:00
|
|
|
rtc_time = LL_RTC_TIME_Get(RTC);
|
|
|
|
|
2022-08-31 13:32:57 +02:00
|
|
|
ticks = rtc_time;
|
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)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(DT_INST_IRQN(0));
|
|
|
|
}
|
|
|
|
#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 13:32:57 +02: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 13:32:57 +02:00
|
|
|
#else
|
|
|
|
uint32_t remain;
|
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_AlarmTypeDef rtc_alarm;
|
2022-01-18 14:01:39 +01:00
|
|
|
struct rtc_stm32_data *data = dev->data;
|
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) {
|
|
|
|
LOG_DBG("Alarm busy\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data->callback = alarm_cfg->callback;
|
|
|
|
data->user_data = alarm_cfg->user_data;
|
|
|
|
|
2022-08-31 13:32:57 +02: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 13:28:34 +01: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
|
|
|
}
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
alarm_val_ss = ticks % counter_get_frequency(dev);
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
2022-08-31 13:32:57 +02: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 13:32:57 +02:00
|
|
|
/* In F1X, an interrupt occurs when the counter expires,
|
|
|
|
* not when the counter matches, so set -1
|
|
|
|
*/
|
|
|
|
remain--;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(COUNTER_NO_DATE)
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
2018-11-29 11:08:41 +01:00
|
|
|
LOG_DBG("Set Alarm: %d\n", ticks);
|
2023-08-16 11:45:55 +02:00
|
|
|
#else /* !CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
LOG_DBG("Set Alarm: %llu\n", ticks);
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
gmtime_r(&alarm_val_s, &alarm_tm);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
/* Apply ALARM_A */
|
|
|
|
rtc_alarm.AlarmTime.TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
|
|
|
|
rtc_alarm.AlarmTime.Hours = alarm_tm.tm_hour;
|
|
|
|
rtc_alarm.AlarmTime.Minutes = alarm_tm.tm_min;
|
|
|
|
rtc_alarm.AlarmTime.Seconds = alarm_tm.tm_sec;
|
|
|
|
|
|
|
|
rtc_alarm.AlarmMask = LL_RTC_ALMA_MASK_NONE;
|
|
|
|
rtc_alarm.AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
|
|
|
|
rtc_alarm.AlarmDateWeekDay = alarm_tm.tm_mday;
|
2022-08-31 13:32:57 +02:00
|
|
|
#else
|
|
|
|
rtc_alarm.AlarmTime.Hours = remain / 3600;
|
|
|
|
remain -= rtc_alarm.AlarmTime.Hours * 3600;
|
|
|
|
rtc_alarm.AlarmTime.Minutes = remain / 60;
|
|
|
|
remain -= rtc_alarm.AlarmTime.Minutes * 60;
|
|
|
|
rtc_alarm.AlarmTime.Seconds = remain;
|
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
LL_RTC_DisableWriteProtection(RTC);
|
2022-08-31 13:32:57 +02:00
|
|
|
ll_func_disable_alarm(RTC);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_EnableWriteProtection(RTC);
|
|
|
|
|
2022-08-31 13:32:57 +02:00
|
|
|
if (ll_func_init_alarm(RTC, LL_RTC_FORMAT_BIN, &rtc_alarm) != SUCCESS) {
|
2018-11-29 11:08:41 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_RTC_DisableWriteProtection(RTC);
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
/* Care about all bits of the subsecond register */
|
|
|
|
LL_RTC_ALMA_SetSubSecondMask(RTC, 0xF);
|
|
|
|
LL_RTC_ALMA_SetSubSecond(RTC, RTC_SYNCPRE - alarm_val_ss);
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
2022-08-31 13:32:57 +02:00
|
|
|
ll_func_enable_alarm(RTC);
|
|
|
|
ll_func_clear_alarm_flag(RTC);
|
|
|
|
ll_func_enable_interrupt_alarm(RTC);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_EnableWriteProtection(RTC);
|
|
|
|
|
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)) {
|
|
|
|
data->irq_on_late = 1;
|
|
|
|
rtc_stm32_set_int_pending();
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_DisableWriteProtection(RTC);
|
2022-08-31 13:32:57 +02:00
|
|
|
ll_func_clear_alarm_flag(RTC);
|
|
|
|
ll_func_disable_interrupt_alarm(RTC);
|
|
|
|
ll_func_disable_alarm(RTC);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_EnableWriteProtection(RTC);
|
|
|
|
|
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
|
|
|
{
|
2022-08-31 13:32:57 +02:00
|
|
|
return ll_func_is_active_alarm(RTC) != 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 18:26:57 +02:00
|
|
|
uint32_t now = rtc_stm32_read(dev);
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2023-08-16 11:45:55 +02:00
|
|
|
if (ll_func_is_active_alarm(RTC) != 0
|
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
|| (data->irq_on_late && ll_func_isenabled_interrupt_alarm(RTC))
|
|
|
|
#endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
) {
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
LL_RTC_DisableWriteProtection(RTC);
|
2022-08-31 13:32:57 +02:00
|
|
|
ll_func_clear_alarm_flag(RTC);
|
|
|
|
ll_func_disable_interrupt_alarm(RTC);
|
|
|
|
ll_func_disable_alarm(RTC);
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RTC_EnableWriteProtection(RTC);
|
2023-08-16 11:45:55 +02:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
|
|
|
|
data->irq_on_late = 0;
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 13:43:37 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32H7X) && defined(CONFIG_CPU_CORTEX_M4)
|
|
|
|
LL_C2_EXTI_ClearFlag_0_31(RTC_EXTI_LINE);
|
2023-03-17 13:24:58 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32C0X) \
|
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32G0X) \
|
2023-04-04 12:40:10 +02:00
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32L5X) \
|
|
|
|
|| defined(CONFIG_SOC_SERIES_STM32H5X)
|
2020-07-24 11:01:36 +02:00
|
|
|
LL_EXTI_ClearRisingFlag_0_31(RTC_EXTI_LINE);
|
2023-11-07 14:55:40 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32U5X) || defined(CONFIG_SOC_SERIES_STM32WBAX)
|
2022-08-09 10:11:12 +02:00
|
|
|
/* in STM32U5 family RTC is not connected to EXTI */
|
2020-04-22 13:43:37 +02:00
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_EXTI_ClearFlag_0_31(RTC_EXTI_LINE);
|
2020-04-22 13:43:37 +02:00
|
|
|
#endif
|
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;
|
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-08-08 13:32:12 +02:00
|
|
|
if (!device_is_ready(clk)) {
|
|
|
|
LOG_ERR("clock control device not ready");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-11 18:49:24 +01:00
|
|
|
LOG_ERR("clock op failed\n");
|
|
|
|
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);
|
2023-04-04 12:40:10 +02:00
|
|
|
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || \
|
|
|
|
defined(PWR_DBPCR_DBP) || defined(PWR_DBPR_DBP)
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_PWR_EnableBkUpAccess();
|
2023-03-17 13:24:42 +01:00
|
|
|
#endif /* PWR_CR_DBP || PWR_CR1_DBP || PWR_DBPR_DBP */
|
2019-05-24 10:25:51 +02:00
|
|
|
|
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) {
|
|
|
|
LOG_ERR("clock configure failed\n");
|
|
|
|
return -EIO;
|
2019-06-04 16:52:23 +02:00
|
|
|
}
|
2018-11-29 11:08:41 +01:00
|
|
|
|
2023-11-07 14:55:40 +01:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32WBAX)
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_RCC_EnableRTC();
|
2023-11-07 14:55:40 +01:00
|
|
|
#endif
|
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 10:24:24 +01:00
|
|
|
#if !defined(CONFIG_COUNTER_RTC_STM32_SAVE_VALUE_BETWEEN_RESETS)
|
2018-11-29 11:08:41 +01:00
|
|
|
if (LL_RTC_DeInit(RTC) != SUCCESS) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2021-03-12 10:24:24 +01:00
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
if (LL_RTC_Init(RTC, ((LL_RTC_InitTypeDef *)
|
|
|
|
&cfg->ll_rtc_config)) != SUCCESS) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RTC_CR_BYPSHAD
|
|
|
|
LL_RTC_DisableWriteProtection(RTC);
|
|
|
|
LL_RTC_EnableShadowRegBypass(RTC);
|
|
|
|
LL_RTC_EnableWriteProtection(RTC);
|
|
|
|
#endif /* RTC_CR_BYPSHAD */
|
|
|
|
|
2020-04-22 13:43:37 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32H7X) && defined(CONFIG_CPU_CORTEX_M4)
|
|
|
|
LL_C2_EXTI_EnableIT_0_31(RTC_EXTI_LINE);
|
2022-08-09 10:11:12 +02:00
|
|
|
LL_EXTI_EnableRisingTrig_0_31(RTC_EXTI_LINE);
|
2023-11-07 14:55:40 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32U5X) || defined(CONFIG_SOC_SERIES_STM32WBAX)
|
2022-08-09 10:11:12 +02:00
|
|
|
/* in STM32U5 family RTC is not connected to EXTI */
|
2020-04-22 13:43:37 +02:00
|
|
|
#else
|
2018-11-29 11:08:41 +01:00
|
|
|
LL_EXTI_EnableIT_0_31(RTC_EXTI_LINE);
|
|
|
|
LL_EXTI_EnableRisingTrig_0_31(RTC_EXTI_LINE);
|
2022-08-09 10:11:12 +02:00
|
|
|
#endif
|
2018-11-29 11:08:41 +01:00
|
|
|
|
|
|
|
rtc_stm32_irq_config(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rtc_stm32_data rtc_data;
|
|
|
|
|
2022-09-05 14:57:25 +02:00
|
|
|
#if DT_INST_NUM_CLOCKS(0) == 1
|
2023-01-04 16:24:23 +01:00
|
|
|
#warning STM32 RTC needs a kernel source clock. Please define it in dts file
|
2022-09-05 14:57:25 +02:00
|
|
|
static const struct stm32_pclken rtc_clk[] = {
|
|
|
|
STM32_CLOCK_INFO(0, DT_DRV_INST(0)),
|
2023-01-04 16:24:23 +01:00
|
|
|
/* Use Kconfig to configure source clocks fields (Deprecated) */
|
2022-09-05 14:57:25 +02:00
|
|
|
/* Fortunately, values are consistent across enabled series */
|
2023-01-04 16:24:23 +01:00
|
|
|
#ifdef CONFIG_COUNTER_RTC_STM32_CLOCK_LSE
|
2022-09-05 14:57:25 +02:00
|
|
|
{.bus = STM32_SRC_LSE, .enr = RTC_SEL(1)}
|
2023-01-04 16:24:23 +01:00
|
|
|
#else
|
|
|
|
{.bus = STM32_SRC_LSI, .enr = RTC_SEL(2)}
|
2022-09-05 14:57:25 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
static const struct stm32_pclken rtc_clk[] = STM32_DT_INST_CLOCKS(0);
|
|
|
|
#endif
|
|
|
|
|
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)),
|
2023-08-16 11:45:55 +02:00
|
|
|
#else /* !CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
|
|
|
|
.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,
|
|
|
|
},
|
|
|
|
.ll_rtc_config = {
|
2023-08-16 09:43:14 +02:00
|
|
|
.AsynchPrescaler = RTC_ASYNCPRE,
|
2022-08-31 13:32:57 +02:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32F1X)
|
2018-11-29 11:08:41 +01:00
|
|
|
.HourFormat = LL_RTC_HOURFORMAT_24HOUR,
|
2023-08-16 09:43:14 +02:00
|
|
|
.SynchPrescaler = RTC_SYNCPRE,
|
2022-08-31 13:32:57 +02:00
|
|
|
#else /* CONFIG_SOC_SERIES_STM32F1X */
|
|
|
|
.OutPutSource = LL_RTC_CALIB_OUTPUT_NONE,
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32F1X */
|
2018-11-29 11:08:41 +01:00
|
|
|
},
|
2022-09-05 14:57:25 +02:00
|
|
|
.pclken = rtc_clk,
|
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) {
|
|
|
|
LOG_ERR("clock op failed\n");
|
|
|
|
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
|
|
|
|
|
|
|
static const struct counter_driver_api 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 22:15:14 +02: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 20:28:48 +01:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
DT_INST_IRQ(0, priority),
|
2020-12-15 19:58:44 +01:00
|
|
|
rtc_stm32_isr, DEVICE_DT_INST_GET(0), 0);
|
2020-03-24 20:28:48 +01:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-11-29 11:08:41 +01:00
|
|
|
}
|