soft pwm for raspberry pi
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
Makefile
README.md
pwm.c

README.md

Soft PWM for Raspberry PI

This module provides soft PWM capability to raspberry pi.

Compilation in Raspbian

  1. Update kernel and install kernel header files
	$ apt-get update
	$ apt-get install --only-upgrade raspberrypi-kernel
	$ apt-get install raspberrypi-kernel-headers
do reboot after kernel update
  1. Build
	$ cd rpi_SoftPwm	
	$ make M=$(pwd) -C /lib/modules/$(uname -r)/build modules	
	$ make M=$(pwd) -C /lib/modules/$(uname -r)/build modules_install	
	$ depmod -a	
	$ modprobe soft_pwm
  1. Auto loading while boot up

    add soft_pwm to /etc/modules

Cross Compilation

In order to cross compiling the module, you need:

  • raspberry pi's kernel sources
  • toolchain
  • Module.symvers file (in order to save some time)
  1. Obtaining the kernel:

    $ git clone --depth=1 https://github.com/raspberrypi/linux

The kernel comes without the .config file. The file must be taken from the pi directly:

$ zcat /proc/config.gz > ~/config
$ ... transfer the config file to the build machine (scp, ftp ...)
$ copy the config file as linux/.config
  1. Obtaining the toolchain

You can install the toolchain using your system's package manager or by cloing the raspberry pi's toolchain repository:

$ git clone --depth=1 https://github.com/raspberrypi/tools
  1. Preparing the kernel

In order to compile any modules we need Module.symvers file. If the kernel versions matches then you can obtain Module.symvers from raspberry pi's repositories. Otherwise you have to devote some time in order to generate it. In order for the file being generate, all kernel modules must be rebuild:

$ cd linux
$ ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- make oldconfig
$ ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- make prepare
$ ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- make uImage
$ ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- make modules

After 30 minutes... (or so) we should have the Module.symvers file. We are good to go to build and install the module.

$ cd ../rpi_SoftPwm
$ ARCH=arm CROSS_COMPILE=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- make M=$PWD -C ../linux modules

On the pi

$ sudo insmod soft_pwm.ko

Usage

The driver creates a sysfs interface under

/sys/class/soft_pwm

containing two file export & unexport. In order to create a PWM channel for a given gpio line, just echo the line number to the export file:

echo 4 > /sys/class/soft_pwm/export

This will create a PWM channel for gpio line 4. pwm-4 sysfs class should now be created in /sys/class/soft_pwm to control the channel. There are relevant files:

  • /sys/class/soft_pwm/pwm-4/frequency - to control the PWM frequency, by default 50Hz, i.e cycle period 20ms.
  • /sys/class/soft_pwm/pwm-4/period_ns - to control the PWM period in nano second, by default 20000000ns(20ms)
  • /sys/class/soft_pwm/pwm-4/duty_cycle_ns - to control the duty_cycle of the pulse in nano second, by default 1500000ns(1.5ms)
  • /sys/class/soft_pwm/pwm-4/duty_cycle - to control the duty_cycle of the pulse in percentage (0-100)

The default values should drive a connected RC servo to the position of 90 degree.

You can create as many channels as you want (as far as the system can handle it). All the channels are independent.

In order to delete a channel, simply pass the gpio number to the unexport file.