2020-07-07 22:04:53 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2020 NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_pit
|
|
|
|
|
|
|
|
#include <drivers/counter.h>
|
|
|
|
#include <fsl_pit.h>
|
|
|
|
|
|
|
|
#define LOG_MODULE_NAME counter_pit
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
|
|
|
struct mcux_pit_config {
|
|
|
|
struct counter_config_info info;
|
|
|
|
PIT_Type *base;
|
|
|
|
bool enableRunInDebug;
|
|
|
|
pit_chnl_t pit_channel;
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2020-07-07 22:04:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_pit_data {
|
|
|
|
counter_alarm_callback_t alarm_callback;
|
|
|
|
counter_top_callback_t top_callback;
|
|
|
|
void *alarm_user_data;
|
|
|
|
void *top_user_data;
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_pit_get_top_value(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
pit_chnl_t channel = config->pit_channel;
|
|
|
|
|
2021-08-04 13:03:20 -05:00
|
|
|
/*
|
|
|
|
* According to RM, the LDVAL trigger = clock ticks -1
|
|
|
|
* The underlying HAL driver function PIT_SetTimerPeriod()
|
|
|
|
* automatically subtracted 1 from the value that ends up in
|
|
|
|
* LDVAL so for reporting purposes we need to add it back in
|
|
|
|
* here to by consistent.
|
|
|
|
*/
|
|
|
|
return (config->base->CHANNEL[channel].LDVAL + 1);
|
2020-07-07 22:04:53 +08:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_start(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
|
|
|
|
LOG_DBG("period is %d", mcux_pit_get_top_value(dev));
|
|
|
|
PIT_EnableInterrupts(config->base, config->pit_channel,
|
|
|
|
kPIT_TimerInterruptEnable);
|
|
|
|
PIT_StartTimer(config->base, config->pit_channel);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_stop(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
|
|
|
|
PIT_DisableInterrupts(config->base, config->pit_channel,
|
|
|
|
kPIT_TimerInterruptEnable);
|
|
|
|
PIT_StopTimer(config->base, config->pit_channel);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_get_value(const struct device *dev, uint32_t *ticks)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
|
|
|
|
*ticks = PIT_GetCurrentTimerCount(config->base, config->pit_channel);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_set_top_value(const struct device *dev,
|
2020-07-07 22:04:53 +08:00
|
|
|
const struct counter_top_cfg *cfg)
|
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
struct mcux_pit_data *data = dev->data;
|
|
|
|
pit_chnl_t channel = config->pit_channel;
|
|
|
|
|
|
|
|
if (cfg->ticks == 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->top_callback = cfg->callback;
|
|
|
|
data->top_user_data = cfg->user_data;
|
|
|
|
|
|
|
|
if (config->base->CHANNEL[channel].TCTRL & PIT_TCTRL_TEN_MASK) {
|
|
|
|
/* Timer already enabled, check flags before resetting */
|
|
|
|
if (cfg->flags & COUNTER_TOP_CFG_DONT_RESET) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
PIT_StopTimer(config->base, channel);
|
|
|
|
PIT_SetTimerPeriod(config->base, channel, cfg->ticks);
|
|
|
|
PIT_StartTimer(config->base, channel);
|
|
|
|
} else {
|
|
|
|
PIT_SetTimerPeriod(config->base, channel, cfg->ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_pit_get_pending_int(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
uint32_t mask = PIT_TFLG_TIF_MASK;
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
flags = PIT_GetStatusFlags(config->base, config->pit_channel);
|
|
|
|
|
|
|
|
return ((flags & mask) == mask);
|
|
|
|
}
|
|
|
|
|
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_pit_isr(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
struct mcux_pit_data *data = dev->data;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t current = 0;
|
|
|
|
|
|
|
|
LOG_DBG("pit counter isr");
|
|
|
|
flags = PIT_GetStatusFlags(config->base, config->pit_channel);
|
|
|
|
PIT_ClearStatusFlags(config->base, config->pit_channel, flags);
|
|
|
|
if (data->top_callback) {
|
|
|
|
data->top_callback(dev, data->top_user_data);
|
|
|
|
}
|
|
|
|
if (data->alarm_callback) {
|
|
|
|
PIT_StopTimer(config->base, config->pit_channel);
|
|
|
|
mcux_pit_get_value(dev, ¤t);
|
|
|
|
data->alarm_callback(dev, config->pit_channel, current,
|
|
|
|
data->alarm_user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_set_alarm(const struct device *dev, uint8_t chan_id,
|
2020-07-07 22:04:53 +08:00
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
struct mcux_pit_data *data = dev->data;
|
|
|
|
|
|
|
|
uint32_t ticks = alarm_cfg->ticks;
|
|
|
|
|
2021-12-15 23:40:15 +01:00
|
|
|
if (chan_id != DT_INST_PROP(0, pit_channel)) {
|
2020-07-07 22:04:53 +08:00
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticks > mcux_pit_get_top_value(dev)) {
|
2022-02-24 12:00:55 +00:00
|
|
|
LOG_ERR("Invalid ticks");
|
2020-07-07 22:04:53 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIT_StopTimer(config->base, chan_id);
|
|
|
|
PIT_SetTimerPeriod(config->base, chan_id, ticks);
|
|
|
|
data->alarm_callback = alarm_cfg->callback;
|
|
|
|
data->alarm_user_data = alarm_cfg->user_data;
|
|
|
|
LOG_DBG("set alarm to %d", ticks);
|
|
|
|
PIT_StartTimer(config->base, chan_id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config = dev->config;
|
|
|
|
struct mcux_pit_data *data = dev->data;
|
|
|
|
|
2021-12-15 23:40:15 +01:00
|
|
|
if (chan_id != DT_INST_PROP(0, pit_channel)) {
|
2020-07-07 22:04:53 +08:00
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIT_DisableInterrupts(config->base, chan_id, kPIT_TimerInterruptEnable);
|
|
|
|
PIT_StopTimer(config->base, chan_id);
|
|
|
|
data->alarm_callback = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_pit_init(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
const struct mcux_pit_config *config =
|
|
|
|
(struct mcux_pit_config *)dev->config;
|
|
|
|
pit_config_t pit_config;
|
|
|
|
|
|
|
|
PIT_GetDefaultConfig(&pit_config);
|
|
|
|
pit_config.enableRunInDebug = config->enableRunInDebug;
|
|
|
|
|
|
|
|
PIT_Init(config->base, &pit_config);
|
|
|
|
|
|
|
|
config->irq_config_func(dev);
|
|
|
|
|
|
|
|
PIT_SetTimerPeriod(config->base, config->pit_channel,
|
2021-12-15 23:40:15 +01:00
|
|
|
USEC_TO_COUNT(DT_INST_PROP(0, pit_period),
|
2020-07-07 22:04:53 +08:00
|
|
|
CLOCK_GetFreq(kCLOCK_BusClk)));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api mcux_pit_driver_api = {
|
|
|
|
.start = mcux_pit_start,
|
|
|
|
.stop = mcux_pit_stop,
|
|
|
|
.get_value = mcux_pit_get_value,
|
|
|
|
.set_top_value = mcux_pit_set_top_value,
|
|
|
|
.set_alarm = mcux_pit_set_alarm,
|
|
|
|
.cancel_alarm = mcux_pit_cancel_alarm,
|
|
|
|
.get_pending_int = mcux_pit_get_pending_int,
|
|
|
|
.get_top_value = mcux_pit_get_top_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This driver is single-instance. If the devicetree contains multiple
|
|
|
|
* instances, this will fail and the driver needs to be revisited.
|
|
|
|
*/
|
|
|
|
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1,
|
|
|
|
"unsupported pit instance");
|
|
|
|
|
|
|
|
static struct mcux_pit_data mcux_pit_data_0;
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_pit_irq_config_0(const struct device *dev);
|
2020-07-07 22:04:53 +08:00
|
|
|
|
|
|
|
static const struct mcux_pit_config mcux_pit_config_0 = {
|
|
|
|
.info = {
|
|
|
|
.max_top_value = UINT32_MAX,
|
|
|
|
.channels = 1,
|
2021-12-15 23:40:15 +01:00
|
|
|
.freq = DT_INST_PROP(0, clock_frequency),
|
2020-07-07 22:04:53 +08:00
|
|
|
},
|
|
|
|
.base = (PIT_Type *)DT_INST_REG_ADDR(0),
|
2021-12-15 23:40:15 +01:00
|
|
|
.pit_channel = DT_INST_PROP(0, pit_channel),
|
2020-07-07 22:04:53 +08:00
|
|
|
.irq_config_func = mcux_pit_irq_config_0,
|
|
|
|
};
|
|
|
|
|
2021-04-28 10:23:42 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, &mcux_pit_init, NULL,
|
2020-07-07 22:04:53 +08:00
|
|
|
&mcux_pit_data_0, &mcux_pit_config_0, POST_KERNEL,
|
2021-10-20 15:15:14 -05:00
|
|
|
CONFIG_COUNTER_INIT_PRIORITY, &mcux_pit_driver_api);
|
2020-07-07 22:04:53 +08:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_pit_irq_config_0(const struct device *dev)
|
2020-07-07 22:04:53 +08:00
|
|
|
{
|
|
|
|
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(0, 0, irq),
|
|
|
|
DT_INST_IRQ_BY_IDX(0, 0, priority), mcux_pit_isr,
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_GET(0), 0);
|
2020-07-07 22:04:53 +08:00
|
|
|
irq_enable(DT_INST_IRQ_BY_IDX(0, 0, irq));
|
|
|
|
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(0, 1, irq),
|
|
|
|
DT_INST_IRQ_BY_IDX(0, 1, priority), mcux_pit_isr,
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_GET(0), 0);
|
2020-07-07 22:04:53 +08:00
|
|
|
irq_enable(DT_INST_IRQ_BY_IDX(0, 1, irq));
|
|
|
|
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(0, 2, irq),
|
|
|
|
DT_INST_IRQ_BY_IDX(0, 2, priority), mcux_pit_isr,
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_GET(0), 0);
|
2020-07-07 22:04:53 +08:00
|
|
|
irq_enable(DT_INST_IRQ_BY_IDX(0, 2, irq));
|
|
|
|
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(0, 3, irq),
|
|
|
|
DT_INST_IRQ_BY_IDX(0, 3, priority), mcux_pit_isr,
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_GET(0), 0);
|
2020-07-07 22:04:53 +08:00
|
|
|
irq_enable(DT_INST_IRQ_BY_IDX(0, 3, irq));
|
|
|
|
}
|