forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_mcux_adc12.c
301 lines (253 loc) · 8.04 KB
/
adc_mcux_adc12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 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
*/
#define DT_DRV_COMPAT nxp_kinetis_adc12
#include <drivers/adc.h>
#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;
void (*irq_config_func)(const struct device *dev);
};
struct mcux_adc12_data {
const struct device *dev;
struct adc_context ctx;
uint16_t *buffer;
uint16_t *repeat_buffer;
uint32_t channels;
uint8_t channel_id;
};
static int mcux_adc12_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
uint8_t channel_id = channel_cfg->channel_id;
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;
}
static int mcux_adc12_start_read(const struct device *dev,
const struct adc_sequence *sequence)
{
const struct mcux_adc12_config *config = dev->config;
struct mcux_adc12_data *data = dev->data;
adc12_hardware_average_mode_t mode;
adc12_resolution_t resolution;
ADC_Type *base = config->base;
int error;
uint32_t tmp32;
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;
}
static int mcux_adc12_read_async(const struct device *dev,
const struct adc_sequence *sequence,
struct k_poll_signal *async)
{
struct mcux_adc12_data *data = dev->data;
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;
}
static int mcux_adc12_read(const struct device *dev,
const struct adc_sequence *sequence)
{
return mcux_adc12_read_async(dev, sequence, NULL);
}
static void mcux_adc12_start_channel(const struct device *dev)
{
const struct mcux_adc12_config *config = dev->config;
struct mcux_adc12_data *data = dev->data;
adc12_channel_config_t channel_config;
uint32_t channel_group = 0U;
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;
}
}
static void mcux_adc12_isr(const struct device *dev)
{
const struct mcux_adc12_config *config = dev->config;
struct mcux_adc12_data *data = dev->data;
ADC_Type *base = config->base;
uint32_t channel_group = 0U;
uint16_t result;
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);
}
}
static int mcux_adc12_init(const struct device *dev)
{
const struct mcux_adc12_config *config = dev->config;
struct mcux_adc12_data *data = dev->data;
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) \
BUILD_ASSERT(val >= min && val <= max, str)
#define ASSERT_ADC12_CLK_DIV_VALID(val, str) \
BUILD_ASSERT(val == 1 || val == 2 || val == 4 || val == 8, str)
#define TO_ADC12_CLOCK_SRC(val) _DO_CONCAT(kADC12_ClockSourceAlt, val)
#define TO_ADC12_CLOCK_DIV(val) _DO_CONCAT(kADC12_ClockDivider, val)
#define ADC12_REF_SRC(n) \
COND_CODE_1(DT_INST_PROP(0, alternate_voltage_reference), \
(kADC12_ReferenceVoltageSourceValt), \
(kADC12_ReferenceVoltageSourceVref))
#define ACD12_MCUX_INIT(n) \
static void mcux_adc12_config_func_##n(const struct device *dev); \
\
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_DT_INST_DEFINE(n, &mcux_adc12_init, \
NULL, &mcux_adc12_data_##n, \
&mcux_adc12_config_##n, POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&mcux_adc12_driver_api); \
\
static void mcux_adc12_config_func_##n(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), mcux_adc12_isr, \
DEVICE_DT_INST_GET(n), 0); \
\
irq_enable(DT_INST_IRQN(n)); \
}
DT_INST_FOREACH_STATUS_OKAY(ACD12_MCUX_INIT)