forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_gecko.c
315 lines (250 loc) · 7.92 KB
/
adc_gecko.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (c) 2023 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT silabs_gecko_adc
#include <zephyr/drivers/adc.h>
#include <em_adc.h>
#include <em_cmu.h>
#define ADC_CONTEXT_USES_KERNEL_TIMER
#include "adc_context.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(adc_gecko, CONFIG_ADC_LOG_LEVEL);
/* Number of channels available. */
#define GECKO_CHANNEL_COUNT 16
struct adc_gecko_channel_config {
bool initialized;
ADC_Ref_TypeDef reference;
ADC_PosSel_TypeDef input_select;
};
struct adc_gecko_data {
const struct device *dev;
struct adc_context ctx;
uint16_t *buffer;
uint16_t *repeat_buffer;
uint32_t channels;
uint8_t channel_id;
ADC_Res_TypeDef resolution;
struct adc_gecko_channel_config channel_config[GECKO_CHANNEL_COUNT];
};
struct adc_gecko_config {
ADC_TypeDef *base;
void (*irq_cfg_func)(void);
uint32_t frequency;
};
static void adc_gecko_set_config(const struct device *dev)
{
struct adc_gecko_data *data = dev->data;
struct adc_gecko_channel_config *channel_config = NULL;
const struct adc_gecko_config *config = dev->config;
ADC_TypeDef *adc_base = (ADC_TypeDef *)config->base;
ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
ADC_InitSingle_TypeDef initSingle = ADC_INITSINGLE_DEFAULT;
channel_config = &data->channel_config[data->channel_id];
init.prescale = ADC_PrescaleCalc(config->frequency, 0);
init.timebase = ADC_TimebaseCalc(0);
initSingle.diff = false;
initSingle.reference = channel_config->reference;
initSingle.resolution = data->resolution;
initSingle.acqTime = adcAcqTime4;
initSingle.posSel = channel_config->input_select;
ADC_Init(adc_base, &init);
ADC_InitSingle(adc_base, &initSingle);
}
static int adc_gecko_check_buffer_size(const struct adc_sequence *sequence,
uint8_t active_channels)
{
size_t needed_buffer_size;
needed_buffer_size = active_channels * sizeof(uint16_t);
if (sequence->options) {
needed_buffer_size *= (1 + sequence->options->extra_samplings);
}
if (sequence->buffer_size < needed_buffer_size) {
LOG_DBG("Provided buffer is too small (%u/%u)",
sequence->buffer_size, needed_buffer_size);
return -ENOMEM;
}
return 0;
}
static int start_read(const struct device *dev, const struct adc_sequence *sequence)
{
struct adc_gecko_data *data = dev->data;
uint32_t channels;
uint8_t channel_count;
uint8_t index;
int res;
/* Check if at least 1 channel is requested */
if (sequence->channels == 0) {
LOG_DBG("No channel requested");
return -EINVAL;
}
if (sequence->oversampling) {
LOG_ERR("Oversampling is not supported");
return -ENOTSUP;
}
/* Verify all requested channels are initialized and store resolution */
channels = sequence->channels;
channel_count = 0;
while (channels) {
/* Iterate through all channels and check if they are initialized */
index = find_lsb_set(channels) - 1;
if (index >= GECKO_CHANNEL_COUNT) {
LOG_DBG("Requested channel index not available: %d", index);
return -EINVAL;
}
if (!data->channel_config[index].initialized) {
LOG_DBG("Channel not initialized");
return -EINVAL;
}
channel_count++;
channels &= ~BIT(index);
}
res = adc_gecko_check_buffer_size(sequence, channel_count);
if (res < 0) {
return res;
}
data->buffer = sequence->buffer;
adc_context_start_read(&data->ctx, sequence);
res = adc_context_wait_for_completion(&data->ctx);
return res;
}
static void adc_gecko_start_channel(const struct device *dev)
{
const struct adc_gecko_config *config = dev->config;
struct adc_gecko_data *data = dev->data;
ADC_TypeDef *adc_base = (ADC_TypeDef *)config->base;
data->channel_id = find_lsb_set(data->channels) - 1;
adc_gecko_set_config(data->dev);
ADC_IntEnable(adc_base, ADC_IEN_SINGLE);
ADC_Start(adc_base, adcStartSingle);
}
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct adc_gecko_data *data = CONTAINER_OF(ctx, struct adc_gecko_data, ctx);
data->channels = ctx->sequence.channels;
adc_gecko_start_channel(data->dev);
}
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
{
struct adc_gecko_data *data = CONTAINER_OF(ctx, struct adc_gecko_data, ctx);
if (repeat_sampling) {
data->buffer = data->repeat_buffer;
}
}
static void adc_gecko_isr(void *arg)
{
const struct device *dev = (const struct device *)arg;
const struct adc_gecko_config *config = dev->config;
struct adc_gecko_data *data = dev->data;
ADC_TypeDef *adc_base = config->base;
uint32_t sample = 0;
uint32_t flags, err;
flags = ADC_IntGet(adc_base);
__ASSERT(flags & ADC_IF_SINGLE, "unexpected ADC IRQ (flags=0x%08x)!", flags);
err = flags & (ADC_IF_EM23ERR | ADC_IF_PROGERR | ADC_IF_VREFOV | ADC_IF_SINGLEOF);
if (!err) {
sample = ADC_DataSingleGet(adc_base);
*data->buffer++ = (uint16_t)sample;
data->channels &= ~BIT(data->channel_id);
if (data->channels) {
adc_gecko_start_channel(dev);
} else {
adc_context_on_sampling_done(&data->ctx, dev);
}
} else {
LOG_ERR("ADC conversion error, flags=%08x", err);
adc_context_complete(&data->ctx, -EIO);
}
ADC_IntClear(adc_base, ADC_IF_SINGLE | err);
}
static int adc_gecko_read(const struct device *dev,
const struct adc_sequence *sequence)
{
struct adc_gecko_data *data = dev->data;
int error;
adc_context_lock(&data->ctx, false, NULL);
error = start_read(dev, sequence);
adc_context_release(&data->ctx, error);
return error;
}
static int adc_gecko_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
struct adc_gecko_data *data = dev->data;
struct adc_gecko_channel_config *channel_config = NULL;
if (channel_cfg->channel_id < GECKO_CHANNEL_COUNT) {
channel_config = &data->channel_config[channel_cfg->channel_id];
} else {
LOG_DBG("Requested channel index not available: %d", channel_cfg->channel_id);
return -EINVAL;
}
channel_config->initialized = false;
channel_config->input_select = channel_cfg->input_positive;
switch (channel_cfg->gain) {
case ADC_GAIN_1:
break;
default:
LOG_ERR("unsupported channel gain '%d'", channel_cfg->gain);
return -ENOTSUP;
}
switch (channel_cfg->reference) {
case ADC_REF_VDD_1:
channel_config->reference = adcRef5V;
break;
case ADC_REF_VDD_1_2:
channel_config->reference = adcRef2V5;
break;
case ADC_REF_VDD_1_4:
channel_config->reference = adcRef1V25;
break;
default:
LOG_ERR("unsupported channel reference type '%d'", channel_cfg->reference);
return -ENOTSUP;
}
channel_config->initialized = true;
return 0;
}
static int adc_gecko_init(const struct device *dev)
{
const struct adc_gecko_config *config = dev->config;
struct adc_gecko_data *data = dev->data;
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_ADC0, true);
data->dev = dev;
data->resolution = adcRes12Bit;
config->irq_cfg_func();
adc_context_unlock_unconditionally(&data->ctx);
return 0;
}
static const struct adc_driver_api api_gecko_adc_driver_api = {
.channel_setup = adc_gecko_channel_setup,
.read = adc_gecko_read,
};
#define GECKO_ADC_INIT(n) \
\
static void adc_gecko_config_func_##n(void); \
\
const static struct adc_gecko_config adc_gecko_config_##n = { \
.base = (ADC_TypeDef *)DT_INST_REG_ADDR(n), \
.irq_cfg_func = adc_gecko_config_func_##n, \
.frequency = DT_INST_PROP(n, frequency), \
}; \
static struct adc_gecko_data adc_gecko_data_##n = { \
ADC_CONTEXT_INIT_TIMER(adc_gecko_data_##n, ctx), \
ADC_CONTEXT_INIT_LOCK(adc_gecko_data_##n, ctx), \
ADC_CONTEXT_INIT_SYNC(adc_gecko_data_##n, ctx), \
}; \
static void adc_gecko_config_func_##n(void) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), \
adc_gecko_isr, DEVICE_DT_INST_GET(n), 0); \
irq_enable(DT_INST_IRQN(n)); \
}; \
DEVICE_DT_INST_DEFINE(n, \
&adc_gecko_init, NULL, \
&adc_gecko_data_##n, &adc_gecko_config_##n,\
POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, \
&api_gecko_adc_driver_api);
DT_INST_FOREACH_STATUS_OKAY(GECKO_ADC_INIT)