Zephyr Project API  3.3.0
A Scalable Open Source RTOS
adc.h
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2018 Nordic Semiconductor ASA
8 * Copyright (c) 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_H_
14#define ZEPHYR_INCLUDE_DRIVERS_ADC_H_
15
16#include <zephyr/device.h>
18#include <zephyr/kernel.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
53};
54
72 int32_t *value);
73
83};
84
91
94
105
129
132
133#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
139 uint8_t input_positive;
140
146 uint8_t input_negative;
147#endif /* CONFIG_ADC_CONFIGURABLE_INPUTS */
148};
149
214#define ADC_CHANNEL_CFG_DT(node_id) { \
215 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
216 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
217 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
218 .channel_id = DT_REG_ADDR(node_id), \
219IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
220 (.differential = DT_NODE_HAS_PROP(node_id, zephyr_input_negative), \
221 .input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
222 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
223}
224
236 const struct device *dev;
237
240
247
254
262
269
276};
277
280#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
281 .dev = DEVICE_DT_GET(ctlr), \
282 .channel_id = input, \
283 ADC_CHANNEL_CFG_FROM_DT_NODE(\
284 ADC_CHANNEL_DT_NODE(ctlr, input)) \
285 }
286
287#define ADC_CHANNEL_DT_NODE(ctlr, input) \
288 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
289
290#define ADC_FOREACH_INPUT(node, input) \
291 IF_ENABLED(IS_EQ(DT_REG_ADDR(node), input), (node))
292
293#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
294 IF_ENABLED(DT_NODE_EXISTS(node_id), \
295 (.channel_cfg_dt_node_exists = true, \
296 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
297 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
298 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
299 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
300
372#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
373 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
374 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
375
386#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
387 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
388
398#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
399
408#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
409
410/* Forward declaration of the adc_sequence structure. */
411struct adc_sequence;
412
419
425
428};
429
449typedef enum adc_action (*adc_sequence_callback)(const struct device *dev,
450 const struct adc_sequence *sequence,
451 uint16_t sampling_index);
452
467
473
479
485};
486
496
505
517 void *buffer;
518
526
535
543
553};
554
555
560typedef int (*adc_api_channel_setup)(const struct device *dev,
561 const struct adc_channel_cfg *channel_cfg);
562
567typedef int (*adc_api_read)(const struct device *dev,
568 const struct adc_sequence *sequence);
569
575typedef int (*adc_api_read_async)(const struct device *dev,
576 const struct adc_sequence *sequence,
577 struct k_poll_signal *async);
578
584__subsystem struct adc_driver_api {
587#ifdef CONFIG_ADC_ASYNC
588 adc_api_read_async read_async;
589#endif
591};
592
605__syscall int adc_channel_setup(const struct device *dev,
606 const struct adc_channel_cfg *channel_cfg);
607
608static inline int z_impl_adc_channel_setup(const struct device *dev,
609 const struct adc_channel_cfg *channel_cfg)
610{
611 const struct adc_driver_api *api =
612 (const struct adc_driver_api *)dev->api;
613
614 return api->channel_setup(dev, channel_cfg);
615}
616
626static inline int adc_channel_setup_dt(const struct adc_dt_spec *spec)
627{
628 if (!spec->channel_cfg_dt_node_exists) {
629 return -ENOTSUP;
630 }
631
632 return adc_channel_setup(spec->dev, &spec->channel_cfg);
633}
634
656__syscall int adc_read(const struct device *dev,
657 const struct adc_sequence *sequence);
658
659static inline int z_impl_adc_read(const struct device *dev,
660 const struct adc_sequence *sequence)
661{
662 const struct adc_driver_api *api =
663 (const struct adc_driver_api *)dev->api;
664
665 return api->read(dev, sequence);
666}
667
688__syscall int adc_read_async(const struct device *dev,
689 const struct adc_sequence *sequence,
690 struct k_poll_signal *async);
691
692
693#ifdef CONFIG_ADC_ASYNC
694static inline int z_impl_adc_read_async(const struct device *dev,
695 const struct adc_sequence *sequence,
696 struct k_poll_signal *async)
697{
698 const struct adc_driver_api *api =
699 (const struct adc_driver_api *)dev->api;
700
701 return api->read_async(dev, sequence, async);
702}
703#endif /* CONFIG_ADC_ASYNC */
704
714static inline uint16_t adc_ref_internal(const struct device *dev)
715{
716 const struct adc_driver_api *api =
717 (const struct adc_driver_api *)dev->api;
718
719 return api->ref_internal;
720}
721
745static inline int adc_raw_to_millivolts(int32_t ref_mv,
746 enum adc_gain gain,
747 uint8_t resolution,
748 int32_t *valp)
749{
750 int32_t adc_mv = *valp * ref_mv;
751 int ret = adc_gain_invert(gain, &adc_mv);
752
753 if (ret == 0) {
754 *valp = (adc_mv >> resolution);
755 }
756
757 return ret;
758}
759
773static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
774 int32_t *valp)
775{
776 int32_t vref_mv;
777 uint8_t resolution;
778
779 if (!spec->channel_cfg_dt_node_exists) {
780 return -ENOTSUP;
781 }
782
784 vref_mv = (int32_t)adc_ref_internal(spec->dev);
785 } else {
786 vref_mv = spec->vref_mv;
787 }
788
789 resolution = spec->resolution;
790
791 /*
792 * For differential channels, one bit less needs to be specified
793 * for resolution to achieve correct conversion.
794 */
795 if (spec->channel_cfg.differential) {
796 resolution -= 1U;
797 }
798
799 return adc_raw_to_millivolts(vref_mv, spec->channel_cfg.gain,
800 resolution, valp);
801}
802
821static inline int adc_sequence_init_dt(const struct adc_dt_spec *spec,
822 struct adc_sequence *seq)
823{
824 if (!spec->channel_cfg_dt_node_exists) {
825 return -ENOTSUP;
826 }
827
828 seq->channels = BIT(spec->channel_id);
829 seq->resolution = spec->resolution;
830 seq->oversampling = spec->oversampling;
831
832 return 0;
833}
834
839#ifdef __cplusplus
840}
841#endif
842
843#include <syscalls/adc.h>
844
845#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_H_ */
int adc_read_async(const struct device *dev, const struct adc_sequence *sequence, struct k_poll_signal *async)
Set an asynchronous read request.
static int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec, int32_t *valp)
Convert a raw ADC value to millivolts using information stored in a struct adc_dt_spec.
Definition: adc.h:773
adc_gain
ADC channel gain factors.
Definition: adc.h:32
int(* adc_api_read)(const struct device *dev, const struct adc_sequence *sequence)
Type definition of ADC API function for setting a read request. See adc_read() for argument descripti...
Definition: adc.h:567
static int adc_sequence_init_dt(const struct adc_dt_spec *spec, struct adc_sequence *seq)
Initialize a struct adc_sequence from information stored in struct adc_dt_spec.
Definition: adc.h:821
int adc_gain_invert(enum adc_gain gain, int32_t *value)
Invert the application of gain to a measurement value.
int adc_read(const struct device *dev, const struct adc_sequence *sequence)
Set a read request.
int adc_channel_setup(const struct device *dev, const struct adc_channel_cfg *channel_cfg)
Configure an ADC channel.
int(* adc_api_channel_setup)(const struct device *dev, const struct adc_channel_cfg *channel_cfg)
Type definition of ADC API function for configuring a channel. See adc_channel_setup() for argument d...
Definition: adc.h:560
adc_action
Action to be performed after a sampling is done.
Definition: adc.h:416
enum adc_action(* adc_sequence_callback)(const struct device *dev, const struct adc_sequence *sequence, uint16_t sampling_index)
Type definition of the optional callback function to be called after a requested sampling is done.
Definition: adc.h:449
adc_reference
ADC references.
Definition: adc.h:75
int(* adc_api_read_async)(const struct device *dev, const struct adc_sequence *sequence, struct k_poll_signal *async)
Type definition of ADC API function for setting an asynchronous read request. See adc_read_async() fo...
Definition: adc.h:575
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition: adc.h:714
static int adc_channel_setup_dt(const struct adc_dt_spec *spec)
Configure an ADC channel from a struct adc_dt_spec.
Definition: adc.h:626
static int adc_raw_to_millivolts(int32_t ref_mv, enum adc_gain gain, uint8_t resolution, int32_t *valp)
Convert a raw ADC value to millivolts.
Definition: adc.h:745
@ ADC_GAIN_64
Definition: adc.h:51
@ ADC_GAIN_3
Definition: adc.h:43
@ ADC_GAIN_4
Definition: adc.h:44
@ ADC_GAIN_1_5
Definition: adc.h:34
@ ADC_GAIN_128
Definition: adc.h:52
@ ADC_GAIN_1_2
Definition: adc.h:38
@ ADC_GAIN_12
Definition: adc.h:47
@ ADC_GAIN_2_5
Definition: adc.h:37
@ ADC_GAIN_16
Definition: adc.h:48
@ ADC_GAIN_24
Definition: adc.h:49
@ ADC_GAIN_1
Definition: adc.h:41
@ ADC_GAIN_6
Definition: adc.h:45
@ ADC_GAIN_1_6
Definition: adc.h:33
@ ADC_GAIN_32
Definition: adc.h:50
@ ADC_GAIN_2_3
Definition: adc.h:39
@ ADC_GAIN_4_5
Definition: adc.h:40
@ ADC_GAIN_8
Definition: adc.h:46
@ ADC_GAIN_1_3
Definition: adc.h:36
@ ADC_GAIN_1_4
Definition: adc.h:35
@ ADC_GAIN_2
Definition: adc.h:42
@ ADC_ACTION_FINISH
Definition: adc.h:427
@ ADC_ACTION_REPEAT
Definition: adc.h:424
@ ADC_ACTION_CONTINUE
Definition: adc.h:418
@ ADC_REF_INTERNAL
Definition: adc.h:80
@ ADC_REF_EXTERNAL1
Definition: adc.h:82
@ ADC_REF_VDD_1_2
Definition: adc.h:77
@ ADC_REF_VDD_1_3
Definition: adc.h:78
@ ADC_REF_VDD_1_4
Definition: adc.h:79
@ ADC_REF_VDD_1
Definition: adc.h:76
@ ADC_REF_EXTERNAL0
Definition: adc.h:81
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
#define ENOTSUP
Definition: errno.h:115
Public kernel APIs.
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INT32_TYPE__ int32_t
Definition: stdint.h:74
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Structure for specifying the configuration of an ADC channel.
Definition: adc.h:88
enum adc_gain gain
Definition: adc.h:90
uint8_t differential
Definition: adc.h:131
uint16_t acquisition_time
Definition: adc.h:104
enum adc_reference reference
Definition: adc.h:93
uint8_t channel_id
Definition: adc.h:128
ADC driver API.
Definition: adc.h:584
adc_api_read read
Definition: adc.h:586
uint16_t ref_internal
Definition: adc.h:590
adc_api_channel_setup channel_setup
Definition: adc.h:585
Container for ADC channel information specified in devicetree.
Definition: adc.h:231
struct adc_channel_cfg channel_cfg
Definition: adc.h:253
uint8_t channel_id
Definition: adc.h:239
uint16_t vref_mv
Definition: adc.h:261
const struct device * dev
Definition: adc.h:236
uint8_t oversampling
Definition: adc.h:275
bool channel_cfg_dt_node_exists
Definition: adc.h:246
uint8_t resolution
Definition: adc.h:268
Structure defining additional options for an ADC sampling sequence.
Definition: adc.h:456
void * user_data
Definition: adc.h:478
uint16_t extra_samplings
Definition: adc.h:484
adc_sequence_callback callback
Definition: adc.h:472
uint32_t interval_us
Definition: adc.h:466
Structure defining an ADC sampling sequence.
Definition: adc.h:490
uint8_t oversampling
Definition: adc.h:542
const struct adc_sequence_options * options
Definition: adc.h:495
uint32_t channels
Definition: adc.h:504
void * buffer
Definition: adc.h:517
bool calibrate
Definition: adc.h:552
size_t buffer_size
Definition: adc.h:525
uint8_t resolution
Definition: adc.h:534
Runtime device structure (in ROM) per driver instance.
Definition: device.h:378
const void * api
Definition: device.h:384
Definition: kernel.h:5459