EBC Exercise 13 Pulse Width Modulation

From eLinux.org
Revision as of 11:51, 20 July 2012 by Yoder (Talk | contribs) (Added bone instructions)

Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder

In a previous exercise (EBC Exercise 03 gpio Polling and Interrupts) you saw how to use the gpio to produce a square wave out using a C program and sysfs. I was able to get a 1.5kHz square wave out; however we can do much better using some built in hardware on the Beagle.

In this exercise we will learn how to use the Beagle's pulse width modulation (pwm) hardware by writing directly to the registers that control it and also learn about pin multiplexing (pin mux) on the way.

PWM on the Bone

AM335x PWM Driver's Guide

Look in cloud9/bonescript/index.js to see how to do pinMuxing via /sys/kernel/debug/omap_mux/

PWM on the xM

PWM and pin MUXing

The DM3730 has 11 general purpose timers, 4 of which (gpt8-gpt11) can be brought out of the chip and used for pulse width modulation (DM3730 TRM page 2689). The problem is the DM3730 has more internal lines than hardware I/O pins. The solution is that I/O pins run though a MUX that selects which internal lines appear on I/O pins. A given pin can have one from as many as eight lines assigned to it.

These MUXes are set at boot time, and must be set when the kernel boots, or in u-boot. I couldn't set them during kernel boot with the 2.6.32 kernel, so I used u-boot. BeagleBoardPinMux is a good place to learn about the pin MUXing. The u-boot details are here.

BeagleBoardPWM is a nice overview of how to do PWM on the Beagle. The version of the kernel and u-boot that I've given you should already be configured to access the PWM pins. If it isn't you'll have to recompile the Kernel and u-boot.

The standard way to interface with the outside world in Linux is through Kernel Drivers. Currently there are no standard PWM driver for the Beagle, though a couple have been proposed ([1], [2] and [3]). BeagleBoardPWM takes a more traditional MCU approach by accessing the memory mapped PWD registers directly using mmap in a C program. Although this approach works, it is really transitional until a standard can be established.

You could even do PWM from a shell command by using devmem2 to write to the memory mapped registers from a command line.

Here's another PWM lead.


If your git repository is set up just:

beagle$ cd exercises
beagle$ git pull
beagle$ cd pwm

(Follow the instructions here if you aren't set up for git.)

  1. Look at the files to see what they are doing.
  2. Run make, then pwd-demo.
  3. Hook up a oscilloscope. (See Table 22 of the Beagle System Reference manual to see where to probe.) Are the pwd outputs doing what you expected?
  4. What's the highest frequency you can generate? What's the lowest?
  5. Create a new C program, based on pwm-demo, that takes 3 parameters, the <pwm to use>, <frequency> and <duty cycle>.
  6. Create a shell file that will call your new program and set up the three pwm's that appear on the expansion header and program them to do something interesting.
  7. Write a shell file that will do the pin MUXing using devmem2.
  8. Rewrite pwm-demo as a shell file that uses devmem2.


  1. BeagleBoardPWM from ECE597
  2. BeagleBoard/GSoC/2010_Projects/Pulse_Width_Modulation Google SoC project
  3. BeagleBoardPinMux, how to set the pin mux.
  4. Buttons and PWM
  5. Shaky PWMs
  6. PWM on the bone

thumb‎ Embedded Linux Class by Mark A. Yoder