PWM using the PRU instead of the PWM pins?

A while back I heard/read/saw/can’t remember where that you can use the PRU units to send PWM signals. Allowing for a lot more PWM pins.

I am getting used to the beagleboard, and I have gotten boxysean’s blink examples to work by using the magic script.
My question is has this been done/is there a library/code available?

Also can the PRU unit write to any pin? or only some of the pins?

Thanks for any help!

Its not so hard really. I assume you understand the concept of PWM, where you have a period of time and for that period of time the output pin is high for a certain percentage of it. Each instruction on the PRU executes in one clock cycle, with the exception of accessing devices outside of its memory map (i.e. most of the GPIO). I can write an example for you later today, although here is an example of using the PRU to time the ping of a sonar. (might help).


that explains what pins are hooked up directly to the PRUs


That helps a lot, also I noticed that in the “Beaglebone PRU connections and modes” table P9_31 is listed twice, i believe that the second one should be P9_29 based on this table:

PS: for the second table does anyone know what the colors refer too?

Do you have a login on the wiki? if not request one please so things like this can be fixed…



Just got approved :slight_smile:

Thanks for spotting and fixing this!


If you intend to use pwm for servo control you can try this:

You may need to update GPIO table to support your own board in pwm_drv_hw.c.
I use inverted logic (to reduce transistor count) so if you want normal logic you need to remove #define INVERTED_LOGIC from pru_pwm_sw.p. everything else should be in the readme.

The prus are there to wiggle pins in any way you want!

The two prus have a combined total of 22 gpio, that can be output, on the beaglebone header, so any of these could be used. Many are not available with the beaglebone black. See the “BeagleBone Capes” section at the circuitco wiki for details.

So, what kind of performance can you get from pru-gpio? The pru just runs a little asm program, so it depends on how few 200MHz clock ticks you can use.

For an example (each line below can be converted to one pru asm instruction):

  1. memory address = 0 (1 tick)

  2. load pin states from the memory address (2 ticks)

  3. set all pin states (1 tick)

  4. inc the memory address (1 tick)

  5. if the memory address < max address, repeat the loop, goto 2 (1 tick)

  6. need to restart pwm cycle, so goto 1 (1 tick)

So, with this, you could load the pru memory with an array of the pru-gpio pin states, and it would repeatedly cycle through them (up to max address states), giving you a pwm signal.

Since there are 5 ticks per pin-state-update loop in the code, and 5ns per tick, that would be 25ns control of the pwm. A 40kHz pwm signal takes 25us to repeat, so that would fit 25us/25ns = 1000 individual pin states, or nearly 10 bits of resolution, for 14 pwm signals (if you used pru1 and disabled hdmi).

The PRU package from ti has an interface that lets you access pru memory as a pointer/array, so the above would just involve updating an array to change any or all of the pwm patterns.

Also can the PRU unit write to any pin? or only some of the pins?

The pru has single tick access to the pru-gpio pins, and something like 3-5 tick (can’t remember exact range I saw) access to all non-privileged system memory. The system memory access varies because the cpu could be doing a read or write at the same time, causing a stall. Since the gpio registers are in non-privileged memory, the pru can be used to toggle regular gpio pins, but going through system memory will be much slower than a pru gpio pin, and put a load on the memory bus.

Please excuse any errors…I’m busy at work. :expressionless: