2018-10-17 01:45:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 blik GmbH
|
|
|
|
* Copyright (c) 2018, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 20:11:52 +01:00
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_rtc
|
|
|
|
|
2019-06-25 21:53:48 +02:00
|
|
|
#include <drivers/counter.h>
|
2018-10-17 01:45:19 +02:00
|
|
|
#include <fsl_rtc.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(mcux_rtc, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
|
|
|
struct mcux_rtc_data {
|
|
|
|
counter_alarm_callback_t alarm_callback;
|
|
|
|
counter_top_callback_t top_callback;
|
|
|
|
void *alarm_user_data;
|
|
|
|
void *top_user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_rtc_config {
|
|
|
|
struct counter_config_info info;
|
|
|
|
RTC_Type *base;
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2018-10-17 01:45:19 +02:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_start(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
|
|
|
|
|
|
|
RTC_StartTimer(config->base);
|
|
|
|
RTC_EnableInterrupts(config->base,
|
|
|
|
kRTC_AlarmInterruptEnable |
|
|
|
|
kRTC_TimeOverflowInterruptEnable |
|
|
|
|
kRTC_TimeInvalidInterruptEnable);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_stop(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
|
|
|
|
|
|
|
RTC_DisableInterrupts(config->base,
|
|
|
|
kRTC_AlarmInterruptEnable |
|
|
|
|
kRTC_TimeOverflowInterruptEnable |
|
|
|
|
kRTC_TimeInvalidInterruptEnable);
|
|
|
|
RTC_StopTimer(config->base);
|
|
|
|
|
|
|
|
/* clear out any set alarms */
|
|
|
|
config->base->TAR = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_rtc_read(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ticks = config->base->TSR;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read TSR seconds twice in case it glitches during an update.
|
|
|
|
* This can happen when a read occurs at the time the register is
|
|
|
|
* incrementing.
|
|
|
|
*/
|
|
|
|
if (config->base->TSR == ticks) {
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
ticks = config->base->TSR;
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_get_value(const struct device *dev, uint32_t *ticks)
|
2020-01-18 15:24:32 +01:00
|
|
|
{
|
|
|
|
*ticks = mcux_rtc_read(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_set_alarm(const struct device *dev, uint8_t chan_id,
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_rtc_data *data = dev->data;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ticks = alarm_cfg->ticks;
|
|
|
|
uint32_t current = mcux_rtc_read(dev);
|
2018-10-17 01:45:19 +02:00
|
|
|
|
|
|
|
LOG_DBG("Current time is %d ticks", current);
|
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
if (chan_id != 0U) {
|
2018-10-17 01:45:19 +02:00
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->alarm_callback != NULL) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2019-05-20 09:46:19 +02:00
|
|
|
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
|
2018-10-17 01:45:19 +02:00
|
|
|
ticks += current;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticks < current) {
|
|
|
|
LOG_ERR("Alarm cannot be earlier than current time");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->alarm_callback = alarm_cfg->callback;
|
|
|
|
data->alarm_user_data = alarm_cfg->user_data;
|
|
|
|
|
|
|
|
config->base->TAR = ticks;
|
|
|
|
LOG_DBG("Alarm set to %d ticks", ticks);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_rtc_data *data = dev->data;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
if (chan_id != 0U) {
|
2018-10-17 01:45:19 +02:00
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->alarm_callback = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_set_top_value(const struct device *dev,
|
2019-03-21 16:31:16 +01:00
|
|
|
const struct counter_top_cfg *cfg)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2019-03-21 16:31:16 +01:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_rtc_data *data = dev->data;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
2019-03-21 16:31:16 +01:00
|
|
|
if (cfg->ticks != info->max_top_value) {
|
|
|
|
LOG_ERR("Wrap can only be set to 0x%x.", info->max_top_value);
|
2018-10-17 01:45:19 +02:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:31:16 +01:00
|
|
|
if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
|
|
|
|
RTC_StopTimer(config->base);
|
|
|
|
config->base->TSR = 0;
|
|
|
|
RTC_StartTimer(config->base);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->top_callback = cfg->callback;
|
|
|
|
data->top_user_data = cfg->user_data;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_rtc_get_pending_int(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
|
|
|
|
|
|
|
return RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_rtc_get_top_value(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
|
|
|
|
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
|
|
|
static void mcux_rtc_isr(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_rtc_data *data = dev->data;
|
2019-05-21 21:58:53 +02:00
|
|
|
counter_alarm_callback_t cb;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t current = mcux_rtc_read(dev);
|
2018-10-17 01:45:19 +02:00
|
|
|
|
2019-05-21 21:58:53 +02:00
|
|
|
|
2018-10-17 01:45:19 +02:00
|
|
|
LOG_DBG("Current time is %d ticks", current);
|
|
|
|
|
|
|
|
if ((RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK) &&
|
|
|
|
(data->alarm_callback)) {
|
2019-05-21 21:58:53 +02:00
|
|
|
cb = data->alarm_callback;
|
|
|
|
data->alarm_callback = NULL;
|
|
|
|
cb(dev, 0, current, data->alarm_user_data);
|
2018-10-17 01:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((RTC_GetStatusFlags(config->base) & RTC_SR_TOF_MASK) &&
|
|
|
|
(data->top_callback)) {
|
|
|
|
data->top_callback(dev, data->top_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear any conditions to ack the IRQ
|
|
|
|
*
|
|
|
|
* callback may have already reset the alarm flag if a new
|
|
|
|
* alarm value was programmed to the TAR
|
|
|
|
*/
|
|
|
|
RTC_StopTimer(config->base);
|
|
|
|
if (RTC_GetStatusFlags(config->base) & RTC_SR_TAF_MASK) {
|
|
|
|
RTC_ClearStatusFlags(config->base, kRTC_AlarmFlag);
|
|
|
|
} else if (RTC_GetStatusFlags(config->base) & RTC_SR_TIF_MASK) {
|
|
|
|
RTC_ClearStatusFlags(config->base, kRTC_TimeInvalidFlag);
|
|
|
|
} else if (RTC_GetStatusFlags(config->base) & RTC_SR_TOF_MASK) {
|
|
|
|
RTC_ClearStatusFlags(config->base, kRTC_TimeOverflowFlag);
|
|
|
|
}
|
|
|
|
RTC_StartTimer(config->base);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_rtc_init(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct counter_config_info *info = dev->config;
|
2018-10-17 01:45:19 +02:00
|
|
|
const struct mcux_rtc_config *config =
|
|
|
|
CONTAINER_OF(info, struct mcux_rtc_config, info);
|
|
|
|
rtc_config_t rtc_config;
|
|
|
|
|
|
|
|
RTC_GetDefaultConfig(&rtc_config);
|
|
|
|
RTC_Init(config->base, &rtc_config);
|
|
|
|
|
|
|
|
/* Enable 32kHz oscillator and wait for 1ms to settle */
|
|
|
|
config->base->CR |= 0x100;
|
|
|
|
k_busy_wait(USEC_PER_MSEC);
|
|
|
|
|
|
|
|
config->irq_config_func(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api mcux_rtc_driver_api = {
|
|
|
|
.start = mcux_rtc_start,
|
|
|
|
.stop = mcux_rtc_stop,
|
2020-01-18 15:24:32 +01:00
|
|
|
.get_value = mcux_rtc_get_value,
|
2018-10-17 01:45:19 +02:00
|
|
|
.set_alarm = mcux_rtc_set_alarm,
|
2019-02-08 00:23:59 +01:00
|
|
|
.cancel_alarm = mcux_rtc_cancel_alarm,
|
2018-10-17 01:45:19 +02:00
|
|
|
.set_top_value = mcux_rtc_set_top_value,
|
|
|
|
.get_pending_int = mcux_rtc_get_pending_int,
|
|
|
|
.get_top_value = mcux_rtc_get_top_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mcux_rtc_data mcux_rtc_data_0;
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_rtc_irq_config_0(const struct device *dev);
|
2018-10-17 01:45:19 +02:00
|
|
|
|
|
|
|
static struct mcux_rtc_config mcux_rtc_config_0 = {
|
2020-03-24 20:11:52 +01:00
|
|
|
.base = (RTC_Type *)DT_INST_REG_ADDR(0),
|
2018-10-17 01:45:19 +02:00
|
|
|
.irq_config_func = mcux_rtc_irq_config_0,
|
|
|
|
.info = {
|
|
|
|
.max_top_value = UINT32_MAX,
|
2020-03-24 20:11:52 +01:00
|
|
|
.freq = DT_INST_PROP(0, clock_frequency) /
|
|
|
|
DT_INST_PROP(0, prescaler),
|
2019-05-17 12:10:55 +02:00
|
|
|
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
|
2018-10-17 01:45:19 +02:00
|
|
|
.channels = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-28 10:23:42 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, &mcux_rtc_init, NULL,
|
2018-10-17 01:45:19 +02:00
|
|
|
&mcux_rtc_data_0, &mcux_rtc_config_0.info,
|
2021-10-20 22:15:14 +02:00
|
|
|
POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY,
|
2018-10-17 01:45:19 +02:00
|
|
|
&mcux_rtc_driver_api);
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_rtc_irq_config_0(const struct device *dev)
|
2018-10-17 01:45:19 +02:00
|
|
|
{
|
2020-03-24 20:11:52 +01:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
DT_INST_IRQ(0, priority),
|
2020-12-15 19:58:44 +01:00
|
|
|
mcux_rtc_isr, DEVICE_DT_INST_GET(0), 0);
|
2020-03-24 20:11:52 +01:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-10-17 01:45:19 +02:00
|
|
|
}
|