2020-07-21 22:34:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/counter.h>
|
|
|
|
#include <soc.h>
|
|
|
|
#include <hw_counter.h>
|
|
|
|
|
|
|
|
#define DT_COUNTER_LABEL counter0
|
|
|
|
#define DRIVER_CONFIG_INFO_FLAGS (COUNTER_CONFIG_INFO_COUNT_UP)
|
|
|
|
#define DRIVER_CONFIG_INFO_CHANNELS 1
|
|
|
|
#define COUNTER_NATIVE_POSIX_IRQ_FLAGS (0)
|
|
|
|
#define COUNTER_NATIVE_POSIX_IRQ_PRIORITY (2)
|
|
|
|
|
|
|
|
#define COUNTER_PERIOD (USEC_PER_SEC / CONFIG_COUNTER_NATIVE_POSIX_FREQUENCY)
|
|
|
|
#define TOP_VALUE (UINT_MAX)
|
|
|
|
|
|
|
|
static struct counter_alarm_cfg pending_alarm;
|
|
|
|
static bool is_alarm_pending;
|
2020-05-01 02:33:38 +08:00
|
|
|
static const struct device *device;
|
2020-07-21 22:34:03 +08:00
|
|
|
|
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 20:58:56 +08:00
|
|
|
static void counter_isr(const void *arg)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
uint32_t current_value = hw_counter_get_value();
|
|
|
|
|
|
|
|
if (is_alarm_pending) {
|
|
|
|
is_alarm_pending = false;
|
|
|
|
pending_alarm.callback(device, 0, current_value,
|
|
|
|
pending_alarm.user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_init(const struct device *dev)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
device = dev;
|
|
|
|
is_alarm_pending = false;
|
|
|
|
|
|
|
|
IRQ_CONNECT(COUNTER_EVENT_IRQ, COUNTER_NATIVE_POSIX_IRQ_PRIORITY,
|
|
|
|
counter_isr, NULL, COUNTER_NATIVE_POSIX_IRQ_FLAGS);
|
|
|
|
hw_counter_set_period(COUNTER_PERIOD);
|
|
|
|
hw_counter_set_target(TOP_VALUE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_start(const struct device *dev)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
hw_counter_start();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_stop(const struct device *dev)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
hw_counter_stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_get_value(const struct device *dev, uint32_t *ticks)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
*ticks = hw_counter_get_value();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static uint32_t ctr_get_pending_int(const struct device *dev)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_set_top_value(const struct device *dev,
|
2020-07-21 22:34:03 +08:00
|
|
|
const struct counter_top_cfg *cfg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
ARG_UNUSED(cfg);
|
|
|
|
|
|
|
|
posix_print_warning("%s not supported\n", __func__);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static uint32_t ctr_get_top_value(const struct device *dev)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
return TOP_VALUE;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_set_alarm(const struct device *dev, uint8_t chan_id,
|
2020-07-21 22:34:03 +08:00
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
if (chan_id >= DRIVER_CONFIG_INFO_CHANNELS) {
|
|
|
|
posix_print_warning("channel %u is not supported\n", chan_id);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_alarm = *alarm_cfg;
|
|
|
|
is_alarm_pending = true;
|
|
|
|
|
|
|
|
if (!(alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE)) {
|
|
|
|
pending_alarm.ticks =
|
|
|
|
hw_counter_get_value() + pending_alarm.ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
hw_counter_set_target(pending_alarm.ticks);
|
|
|
|
irq_enable(COUNTER_EVENT_IRQ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int ctr_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
2020-07-21 22:34:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
if (chan_id >= DRIVER_CONFIG_INFO_CHANNELS) {
|
|
|
|
posix_print_warning("channel %u is not supported\n", chan_id);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_alarm_pending = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api ctr_api = {
|
|
|
|
.start = ctr_start,
|
|
|
|
.stop = ctr_stop,
|
|
|
|
.get_value = ctr_get_value,
|
|
|
|
.set_alarm = ctr_set_alarm,
|
|
|
|
.cancel_alarm = ctr_cancel_alarm,
|
|
|
|
.set_top_value = ctr_set_top_value,
|
|
|
|
.get_pending_int = ctr_get_pending_int,
|
|
|
|
.get_top_value = ctr_get_top_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct counter_config_info ctr_config = {
|
|
|
|
.max_top_value = UINT_MAX,
|
|
|
|
.freq = CONFIG_COUNTER_NATIVE_POSIX_FREQUENCY,
|
|
|
|
.channels = DRIVER_CONFIG_INFO_CHANNELS,
|
|
|
|
.flags = DRIVER_CONFIG_INFO_FLAGS
|
|
|
|
};
|
|
|
|
|
2021-03-05 21:43:32 +08:00
|
|
|
DEVICE_DT_DEFINE(DT_NODELABEL(DT_COUNTER_LABEL), ctr_init,
|
|
|
|
device_pm_control_nop, NULL, &ctr_config, PRE_KERNEL_1,
|
2020-07-21 22:34:03 +08:00
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &ctr_api);
|