1 /**
2 * @file
3 *
4 * @brief Emulated ADC driver
5 */
6
7 /*
8 * Copyright 2021 Google LLC
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13 #define DT_DRV_COMPAT zephyr_adc_emul
14
15 #include <zephyr/drivers/adc.h>
16 #include <zephyr/drivers/adc/adc_emul.h>
17 #include <zephyr/kernel.h>
18 #include <zephyr/logging/log.h>
19 #include <zephyr/sys/byteorder.h>
20 #include <zephyr/sys/util.h>
21
22 LOG_MODULE_REGISTER(adc_emul, CONFIG_ADC_LOG_LEVEL);
23
24 #define ADC_CONTEXT_USES_KERNEL_TIMER
25 #include "adc_context.h"
26
27 #define ADC_EMUL_MAX_RESOLUTION 16
28
29 typedef uint16_t adc_emul_res_t;
30
31 enum adc_emul_input_source {
32 ADC_EMUL_CONST_VALUE,
33 ADC_EMUL_CUSTOM_FUNC,
34 };
35
36 /**
37 * @brief Channel of emulated ADC config
38 *
39 * This structure contains configuration of one channel of emulated ADC.
40 */
41 struct adc_emul_chan_cfg {
42 /** Pointer to function used to obtain input mV */
43 adc_emul_value_func func;
44 /** Pointer to data that are passed to @a func on call */
45 void *func_data;
46 /** Constant mV input value */
47 uint32_t const_value;
48 /** Gain used on output value */
49 enum adc_gain gain;
50 /** Reference source */
51 enum adc_reference ref;
52 /** Input source which is used to obtain input value */
53 enum adc_emul_input_source input;
54 };
55
56 /**
57 * @brief Emulated ADC config
58 *
59 * This structure contains constant data for given instance of emulated ADC.
60 */
61 struct adc_emul_config {
62 /** Number of supported channels */
63 uint8_t num_channels;
64 };
65
66 /**
67 * @brief Emulated ADC data
68 *
69 * This structure contains data structures used by a emulated ADC.
70 */
71 struct adc_emul_data {
72 /** Structure that handle state of ongoing read operation */
73 struct adc_context ctx;
74 /** Pointer to ADC emulator own device structure */
75 const struct device *dev;
76 /** Pointer to memory where next sample will be written */
77 uint16_t *buf;
78 /** Pointer to where will be data stored in case of repeated sampling */
79 uint16_t *repeat_buf;
80 /** Mask with channels that will be sampled */
81 uint32_t channels;
82 /** Mask created from requested resolution in read operation */
83 uint16_t res_mask;
84 /** Reference voltage for ADC_REF_VDD_1 source */
85 uint16_t ref_vdd;
86 /** Reference voltage for ADC_REF_EXTERNAL0 source */
87 uint16_t ref_ext0;
88 /** Reference voltage for ADC_REF_EXTERNAL1 source */
89 uint16_t ref_ext1;
90 /** Reference voltage for ADC_REF_INTERNAL source */
91 uint16_t ref_int;
92 /** Array of each channel configuration */
93 struct adc_emul_chan_cfg *chan_cfg;
94 /** Structure used for acquisition thread */
95 struct k_thread thread;
96 /** Semaphore used to control acquisition thread */
97 struct k_sem sem;
98 /** Mutex used to control access to channels config and ref voltages */
99 struct k_mutex cfg_mtx;
100
101 /** Stack for acquisition thread */
102 K_KERNEL_STACK_MEMBER(stack,
103 CONFIG_ADC_EMUL_ACQUISITION_THREAD_STACK_SIZE);
104 };
105
adc_emul_const_value_set(const struct device * dev,unsigned int chan,uint32_t value)106 int adc_emul_const_value_set(const struct device *dev, unsigned int chan,
107 uint32_t value)
108 {
109 const struct adc_emul_config *config = dev->config;
110 struct adc_emul_data *data = dev->data;
111 struct adc_emul_chan_cfg *chan_cfg;
112
113 if (chan >= config->num_channels) {
114 LOG_ERR("unsupported channel %d", chan);
115 return -EINVAL;
116 }
117
118 chan_cfg = &data->chan_cfg[chan];
119
120 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
121
122 chan_cfg->input = ADC_EMUL_CONST_VALUE;
123 chan_cfg->const_value = value;
124
125 k_mutex_unlock(&data->cfg_mtx);
126
127 return 0;
128 }
129
adc_emul_value_func_set(const struct device * dev,unsigned int chan,adc_emul_value_func func,void * func_data)130 int adc_emul_value_func_set(const struct device *dev, unsigned int chan,
131 adc_emul_value_func func, void *func_data)
132 {
133 const struct adc_emul_config *config = dev->config;
134 struct adc_emul_data *data = dev->data;
135 struct adc_emul_chan_cfg *chan_cfg;
136
137 if (chan >= config->num_channels) {
138 LOG_ERR("unsupported channel %d", chan);
139 return -EINVAL;
140 }
141
142 chan_cfg = &data->chan_cfg[chan];
143
144 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
145
146 chan_cfg->func = func;
147 chan_cfg->func_data = func_data;
148 chan_cfg->input = ADC_EMUL_CUSTOM_FUNC;
149
150 k_mutex_unlock(&data->cfg_mtx);
151
152 return 0;
153 }
154
adc_emul_ref_voltage_set(const struct device * dev,enum adc_reference ref,uint16_t value)155 int adc_emul_ref_voltage_set(const struct device *dev, enum adc_reference ref,
156 uint16_t value)
157 {
158 struct adc_driver_api *api = (struct adc_driver_api *)dev->api;
159 struct adc_emul_data *data = dev->data;
160 int err = 0;
161
162 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
163
164 switch (ref) {
165 case ADC_REF_VDD_1:
166 data->ref_vdd = value;
167 break;
168 case ADC_REF_INTERNAL:
169 data->ref_int = value;
170 api->ref_internal = value;
171 break;
172 case ADC_REF_EXTERNAL0:
173 data->ref_ext0 = value;
174 break;
175 case ADC_REF_EXTERNAL1:
176 data->ref_ext1 = value;
177 break;
178 default:
179 err = -EINVAL;
180 }
181
182 k_mutex_unlock(&data->cfg_mtx);
183
184 return err;
185 }
186
187 /**
188 * @brief Convert @p ref to reference voltage value in mV
189 *
190 * @param data Internal data of ADC emulator
191 * @param ref Select which reference source should be used
192 *
193 * @return Reference voltage in mV
194 * @return 0 on error
195 */
adc_emul_get_ref_voltage(struct adc_emul_data * data,enum adc_reference ref)196 static uint16_t adc_emul_get_ref_voltage(struct adc_emul_data *data,
197 enum adc_reference ref)
198 {
199 uint16_t voltage;
200
201 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
202
203 switch (ref) {
204 case ADC_REF_VDD_1:
205 voltage = data->ref_vdd;
206 break;
207 case ADC_REF_VDD_1_2:
208 voltage = data->ref_vdd / 2;
209 break;
210 case ADC_REF_VDD_1_3:
211 voltage = data->ref_vdd / 3;
212 break;
213 case ADC_REF_VDD_1_4:
214 voltage = data->ref_vdd / 4;
215 break;
216 case ADC_REF_INTERNAL:
217 voltage = data->ref_int;
218 break;
219 case ADC_REF_EXTERNAL0:
220 voltage = data->ref_ext0;
221 break;
222 case ADC_REF_EXTERNAL1:
223 voltage = data->ref_ext1;
224 break;
225 default:
226 voltage = 0;
227 }
228
229 k_mutex_unlock(&data->cfg_mtx);
230
231 return voltage;
232 }
233
adc_emul_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)234 static int adc_emul_channel_setup(const struct device *dev,
235 const struct adc_channel_cfg *channel_cfg)
236 {
237 const struct adc_emul_config *config = dev->config;
238 struct adc_emul_chan_cfg *emul_chan_cfg;
239 struct adc_emul_data *data = dev->data;
240
241 if (channel_cfg->channel_id >= config->num_channels) {
242 LOG_ERR("unsupported channel id '%d'", channel_cfg->channel_id);
243 return -ENOTSUP;
244 }
245
246 if (adc_emul_get_ref_voltage(data, channel_cfg->reference) == 0) {
247 LOG_ERR("unsupported channel reference '%d'",
248 channel_cfg->reference);
249 return -ENOTSUP;
250 }
251
252 if (channel_cfg->differential) {
253 LOG_ERR("unsupported differential mode");
254 return -ENOTSUP;
255 }
256
257 emul_chan_cfg = &data->chan_cfg[channel_cfg->channel_id];
258
259 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
260
261 emul_chan_cfg->gain = channel_cfg->gain;
262 emul_chan_cfg->ref = channel_cfg->reference;
263
264 k_mutex_unlock(&data->cfg_mtx);
265
266 return 0;
267 }
268
269 /**
270 * @brief Check if buffer in @p sequence is big enough to hold all ADC samples
271 *
272 * @param dev ADC emulator device
273 * @param sequence ADC sequence description
274 *
275 * @return 0 on success
276 * @return -ENOMEM if buffer is not big enough
277 */
adc_emul_check_buffer_size(const struct device * dev,const struct adc_sequence * sequence)278 static int adc_emul_check_buffer_size(const struct device *dev,
279 const struct adc_sequence *sequence)
280 {
281 const struct adc_emul_config *config = dev->config;
282 uint8_t channels = 0;
283 size_t needed;
284 uint32_t mask;
285
286 for (mask = BIT(config->num_channels - 1); mask != 0; mask >>= 1) {
287 if (mask & sequence->channels) {
288 channels++;
289 }
290 }
291
292 needed = channels * sizeof(adc_emul_res_t);
293 if (sequence->options) {
294 needed *= (1 + sequence->options->extra_samplings);
295 }
296
297 if (sequence->buffer_size < needed) {
298 return -ENOMEM;
299 }
300
301 return 0;
302 }
303
304 /**
305 * @brief Start processing read request
306 *
307 * @param dev ADC emulator device
308 * @param sequence ADC sequence description
309 *
310 * @return 0 on success
311 * @return -ENOTSUP if requested resolution or channel is out side of supported
312 * range
313 * @return -ENOMEM if buffer is not big enough
314 * (see @ref adc_emul_check_buffer_size)
315 * @return other error code returned by adc_context_wait_for_completion
316 */
adc_emul_start_read(const struct device * dev,const struct adc_sequence * sequence)317 static int adc_emul_start_read(const struct device *dev,
318 const struct adc_sequence *sequence)
319 {
320 const struct adc_emul_config *config = dev->config;
321 struct adc_emul_data *data = dev->data;
322 int err;
323
324 if (sequence->resolution > ADC_EMUL_MAX_RESOLUTION ||
325 sequence->resolution == 0) {
326 LOG_ERR("unsupported resolution %d", sequence->resolution);
327 return -ENOTSUP;
328 }
329
330 if (find_msb_set(sequence->channels) > config->num_channels) {
331 LOG_ERR("unsupported channels in mask: 0x%08x",
332 sequence->channels);
333 return -ENOTSUP;
334 }
335
336 err = adc_emul_check_buffer_size(dev, sequence);
337 if (err) {
338 LOG_ERR("buffer size too small");
339 return err;
340 }
341
342 data->res_mask = BIT_MASK(sequence->resolution);
343 data->buf = sequence->buffer;
344 adc_context_start_read(&data->ctx, sequence);
345
346 return adc_context_wait_for_completion(&data->ctx);
347 }
348
adc_emul_read_async(const struct device * dev,const struct adc_sequence * sequence,struct k_poll_signal * async)349 static int adc_emul_read_async(const struct device *dev,
350 const struct adc_sequence *sequence,
351 struct k_poll_signal *async)
352 {
353 struct adc_emul_data *data = dev->data;
354 int err;
355
356 adc_context_lock(&data->ctx, async ? true : false, async);
357 err = adc_emul_start_read(dev, sequence);
358 adc_context_release(&data->ctx, err);
359
360 return err;
361 }
362
adc_emul_read(const struct device * dev,const struct adc_sequence * sequence)363 static int adc_emul_read(const struct device *dev,
364 const struct adc_sequence *sequence)
365 {
366 return adc_emul_read_async(dev, sequence, NULL);
367 }
368
adc_context_start_sampling(struct adc_context * ctx)369 static void adc_context_start_sampling(struct adc_context *ctx)
370 {
371 struct adc_emul_data *data = CONTAINER_OF(ctx, struct adc_emul_data,
372 ctx);
373
374 data->channels = ctx->sequence.channels;
375 data->repeat_buf = data->buf;
376
377 k_sem_give(&data->sem);
378 }
379
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat_sampling)380 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
381 bool repeat_sampling)
382 {
383 struct adc_emul_data *data = CONTAINER_OF(ctx, struct adc_emul_data,
384 ctx);
385
386 if (repeat_sampling) {
387 data->buf = data->repeat_buf;
388 }
389 }
390
391 /**
392 * @brief Convert input voltage of ADC @p chan to raw output value
393 *
394 * @param data Internal data of ADC emulator
395 * @param chan ADC channel to sample
396 * @param result Raw output value
397 *
398 * @return 0 on success
399 * @return -EINVAL if failed to get reference voltage or unknown input is
400 * selected
401 * @return other error code returned by custom function
402 */
adc_emul_get_chan_value(struct adc_emul_data * data,unsigned int chan,adc_emul_res_t * result)403 static int adc_emul_get_chan_value(struct adc_emul_data *data,
404 unsigned int chan,
405 adc_emul_res_t *result)
406 {
407 struct adc_emul_chan_cfg *chan_cfg = &data->chan_cfg[chan];
408 uint32_t input_mV;
409 uint32_t ref_v;
410 uint64_t temp; /* Temporary 64 bit value prevent overflows */
411 int err = 0;
412
413 k_mutex_lock(&data->cfg_mtx, K_FOREVER);
414
415 /* Get input voltage */
416 switch (chan_cfg->input) {
417 case ADC_EMUL_CONST_VALUE:
418 input_mV = chan_cfg->const_value;
419 break;
420
421 case ADC_EMUL_CUSTOM_FUNC:
422 err = chan_cfg->func(data->dev, chan, chan_cfg->func_data,
423 &input_mV);
424 if (err) {
425 LOG_ERR("failed to read channel %d (err %d)",
426 chan, err);
427 goto out;
428 }
429 break;
430
431 default:
432 LOG_ERR("unknown input source %d", chan_cfg->input);
433 err = -EINVAL;
434 goto out;
435 }
436
437 /* Get reference voltage and apply inverted gain */
438 ref_v = adc_emul_get_ref_voltage(data, chan_cfg->ref);
439 err = adc_gain_invert(chan_cfg->gain, &ref_v);
440 if (ref_v == 0 || err) {
441 LOG_ERR("failed to get ref voltage (channel %d)", chan);
442 err = -EINVAL;
443 goto out;
444 }
445
446 /* Calculate output value */
447 temp = (uint64_t)input_mV * data->res_mask / ref_v;
448
449 /* If output value is greater than resolution, it has to be trimmed */
450 if (temp > data->res_mask) {
451 temp = data->res_mask;
452 }
453
454 *result = temp;
455
456 out:
457 k_mutex_unlock(&data->cfg_mtx);
458
459 return err;
460 }
461
462 /**
463 * @brief Main function of thread which is used to collect samples from
464 * emulated ADC. When adc_context_start_sampling give semaphore,
465 * for each requested channel value function is called. Returned
466 * mV value is converted to output using reference voltage, gain
467 * and requested resolution.
468 *
469 * @param data Internal data of ADC emulator
470 *
471 * @return This thread should not end
472 */
adc_emul_acquisition_thread(struct adc_emul_data * data)473 static void adc_emul_acquisition_thread(struct adc_emul_data *data)
474 {
475 int err;
476
477 while (true) {
478 k_sem_take(&data->sem, K_FOREVER);
479
480 err = 0;
481
482 while (data->channels) {
483 adc_emul_res_t result = 0;
484 unsigned int chan = find_lsb_set(data->channels) - 1;
485
486 LOG_DBG("reading channel %d", chan);
487
488 err = adc_emul_get_chan_value(data, chan, &result);
489 if (err) {
490 adc_context_complete(&data->ctx, err);
491 break;
492 }
493
494 LOG_DBG("read channel %d, result = %d", chan, result);
495
496 *data->buf++ = result;
497 WRITE_BIT(data->channels, chan, 0);
498 }
499
500 if (!err) {
501 adc_context_on_sampling_done(&data->ctx, data->dev);
502 }
503 }
504 }
505
506 /**
507 * @brief Function called on init for each ADC emulator device. It setups all
508 * channels to return constant 0 mV and create acquisition thread.
509 *
510 * @param dev ADC emulator device
511 *
512 * @return 0 on success
513 */
adc_emul_init(const struct device * dev)514 static int adc_emul_init(const struct device *dev)
515 {
516 const struct adc_emul_config *config = dev->config;
517 struct adc_emul_data *data = dev->data;
518 int chan;
519
520 data->dev = dev;
521
522 k_sem_init(&data->sem, 0, 1);
523 k_mutex_init(&data->cfg_mtx);
524
525 for (chan = 0; chan < config->num_channels; chan++) {
526 struct adc_emul_chan_cfg *chan_cfg = &data->chan_cfg[chan];
527
528 chan_cfg->func = NULL;
529 chan_cfg->func_data = NULL;
530 chan_cfg->input = ADC_EMUL_CONST_VALUE;
531 chan_cfg->const_value = 0;
532 }
533
534 k_thread_create(&data->thread, data->stack,
535 CONFIG_ADC_EMUL_ACQUISITION_THREAD_STACK_SIZE,
536 (k_thread_entry_t)adc_emul_acquisition_thread,
537 data, NULL, NULL,
538 CONFIG_ADC_EMUL_ACQUISITION_THREAD_PRIO,
539 0, K_NO_WAIT);
540
541 adc_context_unlock_unconditionally(&data->ctx);
542
543 return 0;
544 }
545
546 #define ADC_EMUL_INIT(_num) \
547 static struct adc_driver_api adc_emul_api_##_num = { \
548 .channel_setup = adc_emul_channel_setup, \
549 .read = adc_emul_read, \
550 .ref_internal = DT_INST_PROP(_num, ref_internal_mv), \
551 IF_ENABLED(CONFIG_ADC_ASYNC, \
552 (.read_async = adc_emul_read_async,)) \
553 }; \
554 \
555 static struct adc_emul_chan_cfg \
556 adc_emul_ch_cfg_##_num[DT_INST_PROP(_num, nchannels)]; \
557 \
558 static const struct adc_emul_config adc_emul_config_##_num = { \
559 .num_channels = DT_INST_PROP(_num, nchannels), \
560 }; \
561 \
562 static struct adc_emul_data adc_emul_data_##_num = { \
563 ADC_CONTEXT_INIT_TIMER(adc_emul_data_##_num, ctx), \
564 ADC_CONTEXT_INIT_LOCK(adc_emul_data_##_num, ctx), \
565 ADC_CONTEXT_INIT_SYNC(adc_emul_data_##_num, ctx), \
566 .chan_cfg = adc_emul_ch_cfg_##_num, \
567 .ref_vdd = DT_INST_PROP(_num, ref_vdd_mv), \
568 .ref_ext0 = DT_INST_PROP(_num, ref_external0_mv), \
569 .ref_ext1 = DT_INST_PROP(_num, ref_external1_mv), \
570 .ref_int = DT_INST_PROP(_num, ref_internal_mv), \
571 }; \
572 \
573 DEVICE_DT_INST_DEFINE(_num, adc_emul_init, NULL, \
574 &adc_emul_data_##_num, \
575 &adc_emul_config_##_num, POST_KERNEL, \
576 CONFIG_ADC_INIT_PRIORITY, \
577 &adc_emul_api_##_num);
578
579 DT_INST_FOREACH_STATUS_OKAY(ADC_EMUL_INIT)
580