main.c
/* Load the ADC library that you ennabled in prj.conf */
#include <zephyr/drivers/adc.h>
/* Define some macros to use some Zephyr macros to help read the DT
configuration based on the ADC channel alias (I have no idea why this
macro is not available in adc.h) */
#define ADC_DT_SPEC_GET_BY_ALIAS(adc_alias) \
{ \
.dev = DEVICE_DT_GET(DT_PARENT(DT_ALIAS(adc_alias))), \
.channel_id = DT_REG_ADDR(DT_ALIAS(adc_alias)), \
ADC_CHANNEL_CFG_FROM_DT_NODE(DT_ALIAS(adc_alias)) \
} \
/* Intialize the ADC struct to store all the DT parameters */
static const struct adc_dt_spec adc_vadc = ADC_DT_SPEC_GET_BY_ALIAS(vadc);
/* Check that the ADC interface is ready */
if (!device_is_ready(adc_vadc.dev)) {
LOG_ERR("ADC controller device(s) not ready");
return -1;
}
/* Configure the ADC channel */
err = adc_channel_setup_dt(&adc_vadc);
if (err < 0) {
LOG_ERR("Could not setup ADC channel (%d)", err);
return err;
}