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