Zephyr Project API  3.2.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
19#ifdef __cplusplus
20extern "C" {
21#endif
22
52};
53
71 int32_t *value);
72
82};
83
90
93
104
128
131
132#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
138 uint8_t input_positive;
139
145 uint8_t input_negative;
146#endif /* CONFIG_ADC_CONFIGURABLE_INPUTS */
147};
148
213#define ADC_CHANNEL_CFG_DT(node_id) { \
214 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
215 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
216 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
217 .channel_id = DT_REG_ADDR(node_id), \
218IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
219 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, zephyr_input_negative), \
220 (.differential = true, \
221 .input_positive = DT_PROP(node_id, zephyr_input_positive), \
222 .input_negative = DT_PROP(node_id, zephyr_input_negative),), \
223 (.input_positive = DT_PROP(node_id, zephyr_input_positive),)))) \
224}
225
237 const struct device *dev;
238
241
248
255
263
270
277};
278
281#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
282 .dev = DEVICE_DT_GET(ctlr), \
283 .channel_id = input, \
284 ADC_CHANNEL_CFG_FROM_DT_NODE(\
285 ADC_CHANNEL_DT_NODE(ctlr, input)) \
286 }
287
288#define ADC_CHANNEL_DT_NODE(ctlr, input) \
289 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
290
291#define ADC_FOREACH_INPUT(node, input) \
292 IF_ENABLED(IS_EQ(DT_REG_ADDR(node), input), (node))
293
294#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
295 IF_ENABLED(DT_NODE_EXISTS(node_id), \
296 (.channel_cfg_dt_node_exists = true, \
297 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
298 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
299 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
300 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
301
373#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
374 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
375 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
376
387#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
388 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
389
399#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
400
409#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
410
411/* Forward declaration of the adc_sequence structure. */
412struct adc_sequence;
413
420
426
429};
430
450typedef enum adc_action (*adc_sequence_callback)(const struct device *dev,
451 const struct adc_sequence *sequence,
452 uint16_t sampling_index);
453
468
474
480
486};
487
497
505
515 void *buffer;
516
524
533
541
551};
552
553
558typedef int (*adc_api_channel_setup)(const struct device *dev,
559 const struct adc_channel_cfg *channel_cfg);
560
565typedef int (*adc_api_read)(const struct device *dev,
566 const struct adc_sequence *sequence);
567
573typedef int (*adc_api_read_async)(const struct device *dev,
574 const struct adc_sequence *sequence,
575 struct k_poll_signal *async);
576
582__subsystem struct adc_driver_api {
585#ifdef CONFIG_ADC_ASYNC
586 adc_api_read_async read_async;
587#endif
589};
590
603__syscall int adc_channel_setup(const struct device *dev,
604 const struct adc_channel_cfg *channel_cfg);
605
606static inline int z_impl_adc_channel_setup(const struct device *dev,
607 const struct adc_channel_cfg *channel_cfg)
608{
609 const struct adc_driver_api *api =
610 (const struct adc_driver_api *)dev->api;
611
612 return api->channel_setup(dev, channel_cfg);
613}
614
624static inline int adc_channel_setup_dt(const struct adc_dt_spec *spec)
625{
626 if (!spec->channel_cfg_dt_node_exists) {
627 return -ENOTSUP;
628 }
629
630 return adc_channel_setup(spec->dev, &spec->channel_cfg);
631}
632
654__syscall int adc_read(const struct device *dev,
655 const struct adc_sequence *sequence);
656
657static inline int z_impl_adc_read(const struct device *dev,
658 const struct adc_sequence *sequence)
659{
660 const struct adc_driver_api *api =
661 (const struct adc_driver_api *)dev->api;
662
663 return api->read(dev, sequence);
664}
665
686__syscall int adc_read_async(const struct device *dev,
687 const struct adc_sequence *sequence,
688 struct k_poll_signal *async);
689
690
691#ifdef CONFIG_ADC_ASYNC
692static inline int z_impl_adc_read_async(const struct device *dev,
693 const struct adc_sequence *sequence,
694 struct k_poll_signal *async)
695{
696 const struct adc_driver_api *api =
697 (const struct adc_driver_api *)dev->api;
698
699 return api->read_async(dev, sequence, async);
700}
701#endif /* CONFIG_ADC_ASYNC */
702
712static inline uint16_t adc_ref_internal(const struct device *dev)
713{
714 const struct adc_driver_api *api =
715 (const struct adc_driver_api *)dev->api;
716
717 return api->ref_internal;
718}
719
743static inline int adc_raw_to_millivolts(int32_t ref_mv,
744 enum adc_gain gain,
745 uint8_t resolution,
746 int32_t *valp)
747{
748 int32_t adc_mv = *valp * ref_mv;
749 int ret = adc_gain_invert(gain, &adc_mv);
750
751 if (ret == 0) {
752 *valp = (adc_mv >> resolution);
753 }
754
755 return ret;
756}
757
771static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
772 int32_t *valp)
773{
774 int32_t vref_mv;
775 uint8_t resolution;
776
777 if (!spec->channel_cfg_dt_node_exists) {
778 return -ENOTSUP;
779 }
780
782 vref_mv = (int32_t)adc_ref_internal(spec->dev);
783 } else {
784 vref_mv = spec->vref_mv;
785 }
786
787 resolution = spec->resolution;
788
789 /*
790 * For differential channels, one bit less needs to be specified
791 * for resolution to achieve correct conversion.
792 */
793 if (spec->channel_cfg.differential) {
794 resolution -= 1U;
795 }
796
797 return adc_raw_to_millivolts(vref_mv, spec->channel_cfg.gain,
798 resolution, valp);
799}
800
819static inline int adc_sequence_init_dt(const struct adc_dt_spec *spec,
820 struct adc_sequence *seq)
821{
822 if (!spec->channel_cfg_dt_node_exists) {
823 return -ENOTSUP;
824 }
825
826 seq->channels = BIT(spec->channel_id);
827 seq->resolution = spec->resolution;
828 seq->oversampling = spec->oversampling;
829
830 return 0;
831}
832
837#ifdef __cplusplus
838}
839#endif
840
841#include <syscalls/adc.h>
842
843#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:771
adc_gain
ADC channel gain factors.
Definition: adc.h:31
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:565
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:819
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:558
adc_action
Action to be performed after a sampling is done.
Definition: adc.h:417
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:450
adc_reference
ADC references.
Definition: adc.h:74
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:573
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition: adc.h:712
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:624
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:743
@ ADC_GAIN_64
Definition: adc.h:50
@ ADC_GAIN_3
Definition: adc.h:42
@ ADC_GAIN_4
Definition: adc.h:43
@ ADC_GAIN_1_5
Definition: adc.h:33
@ ADC_GAIN_128
Definition: adc.h:51
@ ADC_GAIN_1_2
Definition: adc.h:37
@ ADC_GAIN_12
Definition: adc.h:46
@ ADC_GAIN_2_5
Definition: adc.h:36
@ ADC_GAIN_16
Definition: adc.h:47
@ ADC_GAIN_24
Definition: adc.h:48
@ ADC_GAIN_1
Definition: adc.h:40
@ ADC_GAIN_6
Definition: adc.h:44
@ ADC_GAIN_1_6
Definition: adc.h:32
@ ADC_GAIN_32
Definition: adc.h:49
@ ADC_GAIN_2_3
Definition: adc.h:38
@ ADC_GAIN_4_5
Definition: adc.h:39
@ ADC_GAIN_8
Definition: adc.h:45
@ ADC_GAIN_1_3
Definition: adc.h:35
@ ADC_GAIN_1_4
Definition: adc.h:34
@ ADC_GAIN_2
Definition: adc.h:41
@ ADC_ACTION_FINISH
Definition: adc.h:428
@ ADC_ACTION_REPEAT
Definition: adc.h:425
@ ADC_ACTION_CONTINUE
Definition: adc.h:419
@ ADC_REF_INTERNAL
Definition: adc.h:79
@ ADC_REF_EXTERNAL1
Definition: adc.h:81
@ ADC_REF_VDD_1_2
Definition: adc.h:76
@ ADC_REF_VDD_1_3
Definition: adc.h:77
@ ADC_REF_VDD_1_4
Definition: adc.h:78
@ ADC_REF_VDD_1
Definition: adc.h:75
@ ADC_REF_EXTERNAL0
Definition: adc.h:80
#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
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:87
enum adc_gain gain
Definition: adc.h:89
uint8_t differential
Definition: adc.h:130
uint16_t acquisition_time
Definition: adc.h:103
enum adc_reference reference
Definition: adc.h:92
uint8_t channel_id
Definition: adc.h:127
ADC driver API.
Definition: adc.h:582
adc_api_read read
Definition: adc.h:584
uint16_t ref_internal
Definition: adc.h:588
adc_api_channel_setup channel_setup
Definition: adc.h:583
Container for ADC channel information specified in devicetree.
Definition: adc.h:232
struct adc_channel_cfg channel_cfg
Definition: adc.h:254
uint8_t channel_id
Definition: adc.h:240
uint16_t vref_mv
Definition: adc.h:262
const struct device * dev
Definition: adc.h:237
uint8_t oversampling
Definition: adc.h:276
bool channel_cfg_dt_node_exists
Definition: adc.h:247
uint8_t resolution
Definition: adc.h:269
Structure defining additional options for an ADC sampling sequence.
Definition: adc.h:457
void * user_data
Definition: adc.h:479
uint16_t extra_samplings
Definition: adc.h:485
adc_sequence_callback callback
Definition: adc.h:473
uint32_t interval_us
Definition: adc.h:467
Structure defining an ADC sampling sequence.
Definition: adc.h:491
uint8_t oversampling
Definition: adc.h:540
const struct adc_sequence_options * options
Definition: adc.h:496
uint32_t channels
Definition: adc.h:504
void * buffer
Definition: adc.h:515
bool calibrate
Definition: adc.h:550
size_t buffer_size
Definition: adc.h:523
uint8_t resolution
Definition: adc.h:532
Runtime device structure (in ROM) per driver instance.
Definition: device.h:435
const void * api
Definition: device.h:441
Definition: kernel.h:5396