2019-05-16 08:17:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Vestas Wind Systems A/S
|
|
|
|
*
|
|
|
|
* Based on adc_mcux_adc16.c, which is:
|
|
|
|
* Copyright (c) 2017-2018, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 20:11:52 +01:00
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_adc12
|
|
|
|
|
2019-06-25 21:53:45 +02:00
|
|
|
#include <drivers/adc.h>
|
2019-05-16 08:17:18 +02:00
|
|
|
#include <fsl_adc12.h>
|
|
|
|
|
|
|
|
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(adc_mcux_adc12);
|
|
|
|
|
|
|
|
#define ADC_CONTEXT_USES_KERNEL_TIMER
|
|
|
|
#include "adc_context.h"
|
|
|
|
|
|
|
|
struct mcux_adc12_config {
|
|
|
|
ADC_Type *base;
|
|
|
|
adc12_clock_source_t clock_src;
|
|
|
|
adc12_clock_divider_t clock_div;
|
|
|
|
adc12_reference_voltage_source_t ref_src;
|
|
|
|
uint32_t sample_clk_count;
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2019-05-16 08:17:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_adc12_data {
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *dev;
|
2019-05-16 08:17:18 +02:00
|
|
|
struct adc_context ctx;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t *buffer;
|
|
|
|
uint16_t *repeat_buffer;
|
|
|
|
uint32_t channels;
|
|
|
|
uint8_t channel_id;
|
2019-05-16 08:17:18 +02:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_adc12_channel_setup(const struct device *dev,
|
2019-05-16 08:17:18 +02:00
|
|
|
const struct adc_channel_cfg *channel_cfg)
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t channel_id = channel_cfg->channel_id;
|
2019-05-16 08:17:18 +02:00
|
|
|
|
|
|
|
if (channel_id > (ADC_SC1_ADCH_MASK >> ADC_SC1_ADCH_SHIFT)) {
|
|
|
|
LOG_ERR("Invalid channel %d", channel_id);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
|
|
|
|
LOG_ERR("Unsupported channel acquisition time");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_cfg->differential) {
|
|
|
|
LOG_ERR("Differential channels are not supported");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_cfg->gain != ADC_GAIN_1) {
|
|
|
|
LOG_ERR("Unsupported channel gain %d", channel_cfg->gain);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_cfg->reference != ADC_REF_INTERNAL) {
|
|
|
|
LOG_ERR("Unsupported channel reference");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_adc12_start_read(const struct device *dev,
|
2019-05-16 08:17:18 +02:00
|
|
|
const struct adc_sequence *sequence)
|
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_adc12_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_adc12_data *data = dev->data;
|
2019-05-16 08:17:18 +02:00
|
|
|
adc12_hardware_average_mode_t mode;
|
|
|
|
adc12_resolution_t resolution;
|
|
|
|
ADC_Type *base = config->base;
|
|
|
|
int error;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t tmp32;
|
2019-05-16 08:17:18 +02:00
|
|
|
|
|
|
|
switch (sequence->resolution) {
|
|
|
|
case 8:
|
|
|
|
resolution = kADC12_Resolution8Bit;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
resolution = kADC12_Resolution10Bit;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
resolution = kADC12_Resolution12Bit;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERR("Unsupported resolution %d", sequence->resolution);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp32 = base->CFG1 & ~(ADC_CFG1_MODE_MASK);
|
|
|
|
tmp32 |= ADC_CFG1_MODE(resolution);
|
|
|
|
base->CFG1 = tmp32;
|
|
|
|
|
|
|
|
switch (sequence->oversampling) {
|
|
|
|
case 0:
|
|
|
|
mode = kADC12_HardwareAverageDisabled;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mode = kADC12_HardwareAverageCount4;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mode = kADC12_HardwareAverageCount8;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mode = kADC12_HardwareAverageCount16;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
mode = kADC12_HardwareAverageCount32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERR("Unsupported oversampling value %d",
|
|
|
|
sequence->oversampling);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
ADC12_SetHardwareAverage(config->base, mode);
|
|
|
|
|
|
|
|
data->buffer = sequence->buffer;
|
|
|
|
adc_context_start_read(&data->ctx, sequence);
|
|
|
|
error = adc_context_wait_for_completion(&data->ctx);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_adc12_read_async(const struct device *dev,
|
2019-05-16 08:17:18 +02:00
|
|
|
const struct adc_sequence *sequence,
|
|
|
|
struct k_poll_signal *async)
|
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_adc12_data *data = dev->data;
|
2019-05-16 08:17:18 +02:00
|
|
|
int error;
|
|
|
|
|
|
|
|
adc_context_lock(&data->ctx, async ? true : false, async);
|
|
|
|
error = mcux_adc12_start_read(dev, sequence);
|
|
|
|
adc_context_release(&data->ctx, error);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_adc12_read(const struct device *dev,
|
2019-05-16 08:17:18 +02:00
|
|
|
const struct adc_sequence *sequence)
|
|
|
|
{
|
|
|
|
return mcux_adc12_read_async(dev, sequence, NULL);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mcux_adc12_start_channel(const struct device *dev)
|
2019-05-16 08:17:18 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_adc12_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_adc12_data *data = dev->data;
|
2019-05-16 08:17:18 +02:00
|
|
|
|
|
|
|
adc12_channel_config_t channel_config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t channel_group = 0U;
|
2019-05-16 08:17:18 +02:00
|
|
|
|
|
|
|
data->channel_id = find_lsb_set(data->channels) - 1;
|
|
|
|
|
|
|
|
LOG_DBG("Starting channel %d", data->channel_id);
|
|
|
|
channel_config.enableInterruptOnConversionCompleted = true;
|
|
|
|
channel_config.channelNumber = data->channel_id;
|
|
|
|
ADC12_SetChannelConfig(config->base, channel_group, &channel_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adc_context_start_sampling(struct adc_context *ctx)
|
|
|
|
{
|
|
|
|
struct mcux_adc12_data *data =
|
|
|
|
CONTAINER_OF(ctx, struct mcux_adc12_data, ctx);
|
|
|
|
|
|
|
|
data->channels = ctx->sequence.channels;
|
|
|
|
data->repeat_buffer = data->buffer;
|
|
|
|
|
|
|
|
mcux_adc12_start_channel(data->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adc_context_update_buffer_pointer(struct adc_context *ctx,
|
|
|
|
bool repeat_sampling)
|
|
|
|
{
|
|
|
|
struct mcux_adc12_data *data =
|
|
|
|
CONTAINER_OF(ctx, struct mcux_adc12_data, ctx);
|
|
|
|
|
|
|
|
if (repeat_sampling) {
|
|
|
|
data->buffer = data->repeat_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_adc12_isr(const struct device *dev)
|
2019-05-16 08:17:18 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_adc12_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_adc12_data *data = dev->data;
|
2019-05-16 08:17:18 +02:00
|
|
|
ADC_Type *base = config->base;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t channel_group = 0U;
|
|
|
|
uint16_t result;
|
2019-05-16 08:17:18 +02:00
|
|
|
|
|
|
|
result = ADC12_GetChannelConversionValue(base, channel_group);
|
|
|
|
LOG_DBG("Finished channel %d. Result is 0x%04x",
|
|
|
|
data->channel_id, result);
|
|
|
|
|
|
|
|
*data->buffer++ = result;
|
|
|
|
data->channels &= ~BIT(data->channel_id);
|
|
|
|
|
|
|
|
if (data->channels) {
|
|
|
|
mcux_adc12_start_channel(dev);
|
|
|
|
} else {
|
|
|
|
adc_context_on_sampling_done(&data->ctx, dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_adc12_init(const struct device *dev)
|
2019-05-16 08:17:18 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_adc12_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_adc12_data *data = dev->data;
|
2019-05-16 08:17:18 +02:00
|
|
|
ADC_Type *base = config->base;
|
|
|
|
adc12_config_t adc_config;
|
|
|
|
|
|
|
|
ADC12_GetDefaultConfig(&adc_config);
|
|
|
|
|
|
|
|
adc_config.referenceVoltageSource = config->ref_src;
|
|
|
|
adc_config.clockSource = config->clock_src;
|
|
|
|
adc_config.clockDivider = config->clock_div;
|
|
|
|
adc_config.sampleClockCount = config->sample_clk_count;
|
|
|
|
adc_config.resolution = kADC12_Resolution12Bit;
|
|
|
|
adc_config.enableContinuousConversion = false;
|
|
|
|
|
|
|
|
ADC12_Init(base, &adc_config);
|
|
|
|
ADC12_DoAutoCalibration(base);
|
|
|
|
ADC12_EnableHardwareTrigger(base, false);
|
|
|
|
|
|
|
|
config->irq_config_func(dev);
|
|
|
|
data->dev = dev;
|
|
|
|
|
|
|
|
adc_context_unlock_unconditionally(&data->ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct adc_driver_api mcux_adc12_driver_api = {
|
|
|
|
.channel_setup = mcux_adc12_channel_setup,
|
|
|
|
.read = mcux_adc12_read,
|
|
|
|
#ifdef CONFIG_ADC_ASYNC
|
|
|
|
.read_async = mcux_adc12_read_async,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ASSERT_WITHIN_RANGE(val, min, max, str) \
|
2020-03-12 16:16:00 +01:00
|
|
|
BUILD_ASSERT(val >= min && val <= max, str)
|
2019-05-16 08:17:18 +02:00
|
|
|
#define ASSERT_ADC12_CLK_DIV_VALID(val, str) \
|
2020-03-12 16:16:00 +01:00
|
|
|
BUILD_ASSERT(val == 1 || val == 2 || val == 4 || val == 8, str)
|
2019-05-16 08:17:18 +02:00
|
|
|
#define TO_ADC12_CLOCK_SRC(val) _DO_CONCAT(kADC12_ClockSourceAlt, val)
|
|
|
|
#define TO_ADC12_CLOCK_DIV(val) _DO_CONCAT(kADC12_ClockDivider, val)
|
|
|
|
|
2020-04-18 19:52:44 +02:00
|
|
|
#define ADC12_REF_SRC(n) \
|
|
|
|
COND_CODE_1(DT_INST_PROP(0, alternate_voltage_reference), \
|
|
|
|
(kADC12_ReferenceVoltageSourceValt), \
|
|
|
|
(kADC12_ReferenceVoltageSourceVref))
|
|
|
|
|
|
|
|
#define ACD12_MCUX_INIT(n) \
|
2020-07-14 17:02:00 +02:00
|
|
|
static void mcux_adc12_config_func_##n(const struct device *dev); \
|
2020-04-18 19:52:44 +02:00
|
|
|
\
|
|
|
|
ASSERT_WITHIN_RANGE(DT_INST_PROP(n, clk_source), 0, 3, \
|
|
|
|
"Invalid clock source"); \
|
|
|
|
ASSERT_ADC12_CLK_DIV_VALID(DT_INST_PROP(n, clk_divider), \
|
|
|
|
"Invalid clock divider"); \
|
|
|
|
ASSERT_WITHIN_RANGE(DT_INST_PROP(n, sample_time), 2, 256, \
|
|
|
|
"Invalid sample time"); \
|
|
|
|
static const struct mcux_adc12_config mcux_adc12_config_##n = { \
|
|
|
|
.base = (ADC_Type *)DT_INST_REG_ADDR(n), \
|
|
|
|
.clock_src = TO_ADC12_CLOCK_SRC(DT_INST_PROP(n, clk_source)),\
|
|
|
|
.clock_div = \
|
|
|
|
TO_ADC12_CLOCK_DIV(DT_INST_PROP(n, clk_divider)),\
|
|
|
|
.ref_src = ADC12_REF_SRC(n), \
|
|
|
|
.sample_clk_count = DT_INST_PROP(n, sample_time), \
|
|
|
|
.irq_config_func = mcux_adc12_config_func_##n, \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct mcux_adc12_data mcux_adc12_data_##n = { \
|
|
|
|
ADC_CONTEXT_INIT_TIMER(mcux_adc12_data_##n, ctx), \
|
|
|
|
ADC_CONTEXT_INIT_LOCK(mcux_adc12_data_##n, ctx), \
|
|
|
|
ADC_CONTEXT_INIT_SYNC(mcux_adc12_data_##n, ctx), \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
DEVICE_AND_API_INIT(mcux_adc12_##n, DT_INST_LABEL(n), \
|
|
|
|
&mcux_adc12_init, &mcux_adc12_data_##n, \
|
|
|
|
&mcux_adc12_config_##n, POST_KERNEL, \
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
|
|
|
|
&mcux_adc12_driver_api); \
|
|
|
|
\
|
2020-07-14 17:02:00 +02:00
|
|
|
static void mcux_adc12_config_func_##n(const struct device *dev) \
|
2020-04-18 19:52:44 +02:00
|
|
|
{ \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), \
|
|
|
|
DT_INST_IRQ(n, priority), mcux_adc12_isr, \
|
|
|
|
DEVICE_GET(mcux_adc12_##n), 0); \
|
|
|
|
\
|
|
|
|
irq_enable(DT_INST_IRQN(n)); \
|
|
|
|
}
|
2019-05-16 08:17:18 +02:00
|
|
|
|
2020-05-06 20:23:07 +02:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(ACD12_MCUX_INIT)
|