DESK-MX8M-L-AN-0005: Enable PWMs on SBC ORCA

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box


200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.

History
Issue Date Notes
2024/05/29 DESK-MX8M-L 4.1.0


Introduction[edit | edit source]

i.MX8M Plus has up to 4 PWMs interfaces. In the ORCA EVK the J8 expansion connector can be used to enable the available PWMs.

ORCA Evaluation Kit - J8 GPIOs connector

This application note describes how to simply enable the PWM interface modifying the device tree.

Device tree configuration[edit | edit source]

The following nodes has to be inserted in the device tree for enabling the pwm interfaces:

&pwm1 {
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_pwm1e>;
       status = "okay";
};

&pwm2 {
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_pwm2>;
       status = "okay";
};

&pwm3 {
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_pwm3>;
       status = "okay";
};

&pwm4 {
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_pwm4>;
       status = "okay";
};

and the related pad has to be properly configured with the pinmux settings:

&iomuxc {
...
...
       pinctrl_pwm1e: pwm1grp-e {
               fsl,pins = <
                       MX8MP_IOMUXC_SAI5_MCLK__PWM1_OUT 0x116
               >;
       };

       pinctrl_pwm2: pwm2grp {
               fsl,pins = <
                       MX8MP_IOMUXC_SAI5_RXD0__PWM2_OUT 0x116
               >;
       };

       pinctrl_pwm3: pwm3grp {
               fsl,pins = <
                       MX8MP_IOMUXC_SAI5_RXC__PWM3_OUT 0x116
               >;
       };

       pinctrl_pwm4: pwm4grp {
               fsl,pins = <
                       MX8MP_IOMUXC_SAI5_RXFS__PWM4_OUT 0x116
               >;
       };
};

kernel documentation[edit | edit source]

The PWM related kernel documentation can be found here. In this application note, the sysfs interface is used as demonstration purposes.

Userspace PWM interface[edit | edit source]

Once the PWM devices have been enabled in the device tree, sysfs interface allows to see those peripherals nodes available:

root@desk-mx8mp:~# ls -la /sys/class/pwm/
total 0
drwxr-xr-x  2 root root 0 May 29 13:59 .
drwxr-xr-x 87 root root 0 May 29 13:59 ..
lrwxrwxrwx  1 root root 0 May 29 13:59 pwmchip0 -> ../../devices/platform/soc@0/30400000.bus/30660000.pwm/pwm/pwmchip0
lrwxrwxrwx  1 root root 0 May 29 13:59 pwmchip1 -> ../../devices/platform/soc@0/30400000.bus/30670000.pwm/pwm/pwmchip1
lrwxrwxrwx  1 root root 0 May 29 13:59 pwmchip2 -> ../../devices/platform/soc@0/30400000.bus/30680000.pwm/pwm/pwmchip2
lrwxrwxrwx  1 root root 0 May 29 13:59 pwmchip3 -> ../../devices/platform/soc@0/30400000.bus/30690000.pwm/pwm/pwmchip3
root@desk-mx8mp:~#

where pwmchip[x] is the related PWM interface instantiated by the kernel.

The subnodes for a single interface are the following ones:

root@desk-mx8mp:~# ls -la /sys/class/pwm/pwmchip0/
total 0
drwxr-xr-x 3 root root    0 May 29 13:59 .
drwxr-xr-x 3 root root    0 May 29 13:59 ..
lrwxrwxrwx 1 root root    0 May 29 14:01 device -> ../../../30660000.pwm
--w------- 1 root root 4096 May 29 14:23 export
-r--r--r-- 1 root root 4096 May 29 14:01 npwm
drwxr-xr-x 2 root root    0 May 29 14:01 power
lrwxrwxrwx 1 root root    0 May 29 13:59 subsystem -> ../../../../../../../class/pwm
-rw-r--r-- 1 root root 4096 May 29 13:59 uevent
--w------- 1 root root 4096 May 29 14:23 unexport
root@desk-mx8mp:~#

Accessing the PWM device[edit | edit source]

Here below are the simple commands used for testing the PWM interface.

If, for example, the first PWM is to be used, the related pwmchip0 entry has to be selected and the following command will be used

echo 0 > /sys/class/pwm/pwmchip0/export
echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 500000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
  • exports the PWM channel for use with sysfs (write-only)
root@desk-mx8mp:~# echo 0 > /sys/class/pwm/pwmchip0/export
  • total period of the PWM signal: the value is in nanoseconds and is the sum of the active and inactive time of the PWM. In this example, 1KHz is configured (1000000 ns)
root@desk-mx8mp:~# echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period
  • the active time of the PWM signal: the Value is in nanoseconds and must be less than the period. In this example, 50% duty cycle is configured (500000 ns, respect to the period)
root@desk-mx8mp:~# echo 500000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
  • enable/disable the PWM signal
    • 0 - disabled
    • 1 - enabled
root@desk-mx8mp:~# echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
  • once configured, the PWM entry can be disabled for closing the configuration settings using unexport
root@desk-mx8mp:~# echo 0 > /sys/class/pwm/pwmchip0/unexport