Last edited one month ago

TIM device tree configuration: Difference between revisions


Latest revision as of 18:26, 23 June 2025


1. Article purpose[edit | edit source]

The purpose of this article is to explain how to configure the TIM internal peripheral using the device tree mechanism, relying on the bindings documentation, that is the description of the required and optional device-tree properties.

When the peripheral is assigned to Linux® OS, it explains:

  • how to enable PWM, trigger or quadrature encoder.
  • how to configure the board, e.g. TIM input/output pins.

It is used by the TIM_OpenSTLinux_drivers that registers relevant information in PWM, IIO and counter frameworks.

The peripheral can be assigned to different contexts/software components, depending on the final product needs. Refer to How to assign an internal peripheral to an execution context for guidelines on this configuration.

2. DT bindings documentation[edit | edit source]

The device tree binding documents are stored in the Linux kernel repository:

The TIM internal peripheral[1] is a multifunction device (MFD), providing several functions represented by a separate compatible string:

  • st,stm32-timers deals with core resources. (e.g. registers, clock, DMAs)
  • st,stm32-pwm deals with PWM resources. (e.g. PWM input/output pins)
  • st,stm32-timer-trigger deals with trigger resources (e.g. trigger output connected to other STM32 internal peripherals)
  • st,stm32-timer-counter deals with quadrature encoder resources and external events counter.

3. DT configuration[edit | edit source]

This hardware description is a combination of the STM32 microprocessor device tree files (.dtsi extension) and board device tree files (.dts extension). See the Device tree for an explanation of the device-tree file organization.

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

3.1. DT configuration (STM32 level)[edit | edit source]

DT root node (e.g. timers1...) and DT child nodes describe the TIM features such as:

  • PWM
  • trigger
  • counter and quadrature encoder

They also describe hardware parameters such as registers address, clock and DMA.

3.1.1. On STM32MP1 series[edit | edit source]

TIM nodes are declared in stm32mp131.dtsi[2] on STM32MP13x lines More info.png.

TIM nodes are declared in stm32mp151.dtsi[3] on STM32MP15x lines More info.png.

timers1: timer@address {
	Template:Highlight
	compatible = "st,stm32-timers";
	...
	pwm {
		Template:Highlight
		compatible = "st,stm32-pwm";
	};
	timer@0 {
		compatible = "st,stm32h7-timer-trigger";
		Template:Highlight
		reg = <0>;
	};
	counter {
		compatible = "st,stm32-timer-counter";
	};
};

Template:Warning

3.1.2. On STM32MP2 series[edit | edit source]

3.1.2.1. On STM32MP21x lines More info.png[edit | edit source]

TIM nodes are declared in stm32mp211.dtsi[4] on STM32MP21x lines More info.png.

timers1: timer@address {
	Template:Highlight
	compatible = "st,stm32mp21-timers";
	...
	pwm {
		Template:Highlight
		compatible = "st,stm32mp21-pwm";
	};
	timer@0 {
		compatible = "st,stm32mp21-timer-trigger";
		Template:Highlight
		reg = <0>;
	};
	counter {
		compatible = "st,stm32mp21-timer-counter";
	};
};

Template:Warning


3.1.2.2. On STM32MP23x lines More info.png and STM32MP25x lines More info.png[edit | edit source]

TIM nodes are declared in stm32mp231.dtsi[5] on STM32MP23x lines More info.png.

TIM nodes are declared in stm32mp251.dtsi[6] on STM32MP25x lines More info.png.

timers1: timer@address {
	Template:Highlight
	compatible = "st,stm32mp25-timers";
	...
	pwm {
		Template:Highlight
		compatible = "st,stm32mp25-pwm";
	};
	timer@0 {
		compatible = "st,stm32mp25-timer-trigger";
		Template:Highlight
		reg = <0>;
	};
	counter {
		compatible = "st,stm32mp25-timer-counter";
	};
};

Template:Warning

3.2. DT configuration (board level)[edit | edit source]

The objective of this chapter is to explain how to enable and configure the TIM DT nodes for a board:

To enable PWM capture on the board (optional), DMA must be configured:

When PWM capture isn't used, it's recommended to disable DMA channels by default, to spare them for other usage:

Peripheral configuration should be done in specific board device tree files (board dts file and pinctrl dtsi file).

3.3. DT configuration examples[edit | edit source]

3.3.1. TIM configured in PWM mode[edit | edit source]

The example below shows how to configure TIM1 channel 1 to act as:

