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

Interfaces for Digital-to-Analog Converters. More...

Topics

 Device-specific DAC API extensions

Files

file  dac.h
 Main header file for DAC (Digital-to-Analog Converter) driver API.

Data Structures

struct  dac_channel_cfg
 Structure for specifying the configuration of a DAC channel. More...
struct  dac_dt_spec
 Container for DAC channel information specified in devicetree. More...

Macros

#define DAC_CHANNEL_BROADCAST   0xFF
 Broadcast channel identifier for DACs that support it.
#define DAC_CHANNEL_CFG_DT(node_id)
 Get DAC channel configuration from a given devicetree node.
#define DAC_DT_SPEC_GET_BY_NAME(node_id, name)
 Get DAC io-channel information from devicetree by name.
#define DAC_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value)
 Like DAC_DT_SPEC_GET_BY_NAME(), with a fallback to a default value.
#define DAC_DT_SPEC_INST_GET_BY_NAME(inst, name)
 Get DAC io-channel information from a DT_DRV_COMPAT devicetree instance by name.
#define DAC_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value)
 Like DAC_DT_SPEC_INST_GET_BY_NAME(), with a fallback to a default value.
#define DAC_DT_SPEC_GET_BY_IDX(node_id, idx)
 Get DAC io-channel information from devicetree.
#define DAC_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value)
 Like DAC_DT_SPEC_GET_BY_IDX(), with a fallback to a default value.
#define DAC_DT_SPEC_INST_GET_BY_IDX(inst, idx)
 Get DAC io-channel information from a DT_DRV_COMPAT devicetree instance.
#define DAC_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value)
 Like DAC_DT_SPEC_INST_GET_BY_IDX(), with a fallback to a default value.
#define DAC_DT_SPEC_GET(node_id)
 Equivalent to DAC_DT_SPEC_GET_BY_IDX(node_id, 0).
#define DAC_DT_SPEC_GET_OR(node_id, default_value)
 Equivalent to DAC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value).
#define DAC_DT_SPEC_INST_GET(inst)
 Equivalent to DAC_DT_SPEC_INST_GET_BY_IDX(inst, 0).
#define DAC_DT_SPEC_INST_GET_OR(inst, default_value)
 Equivalent to DAC_DT_SPEC_INST_GET_BY_IDX_OR(inst, 0, default).

Typedefs

typedef int(* dac_x_to_raw_fn) (uint32_t ref_mv, uint8_t resolution, uint32_t *valp)
 Conversion from specified input units to raw DAC units.

Functions

int dac_channel_setup (const struct device *dev, const struct dac_channel_cfg *channel_cfg)
 Configure a DAC channel.
static int dac_channel_setup_dt (const struct dac_dt_spec *spec)
 Configure an DAC channel from a struct dac_dt_spec.
int dac_write_value (const struct device *dev, uint8_t channel, uint32_t value)
 Write a single value to a DAC channel.
static int dac_write_value_dt (const struct dac_dt_spec *spec, uint32_t value)
 Write a single value to a DAC channel from a struct dac_dt_spec.
static int dac_millivolts_to_raw (uint32_t ref_mv, uint8_t resolution, uint32_t *valp)
 Convert a millivolts value to a raw DAC value.
static int dac_microvolts_to_raw (uint32_t ref_mv, uint8_t resolution, uint32_t *valp)
 Convert a raw DAC value to microvolts.
static int dac_x_to_raw_dt_chan (dac_x_to_raw_fn conv_func, const struct dac_dt_spec *spec, uint32_t *valp)
 Convert a raw DAC value to an arbitrary output unit.
static int dac_millivolts_to_raw_dt (const struct dac_dt_spec *spec, uint32_t *valp)
 Convert a millivolts value to raw DAC using information stored in a struct dac_dt_spec.
static int dac_microvolts_to_raw_dt (const struct dac_dt_spec *spec, uint32_t *valp)
 Convert a microvolts value to raw DAC value using information stored in a struct dac_dt_spec.
static bool dac_is_ready_dt (const struct dac_dt_spec *spec)
 Validate that the DAC device is ready.

Detailed Description

Interfaces for Digital-to-Analog Converters.

Since
2.3
Version
1.1.0

Macro Definition Documentation

◆ DAC_CHANNEL_BROADCAST

#define DAC_CHANNEL_BROADCAST   0xFF

