Zephyr API Documentation 4.0.0
A Scalable Open Source RTOS
|
ADC driver APIs . More...
Topics | |
Emulated ADC | |
Emulated ADC backend API . | |
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_driver_api |
ADC driver API. 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_INST_GET_BY_NAME(inst, name) |
Get ADC io-channel information from a DT_DRV_COMPAT devicetree instance by name. | |
#define | ADC_DT_SPEC_GET_BY_IDX(node_id, idx) |
Get ADC io-channel information from devicetree. | |
#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_GET(node_id) |
Equivalent to ADC_DT_SPEC_GET_BY_IDX(node_id, 0). | |
#define | ADC_DT_SPEC_INST_GET(inst) |
Equivalent to ADC_DT_SPEC_INST_GET_BY_IDX(inst, 0). | |
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 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. | |
Enumerations | |
enum | adc_gain { ADC_GAIN_1_6 , ADC_GAIN_1_5 , ADC_GAIN_1_4 , 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... | |
Functions | |
int | adc_gain_invert (enum adc_gain gain, int32_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 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_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_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. | |
ADC driver APIs .
#define ADC_CHANNEL_CFG_DT | ( | node_id | ) |
#include <zephyr/drivers/adc.h>
Get ADC channel configuration from a given devicetree node.
This returns a static initializer for a struct adc_channel_cfg
filled with data from a given devicetree node.
Example devicetree fragment:
Example usage:
node_id | Devicetree node identifier. |
#define ADC_DT_SPEC_GET | ( | node_id | ) |
#include <zephyr/drivers/adc.h>
Equivalent to ADC_DT_SPEC_GET_BY_IDX(node_id, 0).
node_id | Devicetree node identifier. |
#define ADC_DT_SPEC_GET_BY_IDX | ( | node_id, | |
idx ) |
#include <zephyr/drivers/adc.h>
Get ADC io-channel information from devicetree.
This returns a static initializer for an adc_dt_spec
structure given a devicetree node and a channel index. The node must have the "io-channels" property defined.
Example devicetree fragment:
Example usage:
node_id | Devicetree node identifier. |
idx | Channel index. |
#define ADC_DT_SPEC_GET_BY_NAME | ( | node_id, | |
name ) |
#include <zephyr/drivers/adc.h>
Get ADC io-channel information from devicetree by name.
This returns a static initializer for an adc_dt_spec
structure given a devicetree node and a channel name. The node must have the "io-channels" property defined.
Example devicetree fragment:
Example usage:
node_id | Devicetree node identifier. |
name | Channel name. |
#define ADC_DT_SPEC_INST_GET | ( | inst | ) |
#include <zephyr/drivers/adc.h>
Equivalent to ADC_DT_SPEC_INST_GET_BY_IDX(inst, 0).
inst | DT_DRV_COMPAT instance number |
#define ADC_DT_SPEC_INST_GET_BY_IDX | ( | inst, | |
idx ) |
#include <zephyr/drivers/adc.h>
Get ADC io-channel information from a DT_DRV_COMPAT devicetree instance.
inst | DT_DRV_COMPAT instance number |
idx | Channel index. |
#define ADC_DT_SPEC_INST_GET_BY_NAME | ( | inst, | |
name ) |
#include <zephyr/drivers/adc.h>
Get ADC io-channel information from a DT_DRV_COMPAT devicetree instance by name.
inst | DT_DRV_COMPAT instance number |
name | Channel name. |
typedef int(* adc_api_channel_setup) (const struct device *dev, const struct adc_channel_cfg *channel_cfg) |
#include <zephyr/drivers/adc.h>
Type definition of ADC API function for configuring a channel.
See adc_channel_setup() for argument descriptions.
typedef int(* adc_api_read) (const struct device *dev, const struct adc_sequence *sequence) |
#include <zephyr/drivers/adc.h>
Type definition of ADC API function for setting a read request.
See adc_read() for argument descriptions.
typedef int(* adc_api_read_async) (const struct device *dev, const struct adc_sequence *sequence, struct k_poll_signal *async) |
#include <zephyr/drivers/adc.h>
Type definition of ADC API function for setting an asynchronous read request.
See adc_read_async() for argument descriptions.
typedef enum adc_action(* adc_sequence_callback) (const struct device *dev, const struct adc_sequence *sequence, uint16_t sampling_index) |
#include <zephyr/drivers/adc.h>
Type definition of the optional callback function to be called after a requested sampling is done.
dev | Pointer to the device structure for the driver instance. |
sequence | Pointer to the sequence structure that triggered the sampling. This parameter points to a copy of the structure that was supplied to the call that started the sampling sequence, thus it cannot be used with the CONTAINER_OF() macro to retrieve some other data associated with the sequence. Instead, the adc_sequence_options::user_data field should be used for such purpose. |
sampling_index | Index (0-65535) of the sampling done. |
enum adc_action |
#include <zephyr/drivers/adc.h>
Action to be performed after a sampling is done.
enum adc_gain |
#include <zephyr/drivers/adc.h>
ADC channel gain factors.
enum adc_reference |
#include <zephyr/drivers/adc.h>
ADC references.
Enumerator | |
---|---|
ADC_REF_VDD_1 | VDD. |
ADC_REF_VDD_1_2 | VDD/2. |
ADC_REF_VDD_1_3 | VDD/3. |
ADC_REF_VDD_1_4 | VDD/4. |
ADC_REF_INTERNAL | Internal. |
ADC_REF_EXTERNAL0 | External, input 0. |
ADC_REF_EXTERNAL1 | External, input 1. |
int adc_channel_setup | ( | const struct device * | dev, |
const struct adc_channel_cfg * | channel_cfg ) |
#include <zephyr/drivers/adc.h>
Configure an ADC channel.
It is required to call this function and configure each channel before it is selected for a read request.
dev | Pointer to the device structure for the driver instance. |
channel_cfg | Channel configuration. |
0 | On success. |
-EINVAL | If a parameter with an invalid value has been provided. |
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Configure an ADC channel from a struct adc_dt_spec.
spec | ADC specification from Devicetree. |
#include <zephyr/drivers/adc.h>
Invert the application of gain to a measurement value.
For example, if the gain passed in is ADC_GAIN_1_6 and the referenced value is 10, the value after the function returns is 60.
gain | the gain used to amplify the input signal. |
value | a pointer to a value that initially has the effect of the applied gain but has that effect removed when this function successfully returns. If the gain cannot be reversed the value remains unchanged. |
0 | if the gain was successfully reversed |
-EINVAL | if the gain could not be interpreted |
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Validate that the ADC device is ready.
spec | ADC specification from devicetree |
true | if the ADC device is ready for use and false otherwise. |
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Convert a raw ADC value to millivolts.
This function performs the necessary conversion to transform a raw ADC measurement to a voltage in millivolts.
ref_mv | the reference voltage used for the measurement, in millivolts. This may be from adc_ref_internal() or a known external reference. |
gain | the ADC gain configuration used to sample the input |
resolution | the number of bits in the absolute value of the sample. For differential sampling this needs to be one less than the resolution in struct adc_sequence. |
valp | pointer to the raw measurement value on input, and the corresponding millivolt value on successful conversion. If conversion fails the stored value is left unchanged. |
0 | on successful conversion |
-EINVAL | if the gain is not reversible |
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Convert a raw ADC value to millivolts using information stored in a struct adc_dt_spec.
[in] | spec | ADC specification from Devicetree. |
[in,out] | valp | Pointer to the raw measurement value on input, and the corresponding millivolt value on successful conversion. If conversion fails the stored value is left unchanged. |
int adc_read | ( | const struct device * | dev, |
const struct adc_sequence * | sequence ) |
#include <zephyr/drivers/adc.h>
Set a read request.
dev | Pointer to the device structure for the driver instance. |
sequence | Structure specifying requested sequence of samplings. |
If invoked from user mode, any sequence struct options for callback must be NULL.
0 | On success. |
-EINVAL | If a parameter with an invalid value has been provided. |
-ENOMEM | If the provided buffer is to small to hold the results of all requested samplings. |
-ENOTSUP | If the requested mode of operation is not supported. |
-EBUSY | If another sampling was triggered while the previous one was still in progress. This may occur only when samplings are done with intervals, and it indicates that the selected interval was too small. All requested samples are written in the buffer, but at least some of them were taken with an extra delay compared to what was scheduled. |
int adc_read_async | ( | const struct device * | dev, |
const struct adc_sequence * | sequence, | ||
struct k_poll_signal * | async ) |
#include <zephyr/drivers/adc.h>
Set an asynchronous read request.
CONFIG_ADC_ASYNCis selected.
If invoked from user mode, any sequence struct options for callback must be NULL.
dev | Pointer to the device structure for the driver instance. |
sequence | Structure specifying requested sequence of samplings. |
async | Pointer to a valid and ready to be signaled struct k_poll_signal. (Note: if NULL this function will not notify the end of the transaction, and whether it went successfully or not). |
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Set a read request from a struct adc_dt_spec.
spec | ADC specification from Devicetree. |
sequence | Structure specifying requested sequence of samplings. |
#include <zephyr/drivers/adc.h>
Get the internal reference voltage.
Returns the voltage corresponding to ADC_REF_INTERNAL, measured in millivolts.
|
inlinestatic |
#include <zephyr/drivers/adc.h>
Initialize a struct adc_sequence from information stored in struct adc_dt_spec.
Note that this function only initializes the following fields:
Other fields should be initialized by the caller.
[in] | spec | ADC specification from Devicetree. |
[out] | seq | Sequence to initialize. |
0 | On success |
-ENOTSUP | If spec does not have valid channel configuration |