This is an old revision of the document!
This page introduces the usage of hardware PWM, and leads you to control the hardware PWM on the 40-Pin Header.
PWM | PIN | GPIO Name | GPIO Number | DT Overlays Node | Device Node | |
---|---|---|---|---|---|---|
VIM1 | PWM_AO_A | 35 | GPIOAO_3 | 504 | pwm_ao_a | /sys/class/pwm/pwmchip4 |
VIM2 | PWM_D | 35 | GPIODV_28 | 478 | pwm_d | /sys/class/pwm/pwmchip1 |
VIM3/3L | PWM_F | 35 | GPIOX_7 | 432 | pwm_f | /sys/class/pwm/pwmchip4 |
VIM4 | PWM_F | 35 | GPIOY_8 | 482 | pwm_f | /sys/class/pwm/pwmchip4 |
VIM1S | PWM_F | 35 | GPIOZ_6 | 503 | pwm_f | /sys/class/pwm/pwmchip0 |
Edge2 | PWM14 | 13 | GPIO3_C2 | 114 | pwm14 | /sys/class/pwm/pwmchip2 |
In order to use the PWM, you need to enable the PWM function via Device Tree Overlay.
Edit /boot/env.txt
to add the pwm node to overlays
node if it doesn't exist.
Take VIM3 as an example to enable PWM_F
, you need to add pwm_f
node to overlays
node if it doesn't exist.
overlays=uart4 pwm_ao_a pwm_f
After reboot, you will see the pwm device node.
$ ls /sys/class/pwm/pwmchip4
device export npwm power subsystem uevent unexport
Edit /boot/dtb/amlogic/kvim4.dtb.overlay.env
to add pwm node to fdt_overlays
node if it doesn't exist.
You need to add pwm_f
to node fdt_overlays
if it doesn't exist to enable PWM_F
.
fdt_overlays=pwm_f
After reboot, you will see the pwm device node.
$ ls /sys/class/pwm/pwmchip4
device export npwm power subsystem uevent unexport
Edit /boot/dtb/amlogic/kvim1s.dtb.overlay.env
to add pwm node to fdt_overlays
node if it doesn't exist.
You need to add pwm_f
to node fdt_overlays
if it doesn't exist to enable PWM_F
.
fdt_overlays=pwm_f
After reboot, you will see the pwm device node.
$ ls /sys/class/pwm/pwmchip0
device export npwm power subsystem uevent unexport
Edit /boot/dtb/rockchip/rk3588s-khadas-edge2.dtb.overlay.env
to add pwm node to fdt_overlays
node if it doesn't exist.
You need to add pwm14
to node fdt_overlays
if it doesn't exist to enable PWM14
.
fdt_overlays=pwm14
After reboot, you will see the pwm device node.
$ ls /sys/class/pwm/pwmchip2
device export npwm power subsystem uevent unexport
If you want to use normal GPIO instead of PWM, you can remove the PWM node in Device Tree Overlay.
Enable PWM:
echo 1 | sudo tee /sys/class/pwm/pwmchip4/export echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
echo 1 | sudo tee /sys/class/pwm/pwmchip1/export echo 1000000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/period echo 500000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/duty_cycle echo 1 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
$ echo 1 | sudo tee /sys/class/pwm/pwmchip0/export $ echo 1000000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/period $ echo 500000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/duty_cycle $ echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm1/enable
echo 0 | sudo tee /sys/class/pwm/pwmchip2/export echo 1000000 | sudo tee /sys/class/pwm/pwmchip2/pwm0/period echo 500000 | sudo tee /sys/class/pwm/pwmchip2/pwm0/duty_cycle echo 1 | sudo tee /sys/class/pwm/pwmchip2/pwm0/enable
Use an oscilloscope to check if the pins have been successfully enabled.
If you use an oscilloscope to view the PWM waveform, remember that the SBC and the oscilloscope need to share the same GND (ground).
Disable PWM:
$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
$ echo 0 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
$ echo 0 | sudo tee /sys/class/pwm/pwmchip0/pwm1/enable
$ echo 0 | sudo tee /sys/class/pwm/pwmchip2/pwm0/enable