#include <zephyr/drivers/dac.h>

Broadcast channel identifier for DACs that support it.

Note
Only for use in dac_write_value().

◆ DAC_CHANNEL_CFG_DT

#define DAC_CHANNEL_CFG_DT ( node_id)

#include <zephyr/drivers/dac.h>

Value:
{ \
.resolution = DT_PROP_OR(node_id, zephyr_resolution, 0), \
.buffered = DT_PROP(node_id, zephyr_buffered), \
.internal = DT_PROP(node_id, zephyr_internal), \
.channel_id = DT_REG_ADDR(node_id), \
}
#define DT_PROP_OR(node_id, prop, default_value)
Like DT_PROP(), but with a fallback to default_value.
Definition devicetree.h:971
#define DT_PROP(node_id, prop)
Get a devicetree property value.
Definition devicetree.h:798
#define DT_REG_ADDR(node_id)
Get a node's (only) register block address.
Definition devicetree.h:2509

Get DAC channel configuration from a given devicetree node.

This returns a static initializer for a struct dac_channel_cfg filled with data from a given devicetree node.

Example devicetree fragment:

&dac {
#address-cells = <1>;
#size-cells = <0>;
channel@0 {
reg = <0>;
zephyr,resolution = <12>;
};
channel@1 {
reg = <1>;
zephyr,resolution = <12>;
zephyr,buffered;
};
};

Example usage:

static const struct dac_channel_cfg ch0_cfg_dt =
static const struct dac_channel_cfg ch1_cfg_dt =
// Initializes 'ch0_cfg_dt' to:
// {
// .channel_id = 0,
// .resolution = 12,
// }
// and 'ch1_cfg_dt' to:
// {
// .channel_id = 1,
// .resolution = 12,
// .buffered = true,
// }
#define DAC_CHANNEL_CFG_DT(node_id)
Get DAC channel configuration from a given devicetree node.
Definition dac.h:113
#define DT_CHILD(node_id, child)
Get a node identifier for a child node.
Definition devicetree.h:437
#define DT_NODELABEL(label)
Get a node identifier for a node label.
Definition devicetree.h:197
Structure for specifying the configuration of a DAC channel.
Definition dac.h:45
Parameters
node_idDevicetree node identifier.
Returns
Static initializer for an dac_channel_cfg structure.

◆ DAC_DT_SPEC_GET

#define DAC_DT_SPEC_GET ( node_id)

#include <zephyr/drivers/dac.h>

Value:
#define DAC_DT_SPEC_GET_BY_IDX(node_id, idx)
Get DAC io-channel information from devicetree.
Definition dac.h:346

Equivalent to DAC_DT_SPEC_GET_BY_IDX(node_id, 0).

See also
DAC_DT_SPEC_GET_BY_IDX()
Parameters
node_idDevicetree node identifier.
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_GET_BY_IDX

#define DAC_DT_SPEC_GET_BY_IDX ( node_id,
idx )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx), \
COND_CODE_1(DT_PHA_HAS_CELL_AT_IDX(node_id, io_channels, idx, output), \
(DT_IO_CHANNELS_OUTPUT_BY_IDX(node_id, idx)), \
(0)))
#define DT_PHA_HAS_CELL_AT_IDX(node_id, pha, idx, cell)
Does a phandle array have a named cell specifier at an index?
Definition devicetree.h:3989
#define DT_IO_CHANNELS_OUTPUT_BY_IDX(node_id, idx)
Get an io-channels specifier output cell at an index.
Definition io-channels.h:280
#define DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx)
Get the node identifier for the node referenced by an io-channels property at an index.
Definition io-channels.h:50
#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

Get DAC io-channel information from devicetree.

This returns a static initializer for an dac_dt_spec structure given a devicetree node and a channel index. The node must have the "io-channels" property defined.

Example devicetree fragment:

/ {
zephyr,user {
io-channels = <&dac0 1>, <&dac0 3>;
};
};
&dac0 {
#address-cells = <1>;
#size-cells = <0>;
channel@3 {
reg = <3>;
zephyr,resolution = <12>;
};
};

Example usage:

static const struct dac_dt_spec dac_chan0 =
DAC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);
static const struct dac_dt_spec dac_chan1 =
DAC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 1);
// Initializes 'dac_chan0' to:
// {
// .dev = DEVICE_DT_GET(DT_NODELABEL(dac0)),
// .channel_id = 1,
// }
// and 'dac_chan1' to:
// {
// .dev = DEVICE_DT_GET(DT_NODELABEL(dac0)),
// .channel_id = 3,
// .channel_cfg_dt_node_exists = true,
// .channel_cfg = {
// .channel_id = 3,
// .resolution = 12
// },
// }
#define DT_PATH(...)
Get a node identifier for a devicetree path.
Definition devicetree.h:141
Container for DAC channel information specified in devicetree.
Definition dac.h:126
See also
DAC_DT_SPEC_GET()
Parameters
node_idDevicetree node identifier.
idxChannel index.
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_GET_BY_IDX_OR

#define DAC_DT_SPEC_GET_BY_IDX_OR ( node_id,
idx,
default_value )

#include <zephyr/drivers/dac.h>

Value:
COND_CODE_1(DT_PROP_HAS_IDX(node_id, io_channels, idx), \
(DAC_DT_SPEC_GET_BY_IDX(node_id, idx)), (default_value))
#define DT_PROP_HAS_IDX(node_id, prop, idx)
Is index idx valid for an array type property?
Definition devicetree.h:872

Like DAC_DT_SPEC_GET_BY_IDX(), with a fallback to a default value.

Parameters
node_idDevicetree node identifier.
idxChannel index.
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.
See also
DAC_DT_SPEC_INST_GET_BY_IDX_OR

◆ DAC_DT_SPEC_GET_BY_NAME

#define DAC_DT_SPEC_GET_BY_NAME ( node_id,
name )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
#define DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name)
Get the node identifier for the node referenced by an io-channels property by name.
Definition io-channels.h:79
#define DT_IO_CHANNELS_OUTPUT_BY_NAME(node_id, name)
Get an io-channels specifier output cell by name.
Definition io-channels.h:322

Get DAC io-channel information from devicetree by name.

This returns a static initializer for an dac_dt_spec structure given a devicetree node and a channel name. The node must have the "io-channels" property defined.

Example devicetree fragment:

/ {
zephyr,user {
io-channels = <&dac0 1>, <&dac0 3>;
io-channel-names = "A0", "A1";
};
};
&dac0 {
#address-cells = <1>;
#size-cells = <0>;
channel@3 {
reg = <3>;
zephyr,resolution = <12>;
};
};

Example usage:

static const struct dac_dt_spec dac_chan0 =
DAC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), a0);
static const struct dac_dt_spec dac_chan1 =
DAC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), a1);
// Initializes 'dac_chan0' to:
// {
// .dev = DEVICE_DT_GET(DT_NODELABEL(dac0)),
// .channel_id = 1,
// }
// and 'dac_chan1' to:
// {
// .dev = DEVICE_DT_GET(DT_NODELABEL(dac0)),
// .channel_id = 3,
// .channel_cfg_dt_node_exists = true,
// .channel_cfg = {
// .channel_id = 3,
// .resolution = 12,
// },
// }
#define DAC_DT_SPEC_GET_BY_NAME(node_id, name)
Get DAC io-channel information from devicetree by name.
Definition dac.h:237
Parameters
node_idDevicetree node identifier.
nameChannel name.
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_GET_BY_NAME_OR

#define DAC_DT_SPEC_GET_BY_NAME_OR ( node_id,
name,
default_value )

#include <zephyr/drivers/dac.h>

Value:
COND_CODE_1(DT_PROP_HAS_NAME(node_id, io_channels, name), \
(DAC_DT_SPEC_GET_BY_NAME(node_id, name)), (default_value))
#define DT_PROP_HAS_NAME(node_id, prop, name)
Is name name available in a foo-names property?
Definition devicetree.h:907

Like DAC_DT_SPEC_GET_BY_NAME(), with a fallback to a default value.

Parameters
node_idDevicetree node identifier.
nameChannel name.
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.
See also
DAC_DT_SPEC_INST_GET_BY_NAME_OR

◆ DAC_DT_SPEC_GET_OR

#define DAC_DT_SPEC_GET_OR ( node_id,
default_value )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
#define DAC_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value)
Like DAC_DT_SPEC_GET_BY_IDX(), with a fallback to a default value.
Definition dac.h:364

Equivalent to DAC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value).

See also
DAC_DT_SPEC_GET_BY_IDX_OR()
Parameters
node_idDevicetree node identifier.
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.

