Zephyr API Documentation 4.4.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
adc.h File Reference

Main header file for ADC (Analog-to-Digital Converter) driver API. More...

#include <zephyr/device.h>
#include <zephyr/dt-bindings/adc/adc.h>
#include <zephyr/kernel.h>
#include <zephyr/rtio/rtio.h>
#include <zephyr/dsp/types.h>
#include <zephyr/syscalls/adc.h>

Go to the source code of this file.

Data Structures

struct  adc_channel_cfg
 Structure for specifying the configuration of an ADC channel. More...
struct  adc_dt_spec
 Container for ADC channel information specified in devicetree. More...
struct  adc_sequence_options
 Structure defining additional options for an ADC sampling sequence. More...
struct  adc_sequence
 Structure defining an ADC sampling sequence. More...
struct  adc_data_header
struct  adc_data
 Data for the adc channel. More...
struct  adc_data::adc_sample_data
struct  adc_stream_trigger
struct  adc_chan_spec
 ADC Channel Specification. More...
struct  adc_read_config
struct  adc_decoder_api
 Decodes a single raw data buffer. More...
struct  adc_driver_api
 ADC driver API. More...
struct  adc_data_generic_header
 Generic header for encoded ADC streaming frames. More...

Macros

#define ADC_CHANNEL_CFG_DT(node_id)
 Get ADC channel configuration from a given devicetree node.
#define ADC_DT_SPEC_GET_BY_NAME(node_id, name)
 Get ADC io-channel information from devicetree by name.
#define ADC_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value)
 Like ADC_DT_SPEC_GET_BY_NAME(), with a fallback to a default value.
#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name)
 Get ADC io-channel information from a DT_DRV_COMPAT devicetree instance by name.
#define ADC_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value)
 Like ADC_DT_SPEC_INST_GET_BY_NAME(), with a fallback to a default value.
#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx)
 Get ADC io-channel information from devicetree.
#define ADC_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value)
 Like ADC_DT_SPEC_GET_BY_IDX(), with a fallback to a default value.
#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx)
 Get ADC io-channel information from a DT_DRV_COMPAT devicetree instance.
#define ADC_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value)
 Like ADC_DT_SPEC_INST_GET_BY_IDX(), with a fallback to a default value.
#define ADC_DT_SPEC_GET(node_id)
 Equivalent to ADC_DT_SPEC_GET_BY_IDX(node_id, 0).
#define ADC_DT_SPEC_GET_OR(node_id, default_value)
 Equivalent to ADC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value).
#define ADC_DT_SPEC_INST_GET(inst)
 Equivalent to ADC_DT_SPEC_INST_GET_BY_IDX(inst, 0).
#define ADC_DT_SPEC_INST_GET_OR(inst, default_value)
 Equivalent to ADC_DT_SPEC_INST_GET_BY_IDX_OR(inst, 0, default).
#define ADC_DECODER_NAME()
 Get the decoder name for the current driver.
#define ADC_DECODER_DT_GET(node_id)
 Statically get the decoder for a given node.
#define ADC_DECODER_API_DT_DEFINE()
 Define a decoder API.
#define ADC_DT_STREAM_IODEV(name, dt_node, adc_dt_spec, ...)
 Define an RTIO IO device for ADC streaming.

Typedefs

typedef 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.
typedef 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.
typedef int(* adc_api_read) (const struct device *dev, const struct adc_sequence *sequence)
 Type definition of ADC API function for setting a read request.
typedef void(* adc_api_submit) (const struct device *dev, struct rtio_iodev_sqe *sqe)
 Type definition of ADC API function for submitting a stream request.
typedef int(* adc_api_get_decoder) (const struct device *dev, const struct adc_decoder_api **api)
 Get the decoder associate with the given device.
typedef 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.
typedef int(* adc_raw_to_x_fn) (int32_t ref_mv, enum adc_gain gain, uint8_t resolution, int32_t *valp)
 Conversion from raw ADC units to a specific output unit.

Enumerations

