An Analog-to-Digital Converter (ADC) is a device that translates an analog voltage to a digital value that a microprocessor can understand.

The STMicroelectronics system-on-module has two ADC controllers, each with up to 20 channels. The ADC controllers are 16-bit successive approximation analog-to-digital converters.

Available ADC pins

On the ConnectCore MP15:

  • Dedicated analog pads ANA0 and ANA1 are available at the LGA pads.

    • ANA0 (ADC1/ADC2 channel 0)

    • ANA1 (ADC1/ADC2 channel 1)

  • A number of ADC channels are available at castellated and LGA pads, multiplexed with other functions. Check the Hardware reference manuals for information about pads and ADC channels.

On the ConnectCore MP15 Development Kit:

  • ANA0 (ADC1/ADC2 channel 0) is accessible on the GPIO expansion connector J41

  • ANA1 (ADC1/ADC2 channel 1) is accessible on the GPIO expansion connector J41

  • GPIOF 12 (ADC1 channel 6) is accessible on MIKROBUS_RST line, connector J30

  • GPIOF 13 (ADC2 channel 2) is accessible on MIKROBUS_INT line, connector J31

Kernel configuration

You can manage the STM32MP15 ADC drivers support through the following kernel configuration options:

  • STM32 ADC core driver (CONFIG_STM32_ADC_CORE)

  • STM32 ADC driver (CONFIG_STM32_ADC)

These options are enabled as built-in on the default ConnectCore MP15 kernel configuration file.

Kernel driver

The ADC drivers are located at:

File Description

drivers/iio/adc/stm32-adc-core.c

STM32MP15 ADC core driver

drivers/iio/adc/stm32-adc.c

STM32MP15 ADC driver

Device tree bindings and customization

The STM32MP15 ADC device tree binding is documented at Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml.

The device tree must contain entries for:

  • The ADC interfaces

  • The enabled channels

  • The IOMUX (for ADC channels on non-dedicated pads)

STM32MP15 ADC interfaces and channels

The STM32MP15 device tree defines:

  • The two ADC interfaces as child nodes of the ADC core

  • The channels of each interface as child nodes inside each ADC interface

STM32MP15 device tree
adc: adc@48003000 {
	compatible = "st,stm32mp1-adc-core";
	[...]
	status = "disabled";

	adc1: adc@0 {
		compatible = "st,stm32mp1-adc";
		[...]
		status = "disabled";
	};

	adc2: adc@100 {
		compatible = "st,stm32mp1-adc";
		[...]
		status = "disabled";

		channel@13 {
			reg = <13>;
			label = "vrefint";
		};
		channel@14 {
			reg = <14>;
			label = "vddcore";
		};
	};
};

ConnectCore MP15 Development Kit ADC interfaces and channels

  • ADC1/ADC2 channels 0 and 1 are enabled. These channels are available at dedicated analog pads ANA0 and ANA1.

ConnectCore MP15 Development Kit device tree
&adc {
	/* ANA0, ANA1 are dedicated pins and don't need pinctrl */
	vref-supply = <&vrefbuf>;
	status = "okay";

	adc1: adc@0 {
		status = "okay";

		channel@0 {
			reg = <0>;
			label = "ANA0";
			/* 16.5 ck_cycles sampling time */
			st,min-sample-time-nsecs = <400>;
		};
		channel@1 {
			reg = <1>;
			label = "ANA1";
			/* 16.5 ck_cycles sampling time */
			st,min-sample-time-nsecs = <400>;
		};
	};
};

Select and configure an ADC channel

Follow these steps to configure ADC channels on your custom design:

  1. Identify the pins you want configured as ADC on the Hardware reference manuals. Pins that are ADC-capable list an additional function like ADCn_INPx, where n is the ADC interface and x the channel.

  2. Add a channel@x as a subnode of the ADCn interface, and property reg = <x>; inside.

  3. Configure the IOMUX of the pads to operate as ANALOG inputs. (Pads ANA0 and ANA1 are dedicated ADC pins and do not need any IOMUX configuration.)

For example, pad PF13 of the CPU has additional function ADC2_INP2. This pad is available on the ConnectCore MP15 Development Kit mikroBUS™ socket connector.