Template:Highlight
pwm1_pins_a: pwm1-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, AF1)>;
		bias-pull-down;
		drive-push-pull;
		slew-rate = <0>;
	};
};

Template:Highlight
pwm1_sleep_pins_a: pwm1-sleep-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, ANALOG)>;
	};
};
Template:Highlight
&timers1 {
	status = "Template:Highlight";
	/* spare all DMA channels since they are not needed for PWM output */
	/delete-property/dmas;
	/delete-property/dma-names;
	/* define pwm1 label */
	Template:Highlight: pwm {
		Template:Highlight
		pinctrl-0 = <&pwm1_pins_a>;
		pinctrl-1 = <&pwm1_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		Template:Highlight
		status = "Template:Highlight";
	};
};
  • PWM DT user example
/ {
	...
	Template:Highlight
	pwmleds {
		compatible = "pwm-leds";
		example {
			label = "stm32-pwm-leds-example";
			Template:Highlight
			/* period in nanoseconds (500000), normal polarity (0) */
			pwms = <Template:Highlight 500000 0>;
			max-brightness = <127>;
		};
	};
};

3.3.2. TIM configured in PWM mode and trigger source[edit | edit source]

The example below shows how to configure TIM1 channel 1 to act as:

Template:Highlight
pwm1_pins_a: pwm1-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, AF1)>;
		bias-pull-down;
		drive-push-pull;
		slew-rate = <0>;
	};
};

Template:Highlight
pwm1_sleep_pins_a: pwm1-sleep-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, ANALOG)>;
	};
};
&timers1 {
	status = "Template:Highlight";
	Template:Highlight
	/delete-property/dmas;
	/delete-property/dma-names;
	pwm {
		Template:Highlight
		pinctrl-0 = <&pwm1_pins_a>;          
		pinctrl-1 = <&pwm1_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		Template:Highlight
		status = "Template:Highlight";
	};
	timer@0 {
		Template:Highlight
		status = "Template:Highlight";
	};
};

3.3.3. TIM configured in counter capture mode[edit | edit source]

The example below shows how to configure TIM10 channel 1 in counter capture mode, based on the counter subsystem.

It is available on available on STM32MP257x-EV1 GPIO expansion connector.

Template:Highlight
	tim10_counter_pins_a: tim10-counter-0 {
		pins {
			pinmux = <STM32_PINMUX('B', 9, AF9)>; /* TIM10_CH1 */
			bias-disable;
		};
	};

Template:Highlight
	tim10_counter_sleep_pins_a: tim10-counter-sleep-0 {
		pins {
			pinmux = <STM32_PINMUX('B', 9, ANALOG)>; /* TIM10_CH1 */
		};
	};
  • Configure and enable the timer 10 to act as counter device:
Template:Highlight
&timers10 {
	status = "okay";
	Template:Highlight
	counter {
		pinctrl-0 = <&tim10_counter_pins_a>;
		pinctrl-1 = <&tim10_counter_sleep_pins_a>;
		pinctrl-names = "default", "sleep";
		status = "okay";
	};
};

3.3.4. TIM configured in PWM input capture mode[edit | edit source]

3.3.5. TIM configured as quadrature encoder interface[edit | edit source]

The example below shows how to configure TIM1 to interface with a quadrature encoder, based on the counter subsystem:

tim1_in_pins_a: tim1-in-pins-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, AF1)>, /* TIM1_CH1 */
			 <STM32_PINMUX('E', 11, AF1)>; /* TIM1_CH2 */
		bias-disable;
	};
};

tim1_in_pins_sleep_a: tim1-in-pins-sleep-0 {
	pins {
		pinmux = <STM32_PINMUX('E', 9, ANALOG)>, /* TIM1_CH1 */
			 <STM32_PINMUX('E', 11, ANALOG)>; /* TIM1_CH2 */
	};
};
&timers1 {
	status = "Template:Highlight";
	/delete-property/dmas;                       Template:Highlight
	/delete-property/dma-names;
	counter {
		pinctrl-0 = <&tim1_in_pins_a>;       Template:Highlight
		pinctrl-1 = <&tim1_in_pins_sleep_a>;
		pinctrl-names = "default", "sleep";
		status = "Template:Highlight";                     Template:Highlight
	};
};

4. How to configure the DT using STM32CubeMX[edit | edit source]

The STM32CubeMX tool can be used to configure the STM32MPU device and get the corresponding platform configuration device tree files.
STM32CubeMX may not support all the properties described in DT binding files listed 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 | edit source]

Refer to the following links for additional information:

Template:ArticleBasedOnModel