2020-02-04 20:04:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Vestas Wind Systems A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2024-05-24 19:19:15 +02:00
|
|
|
#include <zephyr/devicetree.h>
|
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(nxp_kinetis_lptmr)
|
2020-02-04 20:04:02 +01:00
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_lptmr
|
2024-05-24 19:19:15 +02:00
|
|
|
#else
|
|
|
|
#define DT_DRV_COMPAT nxp_lptmr
|
|
|
|
#endif
|
2020-02-04 20:04:02 +01:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/counter.h>
|
2022-10-12 14:25:33 +02:00
|
|
|
#include <zephyr/irq.h>
|
2020-02-04 20:04:02 +01:00
|
|
|
#include <fsl_lptmr.h>
|
|
|
|
|
|
|
|
struct mcux_lptmr_config {
|
|
|
|
struct counter_config_info info;
|
|
|
|
LPTMR_Type *base;
|
|
|
|
lptmr_prescaler_clock_select_t clk_source;
|
|
|
|
lptmr_prescaler_glitch_value_t prescaler_glitch;
|
|
|
|
bool bypass_prescaler_glitch;
|
|
|
|
lptmr_timer_mode_t mode;
|
|
|
|
lptmr_pin_select_t pin;
|
|
|
|
lptmr_pin_polarity_t polarity;
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2020-02-04 20:04:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_lptmr_data {
|
|
|
|
counter_top_callback_t top_callback;
|
|
|
|
void *top_user_data;
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_lptmr_start(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
LPTMR_EnableInterrupts(config->base,
|
|
|
|
kLPTMR_TimerInterruptEnable);
|
|
|
|
LPTMR_StartTimer(config->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_lptmr_stop(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
LPTMR_DisableInterrupts(config->base,
|
|
|
|
kLPTMR_TimerInterruptEnable);
|
|
|
|
LPTMR_StopTimer(config->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_lptmr_get_value(const struct device *dev, uint32_t *ticks)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
*ticks = LPTMR_GetCurrentTimerCount(config->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_lptmr_set_top_value(const struct device *dev,
|
|
|
|
const struct counter_top_cfg *cfg)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_lptmr_data *data = dev->data;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
if (cfg->ticks == 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->top_callback = cfg->callback;
|
|
|
|
data->top_user_data = cfg->user_data;
|
|
|
|
|
|
|
|
if (config->base->CSR & LPTMR_CSR_TEN_MASK) {
|
|
|
|
/* Timer already enabled, check flags before resetting */
|
|
|
|
if (cfg->flags & COUNTER_TOP_CFG_DONT_RESET) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
LPTMR_StopTimer(config->base);
|
|
|
|
LPTMR_SetTimerPeriod(config->base, cfg->ticks);
|
|
|
|
LPTMR_StartTimer(config->base);
|
|
|
|
} else {
|
|
|
|
LPTMR_SetTimerPeriod(config->base, cfg->ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_lptmr_get_pending_int(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t mask = LPTMR_CSR_TCF_MASK | LPTMR_CSR_TIE_MASK;
|
|
|
|
uint32_t flags;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
flags = LPTMR_GetStatusFlags(config->base);
|
|
|
|
|
|
|
|
return ((flags & mask) == mask);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t mcux_lptmr_get_top_value(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
return (config->base->CMR & LPTMR_CMR_COMPARE_MASK) + 1U;
|
|
|
|
}
|
|
|
|
|
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_lptmr_isr(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_lptmr_data *data = dev->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t flags;
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
flags = LPTMR_GetStatusFlags(config->base);
|
|
|
|
LPTMR_ClearStatusFlags(config->base, flags);
|
|
|
|
|
|
|
|
if (data->top_callback) {
|
|
|
|
data->top_callback(dev, data->top_user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_lptmr_init(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_lptmr_config *config = dev->config;
|
2020-02-04 20:04:02 +01:00
|
|
|
lptmr_config_t lptmr_config;
|
|
|
|
|
|
|
|
LPTMR_GetDefaultConfig(&lptmr_config);
|
|
|
|
lptmr_config.timerMode = config->mode;
|
|
|
|
lptmr_config.enableFreeRunning = false;
|
|
|
|
lptmr_config.prescalerClockSource = config->clk_source;
|
|
|
|
lptmr_config.bypassPrescaler = config->bypass_prescaler_glitch;
|
|
|
|
lptmr_config.value = config->prescaler_glitch;
|
|
|
|
|
|
|
|
if (config->mode == kLPTMR_TimerModePulseCounter) {
|
|
|
|
lptmr_config.pinSelect = config->pin;
|
|
|
|
lptmr_config.pinPolarity = config->polarity;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPTMR_Init(config->base, &lptmr_config);
|
|
|
|
|
|
|
|
config->irq_config_func(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api mcux_lptmr_driver_api = {
|
|
|
|
.start = mcux_lptmr_start,
|
|
|
|
.stop = mcux_lptmr_stop,
|
|
|
|
.get_value = mcux_lptmr_get_value,
|
|
|
|
.set_top_value = mcux_lptmr_set_top_value,
|
|
|
|
.get_pending_int = mcux_lptmr_get_pending_int,
|
|
|
|
.get_top_value = mcux_lptmr_get_top_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TO_LPTMR_CLK_SEL(val) _DO_CONCAT(kLPTMR_PrescalerClock_, val)
|
|
|
|
#define TO_LPTMR_PIN_SEL(val) _DO_CONCAT(kLPTMR_PinSelectInput_, val)
|
|
|
|
|
|
|
|
/* Prescaler mapping */
|
|
|
|
#define LPTMR_PRESCALER_2 kLPTMR_Prescale_Glitch_0
|
|
|
|
#define LPTMR_PRESCALER_4 kLPTMR_Prescale_Glitch_1
|
|
|
|
#define LPTMR_PRESCALER_8 kLPTMR_Prescale_Glitch_2
|
|
|
|
#define LPTMR_PRESCALER_16 kLPTMR_Prescale_Glitch_3
|
|
|
|
#define LPTMR_PRESCALER_32 kLPTMR_Prescale_Glitch_4
|
|
|
|
#define LPTMR_PRESCALER_64 kLPTMR_Prescale_Glitch_5
|
|
|
|
#define LPTMR_PRESCALER_128 kLPTMR_Prescale_Glitch_6
|
|
|
|
#define LPTMR_PRESCALER_256 kLPTMR_Prescale_Glitch_7
|
|
|
|
#define LPTMR_PRESCALER_512 kLPTMR_Prescale_Glitch_8
|
|
|
|
#define LPTMR_PRESCALER_1024 kLPTMR_Prescale_Glitch_9
|
|
|
|
#define LPTMR_PRESCALER_2048 kLPTMR_Prescale_Glitch_10
|
|
|
|
#define LPTMR_PRESCALER_4096 kLPTMR_Prescale_Glitch_11
|
|
|
|
#define LPTMR_PRESCALER_8192 kLPTMR_Prescale_Glitch_12
|
|
|
|
#define LPTMR_PRESCALER_16384 kLPTMR_Prescale_Glitch_13
|
|
|
|
#define LPTMR_PRESCALER_32768 kLPTMR_Prescale_Glitch_14
|
|
|
|
#define LPTMR_PRESCALER_65536 kLPTMR_Prescale_Glitch_15
|
|
|
|
#define TO_LPTMR_PRESCALER(val) _DO_CONCAT(LPTMR_PRESCALER_, val)
|
|
|
|
|
|
|
|
/* Glitch filter mapping */
|
|
|
|
#define LPTMR_GLITCH_2 kLPTMR_Prescale_Glitch_1
|
|
|
|
#define LPTMR_GLITCH_4 kLPTMR_Prescale_Glitch_2
|
|
|
|
#define LPTMR_GLITCH_8 kLPTMR_Prescale_Glitch_3
|
|
|
|
#define LPTMR_GLITCH_16 kLPTMR_Prescale_Glitch_4
|
|
|
|
#define LPTMR_GLITCH_32 kLPTMR_Prescale_Glitch_5
|
|
|
|
#define LPTMR_GLITCH_64 kLPTMR_Prescale_Glitch_6
|
|
|
|
#define LPTMR_GLITCH_128 kLPTMR_Prescale_Glitch_7
|
|
|
|
#define LPTMR_GLITCH_256 kLPTMR_Prescale_Glitch_8
|
|
|
|
#define LPTMR_GLITCH_512 kLPTMR_Prescale_Glitch_9
|
|
|
|
#define LPTMR_GLITCH_1024 kLPTMR_Prescale_Glitch_10
|
|
|
|
#define LPTMR_GLITCH_2048 kLPTMR_Prescale_Glitch_11
|
|
|
|
#define LPTMR_GLITCH_4096 kLPTMR_Prescale_Glitch_12
|
|
|
|
#define LPTMR_GLITCH_8192 kLPTMR_Prescale_Glitch_13
|
|
|
|
#define LPTMR_GLITCH_16384 kLPTMR_Prescale_Glitch_14
|
|
|
|
#define LPTMR_GLITCH_32768 kLPTMR_Prescale_Glitch_15
|
|
|
|
#define TO_LPTMR_GLITCH(val) _DO_CONCAT(LPTMR_GLITCH_, val)
|
|
|
|
|
2020-05-06 01:06:32 +02:00
|
|
|
/*
|
|
|
|
* This driver is single-instance. If the devicetree contains multiple
|
|
|
|
* instances, this will fail and the driver needs to be revisited.
|
|
|
|
*/
|
2020-05-06 20:23:07 +02:00
|
|
|
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1,
|
2020-05-06 01:06:32 +02:00
|
|
|
"unsupported lptmr instance");
|
|
|
|
|
2020-05-11 20:56:08 +02:00
|
|
|
#if DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay)
|
2020-02-04 20:04:02 +01:00
|
|
|
static struct mcux_lptmr_data mcux_lptmr_data_0;
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_lptmr_irq_config_0(const struct device *dev);
|
2020-02-04 20:04:02 +01:00
|
|
|
|
|
|
|
static struct mcux_lptmr_config mcux_lptmr_config_0 = {
|
|
|
|
.info = {
|
|
|
|
.max_top_value = UINT16_MAX,
|
|
|
|
.freq = DT_INST_PROP(0, clock_frequency) /
|
|
|
|
DT_INST_PROP(0, prescaler),
|
|
|
|
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
|
|
|
|
.channels = 0,
|
|
|
|
},
|
|
|
|
.base = (LPTMR_Type *)DT_INST_REG_ADDR(0),
|
|
|
|
.clk_source = TO_LPTMR_CLK_SEL(DT_INST_PROP(0, clk_source)),
|
|
|
|
#if DT_INST_NODE_HAS_PROP(0, input_pin)
|
|
|
|
#if DT_INST_PROP(0, prescaler) == 1
|
|
|
|
.bypass_prescaler_glitch = true,
|
|
|
|
#else
|
|
|
|
.prescaler_glitch = TO_LPTMR_GLITCH(DT_INST_PROP(0, prescaler)),
|
|
|
|
#endif
|
|
|
|
.mode = kLPTMR_TimerModePulseCounter,
|
|
|
|
.pin = TO_LPTMR_PIN_SEL(DT_INST_PROP(0, input_pin)),
|
|
|
|
.polarity = DT_INST_PROP(0, active_low),
|
|
|
|
#else /* !DT_INST_NODE_HAS_PROP(0, input_pin) */
|
|
|
|
.mode = kLPTMR_TimerModeTimeCounter,
|
|
|
|
#if DT_INST_PROP(0, prescaler) == 1
|
|
|
|
.bypass_prescaler_glitch = true,
|
|
|
|
#else
|
|
|
|
.prescaler_glitch = TO_LPTMR_PRESCALER(DT_INST_PROP(0, prescaler)),
|
|
|
|
#endif
|
|
|
|
#endif /* !DT_INST_NODE_HAS_PROP(0, input_pin) */
|
|
|
|
.irq_config_func = mcux_lptmr_irq_config_0,
|
|
|
|
};
|
|
|
|
|
2021-04-28 10:23:42 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, &mcux_lptmr_init, NULL,
|
2020-12-15 19:58:44 +01:00
|
|
|
&mcux_lptmr_data_0,
|
2020-02-04 20:04:02 +01:00
|
|
|
&mcux_lptmr_config_0,
|
2021-10-20 22:15:14 +02:00
|
|
|
POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY,
|
2020-02-04 20:04:02 +01:00
|
|
|
&mcux_lptmr_driver_api);
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_lptmr_irq_config_0(const struct device *dev)
|
2020-02-04 20:04:02 +01:00
|
|
|
{
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
|
2020-12-15 19:58:44 +01:00
|
|
|
mcux_lptmr_isr, DEVICE_DT_INST_GET(0), 0);
|
2020-02-04 20:04:02 +01:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
|
|
|
}
|
2020-05-11 20:56:08 +02:00
|
|
|
#endif /* DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay) */
|