Zephyr API Documentation 4.0.0
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
56
74 int32_t *value);
75
86
93
96
107
131
134
135#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
141 uint8_t input_positive;
142
148 uint8_t input_negative;
149#endif /* CONFIG_ADC_CONFIGURABLE_INPUTS */
150
151#ifdef CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN
152 uint8_t current_source_pin_set : 1;
159 uint8_t current_source_pin[2];
160#endif /* CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN */
161
162#ifdef CONFIG_ADC_CONFIGURABLE_VBIAS_PIN
171 uint32_t vbias_pins;
172#endif /* CONFIG_ADC_CONFIGURABLE_VBIAS_PIN */
173};
174
239#define ADC_CHANNEL_CFG_DT(node_id) { \
240 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
241 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
242 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
243 .channel_id = DT_REG_ADDR(node_id), \
244IF_ENABLED(UTIL_OR(DT_PROP(node_id, zephyr_differential), \
245 UTIL_AND(CONFIG_ADC_CONFIGURABLE_INPUTS, \
246 DT_NODE_HAS_PROP(node_id, zephyr_input_negative))), \
247 (.differential = true,)) \
248IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
249 (.input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
250 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
251IF_ENABLED(CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN, \
252 (.current_source_pin_set = DT_NODE_HAS_PROP(node_id, zephyr_current_source_pin), \
253 .current_source_pin = DT_PROP_OR(node_id, zephyr_current_source_pin, {0}),)) \
254IF_ENABLED(CONFIG_ADC_CONFIGURABLE_VBIAS_PIN, \
255 (.vbias_pins = DT_PROP_OR(node_id, zephyr_vbias_pins, 0),)) \
256}
257
310
313#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
314 .dev = DEVICE_DT_GET(ctlr), \
315 .channel_id = input, \
316 ADC_CHANNEL_CFG_FROM_DT_NODE(\
317 ADC_CHANNEL_DT_NODE(ctlr, input)) \
318 }
319
320#define ADC_CHANNEL_DT_NODE(ctlr, input) \
321 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
322
323#define ADC_FOREACH_INPUT(node, input) \
324 IF_ENABLED(IS_EQ(DT_REG_ADDR_RAW(node), input), (node))
325
326#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
327 IF_ENABLED(DT_NODE_EXISTS(node_id), \
328 (.channel_cfg_dt_node_exists = true, \
329 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
330 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
331 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
332 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
333
404#define ADC_DT_SPEC_GET_BY_NAME(node_id, name) \
405 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
406 DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name))
407
418#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name) \
419 ADC_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
420
490#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
491 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
492 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
493
504#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
505 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
506
516#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
517
526#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
527
528/* Forward declaration of the adc_sequence structure. */
529struct adc_sequence;
530
547
567typedef enum adc_action (*adc_sequence_callback)(const struct device *dev,
568 const struct adc_sequence *sequence,
569 uint16_t sampling_index);
570
604
672
673
678typedef int (*adc_api_channel_setup)(const struct device *dev,
679 const struct adc_channel_cfg *channel_cfg);
680
685typedef int (*adc_api_read)(const struct device *dev,
686 const struct adc_sequence *sequence);
687
693typedef int (*adc_api_read_async)(const struct device *dev,
694 const struct adc_sequence *sequence,
695 struct k_poll_signal *async);
696
702__subsystem struct adc_driver_api {
705#ifdef CONFIG_ADC_ASYNC
706 adc_api_read_async read_async;
707#endif
709};
710
723__syscall int adc_channel_setup(const struct device *dev,
724 const struct adc_channel_cfg *channel_cfg);
725
726static inline int z_impl_adc_channel_setup(const struct device *dev,
727 const struct adc_channel_cfg *channel_cfg)
728{
729 const struct adc_driver_api *api =
730 (const struct adc_driver_api *)dev->api;
731
732 return api->channel_setup(dev, channel_cfg);
733}
734
744static inline int adc_channel_setup_dt(const struct adc_dt_spec *spec)
745{
746 if (!spec->channel_cfg_dt_node_exists) {
747 return -ENOTSUP;
748 }
749
750 return adc_channel_setup(spec->dev, &spec->channel_cfg);
751}
752
774__syscall int adc_read(const struct device *dev,
775 const struct adc_sequence *sequence);
776
777static inline int z_impl_adc_read(const struct device *dev,
778 const struct adc_sequence *sequence)
779{
780 const struct adc_driver_api *api =
781 (const struct adc_driver_api *)dev->api;
782
783 return api->read(dev, sequence);
784}
785
795static inline int adc_read_dt(const struct adc_dt_spec *spec,
796 const struct adc_sequence *sequence)
797{
798 return adc_read(spec->dev, sequence);
799}
800
821__syscall int adc_read_async(const struct device *dev,
822 const struct adc_sequence *sequence,
823 struct k_poll_signal *async);
824
825
826#ifdef CONFIG_ADC_ASYNC
827static inline int z_impl_adc_read_async(const struct device *dev,
828 const struct adc_sequence *sequence,
829 struct k_poll_signal *async)
830{
831 const struct adc_driver_api *api =
832 (const struct adc_driver_api *)dev->api;
833
834 return api->read_async(dev, sequence, async);
835}
836#endif /* CONFIG_ADC_ASYNC */
837
847static inline uint16_t adc_ref_internal(const struct device *dev)
848{
849 const struct adc_driver_api *api =
850 (const struct adc_driver_api *)dev->api;
851
852 return api->ref_internal;
853}
854
878static inline int adc_raw_to_millivolts(int32_t ref_mv,
879 enum adc_gain gain,
880 uint8_t resolution,
881 int32_t *valp)
882{
883 int32_t adc_mv = *valp * ref_mv;
884 int ret = adc_gain_invert(gain, &adc_mv);
885
886 if (ret == 0) {
887 *valp = (adc_mv >> resolution);
888 }
889
890 return ret;
891}
892
906static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec,
907 int32_t *valp)
908{
909 int32_t vref_mv;
910 uint8_t resolution;
911
912 if (!spec->channel_cfg_dt_node_exists) {
913 return -ENOTSUP;
914 }
915
917 vref_mv = (int32_t)adc_ref_internal(spec->dev);
918 } else {
919 vref_mv = spec->vref_mv;
920 }
921
922 resolution = spec->resolution;
923
924 /*
925 * For differential channels, one bit less needs to be specified
926 * for resolution to achieve correct conversion.
927 */
928 if (spec->channel_cfg.differential) {
929 resolution -= 1U;
930 }
931
932 return adc_raw_to_millivolts(vref_mv, spec->channel_cfg.gain,
933 resolution, valp);
934}
935
954static inline int adc_sequence_init_dt(const struct adc_dt_spec *spec,
955 struct adc_sequence *seq)
956{
957 if (!spec->channel_cfg_dt_node_exists) {
958 return -ENOTSUP;
959 }
960
961 seq->channels = BIT(spec->channel_id);
962 seq->resolution = spec->resolution;
963 seq->oversampling = spec->oversampling;
964
965 return 0;
966}
967
975static inline bool adc_is_ready_dt(const struct adc_dt_spec *spec)
976{
977 return device_is_ready(spec->dev);
978}
979
984#ifdef __cplusplus
985}
986#endif
987
988#include <zephyr/syscalls/adc.h>
989
990#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:906
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:795
adc_gain
ADC channel gain factors.
Definition adc.h:34
static bool adc_is_ready_dt(const struct adc_dt_spec *spec)
Validate that the ADC device is ready.
Definition adc.h:975
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:685
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:954
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:678
adc_action
Action to be performed after a sampling is done.
Definition adc.h:534
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:567
adc_reference
ADC references.
Definition adc.h:77
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:693
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition adc.h:847
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:744
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:878
@ ADC_GAIN_64
x 64.
Definition adc.h:53
@ ADC_GAIN_3
x 3.
Definition adc.h:45
@ ADC_GAIN_4
x 4.
Definition adc.h:46
@ ADC_GAIN_1_5
x 1/5.
Definition adc.h:36
@ ADC_GAIN_128
x 128.
Definition adc.h:54
@ ADC_GAIN_1_2
x 1/2.
Definition adc.h:40
@ ADC_GAIN_12
x 12.
Definition adc.h:49
@ ADC_GAIN_2_5
x 2/5.
Definition adc.h:39
@ ADC_GAIN_16
x 16.
Definition adc.h:50
@ ADC_GAIN_24
x 24.
Definition adc.h:51
@ ADC_GAIN_1
x 1.
Definition adc.h:43
@ ADC_GAIN_6
x 6.
Definition adc.h:47
@ ADC_GAIN_1_6
x 1/6.
Definition adc.h:35
@ ADC_GAIN_32
x 32.
Definition adc.h:52
@ ADC_GAIN_2_3
x 2/3.
Definition adc.h:41
@ ADC_GAIN_4_5
x 4/5.
Definition adc.h:42
@ ADC_GAIN_8
x 8.
Definition adc.h:48
@ ADC_GAIN_1_3
x 1/3.
Definition adc.h:38
@ ADC_GAIN_1_4
x 1/4.
Definition adc.h:37
@ ADC_GAIN_2
x 2.
Definition adc.h:44
@ ADC_ACTION_FINISH
The sequence should be finished immediately.
Definition adc.h:545
@ ADC_ACTION_REPEAT
The sampling should be repeated.
Definition adc.h:542
@ ADC_ACTION_CONTINUE
The sequence should be continued normally.
Definition adc.h:536
@ ADC_REF_INTERNAL
Internal.
Definition adc.h:82
@ ADC_REF_EXTERNAL1
External, input 1.
Definition adc.h:84
@ ADC_REF_VDD_1_2
VDD/2.
Definition adc.h:79
@ ADC_REF_VDD_1_3
VDD/3.
Definition adc.h:80
@ ADC_REF_VDD_1_4
VDD/4.
Definition adc.h:81
@ ADC_REF_VDD_1
VDD.
Definition adc.h:78
@ ADC_REF_EXTERNAL0
External, input 0.
Definition adc.h:83
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:114
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:90
enum adc_gain gain
Gain selection.
Definition adc.h:92
uint8_t differential
Channel type: single-ended or differential.
Definition adc.h:133
uint16_t acquisition_time
Acquisition time.
Definition adc.h:106
enum adc_reference reference
Reference selection.
Definition adc.h:95
uint8_t channel_id
Channel identifier.
Definition adc.h:130
ADC driver API.
Definition adc.h:702
adc_api_read read
Definition adc.h:704
uint16_t ref_internal
Definition adc.h:708
adc_api_channel_setup channel_setup
Definition adc.h:703
Container for ADC channel information specified in devicetree.
Definition adc.h:264
struct adc_channel_cfg channel_cfg
Configuration of the associated ADC channel specified in devicetree.
Definition adc.h:286
uint8_t channel_id
ADC channel identifier used by this io-channel.
Definition adc.h:272
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:294
const struct device * dev
Pointer to the device structure for the ADC driver instance used by this io-channel.
Definition adc.h:269
uint8_t oversampling
Oversampling setting to be used for that channel.
Definition adc.h:308
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:279
uint8_t resolution
ADC resolution to be used for that channel.
Definition adc.h:301
Structure defining additional options for an ADC sampling sequence.
Definition adc.h:574
void * user_data
Pointer to user data.
Definition adc.h:596
uint16_t extra_samplings
Number of extra samplings to perform (the total number of samplings is 1 + extra_samplings).
Definition adc.h:602
adc_sequence_callback callback
Callback function to be called after each sampling is done.
Definition adc.h:590
uint32_t interval_us
Interval between consecutive samplings (in microseconds), 0 means sample as fast as possible,...
Definition adc.h:584
Structure defining an ADC sampling sequence.
Definition adc.h:608
uint8_t oversampling
Oversampling setting.
Definition adc.h:660
const struct adc_sequence_options * options
Pointer to a structure defining additional options for the sequence.
Definition adc.h:613
uint32_t channels
Bit-mask indicating the channels to be included in each sampling of this sequence.
Definition adc.h:622
void * buffer
Pointer to a buffer where the samples are to be written.
Definition adc.h:635
bool calibrate
Perform calibration before the reading is taken if requested.
Definition adc.h:670
size_t buffer_size
Specifies the actual size of the buffer pointed by the "buffer" field (in bytes).
Definition adc.h:643
uint8_t resolution
ADC resolution.
Definition adc.h:652
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
Definition kernel.h:5768