enum  adc_gain {
  ADC_GAIN_1_6 , ADC_GAIN_1_5 , ADC_GAIN_1_4 , ADC_GAIN_2_7 ,
  ADC_GAIN_1_3 , ADC_GAIN_2_5 , ADC_GAIN_1_2 , ADC_GAIN_2_3 ,
  ADC_GAIN_4_5 , ADC_GAIN_1 , ADC_GAIN_2 , ADC_GAIN_3 ,
  ADC_GAIN_4 , ADC_GAIN_6 , ADC_GAIN_8 , ADC_GAIN_12 ,
  ADC_GAIN_16 , ADC_GAIN_24 , ADC_GAIN_32 , ADC_GAIN_64 ,
  ADC_GAIN_128
}
 ADC channel gain factors. More...
enum  adc_reference {
  ADC_REF_VDD_1 , ADC_REF_VDD_1_2 , ADC_REF_VDD_1_3 , ADC_REF_VDD_1_4 ,
  ADC_REF_INTERNAL , ADC_REF_EXTERNAL0 , ADC_REF_EXTERNAL1
}
 ADC references. More...
enum  adc_action { ADC_ACTION_CONTINUE = 0 , ADC_ACTION_REPEAT , ADC_ACTION_FINISH }
 Action to be performed after a sampling is done. More...
enum  adc_trigger_type {
  ADC_TRIG_DATA_READY , ADC_TRIG_FIFO_WATERMARK , ADC_TRIG_FIFO_FULL , ADC_TRIG_COMMON_COUNT ,
  ADC_TRIG_PRIV_START = ADC_TRIG_COMMON_COUNT , ADC_TRIG_MAX = INT16_MAX
}
 ADC trigger types. More...
enum  adc_stream_data_opt { ADC_STREAM_DATA_INCLUDE = 0 , ADC_STREAM_DATA_NOP = 1 , ADC_STREAM_DATA_DROP = 2 }
 Options for what to do with the associated data when a trigger is consumed. More...

Functions

int adc_gain_invert (enum adc_gain gain, int32_t *value)
 Invert the application of gain to a measurement value.
int adc_gain_invert_64 (enum adc_gain gain, int64_t *value)
 Invert the application of gain to a measurement value.
int adc_channel_setup (const struct device *dev, const struct adc_channel_cfg *channel_cfg)
 Configure an ADC channel.
static int adc_channel_setup_dt (const struct adc_dt_spec *spec)
 Configure an ADC channel from a struct adc_dt_spec.
int adc_read (const struct device *dev, const struct adc_sequence *sequence)
 Set a read request.
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.
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_read_async_dt (const struct adc_dt_spec *spec, const struct adc_sequence *sequence, struct k_poll_signal *async)
 Set an asynchronous read request from a struct adc_dt_spec.
int adc_get_decoder (const struct device *dev, const struct adc_decoder_api **api)
 Get decoder APIs for that device.
static int adc_stream (struct rtio_iodev *iodev, struct rtio *ctx, void *userdata, struct rtio_sqe **handle)
 Start a continuous ADC streaming session.
static uint16_t adc_ref_internal (const struct device *dev)
 Get the internal reference voltage.
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.
static int adc_raw_to_microvolts (int32_t ref_mv, enum adc_gain gain, uint8_t resolution, int32_t *valp)
 Convert a raw ADC value to microvolts.
static int adc_raw_to_x_dt_chan (adc_raw_to_x_fn conv_func, const struct adc_dt_spec *spec, const struct adc_channel_cfg *channel_cfg, int32_t *valp)
 Convert a raw ADC value to an arbitrary output unit.
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.
static int adc_raw_to_microvolts_dt (const struct adc_dt_spec *spec, int32_t *valp)
 Convert a raw ADC value to microvolts using information stored in a struct adc_dt_spec.
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.
static bool adc_is_ready_dt (const struct adc_dt_spec *spec)
 Validate that the ADC device is ready.
 DT_FOREACH_STATUS_OKAY_NODE (Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL) extern const struct rtio_iodev_api __adc_iodev_api

Detailed Description

Main header file for ADC (Analog-to-Digital Converter) driver API.

