Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: adc: add ADC_DT_SPEC_*_BY_NAME() macros #68247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions include/zephyr/drivers/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,91 @@ struct adc_dt_spec {

/** @endcond */

/**
* @brief Get ADC io-channel information from devicetree by name.
*
* This returns a static initializer for an @p adc_dt_spec structure
* given a devicetree node and a channel name. The node must have
* the "io-channels" property defined.
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* zephyr,user {
* io-channels = <&adc0 1>, <&adc0 3>;
* io-channel-names = "A0", "A1";
* };
* };
*
* &adc0 {
* #address-cells = <1>;
* #size-cells = <0>;
*
* channel@3 {
* reg = <3>;
* zephyr,gain = "ADC_GAIN_1_5";
* zephyr,reference = "ADC_REF_VDD_1_4";
* zephyr,vref-mv = <750>;
* zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
* zephyr,resolution = <12>;
* zephyr,oversampling = <4>;
* };
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* static const struct adc_dt_spec adc_chan0 =
* ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), a0);
* static const struct adc_dt_spec adc_chan1 =
* ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), a1);
*
* // Initializes 'adc_chan0' to:
* // {
* // .dev = DEVICE_DT_GET(DT_NODELABEL(adc0)),
* // .channel_id = 1,
* // }
* // and 'adc_chan1' to:
* // {
* // .dev = DEVICE_DT_GET(DT_NODELABEL(adc0)),
* // .channel_id = 3,
* // .channel_cfg_dt_node_exists = true,
* // .channel_cfg = {
* // .channel_id = 3,
* // .gain = ADC_GAIN_1_5,
* // .reference = ADC_REF_VDD_1_4,
* // .acquisition_time = ADC_ACQ_TIME_DEFAULT,
* // },
* // .vref_mv = 750,
* // .resolution = 12,
* // .oversampling = 4,
* // }
* @endcode
*
* @param node_id Devicetree node identifier.
* @param name Channel name.
*
* @return Static initializer for an adc_dt_spec structure.
*/
#define ADC_DT_SPEC_GET_BY_NAME(node_id, name) \
ADC_DT_SPEC_STRUCT(DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name), \
DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name))

/** @brief Get ADC io-channel information from a DT_DRV_COMPAT devicetree
* instance by name.
*
* @see ADC_DT_SPEC_GET_BY_NAME()
*
* @param inst DT_DRV_COMPAT instance number
* @param name Channel name.
*
* @return Static initializer for an adc_dt_spec structure.
*/
#define ADC_DT_SPEC_INST_GET_BY_NAME(inst, name) \
ADC_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)

/**
* @brief Get ADC io-channel information from devicetree.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/devicetree/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/ztest.h>
#include <zephyr/devicetree.h>
#include <zephyr/device.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/mbox.h>

Expand Down Expand Up @@ -1107,6 +1108,27 @@ ZTEST(devicetree_api, test_io_channels)
zassert_equal(DT_INST_IO_CHANNELS_INPUT(0), 10, "");
}

#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api, test_io_channel_names)
{
struct adc_dt_spec adc_spec;

/* ADC_DT_SPEC_GET_BY_NAME */
adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_GET_BY_NAME(TEST_TEMP, ch1);
zassert_equal(adc_spec.channel_id, 10, "");

adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_GET_BY_NAME(TEST_TEMP, ch2);
zassert_equal(adc_spec.channel_id, 20, "");

/* ADC_DT_SPEC_INST_GET_BY_NAME */
adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_INST_GET_BY_NAME(0, ch1);
zassert_equal(adc_spec.channel_id, 10, "");

adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_INST_GET_BY_NAME(0, ch2);
zassert_equal(adc_spec.channel_id, 20, "");
}

#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api, test_dma)
Expand Down