◆ DAC_DT_SPEC_INST_GET

#define DAC_DT_SPEC_INST_GET ( inst)

#include <zephyr/drivers/dac.h>

Value:
#define DAC_DT_SPEC_GET(node_id)
Equivalent to DAC_DT_SPEC_GET_BY_IDX(node_id, 0).
Definition dac.h:405
#define DT_DRV_INST(inst)
Node identifier for an instance of a DT_DRV_COMPAT compatible.
Definition devicetree.h:4222

Equivalent to DAC_DT_SPEC_INST_GET_BY_IDX(inst, 0).

See also
DAC_DT_SPEC_GET()
Parameters
instDT_DRV_COMPAT instance number
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_INST_GET_BY_IDX

#define DAC_DT_SPEC_INST_GET_BY_IDX ( inst,
idx )

#include <zephyr/drivers/dac.h>

Value:

Get DAC io-channel information from a DT_DRV_COMPAT devicetree instance.

See also
DAC_DT_SPEC_GET_BY_IDX()
Parameters
instDT_DRV_COMPAT instance number
idxChannel index.
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_INST_GET_BY_IDX_OR

#define DAC_DT_SPEC_INST_GET_BY_IDX_OR ( inst,
idx,
default_value )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)

Like DAC_DT_SPEC_INST_GET_BY_IDX(), with a fallback to a default value.

Parameters
instDT_DRV_COMPAT instance number
idxChannel index.
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.
See also
DAC_DT_SPEC_GET_BY_IDX_OR

◆ DAC_DT_SPEC_INST_GET_BY_NAME

#define DAC_DT_SPEC_INST_GET_BY_NAME ( inst,
name )

#include <zephyr/drivers/dac.h>

Value:

Get DAC io-channel information from a DT_DRV_COMPAT devicetree instance by name.

See also
DAC_DT_SPEC_GET_BY_NAME()
Parameters
instDT_DRV_COMPAT instance number
nameChannel name.
Returns
Static initializer for an dac_dt_spec structure.

◆ DAC_DT_SPEC_INST_GET_BY_NAME_OR

#define DAC_DT_SPEC_INST_GET_BY_NAME_OR ( inst,
name,
default_value )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
#define DAC_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value)
Like DAC_DT_SPEC_GET_BY_NAME(), with a fallback to a default value.
Definition dac.h:253

Like DAC_DT_SPEC_INST_GET_BY_NAME(), with a fallback to a default value.

Parameters
instDT_DRV_COMPAT instance number
nameChannel name.
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.
See also
DAC_DT_SPEC_GET_BY_NAME_OR

◆ DAC_DT_SPEC_INST_GET_OR

#define DAC_DT_SPEC_INST_GET_OR ( inst,
default_value )

#include <zephyr/drivers/dac.h>

Value:
DAC_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
#define DAC_DT_SPEC_GET_OR(node_id, default_value)
Equivalent to DAC_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value).
Definition dac.h:418

Equivalent to DAC_DT_SPEC_INST_GET_BY_IDX_OR(inst, 0, default).

See also
DAC_DT_SPEC_GET_OR()
Parameters
instDT_DRV_COMPAT instance number
default_valueFallback value to expand to.
Returns
Static initializer for a struct dac_dt_spec for the property, or default_value if the node or property do not exist.

Typedef Documentation

◆ dac_x_to_raw_fn

typedef int(* dac_x_to_raw_fn) (uint32_t ref_mv, uint8_t resolution, uint32_t *valp)

#include <zephyr/drivers/dac.h>

Conversion from specified input units to raw DAC units.

This function performs the necessary conversion to transform a physical voltage to a raw DAC value.

Parameters
ref_mvthe reference voltage used for the value, in millivolts.
resolutionthe number of bits in the absolute value of the output.
valppointer to the physical voltage value on input, and the corresponding raw output value on successful conversion. If conversion fails the stored value is left unchanged.
Return values
0on successful conversion

Function Documentation

◆ dac_channel_setup()

int dac_channel_setup ( const struct device * dev,
const struct dac_channel_cfg * channel_cfg )

#include <zephyr/drivers/dac.h>

Configure a DAC channel.

It is required to call this function and configure each channel before it is selected for a write request.

Parameters
devPointer to the device structure for the driver instance.
channel_cfgChannel configuration.
Return values
0On success.
-EINVALIf a parameter with an invalid value has been provided.
-ENOTSUPIf the requested resolution is not supported.