Macro Definition Documentation

◆ ADC_DECODER_API_DT_DEFINE

#define ADC_DECODER_API_DT_DEFINE ( )
Value:
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
#define ADC_DECODER_NAME()
Get the decoder name for the current driver.
Definition adc.h:1488
#define DT_HAS_COMPAT_STATUS_OKAY(compat)
Does the devicetree have a status okay node with a compatible?
Definition devicetree.h:3896
#define STRUCT_SECTION_ITERABLE(struct_type, varname)
Defines a new element for an iterable section.
Definition iterable_sections.h:216
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition util_macro.h:209
Decodes a single raw data buffer.
Definition adc.h:912

Define a decoder API.

This macro should be created once per compatible string of a adc and will create a statically referenceable decoder API.

.get_frame_count = my_driver_get_frame_count,
.get_timestamp = my_driver_get_timestamp,
.get_shift = my_driver_get_shift,
.decode = my_driver_decode,
};
#define ADC_DECODER_API_DT_DEFINE()
Define a decoder API.
Definition adc.h:1515

◆ ADC_DECODER_DT_GET

#define ADC_DECODER_DT_GET ( node_id)
Value:
&UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __adc_decoder_api)
#define DT_STRING_TOKEN_BY_IDX(node_id, prop, idx)
Get an element out of a string-array property as a token.
Definition devicetree.h:1358
#define UTIL_CAT(a,...)
Definition util_internal.h:145

Statically get the decoder for a given node.

static const adc_decoder_api *decoder = ADC_DECODER_DT_GET(DT_ALIAS(adc));
#define ADC_DECODER_DT_GET(node_id)
Statically get the decoder for a given node.
Definition adc.h:1497
#define DT_ALIAS(alias)
Get a node identifier from /aliases.
Definition devicetree.h:237

◆ ADC_DECODER_NAME

#define ADC_DECODER_NAME ( )
Value:
UTIL_CAT(DT_DRV_COMPAT, __adc_decoder_api)

Get the decoder name for the current driver.

This function depends on DT_DRV_COMPAT being defined.

◆ ADC_DT_STREAM_IODEV

#define ADC_DT_STREAM_IODEV ( name,
dt_node,
adc_dt_spec,
... )
Value:
static struct adc_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
static struct adc_read_config _CONCAT(__adc_read_config_, name) = { \
.adc = DEVICE_DT_GET(dt_node), \
.is_streaming = true, \
.adc_spec = adc_dt_spec, \
.triggers = _CONCAT(__trigger_array_, name), \
.adc_spec_cnt = ARRAY_SIZE(adc_dt_spec), \
.trigger_cnt = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
}; \
RTIO_IODEV_DEFINE(name, &__adc_iodev_api, &_CONCAT(__adc_read_config_, name))
#define DEVICE_DT_GET(node_id)
Get a device reference from a devicetree node identifier.
Definition device.h:317
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:118
Container for ADC channel information specified in devicetree.
Definition adc.h:295
Definition adc.h:896
Definition adc.h:875

Define an RTIO IO device for ADC streaming.

This macro creates the static data structures needed to stream ADC samples via the RTIO framework. It associates an ADC controller (identified by a Devicetree node), a set of ADC channel specifications, and one or more trigger definitions.

Example usage:

ADC_DT_STREAM_IODEV(my_iodev, DT_ALIAS(adc0), adc_channels,
#define ADC_DT_STREAM_IODEV(name, dt_node, adc_dt_spec,...)
Define an RTIO IO device for ADC streaming.
Definition adc.h:1553
@ ADC_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition adc.h:868
@ ADC_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition adc.h:844
Parameters
nameVariable name for the resulting struct rtio_iodev.
dt_nodeDevicetree node identifier of the ADC controller.
adc_dt_specArray of adc_dt_spec channel specifications.
...One or more adc_stream_trigger initializers.

Function Documentation

◆ DT_FOREACH_STATUS_OKAY_NODE()

DT_FOREACH_STATUS_OKAY_NODE ( Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL ) const