zephyr/drivers/counter/counter_ll_stm32_rtc.c

437 lines
11 KiB
C
Raw Normal View History

/*
* 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
*
*/
#define DT_DRV_COMPAT st_stm32_rtc
#include <time.h>
#include <drivers/clock_control/stm32_clock_control.h>
#include <drivers/clock_control.h>
#include <sys/util.h>
#include <kernel.h>
#include <soc.h>
#include <stm32_ll_exti.h>
#include <stm32_ll_pwr.h>
#include <stm32_ll_rcc.h>
#include <stm32_ll_rtc.h>
#include <drivers/counter.h>
#include <sys/timeutil.h>
#include <logging/log.h>
#include "stm32_hsem.h"
LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
/* Seconds from 1970-01-01T00:00:00 to 2000-01-01T00:00:00 */
#define T_TIME_OFFSET 946684800
#if defined(CONFIG_SOC_SERIES_STM32L4X)
#define RTC_EXTI_LINE LL_EXTI_LINE_18
#elif defined(CONFIG_SOC_SERIES_STM32G0X)
#define RTC_EXTI_LINE LL_EXTI_LINE_19
#elif defined(CONFIG_SOC_SERIES_STM32F4X) \
|| defined(CONFIG_SOC_SERIES_STM32F0X) \
|| defined(CONFIG_SOC_SERIES_STM32F2X) \
|| defined(CONFIG_SOC_SERIES_STM32F3X) \
|| defined(CONFIG_SOC_SERIES_STM32F7X) \
|| defined(CONFIG_SOC_SERIES_STM32WBX) \
|| defined(CONFIG_SOC_SERIES_STM32G4X) \
|| defined(CONFIG_SOC_SERIES_STM32L0X) \
|| defined(CONFIG_SOC_SERIES_STM32L1X) \
|| defined(CONFIG_SOC_SERIES_STM32H7X)
#define RTC_EXTI_LINE LL_EXTI_LINE_17
#endif
struct rtc_stm32_config {
struct counter_config_info counter_info;
struct stm32_pclken pclken;
LL_RTC_InitTypeDef ll_rtc_config;
};
struct rtc_stm32_data {
counter_alarm_callback_t callback;
uint32_t ticks;
void *user_data;
};
#define DEV_DATA(dev) ((struct rtc_stm32_data *)(dev)->data)
#define DEV_CFG(dev) \
((const struct rtc_stm32_config * const)(dev)->config)
static void rtc_stm32_irq_config(const struct device *dev);
static int rtc_stm32_start(const struct device *dev)
{
ARG_UNUSED(dev);
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
LL_RCC_EnableRTC();
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
return 0;
}
static int rtc_stm32_stop(const struct device *dev)
{
ARG_UNUSED(dev);
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
LL_RCC_DisableRTC();
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
return 0;
}
static uint32_t rtc_stm32_read(const struct device *dev)
{
struct tm now = { 0 };
time_t ts;
uint32_t rtc_date, rtc_time, ticks;
ARG_UNUSED(dev);
/* Read time and date registers */
rtc_time = LL_RTC_TIME_Get(RTC);
rtc_date = LL_RTC_DATE_Get(RTC);
/* Convert calendar datetime to UNIX timestamp */
/* RTC start time: 1st, Jan, 2000 */
/* time_t start: 1st, Jan, 1970 */
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;
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));
ts = timeutil_timegm(&now);
/* Return number of seconds since RTC init */
ts -= T_TIME_OFFSET;
__ASSERT(sizeof(time_t) == 8, "unexpected time_t definition");
ticks = counter_us_to_ticks(dev, ts * USEC_PER_SEC);
return ticks;
}
static int rtc_stm32_get_value(const struct device *dev, uint32_t *ticks)
{
*ticks = rtc_stm32_read(dev);
return 0;
}
static int rtc_stm32_set_alarm(const struct device *dev, uint8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
struct tm alarm_tm;
time_t alarm_val;
LL_RTC_AlarmTypeDef rtc_alarm;
struct rtc_stm32_data *data = DEV_DATA(dev);
uint32_t now = rtc_stm32_read(dev);
uint32_t ticks = alarm_cfg->ticks;
if (data->callback != NULL) {
LOG_DBG("Alarm busy\n");
return -EBUSY;
}
data->callback = alarm_cfg->callback;
data->user_data = alarm_cfg->user_data;
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
/* 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;
}
LOG_DBG("Set Alarm: %d\n", ticks);
alarm_val = (time_t)(counter_ticks_to_us(dev, ticks) / USEC_PER_SEC);
gmtime_r(&alarm_val, &alarm_tm);
/* 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;
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ALMA_Disable(RTC);
LL_RTC_EnableWriteProtection(RTC);
if (LL_RTC_ALMA_Init(RTC, LL_RTC_FORMAT_BIN, &rtc_alarm) != SUCCESS) {
return -EIO;
}
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ALMA_Enable(RTC);
LL_RTC_ClearFlag_ALRA(RTC);
LL_RTC_EnableIT_ALRA(RTC);
LL_RTC_EnableWriteProtection(RTC);
return 0;
}
static int rtc_stm32_cancel_alarm(const struct device *dev, uint8_t chan_id)
{
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ClearFlag_ALRA(RTC);
LL_RTC_DisableIT_ALRA(RTC);
LL_RTC_ALMA_Disable(RTC);
LL_RTC_EnableWriteProtection(RTC);
DEV_DATA(dev)->callback = NULL;
return 0;
}
static uint32_t rtc_stm32_get_pending_int(const struct device *dev)
{
return LL_RTC_IsActiveFlag_ALRA(RTC) != 0;
}
static uint32_t rtc_stm32_get_top_value(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
return info->max_top_value;
}
static int rtc_stm32_set_top_value(const struct device *dev,
const struct counter_top_cfg *cfg)
{
const struct counter_config_info *info = dev->config;
if ((cfg->ticks != info->max_top_value) ||
!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
return -ENOTSUP;
} else {
return 0;
}
}
static uint32_t rtc_stm32_get_max_relative_alarm(const struct device *dev)
{
const struct counter_config_info *info = dev->config;
return info->max_top_value;
}
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)
{
struct rtc_stm32_data *data = DEV_DATA(dev);
counter_alarm_callback_t alarm_callback = data->callback;
uint32_t now = rtc_stm32_read(dev);
if (LL_RTC_IsActiveFlag_ALRA(RTC) != 0) {
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ClearFlag_ALRA(RTC);
LL_RTC_DisableIT_ALRA(RTC);
LL_RTC_ALMA_Disable(RTC);
LL_RTC_EnableWriteProtection(RTC);
if (alarm_callback != NULL) {
data->callback = NULL;
alarm_callback(dev, 0, now, data->user_data);
}
}
#if defined(CONFIG_SOC_SERIES_STM32H7X) && defined(CONFIG_CPU_CORTEX_M4)
LL_C2_EXTI_ClearFlag_0_31(RTC_EXTI_LINE);
#elif defined(CONFIG_SOC_SERIES_STM32G0X)
LL_EXTI_ClearRisingFlag_0_31(RTC_EXTI_LINE);
#else
LL_EXTI_ClearFlag_0_31(RTC_EXTI_LINE);
#endif
}
static int rtc_stm32_init(const struct device *dev)
{
const struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
const struct rtc_stm32_config *cfg = DEV_CFG(dev);
__ASSERT_NO_MSG(clk);
DEV_DATA(dev)->callback = NULL;
clock_control_on(clk, (clock_control_subsys_t *) &cfg->pclken);
z_stm32_hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY);
LL_PWR_EnableBkUpAccess();
#if defined(CONFIG_COUNTER_RTC_STM32_BACKUP_DOMAIN_RESET)
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
#endif
#if defined(CONFIG_COUNTER_RTC_STM32_CLOCK_LSI)
#if defined(CONFIG_SOC_SERIES_STM32WBX)
LL_RCC_LSI1_Enable();
while (LL_RCC_LSI1_IsReady() != 1) {
}
#else
LL_RCC_LSI_Enable();
while (LL_RCC_LSI_IsReady() != 1) {
}
#endif /* CONFIG_SOC_SERIES_STM32WBX */
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */
#if !defined(CONFIG_SOC_SERIES_STM32F4X) && \
!defined(CONFIG_SOC_SERIES_STM32F2X) && \
!defined(CONFIG_SOC_SERIES_STM32L1X)
LL_RCC_LSE_SetDriveCapability(
CONFIG_COUNTER_RTC_STM32_LSE_DRIVE_STRENGTH);
#endif /*
* !CONFIG_SOC_SERIES_STM32F4X
* && !CONFIG_SOC_SERIES_STM32F2X
* && !CONFIG_SOC_SERIES_STM32L1X
*/
#if defined(CONFIG_COUNTER_RTC_STM32_LSE_BYPASS)
LL_RCC_LSE_EnableBypass();
#endif /* CONFIG_COUNTER_RTC_STM32_LSE_BYPASS */
LL_RCC_LSE_Enable();
/* Wait until LSE is ready */
while (LL_RCC_LSE_IsReady() != 1) {
}
#if defined(CONFIG_CLOCK_STM32_MSI_PLL_MODE)
/* Enable MSI hardware auto calibration */
LL_RCC_MSI_EnablePLLMode();
#endif
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
#endif /* CONFIG_COUNTER_RTC_STM32_CLOCK_SRC */
LL_RCC_EnableRTC();
z_stm32_hsem_unlock(CFG_HW_RCC_SEMID);
if (LL_RTC_DeInit(RTC) != SUCCESS) {
return -EIO;
}
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 */
#if defined(CONFIG_SOC_SERIES_STM32H7X) && defined(CONFIG_CPU_CORTEX_M4)
LL_C2_EXTI_EnableIT_0_31(RTC_EXTI_LINE);
#else
LL_EXTI_EnableIT_0_31(RTC_EXTI_LINE);
#endif
LL_EXTI_EnableRisingTrig_0_31(RTC_EXTI_LINE);
rtc_stm32_irq_config(dev);
return 0;
}
static struct rtc_stm32_data rtc_data;
static const struct rtc_stm32_config rtc_config = {
.counter_info = {
.max_top_value = UINT32_MAX,
.freq = 1,
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
.channels = 1,
},
.pclken = {
.enr = DT_INST_CLOCKS_CELL(0, bits),
.bus = DT_INST_CLOCKS_CELL(0, bus),
},
.ll_rtc_config = {
.HourFormat = LL_RTC_HOURFORMAT_24HOUR,
#if defined(CONFIG_COUNTER_RTC_STM32_CLOCK_LSI)
/* prescaler values for LSI @ 32 KHz */
.AsynchPrescaler = 0x7F,
.SynchPrescaler = 0x00F9,
#else /* CONFIG_COUNTER_RTC_STM32_CLOCK_LSE */
/* prescaler values for LSE @ 32768 Hz */
.AsynchPrescaler = 0x7F,
.SynchPrescaler = 0x00FF,
#endif
},
};
static const struct counter_driver_api rtc_stm32_driver_api = {
.start = rtc_stm32_start,
.stop = rtc_stm32_stop,
.get_value = rtc_stm32_get_value,
.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,
.get_max_relative_alarm = rtc_stm32_get_max_relative_alarm,
};
DEVICE_DT_INST_DEFINE(0, &rtc_stm32_init, device_pm_control_nop,
&rtc_data, &rtc_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &rtc_stm32_driver_api);
static void rtc_stm32_irq_config(const struct device *dev)
{
IRQ_CONNECT(DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),
rtc_stm32_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
}