How to add pwm

Revision as of 02:47, 1 October 2015 by LubOlimex (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to enable PWM libraries for Olimex Allwinner boards

The article describes how to enable the PWM control in the Olimex Allwinner boards under Linux by enabling the "Sunxi PWM Driver" in the kernel.

If the Linux PWM module in the board you are using is not available for use there is a good chance it is not enabled by default in the distribution you are using. A way to enable it is detailed below.

To enable the PWM module you need to perform five major steps:

1. Recompile the kernel following one of the Olimex wordpress guides for the board you have on how to do it. The only difference would be to enable the "Sunxi PWM Driver (pwm-sunxi)" as shown in the picture below.

Pwm enable2.jpg

2. Replace the uImage file from first partition of your SD card with the generated one from ../linux-sunxi/arch/arm/boot/uImage directory

3. Replace the modules from second partition of your SD card (usualy they are located in /lib/modules/xx.xx.x) with the new generated modules from ../linux-sunxi/out/modules/xx.xx.x

if the PWM module is not loaded you can load it with

#modprobe pwm-sunxi

4. Edit the script.fex in the "[pwm0_para]" paragraph adding PWM support for specific pin.

Example config for script.bin:

[pwm0_para]

pwm_used = 1

pwm_period = 10000

pwm_duty_percent = 100

Source and more info on the subject: google sunxi groups discussion on pwm0_para

5. Boot the board and use the library! Info on the pwm class:

- /sys/class/pwm-sunxi 
                     |
                     +----pwmX 
                             | 
                             +---duty 
                             +---duty_percent 
                             +---period 
                             +---polarity 
                             +---pulse 
                             +---pin 
                             +---run 

Information on the parameters:

period (r/w) - period that makes up a cycle. Can be expressed as hz, khz, ms, or us. Whole numbers only.

       Examples: 
       echo 10hz > /sys/class/pwm-sunxi/pwm0/period 
       echo 1khz > /sys/class/pwm-sunxi/pwm0/period 
       echo 100ms > /sys/class/pwm-sunxi/pwm0/period 
       echo 100us > /sys/class/pwm-sunxi/pwm0/period 
       echo 150khz > /sys/class/pwm-sunxi/pwm0/period 

duty (r/w) - portion of the period above that is "active" or on. Same units as above.

       Examples:
       echo 1hz > /sys/class/pwm-sunxi/pwm0/period 
       echo 2hz > /sys/class/pwm-sunxi/pwm0/duty 

duty_percent (r/w) - duty (above) expressed as a percentage. Whole numbers only.

       Example:
       echo 50 > /sys/class/pwm-sunxi/pwm0/duty_percent

polarity(r/w) - polarity of the pin during the duty portion. 1 = high, 0 = low

pulse (r/w) - output one pulse at the specified period and duty

pin (ro) - returns the name of the pin this pwm outputs on. This is hardwired and informational only (the pin for PWM0 is PB2 and the pin for PWM1 is PI3). If you have a display connected it is advisable to use PWM1 (PI3). That is because PWM0's signal PB2 is used for the LCD connection.

       Examples:
       cat /sys/class/pwm-sunxi/pwm0/pin
       nano /sys/class/pwm-sunxi/pwm1/pin

run (r/w) - Enable the PWM with the previously set parameters. Example:

       Example:
       echo 1 > /sys/class/pwm-sunxi/pwm0/run
       echo 0 > /sys/class/pwm-sunxi/pwm0/run

Source and more info on the subject: google sunxi group discussion on PWM kernel module