Example device tree entry for ADC channel
&adc1 {
	status = "okay";

	channel@2 {
		reg = <2>;
		label = "PF13";
		st,min-sample-time-nsecs = <400>;
	};
};

&pinctrl {
	ccmp15_adc_pins: ccmp15_adc_pins-0 {
		pins {
			pinmux = <STM32_PINMUX('F', 13, ANALOG)>;
		};
	};
};

Voltage reference

The ADC block supports the following voltage references:

  • Internal VREFBUF regulator

  • External voltage reference (needs to be supplied through VREF+ pad).

Internal voltage reference (VREFBUF regulator)

VREFBUF is a STM32MP15 internal voltage regulator supplied via the vdda regulator of the PMIC. You can configure VREFBUF to supply one of the following voltages to output at the VREF+ pin:

  • 2.5 V

  • 2.048 V

  • 1.8 V

  • 1.5 V (ADC and DAC peripherals are not compatible with this reference voltage setting)

On the ConnectCore MP15 Development Kit device tree, VREFBUF is configured to output 2.5 V.

ConnectCore MP15 Development Kit device tree
/*
 * Internal VREFBUF can be set to 2.5 V, 2.048 V, 1.8 V or 1.5 V which are output
 * at VREF+ pad.
 * If you plan to use an external voltage reference, disable vrefbuf and change
 * the ADC 'vref-supply' to the external regulator.
 */
&vrefbuf {
	regulator-min-microvolt = <2500000>;
	regulator-max-microvolt = <2500000>;
	status = "okay";
};
If you plan to measure signals of less voltage, you can get more accurate results by changing the regulator-min-microvolt and regulator-max-microvolt properties to any of the other voltages.

External voltage reference

You can use an external voltage reference for the ADC. This external voltage reference must be between the limits:

  • Min: 2 V

  • Max: 3.0 V

To use an external voltage reference:

  1. Add a fixed regulator node for the external regulator, with its voltage: For example:

    / {
    	regulators {
    		/*
    		 * External regulator for ADC.
    		 * This regulator DOES NOT exist on the DVK. It is only
    		 * a helper for testing the ADC channels.
    		 * To make use of it, you need to supply externally the
    		 * configured voltage to the VREF+ pad.
    		 *
    		 * NOTE: This is incompatible with using the internal VREFBUF
    		 *       regulator, which outputs a voltage at VREF+ pad.
    		 */
    		external_vref: regulator@99 {
    			reg = <99>;
    			compatible = "regulator-fixed";
    			regulator-name = "external-vref";
    			regulator-min-microvolt = <3000000>;
    			regulator-max-microvolt = <3000000>;
    		};
    	};
    };
  2. Set the property vref-supply of the ADC node to use the external regulator:

    &adc {
    	vref-supply = <&external_vref>;
    };
  3. If enabled, disable the internal vrefbuf regulator.

    &vrefbuf {
    	status = "disabled";
    };
  4. Enable the VDDA regulator to remain on all the time. This prevents the analog block drain voltage out of your external voltage reference when the system is not using it.

    &vdda {
    	regulator-always-on;
    };
  5. Supply the external voltage at VREF+ pad.

Formula

The value read on the ADC inputs responds to the formula:

\$V = (V_(RAW) + V_(OFFSET)) * V_(SCALE)\$

where:

  • VRAW is the input voltage converted by the ADC.

  • VOFFSET is the ADC voltage offset.

  • VSCALE is the ADC voltage scale value. This is automatically calculated basing on the Voltage reference on your device tree.

Using the ADCs

The ADC drivers are designed as standard Industrial I/O (IIO) device drivers that can be accessed from the sysfs or from user applications.

If you are using the external voltage reference (default), you must supply an external voltage at VREF+. See External voltage reference.

Sysfs access

When enabled, the ADC drivers create the corresponding device entries in the IIO sysfs directory (/sys/bus/iio/devices). To determine which entry corresponds to which driver, read the name descriptors with:

Reading an ADC name through the sysfs
# grep -H "" /sys/bus/iio/devices/iio\:device*/name
/sys/bus/iio/devices/iio:device0/name:40000000.timer:timer@1
/sys/bus/iio/devices/iio:device1/name:40002000.timer:timer@3
/sys/bus/iio/devices/iio:device2/name:48003000.adc:adc@0

