14#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_H_
15#define ZEPHYR_INCLUDE_DRIVERS_ADC_H_
156#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
172#ifdef CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN
173 uint8_t current_source_pin_set : 1;
183#ifdef CONFIG_ADC_CONFIGURABLE_VBIAS_PIN
260#define ADC_CHANNEL_CFG_DT(node_id) { \
261 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
262 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
263 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
264 .channel_id = DT_REG_ADDR(node_id), \
265COND_CODE_1(UTIL_OR(DT_PROP(node_id, zephyr_differential), \
266 UTIL_AND(CONFIG_ADC_CONFIGURABLE_INPUTS, \
267 DT_NODE_HAS_PROP(node_id, zephyr_input_negative))), \
268 (.differential = true,), \
269 (.differential = false,)) \
270IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
271 (.input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
272 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
273IF_ENABLED(CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN, \
274 (.current_source_pin_set = DT_NODE_HAS_PROP(node_id, zephyr_current_source_pin), \
275 .current_source_pin = DT_PROP_OR(node_id, zephyr_current_source_pin, {0}),)) \
276IF_ENABLED(CONFIG_ADC_CONFIGURABLE_VBIAS_PIN, \
277 (.vbias_pins = DT_PROP_OR(node_id, zephyr_vbias_pins, 0),)) \
335#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
336 .dev = DEVICE_DT_GET(ctlr), \
337 .channel_id = input, \
338 ADC_CHANNEL_CFG_FROM_DT_NODE(\
339 ADC_CHANNEL_DT_NODE(ctlr, input)) \
342#define ADC_CHANNEL_DT_NODE(ctlr, input) \
343 DT_FOREACH_CHILD_VARGS(ctlr, ADC_FOREACH_INPUT, input)
345#define ADC_FOREACH_INPUT(node, input) \
346 IF_ENABLED(IS_EQ(DT_REG_ADDR_RAW(node), input), (node))
348#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
349 IF_ENABLED(DT_NODE_EXISTS(node_id), \
350 (.channel_cfg_dt_node_exists = true, \
351 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
352 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
353 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
354 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
426#define ADC_DT_SPEC_GET_BY_NAME(node_id, name) \
427 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
428 DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name))
440#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name) \
441 ADC_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
512#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
513 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
514 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
526#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
527 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
538#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
548#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
847 uint16_t max_count,
void *data_out);
906#ifdef CONFIG_ADC_ASYNC
909#ifdef CONFIG_ADC_STREAM
931static inline int z_impl_adc_channel_setup(
const struct device *dev,
979static inline int z_impl_adc_read(
const struct device *dev,
1024#ifdef CONFIG_ADC_ASYNC
1025static inline int z_impl_adc_read_async(
const struct device *dev,
1029 return DEVICE_API_GET(adc, dev)->read_async(dev, sequence, async);
1033#ifdef CONFIG_ADC_STREAM
1047__syscall
int adc_get_decoder(
const struct device *dev,
1052struct __attribute__((__packed__)) adc_data_generic_header {
1068 struct adc_chan_spec channels[0];
1071static inline int adc_stream(
struct rtio_iodev *iodev,
struct rtio *ctx,
void *userdata,
1085 if (handle !=
NULL) {
1094static inline int z_impl_adc_get_decoder(
const struct device *dev,
1099 __ASSERT_NO_MSG(api !=
NULL);
1101 if (api->get_decoder ==
NULL) {
1106 return api->get_decoder(dev, decoder);
1158 int32_t adc_mv = *valp * ref_mv;
1162 *valp = (adc_mv >> resolution);
1180 *valp = (
int32_t)(adc_uv >> resolution);
1230 return conv_func(vref_mv, channel_cfg->
gain, resolution, valp);
1323#define ADC_DECODER_NAME() UTIL_CAT(DT_DRV_COMPAT, __adc_decoder_api)
1332#define ADC_DECODER_DT_GET(node_id) \
1333 &UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __adc_decoder_api)
1350#define ADC_DECODER_API_DT_DEFINE() \
1351 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
1352 const STRUCT_SECTION_ITERABLE(adc_decoder_api, ADC_DECODER_NAME())
1354#define Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL_IDX(node_id, prop, idx) \
1355 extern const struct adc_decoder_api UTIL_CAT( \
1356 DT_STRING_TOKEN_BY_IDX(node_id, prop, idx), __adc_decoder_api);
1358#define Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL(node_id) \
1359 COND_CODE_1(DT_NODE_HAS_PROP(node_id, compatible), \
1360 (DT_FOREACH_PROP_ELEM(node_id, compatible, \
1361 Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL_IDX)), \
1369#define ADC_DT_STREAM_IODEV(name, dt_node, adc_dt_spec, ...) \
1370 static struct adc_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
1371 static struct adc_read_config _CONCAT(__adc_read_config_, name) = { \
1372 .adc = DEVICE_DT_GET(dt_node), \
1373 .is_streaming = true, \
1374 .adc_spec = adc_dt_spec, \
1375 .triggers = _CONCAT(__trigger_array_, name), \
1376 .adc_spec_cnt = ARRAY_SIZE(adc_dt_spec), \
1377 .trigger_cnt = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
1379 RTIO_IODEV_DEFINE(name, &__adc_iodev_api, &_CONCAT(__adc_read_config_, name))
1385#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:1350
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:1247
int(* adc_api_get_decoder)(const struct device *dev, const struct adc_decoder_api **api)
Get the decoder associate with the given device.
Definition adc.h:885
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.
Definition adc.h:1202
int adc_gain_invert_64(enum adc_gain gain, int64_t *value)
Invert the application of gain to a measurement value.
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:994
adc_gain
ADC channel gain factors.
Definition adc.h:37
static bool adc_is_ready_dt(const struct adc_dt_spec *spec)
Validate that the ADC device is ready.
Definition adc.h:1309
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.
Definition adc.h:1147
adc_stream_data_opt
Options for what to do with the associated data when a trigger is consumed.
Definition adc.h:756
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.
Definition adc.h:1173
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.
Definition adc.h:1265
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:870
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:1288
int adc_gain_invert(enum adc_gain gain, int32_t *value)
Invert the application of gain to a measurement value.
void(* adc_api_submit)(const struct device *dev, struct rtio_iodev_sqe *sqe)
Type definition of ADC API function for setting an submit stream request.
Definition adc.h:877
int adc_read(const struct device *dev, const struct adc_sequence *sequence)
Set a read request.
adc_trigger_type
ADC trigger types.
Definition adc.h:726
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:863
adc_action
Action to be performed after a sampling is done.
Definition adc.h:556
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:589
adc_reference
ADC references.
Definition adc.h:98
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:894
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition adc.h:1119
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:946
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:1155
@ ADC_GAIN_64
x 64.
Definition adc.h:57
@ ADC_GAIN_3
x 3.
Definition adc.h:49
@ ADC_GAIN_4
x 4.
Definition adc.h:50
@ ADC_GAIN_1_5
x 1/5.
Definition adc.h:39
@ ADC_GAIN_128
x 128.
Definition adc.h:58
@ ADC_GAIN_1_2
x 1/2.
Definition adc.h:44
@ ADC_GAIN_2_7
x 2/7.
Definition adc.h:41
@ ADC_GAIN_12
x 12.
Definition adc.h:53
@ ADC_GAIN_2_5
x 2/5.
Definition adc.h:43
@ ADC_GAIN_16
x 16.
Definition adc.h:54
@ ADC_GAIN_24
x 24.
Definition adc.h:55
@ ADC_GAIN_1
x 1.
Definition adc.h:47
@ ADC_GAIN_6
x 6.
Definition adc.h:51
@ ADC_GAIN_1_6
x 1/6.
Definition adc.h:38
@ ADC_GAIN_32
x 32.
Definition adc.h:56
@ ADC_GAIN_2_3
x 2/3.
Definition adc.h:45
@ ADC_GAIN_4_5
x 4/5.
Definition adc.h:46
@ ADC_GAIN_8
x 8.
Definition adc.h:52
@ ADC_GAIN_1_3
x 1/3.
Definition adc.h:42
@ ADC_GAIN_1_4
x 1/4.
Definition adc.h:40
@ ADC_GAIN_2
x 2.
Definition adc.h:48
@ ADC_STREAM_DATA_NOP
Do nothing with the associated trigger data, it may be consumed later.
Definition adc.h:760
@ ADC_STREAM_DATA_DROP
Flush/clear whatever data is associated with the trigger.
Definition adc.h:762
@ ADC_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition adc.h:758
@ ADC_TRIG_PRIV_START
This and higher values are adc specific.
Definition adc.h:745
@ ADC_TRIG_DATA_READY
Trigger fires whenever new data is ready.
Definition adc.h:728
@ ADC_TRIG_FIFO_WATERMARK
Trigger fires when the FIFO watermark has been reached.
Definition adc.h:731
@ ADC_TRIG_MAX
Maximum value describing a adc trigger type.
Definition adc.h:750
@ ADC_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition adc.h:734
@ ADC_TRIG_COMMON_COUNT
Number of all common adc triggers.
Definition adc.h:739
@ ADC_ACTION_FINISH
The sequence should be finished immediately.
Definition adc.h:567
@ ADC_ACTION_REPEAT
The sampling should be repeated.
Definition adc.h:564
@ ADC_ACTION_CONTINUE
The sequence should be continued normally.
Definition adc.h:558
@ ADC_REF_INTERNAL
Internal.
Definition adc.h:103
@ ADC_REF_EXTERNAL1
External, input 1.
Definition adc.h:105
@ ADC_REF_VDD_1_2
VDD/2.
Definition adc.h:100
@ ADC_REF_VDD_1_3
VDD/3.
Definition adc.h:101
@ ADC_REF_VDD_1_4
VDD/4.
Definition adc.h:102
@ ADC_REF_VDD_1
VDD.
Definition adc.h:99
@ ADC_REF_EXTERNAL0
External, input 0.
Definition adc.h:104
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition devicetree.h:3000
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition types.h:35
#define RTIO_PRIO_NORM
Normal priority.
Definition rtio.h:71
int rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes, struct rtio_sqe **handle, size_t sqe_count)
Copy an array of SQEs into the queue and get resulting handles back.
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition rtio.h:1102
static void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Definition rtio.h:632
int rtio_submit(struct rtio *r, uint32_t wait_count)
Submit I/O requests to the underlying executor.
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:148
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define ENOMEM
Not enough core.
Definition errno.h:50
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define NULL
Definition iar_missing_defs.h:20
Size of off_t must be equal or less than size of size_t
Definition retained_mem.h:29
Real-Time IO device API for moving bytes with low effort.
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
#define INT16_MAX
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:75
__INT8_TYPE__ int8_t
Definition stdint.h:72
__INT16_TYPE__ int16_t
Definition stdint.h:73
ADC Channel Specification.
Definition adc.h:777
uint8_t chan_resolution
A ADC channel resolution.
Definition adc.h:779
uint8_t chan_idx
A ADC channel index.
Definition adc.h:778
Structure for specifying the configuration of an ADC channel.
Definition adc.h:111
enum adc_gain gain
Gain selection.
Definition adc.h:113
uint8_t differential
Channel type: single-ended or differential.
Definition adc.h:154
uint16_t acquisition_time
Acquisition time.
Definition adc.h:127
enum adc_reference reference
Reference selection.
Definition adc.h:116
uint8_t channel_id
Channel identifier.
Definition adc.h:151
q31_t value
Definition adc.h:718
uint32_t timestamp_delta
Definition adc.h:716
Data for the adc channel.
Definition adc.h:712
int8_t shift
Definition adc.h:714
struct adc_data::adc_sample_data readings[1]
struct adc_data_header header
Definition adc.h:713
Decodes a single raw data buffer.
Definition adc.h:802
bool(* has_trigger)(const uint8_t *buffer, enum adc_trigger_type trigger)
Check if the given trigger type is present.
Definition adc.h:856
int(* get_frame_count)(const uint8_t *buffer, uint32_t channel, uint16_t *frame_count)
Get the number of frames in the current buffer.
Definition adc.h:812
int(* get_size_info)(struct adc_dt_spec adc_spec, uint32_t channel, size_t *base_size, size_t *frame_size)
Get the size required to decode a given channel.
Definition adc.h:828
int(* decode)(const uint8_t *buffer, uint32_t channel, uint32_t *fit, uint16_t max_count, void *data_out)
Decode up to max_count samples from the buffer.
Definition adc.h:846
ADC driver API.
Definition adc.h:903
adc_api_read read
Definition adc.h:905
uint16_t ref_internal
Definition adc.h:913
adc_api_channel_setup channel_setup
Definition adc.h:904
Container for ADC channel information specified in devicetree.
Definition adc.h:286
struct adc_channel_cfg channel_cfg
Configuration of the associated ADC channel specified in devicetree.
Definition adc.h:308
uint8_t channel_id
ADC channel identifier used by this io-channel.
Definition adc.h:294
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:316
const struct device * dev
Pointer to the device structure for the ADC driver instance used by this io-channel.
Definition adc.h:291
uint8_t oversampling
Oversampling setting to be used for that channel.
Definition adc.h:330
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:301
uint8_t resolution
ADC resolution to be used for that channel.
Definition adc.h:323
const struct adc_dt_spec * adc_spec
Definition adc.h:789
uint16_t fifo_mode
Definition adc.h:793
uint16_t fifo_watermark_lvl
Definition adc.h:792
const struct device * adc
Definition adc.h:787
size_t trigger_cnt
Definition adc.h:795
struct adc_sequence * sequence
Definition adc.h:791
size_t adc_spec_cnt
Definition adc.h:794
const struct adc_stream_trigger * triggers
Definition adc.h:790
const bool is_streaming
Definition adc.h:788
Structure defining additional options for an ADC sampling sequence.
Definition adc.h:596
void * user_data
Pointer to user data.
Definition adc.h:618
uint16_t extra_samplings
Number of extra samplings to perform (the total number of samplings is 1 + extra_samplings).
Definition adc.h:624
adc_sequence_callback callback
Callback function to be called after each sampling is done.
Definition adc.h:612
uint32_t interval_us
Interval between consecutive samplings (in microseconds), 0 means sample as fast as possible,...
Definition adc.h:606
Structure defining an ADC sampling sequence.
Definition adc.h:630
uint8_t oversampling
Oversampling setting.
Definition adc.h:682
const struct adc_sequence_options * options
Pointer to a structure defining additional options for the sequence.
Definition adc.h:635
uint32_t channels
Bit-mask indicating the channels to be included in each sampling of this sequence.
Definition adc.h:644
void * buffer
Pointer to a buffer where the samples are to be written.
Definition adc.h:657
bool calibrate
Perform calibration before the reading is taken if requested.
Definition adc.h:692
size_t buffer_size
Specifies the actual size of the buffer pointed by the "buffer" field (in bytes).
Definition adc.h:665
uint8_t resolution
ADC resolution.
Definition adc.h:674
enum adc_stream_data_opt opt
Definition adc.h:767
enum adc_trigger_type trigger
Definition adc.h:766
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
API that an RTIO IO device should implement.
Definition rtio.h:525
Compute the mempool block index for a given pointer.
Definition rtio.h:515
struct rtio_sqe sqe
Definition rtio.h:516
An IO device with a function table for submitting requests.
Definition rtio.h:540
A submission queue event.
Definition rtio.h:296
void * userdata
User provided data which is returned upon operation completion.
Definition rtio.h:314
const struct rtio_iodev * iodev
Device to operation on.
Definition rtio.h:305
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:419