> For the complete documentation index, see [llms.txt](https://tanzhs-private-organization.gitbook.io/kickpi-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tanzhs-private-organization.gitbook.io/kickpi-book/rk3576/en/05-drivers_development/pwm.md).

# PWM

## Symbol Explanation

* `SDK$`: Refers to the source code path.
* `console$`: Generally refers to the command-line console of the motherboard. [Mainboard Command-line Console](https://github.com/tan-zhihao1/kickpi-book/blob/master/rk3576/en/02-Getting_Started_Guide/02-Quick_Start_Guide.md#console_readme)
* `ADB$`: Android Debug Bridge command-line tool, generally refers to an environment where ADB can be run.

The Extend 40Pin interface contains multiple channels of PWM. The specific pins are shown in the Expansion Pin Section.

The following uses PWM2\_CH7\_M2 as an example for explanation.

![image-20250421155643696](C:%5CUsers%5C16708%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20250421155643696.png)

For pwmX\_Ych\_Z, X represents the controller ID, Y represents the total number of channels supported by the current controller, and Z represents the channel ID.

PWM2\_CH7\_M2 represents:

> pwm0\_2ch\_1: pwm\@27331000
>
> pwm2\_8ch\_6: pwm\@2ade6000
>
> pwm2\_8ch\_7: pwm\@2ade7000

List relevant PWM nodes:

```
$ ls /sys/class/pwm/
pwmchip0  pwmchip1  pwmchip2  pwmchip3
```

You can view the corresponding PWM DTS nodes:

```
$ cat /sys/class/pwm/pwmchip0/device/uevent | grep FULLNAME
OF_FULLNAME=/pwm@27331000
```

> It indicates that pwnchip0 corresponds to PWM0 Channel 1.

Configure the PWM channel:

Example: Set PWM0\_CH1 channel, with a period of 10000ns, a duty cycle of 5000ns, and the polarity as normal.

```
$ echo 0 > /sys/class/pwm/pwmchip0/export
$ echo 10000 > /sys/class/pwm/pwmchip0/pwm0/period
$ echo 5000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
$ echo normal > /sys/class/pwm/pwmchip0/pwm0/polarity
$ echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
```

> After the configuration parameters are successfully set according to the example, you can use a multimeter to measure the PWM0 pin. The correct voltage should be around 1.6V.

Export the PWM channel:

```
echo 0 > /sys/class/pwm/pwmchip0/export
```

Set the PWM period (frequency):

```
echo x > /sys/class/pwm/pwmchip0/pwm0/period
```

Set the PWM duty cycle:

```
echo x > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
```

Set the PWM polarity:

```
echo normal > /sys/class/pwm/pwmchip0/pwm0/polarity
echo inverted > /sys/class/pwm/pwmchip0/pwm0/polarity
```

Enable PWM output:

```
echo 1/0 > /sys/class/pwm/pwmchip0/pwm0/enable
```
