#How to use the ADC lines

<abstract>
This article illustrates how to use the ADC lines on Roadrunner board
</abstract>

Tested on Linux Kernel: __4.19.78__

## Pinout of ADC lines

Up to 12 ADC lines are available on the [RoadRunner SOM](/roadrunner). Some of them
are available also on [Fox Board D27](/foxd27)

| Signal | Description                        | Line        | FOX D27 |
|--------|------------------------------------|-------------|---------|
| 3.3V   | 3.3 volt power line                |             |         |
| AVDD   | Clean 3.24V out for A/D circuitry  |             |         |
| VREF   | A/D voltage reference input        |             |         |
| AGND   | Analog GND                         |             |         |
|  AD0   | Analog input 0                     | PD19        | J2-09   |
|  AD1   | Analog input 1                     | PD20        | J2-10   |
|  AD2   | Analog input 2                     | PD21        | J2-11   |
|  AD3   | Analog input 3                     | PD22        | J2-12   |
|  AD4   | Analog input 4                     | PD23        | J1-13   |
|  AD5   | Analog input 5                     | PD24        | J1-10   |
|  AD6   | Analog input 6                     | PD25        | J1-12   |
|  AD7   | Analog input 7                     | PD26        |         | 
|  AD8   | Analog input 8                     | PD27        |         |
|  AD9   | Analog input 9                     | PD28        |         |
|  AD10  | Analog input 10                    | PD29        | J1-15   |
|  AD11  | Analog input 11                    | PD30        |         |
|  GND   | Digital GND                        |             |         |

### Enable the Linux Kernel ADC modules

To enable the ADC Linux driver and the IIO user space interface inside <code>make ARCH=arm menuconfig</code>:

	Device Drivers  ---> 
		<*> Industrial I/O support  ---> 
			<*> Enable software IIO device support 
			<*> Enable software triggers support
			Analog to digital converters  --->
		      	<*> Atmel AT91 ADC
				<*> Atmel AT91 SAMA5D2 ADC      

Follow this article @article='roadrunner_compile_kernel_4_19_78' to compile the Linux Kernel,
enable the right drivers and use the right device tree definitions.

### Configure the device tree

Edit the device tree source at <code>arch/arm/boot/dts/at91-sama5d2_roadrunner.dts</code> of your board adding these lines:

Insert this definition to enable the driver:

	vddin_3v3: fixed-regulator-vddin_3v3 {
		compatible = "regulator-fixed";
	
		regulator-name = "VDDIN_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
		status = "okay";
	};
	
	vddana: fixed-regulator-vddana {
		compatible = "regulator-fixed";
	
		regulator-name = "VDDANA";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vddin_3v3>;
		status = "okay";
	};
	
	advref: fixed-regulator-advref {
		compatible = "regulator-fixed";
	
		regulator-name = "advref";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
		vin-supply = <&vddana>;
		status = "okay";
	};
	
	adc: adc@fc030000 {
		vddana-supply = <&vddana>; 
		vref-supply =  <&vddana>; 
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_adc_default>;
		status = "okay";
	};

Insert this definition to select the pin to use:

	pinctrl_adc_default: adc_default {
		pinmux = <PIN_PD19__GPIO>,
				<PIN_PD20__GPIO>,
				<PIN_PD21__GPIO>,
				<PIN_PD22__GPIO>,
				<PIN_PD23__GPIO>,
				<PIN_PD24__GPIO>,
				<PIN_PD25__GPIO>,
				<PIN_PD26__GPIO>,
				<PIN_PD27__GPIO>,
				<PIN_PD28__GPIO>,
				<PIN_PD29__GPIO>,
				<PIN_PD30__GPIO>;
		bias-disable;
	};

Generate the <code>at91-sama5d2_roadrunner.dtb</code>:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- at91-sama5d2_roadrunner.dtb

Rename it in <code>acme-roadrunner.dtb</code> and save it inside boot partition.

### Use the ADC from command line

	cat /sys/bus/iio/devices/iio\:device0/in_voltage0_raw
	cat /sys/bus/iio/devices/iio\:device0/in_voltage1_raw
	...
	cat /sys/bus/iio/devices/iio\:device0/in_voltage11_raw

More in-depth examples are available from Microchip at:

* [Using the SAMA5D2-compatible ADC device](https://www.at91.com/linux4sam/bin/view/Linux4SAM/UsingSAMA5D2ADCDevice)


### Python example

Without using mpio library:

	import time
	
	def get_adc(id):
		with open("/sys/bus/iio/devices/iio:device0/in_voltage%d_raw" % id) as f:
			value=int(f.read())
			return value
	
	while True:
		print "ADC value: %d" % get_adc(0)
		time.sleep(1)
    
Using mpio library:

	from mpio import ADC
	import time

	while True:
		adc = ADC(0)
		print "ADC value: %d" % adc.value(0)
		time.sleep(1)

## Links

* [Using the SAMA5D2-compatible ADC device](https://www.at91.com/linux4sam/bin/view/Linux4SAM/UsingSAMA5D2ADCDevice)
* @article='roadrunner_mpio'
* [Microchip ADC class source](https://github.com/linux4sam/mpio/blob/master/mpio/adc.py)
* [How to Use the SAMA5D2 ADC Under Linux](http://ww1.microchip.com/downloads/en/Appnotes/AN_3250-How-to-use-SAMA5D2-ADC-under-Linux-00003250a.pdf)
* [Patch: Add fixed regulators for the ADC](https://patchwork.kernel.org/patch/10605837/)
* [Use of the AT91 ADC driver by Atmel](http://www.at91.com/linux4sam/bin/view/Linux4SAM/IioAdcDriver)


@include='adv_roadrunner'