◆ dac_channel_setup_dt()

int dac_channel_setup_dt ( const struct dac_dt_spec * spec)
inlinestatic

#include <zephyr/drivers/dac.h>

Configure an DAC channel from a struct dac_dt_spec.

Parameters
specDAC specification from Devicetree.
Returns
A value from dac_channel_setup() or -ENOTSUP if information from Devicetree is not valid.
See also
dac_channel_setup()

◆ dac_is_ready_dt()

bool dac_is_ready_dt ( const struct dac_dt_spec * spec)
inlinestatic

#include <zephyr/drivers/dac.h>

Validate that the DAC device is ready.

Parameters
specDAC specification from devicetree
Returns
true if the DAC device is ready for use and false otherwise.

◆ dac_microvolts_to_raw()

int dac_microvolts_to_raw ( uint32_t ref_mv,
uint8_t resolution,
uint32_t * valp )
inlinestatic

#include <zephyr/drivers/dac.h>

Convert a raw DAC value to microvolts.

See also
dac_x_to_raw_fn

◆ dac_microvolts_to_raw_dt()

int dac_microvolts_to_raw_dt ( const struct dac_dt_spec * spec,
uint32_t * valp )
inlinestatic

#include <zephyr/drivers/dac.h>

Convert a microvolts value to raw DAC value using information stored in a struct dac_dt_spec.

Parameters
[in]specDAC specification from Devicetree.
[in,out]valpPointer to the raw measurement value on input, and the corresponding microvolt value on successful conversion. If conversion fails the stored value is left unchanged.
Returns
A value from dac_microvolts_to_raw() or -ENOTSUP if information from Devicetree is not valid.
See also
dac_microvolts_to_raw()

◆ dac_millivolts_to_raw()

int dac_millivolts_to_raw ( uint32_t ref_mv,
uint8_t resolution,
uint32_t * valp )
inlinestatic

#include <zephyr/drivers/dac.h>

Convert a millivolts value to a raw DAC value.

See also
dac_x_to_raw_fn

◆ dac_millivolts_to_raw_dt()

int dac_millivolts_to_raw_dt ( const struct dac_dt_spec * spec,
uint32_t * valp )
inlinestatic

#include <zephyr/drivers/dac.h>

Convert a millivolts value to raw DAC using information stored in a struct dac_dt_spec.

Parameters
[in]specDAC specification from Devicetree.
[in,out]valpPointer to the raw measurement value on input, and the corresponding millivolt value on successful conversion. If conversion fails the stored value is left unchanged.
Returns
A value from dac_millivolts_to_raw() or -ENOTSUP if information from Devicetree is not valid.
See also
dac_millivolts_to_raw()

◆ dac_write_value()

int dac_write_value ( const struct device * dev,
uint8_t channel,
uint32_t value )

#include <zephyr/drivers/dac.h>

Write a single value to a DAC channel.

Parameters
devPointer to the device structure for the driver instance.
channelNumber of the channel to be used.
valueData to be written to DAC output registers.
Return values
0On success.
-EINVALIf a parameter with an invalid value has been provided.

◆ dac_write_value_dt()

int dac_write_value_dt ( const struct dac_dt_spec * spec,
uint32_t value )
inlinestatic

#include <zephyr/drivers/dac.h>

Write a single value to a DAC channel from a struct dac_dt_spec.

Parameters
specDAC specification from Devicetree.
valueData to be written to DAC output registers.
Returns
A value from dac_write_value() or -ENOTSUP if information from Devicetree is not valid.
See also
dac_write_value()

◆ dac_x_to_raw_dt_chan()

int dac_x_to_raw_dt_chan ( dac_x_to_raw_fn conv_func,
const struct dac_dt_spec * spec,
uint32_t * valp )
inlinestatic

#include <zephyr/drivers/dac.h>

Convert a raw DAC value to an arbitrary output unit.

Parameters
[in]conv_funcFunction that converts to the final output unit.
[in]specDAC specification from Devicetree.
[in,out]valppointer to the physical voltage value on input, and the corresponding raw output value on successful conversion. If conversion fails the stored value is left unchanged.
Returns
A value from dac_x_to_raw_dt_chan or -ENOTSUP if information from Devicetree is not valid.
See also
dac_x_to_raw_fn