Difference between revisions of "ADC device tree configuration"

[quality revision] [quality revision]
m (Common resources for all ADCs)
m

1 Article purpose[edit]

The purpose of this article is to explain how to configure the analog-to-digital converter (ADC)[1] when the peripheral is assigned to Linux® OS, and in particular:

  • how to configure the ADC peripheral
  • how to configure the board, e.g. the ADC voltage reference regulator, channels, pins and sampling time.

The configuration is performed using the device tree mechanism[2].

It is used by the ADC Linux driver that registers relevant information in IIO framework, such as IIO devices, channels and voltage scale for each ADC.

If the peripheral is assigned to another execution context, refer to How to assign an internal peripheral to a runtime context article for guidelines on peripheral assignment and configuration.

2 DT bindings documentation[edit]

STM32 ADC device tree bindings[3] describe all the required and optional functions.

3 DT configuration[edit]

This hardware description is a combination of STM32 microprocessor and board device tree files. See Device tree for more explanations on device tree file split.

The STM32CubeMX can be used to generate the board device tree. Refer to How to configure the DT using STM32CubeMX for more details.

ADC common resources ADC common resources ADC common resources ADC private resources ADC private resources Regulator Pinctrl How to setup TIM or LPTIM triggers
ADC DT configuration

3.1 DT configuration (STM32 level)[edit]

The ADC nodes are declared in stm32mp157cstm32mp151.dtsi[4].

  • DT root node ('adc') describes the ADC hardware block parameters such as register areas, clocks and interrupts.
  • DT child nodes ('adc1' and 'adc2') describe ADC1 and ADC2 independently.
adc: adc@address {
	compatible = "st,stm32mp1-adc-core";
	...                                      /* common resources in 'adc' root node. */
	adc1: adc@0 {
		compatible = "st,stm32mp1-adc";
		...                              /* private resources in 'adc1' child node. */
	};
	adc2: adc@100 {
		compatible = "st,stm32mp1-adc";
		...                              /* private resources in 'adc2' child node. */
	};
};
Warning white.png Warning
This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.

3.2 DT configuration (board level)[edit]

Follow the sequences described in the below chapters to configure and enable the ADC on your board.

3.2.1 Common resources for all ADCs[edit]

The DT root node ('adc') must be filled in:

  • Enable the ADC block by setting status = "okay".
  • Configure the pins in use via pinctrl, through pinctrl-0 and pinctrl-names.
  • Configure the analog supply voltage regulator[5] by setting vdda-supply = <&your_vdda_regulator>.
  • Configure the analog reference voltage regulator[5] by setting vref-supply = <&your_vref_regulator>.
Info white.png Information
The ADC can use the internal VREFBUF[6] or any other external regulator[5] wired to VREF+ pin.

3.2.2 Resources dedicated to ADC1 and ADC2[edit]

The DT child nodes ('adc1' and/or 'adc2') must be filled in:

  • Enable 'adc1' and/or 'adc2' by setting status = "okay".
  • Enable single-ended channel(s) (<vinp...>) by setting st,adc-channels = <0 1 2...>.
  • Enable differential channel(s) pairs (<vinp vinn>, ...) by setting st,adc-diff-channels = <1 0>, <2 6>, ....
  • Set the minimum sampling time [7] for each or all channels by setting st,min-sample-time-nsecs = <10000> (optional).
  • Set the resolution by setting assigned-resolution-bits = <12> (optional).

3.3 DT configuration example[edit]

The example below shows how to configure ADC1:

  • Input pin: use Pinctrl device tree configuration to configure PF12 as analog input.
  • Analog supply: it is provided by one of the PMIC LDO regulators.
  • Voltage reference: it is provided by the VREFBUF internal regulator.
  • Input channel: configure ADC1_IN6 (e.g on PF12).
  • Sampling time: the minimum sampling time is 10 µs.
# part of pin-controller dt node
adc1_in6_pins_a: adc1-in6 {
	pins {
		pinmux = <STM32_PINMUX('F', 12, ANALOG)>; /* configure 'PF12' as ANALOG */
	};
};
&adc {
	/* ADC1 & ADC2 common resources */
	pinctrl-names = "default";
	pinctrl-0 = <&adc1_in6_pins_a>;              /* Use PF12 pin as ANALOG */
	vdda-supply = <&vdda>;                       /* Example to supply vdda pin by using a PMIC regulator
	vref-supply = <&vrefbuf>;                    /* Example to use VREFBUF (It needs to be enabled as well) */
	status = "okay";                             /* Enable ADC12 block */
	adc1: adc@0 {
		/* private resources for ADC1 */
		st,adc-channels = <6>;               /* ADC1 in6 channel is used */
		st,min-sample-time-nsecs = <10000>;  /* 10µs sampling time */
		status = "okay";                     /* Enable ADC1 */
	};
	adc2: adc@100 {
		/* private resources for ADC2 */
		...
	};
};

