PWM: Difference between revisions

From ArmadeusWiki
Line 17: Line 17:


== Usage ==
== Usage ==
* Load the module:
* Load the driver:
<pre class="apf">
<pre class="apf">
# modprobe imx-pwm
# modprobe imx-pwm
Initializing PWM class.
i.MX PWM driver v0.8
  Initializing PWM#0...PWM major number = 251
imx-pwm imx-pwm.0: initialized
PWM driver v0.7
done.
</pre>
* If you plan to use sound, create ''/dev/pwm'' device file if not already existing (Major is dynamically allocated)
<pre class="apf">
# DEV=`cat /proc/devices | grep imx-pwm | cut -d " " -f 1`; mknod /dev/pwm c $DEV 0
</pre>
</pre>
* For the one who plan to use sound, ''/dev/pwm0'' device file should be automatically created.


=== PWM Mode ===
=== PWM Mode ===


<strong>Remark:</strong>
{{Note|The PWM has a resolution of 1uS}}
* The pwm has a resolution of 1uS


After driver loading, you have access to ''/sys/class/pwm/...'' functionnalities:
After driver loading, you have access to ''/sys/class/pwm/...'' functionalities:
* to change frequency (for example 500 Hz):
* to change frequency (for example 500 Hz):
<pre class="apf">
<pre class="apf">
  # echo 500 > /sys/class/pwm/pwm0/frequency
  # echo 500 > /sys/class/pwm/pwm0/frequency
</pre>
</pre>
* to change period (for example 100us):
* if you prefer working with periods (for example 100us):
<pre class="apf">
<pre class="apf">
  # echo 100 > /sys/class/pwm/pwm0/period
  # echo 100 > /sys/class/pwm/pwm0/period
Line 103: Line 97:
=== Audio Mode ===
=== Audio Mode ===
Audio playback mode is working since v0.5 of PWM driver (thanks to [[User:SebastienR|SebastienR]]).
Audio playback mode is working since v0.5 of PWM driver (thanks to [[User:SebastienR|SebastienR]]).
You can play 8, 16 & 32 KHz 8bit linear RAW sound. It's recommended to play 8kHz one, as they use less CPU ressources.
You can play 8, 16 & 32 KHz 8bit linear RAW sound. It's recommended to play 8kHz one, as they are using less CPU resources.
To convert a sound to the right format we recommend to use SoX (on Linux).
To convert a sound to the right format we recommend to use SoX (on Linux).
For exemple:
For example:
  [host]$ sox /usr/share/sounds/KDE_Chimes_1.ogg -r 8192 -b -u -c 1 Chimes.raw
  [host]$ sox /usr/share/sounds/KDE_Chimes_1.ogg -r 8192 -b -u -c 1 Chimes.raw
  -r -> sampling rate
  -r -> sampling rate

Revision as of 13:02, 18 March 2010

On this page, you will find usefull informations to configure and use the Pulse Width Modulation (PWM) capabilities of your APF9328/APF27 boards.

The i.MX processor is capable of driving 2 PWM signals:

  • one is linked to the LCD controller and allow to control the LCD backlight (fixed frequency)
  • the other can be used to generate a PWM signal in several mode:
    • audio playback
    • real PWM

Hardware

i.MX PWM module is using PWMO (pin 2 of PortA on APF9328 and pin 5 of PortE on the APF27) as output.

Linux driver installation

i.MX PWM driver is installed by default in standard Armadeus rootfs.

Usage

  • Load the driver:
# modprobe imx-pwm
i.MX PWM driver v0.8
imx-pwm imx-pwm.0: initialized
  • For the one who plan to use sound, /dev/pwm0 device file should be automatically created.

PWM Mode

Note Note: The PWM has a resolution of 1uS


After driver loading, you have access to /sys/class/pwm/... functionalities:

  • to change frequency (for example 500 Hz):
 # echo 500 > /sys/class/pwm/pwm0/frequency
  • if you prefer working with periods (for example 100us):
 # echo 100 > /sys/class/pwm/pwm0/period
  • to change duty cycle (for example 50.0%)
 # echo 500 > /sys/class/pwm/pwm0/duty
  • to activate PWM:
 # echo 1 > /sys/class/pwm/pwm0/active
  • to de-activate PWM:
 # echo 0 > /sys/class/pwm/pwm0/active

Bash test script

  • A Bash test script is available given below. Just enter the desired frequency and duty cycle.
#!/bin/sh
#
# script to command imx pwm 
# usage : pwm_imx frequency duty 
#

if [ $# -lt 2 ] 
then
echo "Provide two arguments to this script !"
echo "pwm_imx frequency dutycycle"
exit 1
fi

SYS_DIR="/sys/class/pwm/pwm0/"
FREQUENCY=$1
DUTY=$2

DUTY=`(echo $DUTY | awk '{ print $1*10}')`

if [ ! -d "$SYS_DIR" ]; then
echo "Can't find /sys/ interface for PWM"
exit 1
fi

echo "Starting PWM"
echo 1 > $SYS_DIR/active

#SET FREQUENCY
echo $FREQUENCY > $SYS_DIR/frequency
echo "Setting PWM to $FREQUENCY Hz"

#SET DUTY CYCLE
echo $DUTY > $SYS_DIR/duty
echo "Setting DUTY to $2 %"

exit 0


Audio Mode

Audio playback mode is working since v0.5 of PWM driver (thanks to SebastienR). You can play 8, 16 & 32 KHz 8bit linear RAW sound. It's recommended to play 8kHz one, as they are using less CPU resources. To convert a sound to the right format we recommend to use SoX (on Linux). For example:

[host]$ sox /usr/share/sounds/KDE_Chimes_1.ogg -r 8192 -b -u -c 1 Chimes.raw
-r -> sampling rate
-b -> byte/8bit signal
-u -> linear /not compressed
-c 1 -> Mono / 1 channel

Then you can test it using target/linux/modules/pwm/test/testplaymode, which can be compiled that way:

 $ make shell_env
 $ . armadeus_env.sh
 $ export PATH=$PATH:$ARMADEUS_TOOLCHAIN_PATH
 $ make -C target/linux/modules/pwm/test/
 $ cp target/linux/modules/pwm/test/testplaymode /tftpboot/
 $ cp target/linux/modules/pwm/test/KDE_Startup_2.raw /tftpboot/

on target do:

 # testplaymode

or

 # testplaymode KDE_Startup_2.raw

Going further

If you need more than one PWM at a time, you can use the APF's FPGA with the following project.

Links