In this example, device2 corresponds to ADC1.

The driver creates:

  • A file entry in_voltage_offset with the voltage offset.

  • A file entry in_voltage_scale with the voltage scale.

  • A file entry in_voltageX_raw for each ADC channel, where X corresponds to the channel number.

Read the input value of a channel with:

Reading an ADC through the sysfs
# cat /sys/bus/iio/devices/iio\:device2/in_voltage2_raw
37474
# cat /sys/bus/iio/devices/iio\:device2/in_voltage_offset
0
# cat /sys/bus/iio/devices/iio\:device2/in_voltage_scale
0.038146972

The returned value in in_voltage2_raw is a decimal number with the result of the conversion. In the example, and according to the Formula:

\$V = (V_(RAW) + V_(OFFSET)) * V_(SCALE) -> V = (37474 + 0) * 0.038146972 = 1429 mV\$

Sample application

An example application called apix-adc-example is included in the dey-examples-digiapix recipe (part of dey-examples package) of the meta-digi layer. This application shows how to access the ADCs using Digi APIx library on the ConnectCore MP15 platform.

Go to GitHub to see the application instructions and source code.

See ADC API for more information about the ADC APIx.

Differential mode

In differential mode, the analog voltage to be converted for a channel is equal to the difference between the voltages at ADCn_INPx (positive input) and ADCn_INNx (negative input).

The converted raw value read from the ADC is calculated in the following way.

\$V_\text{RAW} = 2^(16-1) * (1 + ((V_\text{INP} - V_(\text{INN})) / V_\text{REF}))\$

Configure differential mode

To enable differential mode, on your Linux device tree:

  • Select the ADC channel node that corresponds to the ADCn_INPx functionality (positive input)

  • Add to this node the property diff-channels = <x y>, where:

    • x is the channel number for the ADCn_INPx functionality (positive input)

    • y is

      • the channel number for the ADCn_INPy functionality that also multiplexes ADCn_INNx (negative input), or

      • any channel number if ADCn_INNx is connected to VSSA internally

Refer to the chapter Pad signals and multiplexing of the https://www.digi.com/resources/documentation/digidocs/90002510/#reference/r_external-signals-multiplexing.htm Hardware Reference Manual to locate the pads that have the additional functionality ADCn_INP and/or ADCn_INN.

Refer to ADC1 connectivity and ADC2 connectivity of the STMicroelectronics STM32MP15 Hardware Reference Manual, for information on internal connections of ADCn_INP and ADCn_INN.

For instance, we locate ADC1_INP1 on pad ANA1:

ADC1_INP1

This is ADC1 channel 1 positive input. (x is 1.)

Now we need to locate ADC1 channel 1 negative input, that is ADC1_INN1. We find this functionality on pad ANA0

ADC1_INN1

To find the y, we need to select the channel positive input that’s multiplexed on that same pad for ADC1. This is ADC1_INP0. (y is 0.)

ADC1_INP0

So, with a property diff-channels = <1 0>; we can measure the voltage difference (ADC1_INP1 - ADC1_INN1). The device tree would look like this:

	adc1: adc@0 {
		status = "okay";

		channel@1 {
			reg = <1>;
			label = "INP1-INN1";
			diff-channels = <1 0>;
			st,min-sample-time-ns = <10000>;
		};
	};
This device tree excerpt is limited to the ADC. If the used pads are not analog-dedicated, you also need to configure their pinctrl to ANALOG, and disable any conflicting interface that’s trying to make use of such pads for a different purpose.

Read the value of a differential channel

When configured in differential mode, the offset and scale values are different than those in normal mode.

Check the offset and scale values:

# cat /sys/bus/iio/devices/iio\:device2/in_voltage-voltage_offset
-32768
# cat /sys/bus/iio/devices/iio\:device2/in_voltage-voltage_scale
0.076293945

To read the value of the differential ADC channel:

# cat /sys/bus/iio/devices/iio\:device2/in_voltage1-voltage0_raw
47157

To calculate the voltage (in mV) use the formula:

\$V = (V_(RAW) + V_(OFFSET)) * V_(SCALE)\$

For the example:

V = (47157 + (-32768)) * 0.076293945 = 1098 mV