Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13
14#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_H_
15#define ZEPHYR_INCLUDE_DRIVERS_ADC_H_
16
17#include <zephyr/device.h>
19#include <zephyr/kernel.h>
20#include <zephyr/rtio/rtio.h>
21#include <zephyr/dsp/types.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
40
66
83int adc_gain_invert(enum adc_gain gain, int32_t *value);
84
101int adc_gain_invert_64(enum adc_gain gain, int64_t *value);
102
113
120
123
134
158
161
162#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
168 uint8_t input_positive;
169
175 uint8_t input_negative;
176#endif /* CONFIG_ADC_CONFIGURABLE_INPUTS */
177
178#ifdef CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN
179 uint8_t current_source_pin_set : 1;
186 uint8_t current_source_pin[2];
187#endif /* CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN */
188
189#ifdef CONFIG_ADC_CONFIGURABLE_VBIAS_PIN
198 uint32_t vbias_pins;
199#endif /* CONFIG_ADC_CONFIGURABLE_VBIAS_PIN */
200};
201
266#define ADC_CHANNEL_CFG_DT(node_id) { \
267 .gain = DT_STRING_TOKEN(node_id, zephyr_gain), \
268 .reference = DT_STRING_TOKEN(node_id, zephyr_reference), \
269 .acquisition_time = DT_PROP(node_id, zephyr_acquisition_time), \
270 .channel_id = DT_REG_ADDR(node_id), \
271COND_CODE_1(UTIL_OR(DT_PROP(node_id, zephyr_differential), \
272 UTIL_AND(CONFIG_ADC_CONFIGURABLE_INPUTS, \
273 DT_NODE_HAS_PROP(node_id, zephyr_input_negative))), \
274 (.differential = true,), \
275 (.differential = false,)) \
276IF_ENABLED(CONFIG_ADC_CONFIGURABLE_INPUTS, \
277 (.input_positive = DT_PROP_OR(node_id, zephyr_input_positive, 0), \
278 .input_negative = DT_PROP_OR(node_id, zephyr_input_negative, 0),)) \
279IF_ENABLED(CONFIG_ADC_CONFIGURABLE_EXCITATION_CURRENT_SOURCE_PIN, \
280 (.current_source_pin_set = DT_NODE_HAS_PROP(node_id, zephyr_current_source_pin), \
281 .current_source_pin = DT_PROP_OR(node_id, zephyr_current_source_pin, {0}),)) \
282IF_ENABLED(CONFIG_ADC_CONFIGURABLE_VBIAS_PIN, \
283 (.vbias_pins = DT_PROP_OR(node_id, zephyr_vbias_pins, 0),)) \
284}
285
342
344
345#define ADC_DT_SPEC_STRUCT(ctlr, input) { \
346 .dev = DEVICE_DT_GET(ctlr), \
347 .channel_id = input, \
348 ADC_CHANNEL_CFG_FROM_DT_NODE(\
349 ADC_CHANNEL_DT_NODE(ctlr, input)) \
350 }
351
352#define ADC_CHANNEL_DT_NODE(ctlr, input) \
353 DT_CHILD_BY_UNIT_ADDR_INT(ctlr, input)
354
355#define ADC_CHANNEL_CFG_FROM_DT_NODE(node_id) \
356 IF_ENABLED(DT_NODE_EXISTS(node_id), \
357 (.channel_cfg_dt_node_exists = true, \
358 .channel_cfg = ADC_CHANNEL_CFG_DT(node_id), \
359 .vref_mv = DT_PROP_OR(node_id, zephyr_vref_mv, 0), \
360 .resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
361 .oversampling = DT_PROP_OR(node_id, zephyr_oversampling, 0),))
362
364
433#define ADC_DT_SPEC_GET_BY_NAME(node_id, name) \
434 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
435 DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name))
436
449#define ADC_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
450 COND_CODE_1(DT_PROP_HAS_NAME(node_id, io_channels, name), \
451 (ADC_DT_SPEC_GET_BY_NAME(node_id, name)), (default_value))
452
463#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name) \
464 ADC_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
465
478#define ADC_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
479 ADC_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
480
550#define ADC_DT_SPEC_GET_BY_IDX(node_id, idx) \
551 ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
552 DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx))
553
566#define ADC_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
567 COND_CODE_1(DT_PROP_HAS_IDX(node_id, io_channels, idx), \
568 (ADC_DT_SPEC_GET_BY_IDX(node_id, idx)), (default_value))
569
580#define ADC_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
581 ADC_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
582
595#define ADC_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
596 ADC_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
597
606#define ADC_DT_SPEC_GET(node_id) ADC_DT_SPEC_GET_BY_IDX(node_id, 0)
607
619#define ADC_DT_SPEC_GET_OR(node_id, default_value) \
620 ADC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
621
631#define ADC_DT_SPEC_INST_GET(inst) ADC_DT_SPEC_GET(DT_DRV_INST(inst))
632
644#define ADC_DT_SPEC_INST_GET_OR(inst, default_value) \
645 ADC_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
646
647/* Forward declaration of the adc_sequence structure. */
648struct adc_sequence;
649
666
686typedef enum adc_action (*adc_sequence_callback)(const struct device *dev,
687 const struct adc_sequence *sequence,
688 uint16_t sampling_index);
689
723
733
742
754 void *buffer;
755
763
764#if defined(CONFIG_ADC_SEQUENCE_PRIORITY) || defined(__DOXYGEN__)
776#endif
777
786
794
804};
805
819
833
863
875
880
892
893/*
894 * Internal data structure used to store information about the IODevice for async reading and
895 * streaming adc data.
896 */
908
923 int (*get_frame_count)(const uint8_t *buffer, uint32_t channel,
924 uint16_t *frame_count);
925
939 int (*get_size_info)(struct adc_dt_spec adc_spec, uint32_t channel, size_t *base_size,
940 size_t *frame_size);
941
957 int (*decode)(const uint8_t *buffer, uint32_t channel, uint32_t *fit,
958 uint16_t max_count, void *data_out);
959
967 bool (*has_trigger)(const uint8_t *buffer, enum adc_trigger_type trigger);
968};
969
975
980typedef int (*adc_api_channel_setup)(const struct device *dev,
981 const struct adc_channel_cfg *channel_cfg);
982
987typedef int (*adc_api_read)(const struct device *dev,
988 const struct adc_sequence *sequence);
989
993typedef void (*adc_api_submit)(const struct device *dev,
994 struct rtio_iodev_sqe *sqe);
995
1001typedef int (*adc_api_get_decoder)(const struct device *dev,
1002 const struct adc_decoder_api **api);
1003
1004
1010typedef int (*adc_api_read_async)(const struct device *dev,
1011 const struct adc_sequence *sequence,
1012 struct k_poll_signal *async);
1013
1019typedef int (*adc_api_ref_get)(const struct device *dev, enum adc_reference ref,
1020 uint16_t *vref_mv);
1021
1025__subsystem struct adc_driver_api {
1030#if defined(CONFIG_ADC_ASYNC) || defined(__DOXYGEN__)
1036#endif
1037#if defined(CONFIG_ADC_STREAM) || defined(__DOXYGEN__)
1048#endif
1068};
1069
1071
1084__syscall int adc_channel_setup(const struct device *dev,
1085 const struct adc_channel_cfg *channel_cfg);
1086
1087static inline int z_impl_adc_channel_setup(const struct device *dev,
1088 const struct adc_channel_cfg *channel_cfg)
1089{
1090 return DEVICE_API_GET(adc, dev)->channel_setup(dev, channel_cfg);
1091}
1092
1102static inline int adc_channel_setup_dt(const struct adc_dt_spec *spec)
1103{
1104 if (!spec->channel_cfg_dt_node_exists) {
1105 return -ENOTSUP;
1106 }
1107
1108 return adc_channel_setup(spec->dev, &spec->channel_cfg);
1109}
1110
1132__syscall int adc_read(const struct device *dev,
1133 const struct adc_sequence *sequence);
1134
1135static inline int z_impl_adc_read(const struct device *dev,
1136 const struct adc_sequence *sequence)
1137{
1138 return DEVICE_API_GET(adc, dev)->read(dev, sequence);
1139}
1140
1150static inline int adc_read_dt(const struct adc_dt_spec *spec,
1151 const struct adc_sequence *sequence)
1152{
1153 return adc_read(spec->dev, sequence);
1154}
1155
1176__syscall int adc_read_async(const struct device *dev,
1177 const struct adc_sequence *sequence,
1178 struct k_poll_signal *async);
1179
1180#ifdef CONFIG_ADC_ASYNC
1181static inline int z_impl_adc_read_async(const struct device *dev,
1182 const struct adc_sequence *sequence,
1183 struct k_poll_signal *async)
1184{
1185 return DEVICE_API_GET(adc, dev)->read_async(dev, sequence, async);
1186}
1187#endif /* CONFIG_ADC_ASYNC */
1188
1208static inline int adc_read_async_dt(const struct adc_dt_spec *spec,
1209 const struct adc_sequence *sequence,
1210 struct k_poll_signal *async)
1211{
1212 return adc_read_async(spec->dev, sequence, async);
1213}
1214
1227__syscall int adc_get_decoder(const struct device *dev,
1228 const struct adc_decoder_api **api);
1229
1236struct __attribute__((__packed__)) adc_data_generic_header {
1239
1242
1245
1246 /* This padding is needed to make sure that the 'channels' field is aligned */
1247 int16_t _padding;
1248
1251};
1252
1277static inline int adc_stream(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata,
1278 rtio_sqe_handle_t *handle)
1279{
1280 if (IS_ENABLED(CONFIG_USERSPACE)) {
1281 struct rtio_sqe sqe;
1282
1284 rtio_sqe_copy_in_get_handles(ctx, &sqe, handle, 1);
1285 } else {
1286 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1287
1288 if (sqe == NULL) {
1289 return -ENOMEM;
1290 }
1291 if (handle != NULL) {
1292 *handle = rtio_sqe_handle(ctx, sqe);
1293 }
1295 }
1296 rtio_submit(ctx, 0);
1297 return 0;
1298}
1299
1300#ifdef CONFIG_ADC_STREAM
1301static inline int z_impl_adc_get_decoder(const struct device *dev,
1302 const struct adc_decoder_api **decoder)
1303{
1304 const struct adc_driver_api *api = DEVICE_API_GET(adc, dev);
1305
1306 __ASSERT_NO_MSG(api != NULL);
1307
1308 if (api->get_decoder == NULL) {
1309 *decoder = NULL;
1310 return -1;
1311 }
1312
1313 return api->get_decoder(dev, decoder);
1314}
1315#endif /* CONFIG_ADC_STREAM */
1316
1337static inline int adc_ref_get(const struct device *dev, enum adc_reference ref,
1338 uint16_t *vref_mv)
1339{
1340 const struct adc_driver_api *api;
1341
1342 if (vref_mv == NULL) {
1343 return -EINVAL;
1344 }
1345
1346 api = DEVICE_API_GET(adc, dev);
1347
1348 if (api->ref_get != NULL) {
1349 return api->ref_get(dev, ref, vref_mv);
1350 }
1351
1352 if (ref != ADC_REF_INTERNAL) {
1353 return -ENOTSUP;
1354 }
1355
1356 *vref_mv = api->ref_internal;
1357 return (*vref_mv == 0U) ? -ENODATA : 0;
1358}
1359
1376static inline uint16_t adc_ref_internal(const struct device *dev)
1377{
1378 uint16_t vref_mv;
1379 int ret;
1380
1381 ret = adc_ref_get(dev, ADC_REF_INTERNAL, &vref_mv);
1382
1383 return ret == 0 ? vref_mv : 0U;
1384}
1385
1409typedef int (*adc_raw_to_x_fn)(int32_t ref_mv, enum adc_gain gain, uint8_t resolution,
1410 int32_t *valp);
1411
1417static inline int adc_raw_to_millivolts(int32_t ref_mv, enum adc_gain gain, uint8_t resolution,
1418 int32_t *valp)
1419{
1420 int64_t adc_mv = (int64_t)*valp * (int64_t)ref_mv;
1421 int ret = adc_gain_invert_64(gain, &adc_mv);
1422
1423 if (ret == 0) {
1424 adc_mv = adc_mv >> resolution;
1425 if (adc_mv > INT32_MAX || adc_mv < INT32_MIN) {
1426 __ASSERT(false, "conversion result is out of range");
1427 }
1428
1429 *valp = (int32_t)adc_mv;
1430 }
1431
1432 return ret;
1433}
1434
1440static inline int adc_raw_to_microvolts(int32_t ref_mv, enum adc_gain gain, uint8_t resolution,
1441 int32_t *valp)
1442{
1443 int64_t adc_uv = (int64_t)*valp * ref_mv * 1000;
1444 int ret = adc_gain_invert_64(gain, &adc_uv);
1445
1446 if (ret == 0) {
1447 *valp = (int32_t)(adc_uv >> resolution);
1448 }
1449
1450 return ret;
1451}
1452
1469static inline int adc_raw_to_x_dt_chan(adc_raw_to_x_fn conv_func,
1470 const struct adc_dt_spec *spec,
1471 const struct adc_channel_cfg *channel_cfg,
1472 int32_t *valp)
1473{
1474 int32_t vref_mv;
1475 uint16_t vref_mv_u16;
1476 uint8_t resolution;
1477
1478 if (!spec->channel_cfg_dt_node_exists) {
1479 return -ENOTSUP;
1480 }
1481
1482 if (adc_ref_get(spec->dev, channel_cfg->reference, &vref_mv_u16) == 0) {
1483 vref_mv = (int32_t)vref_mv_u16;
1484 } else {
1485 vref_mv = spec->vref_mv;
1486 }
1487
1488 resolution = spec->resolution;
1489
1490 /*
1491 * For differential channels, one bit less needs to be specified
1492 * for resolution to achieve correct conversion.
1493 */
1494 if (channel_cfg->differential) {
1495 resolution -= 1U;
1496 }
1497
1498 return conv_func(vref_mv, channel_cfg->gain, resolution, valp);
1499}
1500
1501
1515static inline int adc_raw_to_millivolts_dt(const struct adc_dt_spec *spec, int32_t *valp)
1516{
1517 return adc_raw_to_x_dt_chan(adc_raw_to_millivolts, spec, &spec->channel_cfg, valp);
1518}
1519
1533static inline int adc_raw_to_microvolts_dt(const struct adc_dt_spec *spec, int32_t *valp)
1534{
1535 return adc_raw_to_x_dt_chan(adc_raw_to_microvolts, spec, &spec->channel_cfg, valp);
1536}
1537
1556static inline int adc_sequence_init_dt(const struct adc_dt_spec *spec,
1557 struct adc_sequence *seq)
1558{
1559 if (!spec->channel_cfg_dt_node_exists) {
1560 return -ENOTSUP;
1561 }
1562
1563 seq->channels = BIT(spec->channel_id);
1564 seq->resolution = spec->resolution;
1565 seq->oversampling = spec->oversampling;
1566
1567 return 0;
1568}
1569
1577static inline bool adc_is_ready_dt(const struct adc_dt_spec *spec)
1578{
1579 return device_is_ready(spec->dev);
1580}
1581
1587#define ADC_DECODER_NAME() UTIL_CAT(DT_DRV_COMPAT, __adc_decoder_api)
1588
1596#define ADC_DECODER_DT_GET(node_id) \
1597 &UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __adc_decoder_api)
1598
1614#define ADC_DECODER_API_DT_DEFINE() \
1615 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
1616 const STRUCT_SECTION_ITERABLE(adc_decoder_api, ADC_DECODER_NAME())
1617
1618#define Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL_IDX(node_id, prop, idx) \
1619 extern const struct adc_decoder_api UTIL_CAT( \
1620 DT_STRING_TOKEN_BY_IDX(node_id, prop, idx), __adc_decoder_api);
1621
1622#define Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL(node_id) \
1623 COND_CODE_1(DT_NODE_HAS_PROP(node_id, compatible), \
1624 (DT_FOREACH_PROP_ELEM(node_id, compatible, \
1625 Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL_IDX)), \
1626 ())
1627
1628DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_ADC_DECODER_DECLARE_INTERNAL)
1629
1630/* The default adc iodev API */
1631extern const struct rtio_iodev_api __adc_iodev_api;
1632
1652#define ADC_DT_STREAM_IODEV(name, dt_node, adc_dt_spec, ...) \
1653 static struct adc_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
1654 static struct adc_read_config _CONCAT(__adc_read_config_, name) = { \
1655 .adc = DEVICE_DT_GET(dt_node), \
1656 .is_streaming = true, \
1657 .adc_spec = adc_dt_spec, \
1658 .triggers = _CONCAT(__trigger_array_, name), \
1659 .adc_spec_cnt = ARRAY_SIZE(adc_dt_spec), \
1660 .trigger_cnt = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
1661 }; \
1662 RTIO_IODEV_DEFINE(name, &__adc_iodev_api, &_CONCAT(__adc_read_config_, name))
1663
1667
1668#ifdef __cplusplus
1669}
1670#endif
1671
1672#include <zephyr/syscalls/adc.h>
1673
1674#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_H_ */
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1501
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:1001
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:987
void(* adc_api_submit)(const struct device *dev, struct rtio_iodev_sqe *sqe)
Type definition of ADC API function for submitting a stream request.
Definition adc.h:993
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:980
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:1010
int(* adc_api_ref_get)(const struct device *dev, enum adc_reference ref, uint16_t *vref_mv)
Type definition of ADC API function for getting a reference voltage in millivolts.
Definition adc.h:1019
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:1515
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:1469
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:1150
adc_gain
ADC channel gain factors.
Definition adc.h:42
static bool adc_is_ready_dt(const struct adc_dt_spec *spec)
Validate that the ADC device is ready.
Definition adc.h:1577
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:1409
adc_stream_data_opt
Options for what to do with the associated data when a trigger is consumed.
Definition adc.h:867
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:1440
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:1533
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:1556
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.
adc_trigger_type
ADC trigger types.
Definition adc.h:837
static int adc_ref_get(const struct device *dev, enum adc_reference ref, uint16_t *vref_mv)
Get the voltage for a selected ADC reference.
Definition adc.h:1337
int adc_channel_setup(const struct device *dev, const struct adc_channel_cfg *channel_cfg)
Configure an ADC channel.
adc_action
Action to be performed after a sampling is done.
Definition adc.h:653
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:686
adc_reference
ADC references.
Definition adc.h:104
static int adc_stream(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata, rtio_sqe_handle_t *handle)
Start a continuous ADC streaming session.
Definition adc.h:1277
static uint16_t adc_ref_internal(const struct device *dev)
Get the internal reference voltage.
Definition adc.h:1376
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.
Definition adc.h:1208
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:1102
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:1417
int adc_get_decoder(const struct device *dev, const struct adc_decoder_api **api)
Get decoder APIs for that device.
@ ADC_GAIN_64
x 64.
Definition adc.h:63
@ ADC_GAIN_3
x 3.
Definition adc.h:55
@ ADC_GAIN_4
x 4.
Definition adc.h:56
@ ADC_GAIN_1_5
x 1/5.
Definition adc.h:44
@ ADC_GAIN_128
x 128.
Definition adc.h:64
@ ADC_GAIN_1_2
x 1/2.
Definition adc.h:50
@ ADC_GAIN_2_7
x 2/7.
Definition adc.h:46
@ ADC_GAIN_15_32
x 15/32.
Definition adc.h:49
@ ADC_GAIN_12
x 12.
Definition adc.h:59
@ ADC_GAIN_2_5
x 2/5.
Definition adc.h:48
@ ADC_GAIN_16
x 16.
Definition adc.h:60
@ ADC_GAIN_24
x 24.
Definition adc.h:61
@ ADC_GAIN_1
x 1.
Definition adc.h:53
@ ADC_GAIN_6
x 6.
Definition adc.h:57
@ ADC_GAIN_1_6
x 1/6.
Definition adc.h:43
@ ADC_GAIN_32
x 32.
Definition adc.h:62
@ ADC_GAIN_2_3
x 2/3.
Definition adc.h:51
@ ADC_GAIN_4_5
x 4/5.
Definition adc.h:52
@ ADC_GAIN_8
x 8.
Definition adc.h:58
@ ADC_GAIN_1_3
x 1/3.
Definition adc.h:47
@ ADC_GAIN_1_4
x 1/4.
Definition adc.h:45
@ ADC_GAIN_2
x 2.
Definition adc.h:54
@ ADC_STREAM_DATA_NOP
Do nothing with the associated trigger data, it may be consumed later.
Definition adc.h:871
@ ADC_STREAM_DATA_DROP
Flush/clear whatever data is associated with the trigger.
Definition adc.h:873
@ ADC_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition adc.h:869
@ ADC_TRIG_PRIV_START
This and higher values are adc specific.
Definition adc.h:856
@ ADC_TRIG_DATA_READY
Trigger fires whenever new data is ready.
Definition adc.h:839
@ ADC_TRIG_FIFO_WATERMARK
Trigger fires when the FIFO watermark has been reached.
Definition adc.h:842
@ ADC_TRIG_MAX
Maximum value describing a adc trigger type.
Definition adc.h:861
@ ADC_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition adc.h:845
@ ADC_TRIG_COMMON_COUNT
Number of all common adc triggers.
Definition adc.h:850
@ ADC_ACTION_FINISH
The sequence should be finished immediately.
Definition adc.h:664
@ ADC_ACTION_REPEAT
The sampling should be repeated.
Definition adc.h:661
@ ADC_ACTION_CONTINUE
The sequence should be continued normally.
Definition adc.h:655
@ ADC_REF_INTERNAL
Internal.
Definition adc.h:109
@ ADC_REF_EXTERNAL1
External, input 1.
Definition adc.h:111
@ ADC_REF_VDD_1_2
VDD/2.
Definition adc.h:106
@ ADC_REF_VDD_1_3
VDD/3.
Definition adc.h:107
@ ADC_REF_VDD_1_4
VDD/4.
Definition adc.h:108
@ ADC_REF_VDD_1
VDD.
Definition adc.h:105
@ ADC_REF_EXTERNAL0
External, input 0.
Definition adc.h:110
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:3593
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition types.h:35
#define RTIO_PRIO_NORM
Normal priority.
Definition sqe.h:56
static rtio_sqe_handle_t rtio_sqe_handle(const struct rtio *r, const struct rtio_sqe *sqe)
Capture an opaque, validatable handle for a live submission.
Definition rtio.h:948
int rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes, rtio_sqe_handle_t *handles, size_t sqe_count)
Copy an array of SQEs into the queue and get resulting handles back.
uint32_t rtio_sqe_handle_t
Opaque identity for a submission queue entry.
Definition sqe.h:331
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition rtio.h:365
static void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Prepare a multishot read op submission with context's mempool.
Definition sqe.h:592
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:154
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define ENODATA
Missing expected message data.
Definition errno.h:78
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOMEM
Not enough core.
Definition errno.h:51
#define ENOTSUP
Unsupported value.
Definition errno.h:115
Public kernel APIs.
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
#define INT32_MAX
Definition stdint.h:18
__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 INT32_MIN
Definition stdint.h:24
#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:888
uint8_t chan_resolution
A ADC channel resolution.
Definition adc.h:890
uint8_t chan_idx
A ADC channel index.
Definition adc.h:889
Structure for specifying the configuration of an ADC channel.
Definition adc.h:117
enum adc_gain gain
Gain selection.
Definition adc.h:119
uint8_t differential
Channel type: single-ended or differential.
Definition adc.h:160
uint16_t acquisition_time
Acquisition time.
Definition adc.h:133
enum adc_reference reference
Reference selection.
Definition adc.h:122
uint8_t channel_id
Channel identifier.
Definition adc.h:157
Definition adc.h:826
q31_t value
Definition adc.h:829
uint32_t timestamp_delta
Definition adc.h:827
Generic header for encoded ADC streaming frames.
Definition adc.h:1236
uint8_t num_channels
Number of channels present in this frame.
Definition adc.h:1241
int8_t shift
Shift value for Q-format decoding of all samples in the frame.
Definition adc.h:1244
uint64_t timestamp_ns
Timestamp (in nanoseconds) at which the data was collected.
Definition adc.h:1238
struct adc_chan_spec channels[0]
Channel specifications for the channels present in the frame.
Definition adc.h:1250
Definition adc.h:806
uint64_t base_timestamp_ns
The closest timestamp for when the first frame was generated as attained by :c:func:k_uptime_ticks.
Definition adc.h:811
uint16_t reading_count
The number of elements in the 'readings' array.
Definition adc.h:817
Data for the adc channel.
Definition adc.h:823
int8_t shift
Definition adc.h:825
struct adc_data::adc_sample_data readings[1]
struct adc_data_header header
Definition adc.h:824
Decodes a single raw data buffer.
Definition adc.h:913
bool(* has_trigger)(const uint8_t *buffer, enum adc_trigger_type trigger)
Check if the given trigger type is present.
Definition adc.h:967
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:923
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:939
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:957
<span class="mlabel">Driver Operations</span> ADC driver operations
Definition adc.h:1025
adc_api_ref_get ref_get
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition adc.h:1067
adc_api_read read
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition adc.h:1029
adc_api_get_decoder get_decoder
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition adc.h:1047
uint16_t ref_internal
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition adc.h:1055
adc_api_channel_setup channel_setup
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition adc.h:1027
adc_api_submit submit
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition adc.h:1042
adc_api_read_async read_async
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition adc.h:1035
Container for ADC channel information specified in devicetree.
Definition adc.h:296
struct adc_channel_cfg channel_cfg
Configuration of the associated ADC channel specified in devicetree.
Definition adc.h:318
uint8_t channel_id
ADC channel identifier used by this io-channel.
Definition adc.h:304
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:326
const struct device * dev
Pointer to the device structure for the ADC driver instance used by this io-channel.
Definition adc.h:301
uint8_t oversampling
Oversampling setting to be used for that channel.
Definition adc.h:340
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:311
uint8_t resolution
ADC resolution to be used for that channel.
Definition adc.h:333
Definition adc.h:897
const struct adc_dt_spec * adc_spec
Definition adc.h:900
uint16_t fifo_mode
Definition adc.h:904
uint16_t fifo_watermark_lvl
Definition adc.h:903
const struct device * adc
Definition adc.h:898
size_t trigger_cnt
Definition adc.h:906
struct adc_sequence * sequence
Definition adc.h:902
size_t adc_spec_cnt
Definition adc.h:905
const struct adc_stream_trigger * triggers
Definition adc.h:901
const bool is_streaming
Definition adc.h:899
Structure defining additional options for an ADC sampling sequence.
Definition adc.h:693
void * user_data
Pointer to user data.
Definition adc.h:715
uint16_t extra_samplings
Number of extra samplings to perform (the total number of samplings is 1 + extra_samplings).
Definition adc.h:721
adc_sequence_callback callback
Callback function to be called after each sampling is done.
Definition adc.h:709
uint32_t interval_us
Interval between consecutive samplings (in microseconds), 0 means sample as fast as possible,...
Definition adc.h:703
Structure defining an ADC sampling sequence.
Definition adc.h:727
uint8_t oversampling
Oversampling setting.
Definition adc.h:793
const struct adc_sequence_options * options
Pointer to a structure defining additional options for the sequence.
Definition adc.h:732
uint32_t priority
Channel priority for arbitration.
Definition adc.h:775
uint32_t channels
Bit-mask indicating the channels to be included in each sampling of this sequence.
Definition adc.h:741
void * buffer
Pointer to a buffer where the samples are to be written.
Definition adc.h:754
bool calibrate
Perform calibration before the reading is taken if requested.
Definition adc.h:803
size_t buffer_size
Specifies the actual size of the buffer pointed by the "buffer" field (in bytes).
Definition adc.h:762
uint8_t resolution
ADC resolution.
Definition adc.h:785
Definition adc.h:876
enum adc_stream_data_opt opt
Definition adc.h:878
enum adc_trigger_type trigger
Definition adc.h:877
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
Poll signal object.
Definition kernel.h:6814
API that an RTIO IO device should implement.
Definition iodev.h:38
IO device submission queue entry.
Definition sqe.h:468
struct rtio_sqe sqe
The submission queue entry this wraps.
Definition sqe.h:470
An IO device with a function table for submitting requests.
Definition iodev.h:83
A submission queue event.
Definition sqe.h:346
void * userdata
User provided data which is returned upon operation completion.
Definition sqe.h:364
const struct rtio_iodev * iodev
Device to operation on.
Definition sqe.h:355
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:85