Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:common:applications:gpio:pwm

This is an old revision of the document!


PWM

Introduction

This document mainly describes the usage of hardware PWM. You will learn how to control the hardware PWM on the 40-Pin Header.

PWM Information

VIM1

PWM PIN & GPIO Number DT Overlays Node Device Node
PWM_AO_A PIN35 - GPIOAO_3 (#504) pwm_ao_a /sys/class/pwm/pwmchip4

VIM2

PWM PIN & GPIO Number DT Overlays Node Device Node
PWM_D PIN35 - GPIODV_28 (#478) pwm_d /sys/class/pwm/pwmchip1

VIM3/3L

PWM PIN & GPIO Number DT Overlays Node Device Node
PWM_F PIN35 - GPIOX_7 (#432) pwm_f /sys/class/pwm/pwmchip4

VIM4

PWM PIN & GPIO Number DT Overlays Node Device Node
PWM_F PIN35 - GPIOY_8 (#482) pwm_f /sys/class/pwm/pwmchip4

Enabel PWM

In order to use the PWM, you need to enable the PWM function via Device Tree Overlay.

VIM1/2/3/3L/Edge1

Edit /boot/env.txt to add the pwm node to overlays node if it is not exist.

Take VIM3 as an example to enable PWM_F, you need to add pwm_f node to overlays node.

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

VIM4

Edit /boot/dtb/amlogic/kvim4.dtb.overlay.env to add pwm node to fdt_overlays node if it is not exist.

e.g. Enable PWM_F, you need to add pwm_f to node fdt_overlays.

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

Disable PWM to Use GPIO

If you want to use normal GPIO instead of PWM, you can remove the PWM node in device tree overlays configration file.

Control PWM

Enable PWM:

VIM1/3/3L/4

$ 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

VIM2

$ 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

Use an oscilloscope to check if the pins have been successfully enabled.

pwm-oscilloscope

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:

VIM1/3/3L/4

$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable

VIM2

$ echo 0 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
Last modified: 2022/07/12 06:12 by nick