Sysfs Interface Example

In the user space, you can easily control PWM through sysfs interface supported by Linux.

The following procedure provides an example for it.
  1. Enter the following directory, you can see all the registered PWM chips.
    # cd /sys/class/pwm/
    # ls
    pwmchip0
    
  2. Enter the directory pwmchip*, run command echo X > export.
    Note: In the command, the parameter X is the PWM channel which you want to control.
    Result: Then the directory pwmX is generated.
    # cd pwmchip0/
    # ls
    device     export     npwm       power      subsystem  uevent     unex-port
    # echo 0 > export
    # ls
    device     npwm       pwm0       uevent
    export     power      subsystem  unexport
    # echo 1 > export
    # ls
    device     npwm       pwm0       subsystem  unexport
    export     power      pwm1       uevent
    
  3. Enter the directory pwmX, you can see all states of this PWM channel.

    The following list provides descriptions for the parameters.
    • enable: Enable or disable the PWM channel. 1 for enable, 0 for disable.
    • polarity: Invert polarity or not. 1 for invert polarity, 0 for normal (do not invert).
    • period: Enter the period of PWM signal (unit: ns)
    • duty_cycle: Enter the duty cycle of PWM signal (unit: ns)
  4. Now, you can change the states of PWM channel using commands of echo and cat.
    # cd pwm0/
    # ls
    capture     enable      polarity    uevent
    duty_cycle  period      power
    # echo 10000 > period
    # echo 5000 > duty_cycle 
    # cat period 
    10000
    # cat duty_cycle 
    5000
    # cat polarity 
    normal
    # cat enable 
    1