Skip to end of metadata
Go to start of metadata

 

Targeted HardwarephyBOARD-Zeta (i.MX7)
Targeted SoftwareBSP Yocto FSL iMX7 PD18.2.0
Date

 

Summary


Pulse-width modulation (PWM) is a modulation technique used for a variety of purposes in electronics. Typical PWM signals are repeating signals characterized by their period and duty cycle. A PWM signal period is the length of time from the start to the end of one cycle of the PWM signal. A PWM signal duty cycle is the proportion of the PWM signal period during which the signal is 'on' or active. This how-to guide will detail the steps necessary to enable and use PWM on the phyBOARD-Zeta (i.MX7) in Linux.

Step-by-step guide


Enabling all available PWMs in Linux device tree

Two PWM signals are brought out to the phyBOARD-Zeta carrier board: X_PWM2 and X_PWM3. These two signals correspond to the PWM outputs on the i.MX7 processor. By default, only PWM3 is enabled in the device tree files for use with the PHYTEC PEB-AV-02 LCD adapter as a backlight signal. To use both PWM2 and PWM3 in Linux, you must enable PWM2 and disable the LCD adapter. To enable PWM2, you will need to modify the device tree file imx7s-pba-c-09.dtsi.

Find the '&iomuxc_lpsr' entry in the file and, under the imx7d-phycore node, add the following lines to pinmux GPIO1_IO02 as PWM2_OUT:

imx7s-pba-c-09.dtsi
pinctrl_pwm2: pwm2grp {
        fsl,pins = <
                MX7D_PAD_GPIO1_IO02__PWM2_OUT 0x30
        >;
};

To disable the LCD adapter, you will need to modify the device tree file imx7d-phyboard-zeta-001.dts.

Comment out (or delete) the following line to disable the LCD adapter:

imx7d-phyboard-zeta-001.dts
#include "imx7-peb-av-02.dtsi"        /* LCD adapter */

Enabling the PWMs in Linux

Controlling the PWM signals in Linux is handled through the sysfs interface. Run the following commands to export (request access to) PWM2 and PWM3:

Linux (PWM2)
echo 0 > /sys/class/pwm/pwmchip0/export
Linux (PWM3)
echo 0 > /sys/class/pwm/pwmchip1/export

To unexport (free up) PWM2 and PWM3, run the following commands:

Linux (PWM2)
echo 0 > /sys/class/pwm/pwmchip0/unexport
Linux (PWM3)
echo 0 > /sys/class/pwm/pwmchip1/unexport

Configuring the PWM period, duty cycle, and polarity

The period of the PWM signals in Linux is measured in nanoseconds. To set the period of the PWM2 signal to one second, run the following command after PWM2 has been exported:

Linux (PWM2)
echo 1000000000 > /sys/class/pwm/pwmchip0/pwm0/period

The duty cycle of the PWM signals in Linux is also measured in nanoseconds. To set the duty cycle of the PWM2 to 50%, or a half-second 'on' time, run the following command:

Linux (PWM2)
echo 500000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle

The polarity of the PWM signals in Linux controls whether the 'on' time of the signal is active high or active low. To set the polarity of PWM2 to active high, run the following command:

Linux (PWM2)
echo 'normal' > /sys/class/pwm/pwmchip0/pwm0/polarity

To set the polarity of PWM2 to active low, run the following command:

Linux (PWM2)
echo 'inversed' > /sys/class/pwm/pwmchip0/pwm0/polarity

Starting the PWM signal

You must start a PWM signal in Linux after configuring it. To start PWM2, run the following command after following the instructions above:

Linux (PWM2)
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable

Finally, to disable PWM2 when it is no longer needed, run the following command:

Linux (PWM2)
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable

Official Linux PWM documentation

You can find more details about the above commands and other Linux PWM interface information in the official documentation found online here or in your local Linux kernel source file Documentation/pwm.txt.