PWM (Pulse Width Modulation) is a modulation technique used to control the average power level of a signal. Its basic working principle relies on adjusting the “on” and “off” durations (duty cycle) of a square wave with a fixed period. In embedded systems, it is used for various purposes such as motor speed control, LED brightness adjustment, servo motor control, power converters, and analog signal generation. For example, when controlling a motor’s speed with PWM, as the duty cycle value increases, the average voltage delivered to the motor increases, causing the motor to rotate faster. The period, one of the fundamental parameters of a PWM signal, is the duration of one complete waveform cycle. The duty cycle is the duration during which the signal remains “on” within one period. Polarity determines the active state of the PWM signal (high or low level). For instance, in normal polarity, the duty cycle represents the high level, while in inverted polarity, it represents the low level.

PWM Interfaces

The PWM interfaces available on the T3-GEM-O1 board are listed in the table below.
GPIO
PWM-ECAP0pwmchip0/pwm0GPIO-12k3-am67a-t3-gem-o1-pwm-ecap0-gpio12.dtso
PWM-ECAP1pwmchip1/pwm0GPIO-16k3-am67a-t3-gem-o1-pwm-ecap1-gpio16.dtso
PWM-ECAP2pwmchip2/pwm0GPIO-18k3-am67a-t3-gem-o1-pwm-ecap2-gpio18.dtso
PWM-0Apwmchip3/pwm0GPIO-5k3-am67a-t3-gem-o1-pwm-epwm0-gpio5.dtso
PWM-0Bpwmchip3/pwm1GPIO-14k3-am67a-t3-gem-o1-pwm-epwm0-gpio14.dtso
PWM-1Apwmchip5/pwm0GPIO-6k3-am67a-t3-gem-o1-pwm-epwm1-gpio6.dtso
PWM-1Bpwmchip5/pwm1GPIO-13k3-am67a-t3-gem-o1-pwm-epwm1-gpio13.dtso
PWM-2Apwmchip7/pwm0FAN headerk3-am67a-t3-gem-o1-gpio-fan.dtso
To enable PWM-0A and PWM-0B together: k3-am67a-t3-gem-o1-pwm-epwm0-gpio5-gpio14.dtsoTo enable PWM-1A and PWM-1B together: k3-am67a-t3-gem-o1-pwm-epwm1-gpio6-gpio13.dtso
The pwmchip3 and pwmchip5 chips have two channels, and each channel can be used as a PWM device. However, different period values CANNOT be assigned to the two channels of a single PWM chip (e.g., PWM-0A and PWM-0B). As long as the same period value is given to both channels, there is no issue in assigning different duty cycles.
The names of the overlay files for the interfaces to be enabled must be added to the overlays variable in the /boot/uEnv.txt file. Detailed information about overlays can be found in the Device Tree section. When all overlays are enabled, there are 8 separate PWM interfaces available: 1 from the FAN header and 7 from the 40-pin header.

PWM Usage

PWM chips can be accessed via the /sys/class/pwm directory. Before using a PWM device, an export operation must be performed. For example, referring to the table above, the PWM-ECAP0 interface corresponds to the pwm0 device of the pwmchip0 chip. After executing the following command, the /sys/class/pwm/pwmchip0/pwm0 device will be created. The reason for writing echo 0 is that it corresponds to pwm0, i.e., device number 0.
gemstone@t3-gem-o1:~$ echo 0 | sudo tee /sys/class/pwm/pwmchip0/export
# 0
Similarly, if the PWM-0B interface is to be used, the pwm1 device of the pwmchip3 chip should be activated as shown in the following command. The reason for writing echo 1 is that it corresponds to pwm1, i.e., device number 1.
gemstone@t3-gem-o1:~$ echo 1 | sudo tee /sys/class/pwm/pwmchip3/export
# 1
After the export operation, the period and duty_cycle variables (in nanoseconds) can be set to desired values as shown in the example below. Once the enable variable is set to 1, the PWM output can be obtained.
gemstone@t3-gem-o1:~$ echo 2000000 | sudo tee /sys/class/pwm/pwmchip0/pwm0/period     # 500 Hz
# 2000000
gemstone@t3-gem-o1:~$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip0/pwm0/duty_cycle # 50%
# 1000000
gemstone@t3-gem-o1:~$ echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm0/enable
# 1
To stop the PWM output, set the enable variable to 0.
gemstone@t3-gem-o1:~$ echo 0 | sudo tee /sys/class/pwm/pwmchip0/pwm0/enable
# 1
If the PWM device is no longer needed, an unexport operation can be performed. This will remove the device from the system. The reason for writing echo 0 is that it corresponds to pwm0, i.e., device number 0.
gemstone@t3-gem-o1:~$ echo 0 | sudo tee /sys/class/pwm/pwmchip0/unexport
# 0