Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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#ifdef CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN
150 uint8_t current_source_pin_set : 1;
157 uint8_t current_source_pin[2];
158#endif /* CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN */
159};
160
225#define ADC_CHANNEL_CFG_DT(node_id) { \
226 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
227 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
228 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
229 .channel_id = DT_REG_ADDR(node_id), \
230IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
231 (.differential = DT_NODE_HAS_PROP(node_id, zephyr_input_negative), \
232 .input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
233 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
234IF_ENABLED(DT_PROP(node_id, zephyr_differential), \
235 (.differential = true,)) \
236IF_ENABLED(CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN, \
237 (.current_source_pin_set = DT_NODE_HAS_PROP(node_id, zephyr_current_source_pin), \
238 .current_source_pin = DT_PROP_OR(node_id, zephyr_current_source_pin, {0}),)) \
239}
240
252 const struct device *dev;
253
256
263
270
278
285
292};
293
296#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
297 .dev = DEVICE_DT_GET(ctlr), \
298 .channel_id = input, \
299 ADC_CHANNEL_CFG_FROM_DT_NODE(\
300 ADC_CHANNEL_DT_NODE(ctlr, input)) \
301 }
302
303#define ADC_CHANNEL_DT_NODE(ctlr, input) \
304 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
305
306#define ADC_FOREACH_INPUT(node, input) \
307 IF_ENABLED(IS_EQ(DT_REG_ADDR(node), input), (node))
308
309#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
310 IF_ENABLED(DT_NODE_EXISTS(node_id), \
311 (.channel_cfg_dt_node_exists = true, \
312 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
313 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
314 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
315 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
316
388#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
389 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
390 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
391
402#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
403 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
404
414#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
415
424#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
425
426/* Forward declaration of the adc_sequence structure. */
427struct adc_sequence;
428
435
441
444};
445
465typedef enum adc_action (*adc_sequence_callback)(const struct device *dev,
466 const struct adc_sequence *sequence,
467 uint16_t sampling_index);
468
483
489
495
501};
502
512
521
533 void *buffer;
534
542
551
559
569};
570
571
576typedef int (*adc_api_channel_setup)(const struct device *dev,
577 const struct adc_channel_cfg *channel_cfg);
578
583typedef int (*adc_api_read)(const struct device *dev,
584 const struct adc_sequence *sequence);
585
591typedef int (*adc_api_read_async)(const struct device *dev,
592 const struct adc_sequence *sequence,
593 struct k_poll_signal *async);
594
600__subsystem struct adc_driver_api {
603#ifdef CONFIG_ADC_ASYNC
604 adc_api_read_async read_async;
605#endif
607};
608
621__syscall int adc_channel_setup(const struct device *dev,
622 const struct adc_channel_cfg *channel_cfg);
623
624static inline int z_impl_adc_channel_setup(const struct device *dev,
625 const struct adc_channel_cfg *channel_cfg)
626{
627 const struct adc_driver_api *api =
628 (const struct adc_driver_api *)dev->api;
629
630 return api->channel_setup(dev, channel_cfg);
631}
632
642static inline int adc_channel_setup_dt(const struct adc_dt_spec *spec)
643{
644 if (!spec->channel_cfg_dt_node_exists) {
645 return -ENOTSUP;
646 }
647
648 return adc_channel_setup(spec->dev, &spec->channel_cfg);
649}
650
672__syscall int adc_read(const struct device *dev,
673 const struct adc_sequence *sequence);
674
675static inline int z_impl_adc_read(const struct device *dev,
676 const struct adc_sequence *sequence)
677{
678 const struct adc_driver_api *api =
679 (const struct adc_driver_api *)dev->api;
680
681 return api->read(dev, sequence);
682}
683
693static inline int adc_read_dt(const struct adc_dt_spec *spec,
694 const struct adc_sequence *sequence)
695{
696 return adc_read(spec->dev, sequence);
697}
698
719__syscall int adc_read_async(const struct device *dev,
720 const struct adc_sequence *sequence,
721 struct k_poll_signal *async);
722
723
724#ifdef CONFIG_ADC_ASYNC
725static inline int z_impl_adc_read_async(const struct device *dev,
726 const struct adc_sequence *sequence,
727 struct k_poll_signal *async)
728{
729 const struct adc_driver_api *api =
730 (const struct adc_driver_api *)dev->api;
731
732 return api->read_async(dev, sequence, async);
733}
734#endif /* CONFIG_ADC_ASYNC */
735
745static inline uint16_t adc_ref_internal(const struct device *dev)
746{
747 const struct adc_driver_api *api =
748 (const struct adc_driver_api *)dev->api;
749
750 return api->ref_internal;
751}
752
776static inline int adc_raw_to_millivolts(int32_t ref_mv,
777 enum adc_gain gain,
778 uint8_t resolution,
779 int32_t *valp)
780{
781 int32_t adc_mv = *valp * ref_mv;
782 int ret = adc_gain_invert(gain, &adc_mv);
783
784 if (ret == 0) {
785 *valp = (adc_mv >> resolution);
786 }
787
788 return ret;
789}
790
804static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
805 int32_t *valp)
806{
807 int32_t vref_mv;
808 uint8_t resolution;
809
810 if (!spec->channel_cfg_dt_node_exists) {
811 return -ENOTSUP;
812 }
813
815 vref_mv = (int32_t)adc_ref_internal(spec->dev);
816 } else {
817 vref_mv = spec->vref_mv;
818 }
819
820 resolution = spec->resolution;
821
822 /*
823 * For differential channels, one bit less needs to be specified
824 * for resolution to achieve correct conversion.
825 */
826 if (spec->channel_cfg.differential) {
827 resolution -= 1U;
828 }
829
830 return adc_raw_to_millivolts(vref_mv, spec->channel_cfg.gain,
831 resolution, valp);
832}
833
852static inline int adc_sequence_init_dt(const struct adc_dt_spec *spec,
853 struct adc_sequence *seq)
854{
855 if (!spec->channel_cfg_dt_node_exists) {
856 return -ENOTSUP;
857 }
858
859 seq->channels = BIT(spec->channel_id);
860 seq->resolution = spec->resolution;
861 seq->oversampling = spec->oversampling;
862
863 return 0;
864}
865
873static inline bool adc_is_ready_dt(const struct adc_dt_spec *spec)
874{
875 return device_is_ready(spec->dev);
876}
877
882#ifdef __cplusplus
883}
884#endif
885
886#include <syscalls/adc.h>
887
888#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:804
static int adc_read_dt(const struct adc_dt_spec *spec, const struct adc_sequence *sequence)
Set a read request from a struct adc_dt_spec.
Definition: adc.h:693
adc_gain
ADC channel gain factors.
Definition: adc.h:32
static bool adc_is_ready_dt(const struct adc_dt_spec *spec)
Validate that the ADC device is ready.
Definition: adc.h:873
int(* adc_api_read)(const struct device *dev, const struct adc_sequence *sequence)
Type definition of ADC API function for setting a read request.
Definition: adc.h:583
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:852
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.
Definition: adc.h:576
adc_action
Action to be performed after a sampling is done.
Definition: adc.h:432
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:465
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.
Definition: adc.h:591
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition: adc.h:745
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:642
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:776
@ ADC_GAIN_64
x 64.
Definition: adc.h:51
@ ADC_GAIN_3
x 3.
Definition: adc.h:43
@ ADC_GAIN_4
x 4.
Definition: adc.h:44
@ ADC_GAIN_1_5
x 1/5.
Definition: adc.h:34
@ ADC_GAIN_128
x 128.
Definition: adc.h:52
@ ADC_GAIN_1_2
x 1/2.
Definition: adc.h:38
@ ADC_GAIN_12
x 12.
Definition: adc.h:47
@ ADC_GAIN_2_5
x 2/5.
Definition: adc.h:37
@ ADC_GAIN_16
x 16.
Definition: adc.h:48
@ ADC_GAIN_24
x 24.
Definition: adc.h:49
@ ADC_GAIN_1
x 1.
Definition: adc.h:41
@ ADC_GAIN_6
x 6.
Definition: adc.h:45
@ ADC_GAIN_1_6
x 1/6.
Definition: adc.h:33
@ ADC_GAIN_32
x 32.
Definition: adc.h:50
@ ADC_GAIN_2_3
x 2/3.
Definition: adc.h:39
@ ADC_GAIN_4_5
x 4/5.
Definition: adc.h:40
@ ADC_GAIN_8
x 8.
Definition: adc.h:46
@ ADC_GAIN_1_3
x 1/3.
Definition: adc.h:36
@ ADC_GAIN_1_4
x 1/4.
Definition: adc.h:35
@ ADC_GAIN_2
x 2.
Definition: adc.h:42
@ ADC_ACTION_FINISH
The sequence should be finished immediately.
Definition: adc.h:443
@ ADC_ACTION_REPEAT
The sampling should be repeated.
Definition: adc.h:440
@ ADC_ACTION_CONTINUE
The sequence should be continued normally.
Definition: adc.h:434
@ ADC_REF_INTERNAL
Internal.
Definition: adc.h:80
@ ADC_REF_EXTERNAL1
External, input 1.
Definition: adc.h:82
@ ADC_REF_VDD_1_2
VDD/2.
Definition: adc.h:77
@ ADC_REF_VDD_1_3
VDD/3.
Definition: adc.h:78
@ ADC_REF_VDD_1_4
VDD/4.
Definition: adc.h:79
@ ADC_REF_VDD_1
VDD.
Definition: adc.h:76
@ ADC_REF_EXTERNAL0
External, input 0.
Definition: adc.h:81
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
#define ENOTSUP
Unsupported value.
Definition: errno.h:115
Public kernel APIs.
__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
Gain selection.
Definition: adc.h:90
uint8_t differential
Channel type: single-ended or differential.
Definition: adc.h:131
uint16_t acquisition_time
Acquisition time.
Definition: adc.h:104
enum adc_reference reference
Reference selection.
Definition: adc.h:93
uint8_t channel_id
Channel identifier.
Definition: adc.h:128
ADC driver API.
Definition: adc.h:600
adc_api_read read
Definition: adc.h:602
uint16_t ref_internal
Definition: adc.h:606
adc_api_channel_setup channel_setup
Definition: adc.h:601
Container for ADC channel information specified in devicetree.
Definition: adc.h:247
struct adc_channel_cfg channel_cfg
Configuration of the associated ADC channel specified in devicetree.
Definition: adc.h:269
uint8_t channel_id
ADC channel identifier used by this io-channel.
Definition: adc.h:255
uint16_t vref_mv
Voltage of the reference selected for the channel or 0 if this value is not provided in devicetree.
Definition: adc.h:277
const struct device * dev
Pointer to the device structure for the ADC driver instance used by this io-channel.
Definition: adc.h:252
uint8_t oversampling
Oversampling setting to be used for that channel.
Definition: adc.h:291
bool channel_cfg_dt_node_exists
Flag indicating whether configuration of the associated ADC channel is provided as a child node of th...
Definition: adc.h:262
uint8_t resolution
ADC resolution to be used for that channel.
Definition: adc.h:284
Structure defining additional options for an ADC sampling sequence.
Definition: adc.h:472
void * user_data
Pointer to user data.
Definition: adc.h:494
uint16_t extra_samplings
Number of extra samplings to perform (the total number of samplings is 1 + extra_samplings).
Definition: adc.h:500
adc_sequence_callback callback
Callback function to be called after each sampling is done.
Definition: adc.h:488
uint32_t interval_us
Interval between consecutive samplings (in microseconds), 0 means sample as fast as possible,...
Definition: adc.h:482
Structure defining an ADC sampling sequence.
Definition: adc.h:506
uint8_t oversampling
Oversampling setting.
Definition: adc.h:558
const struct adc_sequence_options * options
Pointer to a structure defining additional options for the sequence.
Definition: adc.h:511
uint32_t channels
Bit-mask indicating the channels to be included in each sampling of this sequence.
Definition: adc.h:520
void * buffer
Pointer to a buffer where the samples are to be written.
Definition: adc.h:533
bool calibrate
Perform calibration before the reading is taken if requested.
Definition: adc.h:568
size_t buffer_size
Specifies the actual size of the buffer pointed by the "buffer" field (in bytes).
Definition: adc.h:541
uint8_t resolution
ADC resolution.
Definition: adc.h:550
Runtime device structure (in ROM) per driver instance.
Definition: device.h:387
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:393
Definition: kernel.h:5622