linux 内核中的pwm


原文链接: linux 内核中的pwm

PWM常用来做电机控制、LED背光亮度调节、开关电源等。

Linux pwm driver with sysfs

TI linux pwm user guide: http://processors.wiki.ti.com/index.php/Linux_Core_PWM_User%27s_Guide#eHRPWM

Freescale: https://support.bluetechnix.at/wiki/Linux_Software_User_Manual_(i.MX6)#PWM

Gateworks: http://trac.gateworks.com/wiki/linux/pwm

对于TI的pwm来说

首先配置内核支持pwm模块,其中eHRPWM:Enhanced High Resolution PWM, eCAP:Enhanced Capture.

Procedure to build eHRPWM driver
         Device Drivers --->
                 <*> Pulse Width Modulation(PWM) Support --->
                    <*> eHRPWM PWM support   
Procedure to build eCAP driver
         Device Drivers --->
                 <*> Pulse Width Modulation(PWM) Support --->
                    <*> eCAP PWM support   

其次,申请channel
Request the Device:
target$
echo 0 > /sys/class/pwm/pwmchip0/export
free the device:
target$
echo 0 > /sys/class/pwm/pwmchip0/unexport
使能/失能通道,注意:在使能前需要先设置下面参数,如周期和占空比
Enable the PWM
target$ echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
Disable the PWM
target$ echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
设置周期/占空比/极性

i.Setting the Period
Following attributes set the period of the PWM waveform.
period Attribute
Enter the period in nano seconds value.

Example
if the period is 1 sec , enter

target$ echo 1000000000 > /sys /class/pwm/pwmchip0/pwm0/period
ii.Setting the Duty
Following attributes set the duty of the PWM waveform.

duty_cycle Attribute
Enter the Duty cycle value in nanoseconds.

target$ echo val > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
iii.Setting the Polarity
Polarity Attribute.
Setup Signal Polarity

Example
To set the polarity to Active High, Enter

target$ echo 1 > /sys /class/pwm/pwmchip0/pwm0/polarity
```
这样就完成了pwm的设置。

那sysfs和kernel中的驱动文件是怎么匹配起来的?

主要有3个文件:在drivers/pwm下得sysfs.c/core.c/pwm-imx.c

举个period的例子:

-->static DEVICE_ATTR(period, 0644, pwm_period_show, pwm_period_store)   //sysfs.c

-->pwm_period_store(...)  //sysfs.c

  -->pwm_config(...)    //core.c

  -->pwm->chip->ops->config(...)  //core.c

    -->imx_pwm_config(...)    //pwm-imx.c

      -->imx_pwm_config_v1(imx1) or imx_pwm_config_v2(imx27在dts中Imx6使用)

这样就和驱动文件关联起来了。

`