4 How to configure the DT using STM32CubeMX[edit]

The STM32CubeMX tool can be used to configure the STM32MPU device and get the corresponding platform configuration device tree files.
The STM32CubeMX may not support all the properties described in the above DT bindings documentation paragraph. If so, the tool inserts user sections in the generated device tree. These sections can then be edited to add some properties and they are preserved from one generation to another. Refer to STM32CubeMX user manual for further information.

5 References[edit]

For additional information, refer to the following links:




== Article purpose ==
The purpose of this article is to explain how to configure the analog-to-digital converter (''ADC'')<ref name="ADC internal peripheral">[[ADC internal peripheral]]</ref> ''' when the peripheral is assigned to Linux<sup>&reg;</sup> OS''', and in particular:
* how to configure the ADC '''peripheral'''
* how to configure the '''board''', e.g. the ADC voltage reference regulator, channels, pins and sampling time.

The configuration is performed using the '''device tree mechanism'''<ref> [[Device tree]]</ref>.

It is used by the [[ADC Linux driver]] that registers relevant information in [[IIO overview|IIO framework]], such as IIO devices, channels and voltage scale for each ADC.

If the peripheral is assigned to another execution context, refer to [[How to assign an internal peripheral to a runtime context]] article for guidelines on peripheral assignment and configuration.

== DT bindings documentation ==
''STM32 ADC device tree bindings''<ref>{{CodeSource | Linux kernel | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.txt | Documentation/devicetree/bindings/iio/adc/st,stm32-adc.txt}}, STM32 ADC device tree bindings</ref> describe all the required and optional functions.

== DT configuration ==
This hardware description is a combination of STM32 microprocessor and board device tree files. See [[Device tree]] for more explanations on device tree file split.

