Skip to content

Software: PWM Control

Jeremy Tan edited this page Oct 31, 2013 · 5 revisions

Special mention to be made, to the unique way in which PWM control works on the BeagleBone. For a more general overview of pin control, see Software: Pin Access. The short version of enabling PWM access is observing how it's done in the run script, run.sh.

There are actually two methods of controlling PWM, accessible via two file paths. The type used for the server software is to use the sysfs option, because it gives a fixed path to the control. However, both methods are outlined below, as they are heavily interlinked, and in fact the sysfs option can't be enabled without understanding the other option.

Important note

The guide outlined below is for the process of controlling the PWM pins manually, through a shell interface. The server program itself has internalized all of this in bbb_pin.c.

Bone cape manager

The BBB makes use of a device tree with things called 'device tree overlays' to enable pins and the such like. To enable PWM, the PWM driver must first be enabled. To do this, what is termed the BeagleBone 'cape manager' is required. Highly recommended prior reading is this video by Derek Molloy, which although it covers only GPIO pins, the material is very similar. He also produced the header tables from which the simplified version is based on (although the simplified version was also mostly tested with a CRO).

The cape manager is just a file, accessible from:

/sys/devices/bone_capemgr.*/slots

Where the * is a number that can vary depending on the operating system installed. For example, it can be something like /sys/devices/bone_capemgr.9/slots.

Enabling the PWM driver

The PWM driver is called 'am33xx_pwm' and is enabled as such (must be root):

echo am33xx_pwm > /sys/devices/bone_capemgr.*/slots

This is also called a 'cape'. To see what capes are enabled, do:

cat /sys/devices/bone_capemgr.*/slots

You should see that am33xx_pwm is one of the entries listed.

Enabling the PWM pin

Each PWM pin must then be enabled individually in a similar manner. For example, to enable EHRPWM0A:

echo bone_pwm_P9_22 >  /sys/devices/bone_capemgr.*/slots

This will enable it, and create a folder, with a path similar to /sys/devices/ocp.3/pwm_test_P9_22.*, although again, the path is not fixed. It was found that the ocp number could change. Observing the folder:

cd /sys/devices/ocp.3/pwm_test_P9_22.15
ls
driver  duty  modalias  period  polarity  power  run  subsystem  uevent

Controlling the PWM pin

Within that folder are a number of files, labeled period, duty, etc. Reading these files tells you the current setting, while writing to them sets the value. E.g, to set:

echo 2000 > duty
echo 4000 > period
echo 1 > run

E.g, to read the current state:

cat run
cat duty
cat period

To turn it off:

echo 0 > run

PWM and sysfs

With sysfs, the PWM pins become accessible from a relatively fixed path; /sys/class/pwm/pwm0, for example. 8 PWMs can be enabled in total, from PWM0 to PWM7.

Enabling a PWM pin

All the steps listed above for enabling the PWM driver have to be done. What next follows a very peculiar process:

  1. The PWM pins must be 'exported'. There is a file named /sys/class/pwm/export. Export pins 0-7 by doing (as root):
cd /sys/class/pwm
echo 0 > export
echo 1 > export
...
echo 7 > export
  1. The relevant cape for the pin must then be installed by following 'Enabling the PWM pin' in the previous section. e.g
echo bone_pwm_P9_22 >  /sys/devices/bone_capemgr.*/slots

To enable PWM0.

This process must be done in this order; if at any time the cape is enabled before the pin has been exported, then this will fail, and the BeagleBone has to be restarted before trying again.

Controlling a PWM pin

When the pin is exported, a new folder is made for that pin with the files to control it. E.g, /sys/class/pwm/pwm0, the following files should be found:

device  duty_ns  period_ns  polarity  power  run  subsystem  uevent

The very same steps outlined above for the other method work the same for this step.

PWM number to pin number

Determining which PWM number (e.g PWM0) corresponded to which pin (e.g P9_22) was determined mostly through trial and error, as well as observing the contents of one file, /sys/kernel/debug/pwm.

In short, refer to the pin out diagram created, or run.sh for the correspondence.

Caveats

Enabling the PWM driver

It was found that the am33xx_pwm driver can only be enabled once. Trying to enable it again when it is already enabled will cause weird issues to occur. Rebooting the BeagleBone will fix this.

Period values are fixed between pins

The BeagleBone has a number of PWM driver modules, and the pins are usually paired to one such module. This means that every two pins (e.g EHRPWM0A - PWM0 and EHRPWM0B - PWM1) have to share the same period.

If both pins have been exported, and both pins have been enabled, the period is fixed at whichever was enabled last. The period can now never be changed, until you disable both and then unexport them. Still,there have been problems with this, and the easiest method if this doesn't work is to restart the BeagleBone.