13#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_H_
14#define ZEPHYR_INCLUDE_DRIVERS_ADC_H_
136#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
152#ifdef CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN
153 uint8_t current_source_pin_set : 1;
163#ifdef CONFIG_ADC_CONFIGURABLE_VBIAS_PIN
240#define ADC_CHANNEL_CFG_DT(node_id) { \
241 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
242 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
243 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
244 .channel_id = DT_REG_ADDR(node_id), \
245IF_ENABLED(UTIL_OR(DT_PROP(node_id, zephyr_differential), \
246 UTIL_AND(CONFIG_ADC_CONFIGURABLE_INPUTS, \
247 DT_NODE_HAS_PROP(node_id, zephyr_input_negative))), \
248 (.differential = true,)) \
249IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
250 (.input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
251 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
252IF_ENABLED(CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN, \
253 (.current_source_pin_set = DT_NODE_HAS_PROP(node_id, zephyr_current_source_pin), \
254 .current_source_pin = DT_PROP_OR(node_id, zephyr_current_source_pin, {0}),)) \
255IF_ENABLED(CONFIG_ADC_CONFIGURABLE_VBIAS_PIN, \
256 (.vbias_pins = DT_PROP_OR(node_id, zephyr_vbias_pins, 0),)) \
314#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
315 .dev = DEVICE_DT_GET(ctlr), \
316 .channel_id = input, \
317 ADC_CHANNEL_CFG_FROM_DT_NODE(\
318 ADC_CHANNEL_DT_NODE(ctlr, input)) \
321#define ADC_CHANNEL_DT_NODE(ctlr, input) \
322 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
324#define ADC_FOREACH_INPUT(node, input) \
325 IF_ENABLED(IS_EQ(DT_REG_ADDR_RAW(node), input), (node))
327#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
328 IF_ENABLED(DT_NODE_EXISTS(node_id), \
329 (.channel_cfg_dt_node_exists = true, \
330 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
331 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
332 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
333 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
405#define ADC_DT_SPEC_GET_BY_NAME(node_id, name) \
406 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
407 DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name))
419#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name) \
420 ADC_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
491#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
492 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
493 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
505#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
506 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
517#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
527#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
706#ifdef CONFIG_ADC_ASYNC
727static inline int z_impl_adc_channel_setup(
const struct device *dev,
775static inline int z_impl_adc_read(
const struct device *dev,
821#ifdef CONFIG_ADC_ASYNC
822static inline int z_impl_adc_read_async(
const struct device *dev,
826 return DEVICE_API_GET(adc, dev)->read_async(dev, sequence, async);
872 int32_t adc_mv = *valp * ref_mv;
876 *valp = (adc_mv >> resolution);
977#include <zephyr/syscalls/adc.h>
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1263
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:895
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:790
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:964
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:686
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:943
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:679
adc_action
Action to be performed after a sampling is done.
Definition adc.h:535
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:568
adc_reference
ADC references.
Definition adc.h:78
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:694
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition adc.h:839
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:742
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:867
@ ADC_GAIN_64
x 64.
Definition adc.h:54
@ ADC_GAIN_3
x 3.
Definition adc.h:46
@ ADC_GAIN_4
x 4.
Definition adc.h:47
@ ADC_GAIN_1_5
x 1/5.
Definition adc.h:36
@ ADC_GAIN_128
x 128.
Definition adc.h:55
@ ADC_GAIN_1_2
x 1/2.
Definition adc.h:41
@ ADC_GAIN_2_7
x 2/7.
Definition adc.h:38
@ ADC_GAIN_12
x 12.
Definition adc.h:50
@ ADC_GAIN_2_5
x 2/5.
Definition adc.h:40
@ ADC_GAIN_16
x 16.
Definition adc.h:51
@ ADC_GAIN_24
x 24.
Definition adc.h:52
@ ADC_GAIN_1
x 1.
Definition adc.h:44
@ ADC_GAIN_6
x 6.
Definition adc.h:48
@ ADC_GAIN_1_6
x 1/6.
Definition adc.h:35
@ ADC_GAIN_32
x 32.
Definition adc.h:53
@ ADC_GAIN_2_3
x 2/3.
Definition adc.h:42
@ ADC_GAIN_4_5
x 4/5.
Definition adc.h:43
@ ADC_GAIN_8
x 8.
Definition adc.h:49
@ ADC_GAIN_1_3
x 1/3.
Definition adc.h:39
@ ADC_GAIN_1_4
x 1/4.
Definition adc.h:37
@ ADC_GAIN_2
x 2.
Definition adc.h:45
@ ADC_ACTION_FINISH
The sequence should be finished immediately.
Definition adc.h:546
@ ADC_ACTION_REPEAT
The sampling should be repeated.
Definition adc.h:543
@ ADC_ACTION_CONTINUE
The sequence should be continued normally.
Definition adc.h:537
@ ADC_REF_INTERNAL
Internal.
Definition adc.h:83
@ ADC_REF_EXTERNAL1
External, input 1.
Definition adc.h:85
@ ADC_REF_VDD_1_2
VDD/2.
Definition adc.h:80
@ ADC_REF_VDD_1_3
VDD/3.
Definition adc.h:81
@ ADC_REF_VDD_1_4
VDD/4.
Definition adc.h:82
@ ADC_REF_VDD_1
VDD.
Definition adc.h:79
@ ADC_REF_EXTERNAL0
External, input 0.
Definition adc.h:84
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
__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:91
enum adc_gain gain
Gain selection.
Definition adc.h:93
uint8_t differential
Channel type: single-ended or differential.
Definition adc.h:134
uint16_t acquisition_time
Acquisition time.
Definition adc.h:107
enum adc_reference reference
Reference selection.
Definition adc.h:96
uint8_t channel_id
Channel identifier.
Definition adc.h:131
ADC driver API.
Definition adc.h:703
adc_api_read read
Definition adc.h:705
uint16_t ref_internal
Definition adc.h:709
adc_api_channel_setup channel_setup
Definition adc.h:704
Container for ADC channel information specified in devicetree.
Definition adc.h:265
struct adc_channel_cfg channel_cfg
Configuration of the associated ADC channel specified in devicetree.
Definition adc.h:287
uint8_t channel_id
ADC channel identifier used by this io-channel.
Definition adc.h:273
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:295
const struct device * dev
Pointer to the device structure for the ADC driver instance used by this io-channel.
Definition adc.h:270
uint8_t oversampling
Oversampling setting to be used for that channel.
Definition adc.h:309
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:280
uint8_t resolution
ADC resolution to be used for that channel.
Definition adc.h:302
Structure defining additional options for an ADC sampling sequence.
Definition adc.h:575
void * user_data
Pointer to user data.
Definition adc.h:597
uint16_t extra_samplings
Number of extra samplings to perform (the total number of samplings is 1 + extra_samplings).
Definition adc.h:603
adc_sequence_callback callback
Callback function to be called after each sampling is done.
Definition adc.h:591
uint32_t interval_us
Interval between consecutive samplings (in microseconds), 0 means sample as fast as possible,...
Definition adc.h:585
Structure defining an ADC sampling sequence.
Definition adc.h:609
uint8_t oversampling
Oversampling setting.
Definition adc.h:661
const struct adc_sequence_options * options
Pointer to a structure defining additional options for the sequence.
Definition adc.h:614
uint32_t channels
Bit-mask indicating the channels to be included in each sampling of this sequence.
Definition adc.h:623
void * buffer
Pointer to a buffer where the samples are to be written.
Definition adc.h:636
bool calibrate
Perform calibration before the reading is taken if requested.
Definition adc.h:671
size_t buffer_size
Specifies the actual size of the buffer pointed by the "buffer" field (in bytes).
Definition adc.h:644
uint8_t resolution
ADC resolution.
Definition adc.h:653
Runtime device structure (in ROM) per driver instance.
Definition device.h:453