The '''STM32CubeMX''' can be used to generate the board device tree. Refer to [[#How_to_configure_the_DT_using_STM32CubeMX|How to configure the DT using STM32CubeMX]] for more details.
{{
ImageMap|Image:ADC DT configuration.png {{!}} frame {{!}} center{{!}} ADC DT configuration
rect 233 249 295 287 [[#Common_resources_for_all_ADCs|ADC common resources]]
rect 333 158 395 197 [[#Common_resources_for_all_ADCs|ADC common resources]]
rect 511 91 573 130 [[#Common_resources_for_all_ADCs|ADC common resources]]
rect 333 313 395 352 [[#Private_resources_for_an_ADC|ADC private resources]]
rect 511 283 573 323 [[#Private_resources_for_an_ADC|ADC private resources]]
rect 141 157 227 183 [[Regulator overview|Regulator]]
rect 141 269 227 293 [[Pinctrl overview|Pinctrl]]
rect 141 307 227 330 [[How to use the IIO user space interface#How to set up a TIM or LPTIM trigger using the sysfs interface|How to setup TIM or LPTIM triggers]]
}}

=== DT configuration (STM32 level) ===
The ADC nodes are declared in stm32mp157cstm32mp151.dtsi<ref name="stm32mp157cstm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp157cstm32mp151.dtsi | STM32MP157CSTM32MP151 device tree file}}</ref>.

* '''DT root node''' (''''adc'''') describes the ADC hardware block parameters such as register areas, clocks and interrupts.
* '''DT child nodes''' (''''adc1'''' and ''''adc2'''') describe ADC1 and ADC2 independently.
 adc: adc@address {
 	compatible = "st,stm32mp1-adc-core";
 	...                                      {{highlight|/* common resources in 'adc' root node. */}}
 	adc1: adc@0 {
 		compatible = "st,stm32mp1-adc";
 		...                              {{highlight|/* private resources in 'adc1' child node. */}}
 	};
 	adc2: adc@100 {
 		compatible = "st,stm32mp1-adc";
 		...                              {{highlight|/* private resources in 'adc2' child node. */}}
 	};
 };
{{Warning|This device tree part is related to STM32 microprocessors. It must be kept as is, without being modified by the end-user.}}

=== DT configuration (board level) ===
Follow the sequences described in the below chapters to configure and enable the ADC on your board.

==== Common resources for all ADCs ====
The '''DT root node''' (''''adc'''') must be filled in:
* Enable the ADC block by setting '''status = "okay".'''
* Configure the pins in use via [[Pinctrl overview|pinctrl]], through '''pinctrl-0''' and '''pinctrl-names'''.
* Configure the analog supply voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vdda-supply = <&your_vdda_regulator>'''.
* Configure the analog reference voltage regulator<ref name="Regulator overview">[[Regulator overview]]</ref> by setting '''vref-supply = <&your_vref_regulator>'''.
{{info|The ADC can use the internal VREFBUF<ref name="VREFBUF internal peripheral">[[VREFBUF internal peripheral]]</ref> or any other external regulator<ref name="Regulator overview"/> wired to VREF+ pin.}}

==== Resources dedicated to ADC1 and ADC2====
The '''DT child nodes''' (''''adc1'''' and/or ''''adc2'''') must be filled in:
* Enable 'adc1' and/or 'adc2' by setting '''status = "okay".'''
* Enable single-ended channel(s) (''<vinp...>'') by setting '''st,adc-channels = <0 1 2...>.'''
* Enable differential channel(s) pairs (''<vinp vinn>, ...'') by setting '''st,adc-diff-channels = <1 0>, <2 6>, ....'''
* Set the minimum sampling time <ref>[http://www.st.com/content/ccc/resource/technical/document/application_note/group0/3f/4c/a4/82/bd/63/4e/92/CD00211314/files/CD00211314.pdf/jcr:content/translations/en.CD00211314.pdf How to get the best ADC accuracy in STM32], by STMicroelectronics</ref> for each or all channels by setting '''st,min-sample-time-nsecs = <10000>''' (optional).
* Set the resolution by setting '''assigned-resolution-bits = <12>''' (optional).

=== DT configuration example ===
The example below shows how to configure ADC1:
* Input pin: use [[Pinctrl device tree configuration]] to configure PF12 as analog input.
* Analog supply: it is provided by one of the [[PMIC hardware components|PMIC]] LDO regulators.
* Voltage reference: it is provided by the [[Regulator_overview#Microprocessor device internal regulators|VREFBUF internal regulator]].
* Input channel: configure ADC1_IN6 (e.g on PF12).
* Sampling time: the minimum sampling time is 10 µs. 

 # part of pin-controller dt node
 adc1_in6_pins_a: adc1-in6 {
 	pins {
 		pinmux = <STM32_PINMUX({{highlight|'F'}},{{highlight| 12}}, ANALOG)>; {{highlight|/* configure 'PF12' as ANALOG */}}
 	};
 };

 &adc {
 	/* ADC1 & ADC2 common resources */
 	pinctrl-names = "default";
 	pinctrl-0 = {{highlight|<&adc1_in6_pins_a>}};              {{highlight|/* Use PF12 pin as ANALOG */}}
 	vdda-supply ={{highlight| <&vdda>}};                       {{highlight|/* Example to supply vdda pin by using a PMIC regulator}}
 	vref-supply ={{highlight| <&vrefbuf>}};                    {{highlight|/* Example to use VREFBUF (It needs to be enabled as well) */}}
 	status = "'''okay'''";                             {{highlight|/* Enable ADC12 block */}}
 	adc1: adc@0 {
 		/* private resources for ADC1 */
 		st,adc-channels = {{highlight|<6>}};               {{highlight|/* ADC1 in6 channel is used */}}
 		st,min-sample-time-nsecs = {{highlight|<10000>}};  {{highlight|/* 10µs sampling time */}}
 		status = "'''okay'''";                     {{highlight|/* Enable ADC1 */}}
 	};
 	adc2: adc@100 {
 		/* private resources for ADC2 */
 		...
 	};
 };

==How to configure the DT using STM32CubeMX==
The [[STM32CubeMX]] tool can be used to configure the STM32MPU device and get the corresponding [[Device_tree#STM32|platform configuration device tree]] files.<br />

The STM32CubeMX may not support all the properties described in the above [[#DT bindings documentation|DT bindings documentation]] paragraph. If so, the tool inserts '''user sections''' in the generated device tree. These sections can then be edited to add some properties and they are preserved from one generation to another. Refer to [[STM32CubeMX]] user manual for further information.

==References==
For additional information, refer to the following links:
<references />

<noinclude>

{{ArticleBasedOnModel | Peripheral or framework device tree configuration model}}
{{PublicationRequestId | 8779 | 2018-09-13 | AnneJ}}

[[Category:Device tree configuration]]
[[Category:IIO]]

{{ReviewsComments|JCT 1840: alignment needed with the last version of the model<br>

[[Category:ToBeAlignedWithModel]]
}}</noinclude>
Line 30: Line 30:
   
 
=== DT configuration (STM32 level) ===
 
=== DT configuration (STM32 level) ===
The ADC nodes are declared in stm32mp157c.dtsi<ref name="stm32mp157c_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp157c.dtsi | STM32MP157C device tree file}}</ref>.
+
The ADC nodes are declared in stm32mp151.dtsi<ref name="stm32mp151_dtsi">{{CodeSource | Linux kernel | arch/arm/boot/dts/stm32mp151.dtsi | STM32MP151 device tree file}}</ref>.
   
 
* '''DT root node''' (''''adc'''') describes the ADC hardware block parameters such as register areas, clocks and interrupts.
 
* '''DT root node''' (''''adc'''') describes the ADC hardware block parameters such as register areas, clocks and interrupts.