ADC
AM335x ADC
Features Supported
ADC API (
<zephyr/drivers/adc.h>):adc_channel_setup(),adc_read()and, withCONFIG_ADC_ASYNC,adc_read_async()Several channels per
adc_read()call, converted back to back. Conversions can repeat withextra_samplingsand waitinterval_usbetween rounds.Software-started conversions
Single-ended and differential inputs (
differentialinadc_channel_cfg)Analog input selection with
input_positiveandinput_negativewhenCONFIG_ADC_CONFIGURABLE_INPUTSis enabled. Without it, channel n reads analog input n.Hardware averaging of 1, 2, 4, 8 or 16 conversions per sample, set per channel with
zephyr,oversampling(0 to 4)Per-channel timing:
ti,open-delay: wait, in ADC sample-clock periods, before the input is sampled (up to 262143)acquisition_time(zephyr,acquisition-time): settling time, 0 to 255, plus two sample-clock periods
Internal result buffer selection (
ti,fifo, 0 or 1); both give the same resultsRecovery from result buffer overflow or underflow: the driver stops the ADC, clears the error and restarts it
Internal reference voltage from
ti,vrefp(in mV) forADC_REF_INTERNALDefault pinctrl state applied at initialization
Features NOT Supported
Hardware features the driver does not expose:
external hardware triggers (
ADC_EXT_TRIGGER0andADC_EXT_TRIGGER1); software starts every conversioncontinuous conversion; use the Zephyr sequence options for repeated sampling
DMA; the processor copies results in the interrupt handler
Device power management.
Usage
Enabling the Driver
The application must set CONFIG_ADC:
CONFIG_ADC=y
CONFIG_ADC_TI_AM335X is enabled by default when an enabled ti,am335x-adc node exists. These
options are optional:
CONFIG_ADC_ASYNC=y
CONFIG_ADC_CONFIGURABLE_INPUTS=y
Devicetree Configuration
The controller node, as the SoC devicetree defines it:
main_adc0: adc@28001000 {
compatible = "ti,am335x-adc";
reg = <0x28001000 DT_SIZE_K(1)>;
interrupts = <GIC_SPI 253 IRQ_TYPE_LEVEL IRQ_DEFAULT_PRIORITY>;
interrupt-parent = <&gic>;
power-domains = <&main_adc0_pd>;
clocks = <&scmi_clk 4>;
#io-channel-cells = <1>;
#address-cells = <1>;
#size-cells = <0>;
};
To use the ADC, set status = "okay", apply the pin configuration, and add one child node per
channel (reg is the channel number):
&main_adc0 {
ti,fifo = <0>;
pinctrl-0 = <&adc0_ext_trigger0>, <&adc0_ext_trigger1>;
pinctrl-names = "default";
status = "okay";
channel@0 {
reg = <0>;
ti,open-delay = <0>;
zephyr,gain = "ADC_GAIN_1";
zephyr,reference = "ADC_REF_VDD_1";
zephyr,acquisition-time = <0>;
zephyr,resolution = <12>;
zephyr,oversampling = <4>;
zephyr,vref-mv = <1800>;
};
};
Reading the Converter
#include <zephyr/device.h>
#include <zephyr/drivers/adc.h>
static const struct adc_dt_spec adc_chan =
ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);
int main(void)
{
uint16_t sample;
struct adc_sequence sequence = {
.buffer = &sample,
.buffer_size = sizeof(sample),
};
int32_t mv;
int ret;
if (!adc_is_ready_dt(&adc_chan)) {
return -ENODEV;
}
ret = adc_channel_setup_dt(&adc_chan);
if (ret < 0) {
return ret;
}
(void)adc_sequence_init_dt(&adc_chan, &sequence);
ret = adc_read_dt(&adc_chan, &sequence);
if (ret < 0) {
return ret;
}
mv = sample;
(void)adc_raw_to_millivolts_dt(&adc_chan, &mv);
printk("raw %u, %d mV\n", sample, mv);
return 0;
}
Raw values go from 0 to 1023.
Channels in a sequence are converted, and their samples stored, in the order of first
adc_channel_setup(), not channel number. Set channels up in ascending order for the usual layout.Each channel needs 2 bytes in the buffer per sampling.
Calling
adc_channel_setup()again for a channel replaces its settings and keeps its position in the order.
Samples
ADC devicetree driver sample (
samples/drivers/adc/adc_dt)