BeagleBone Black programmable realtime unit (PRU) Hello World – Part 1

Introduction

This part covers the device tree overlay for our little Hello-World Example. I will not cover linux device trees here or why they are useful, because other articles in the interweb already contain great articles about this topic. I will give you some links at the end of this page.

The overlay source-code

So this part will be pretty short. I will just throw some code at you and explain what it does. Once you have read at least one of the articles linked on the bottom of this page, you should understand what it does, even without comments (Yes it’s that simple).

/dts-v1/;
/plugin/;

/ {
 /* This overlay is compatible with the following devices */
 compatible = "ti,beaglebone", "ti,beaglebone-black";

 /* overlay identification */
 part-number = "BB-WAVE-GEN";
 version = "00A0";

 /* Define the ressources used by this overlay */
 exclusive-use = "P9.27", "pru0";

 fragment@0{
 /* target in the device tree */
 target = <&am33xx_pinmux>;
 
 /* The overlay we define */
 __overlay__{
     /* Change the GPIO-Pin mode according to the pinmuxing table */
     pru_pru_pins: pinmux_pru_pru_pins{
         /* The first value (0x1a4) is the offset of the pin's address */
         /* Value two is the pin-multiplexing (pinmux) mode, we want */
         /* 25 is hex for mode 5, output, pull-up enabled */
         /* See the pinmux table (very bottom) */
         pinctrl-single,pins = < 0x1a4 0x25 >;
         };
     };
 };

 fragment@2{
     target = <&pruss>;
     __overlay__{
         status = "okay";

         pinctrl-names = "default";
         pinctrl-0 = <&pru_pru_pins>;
         };
     };
};

The Pinmuxing (Pin-Multiplexing) tables are available for download here.

Compiling the code

After you have copied the above code to your beaglebone black (save it as BB-WAVE-GEN-00A0.dts), you’ll have to compile it. To do so, use the following bash command:

dtc -I dts -O dtb -o BB-WAVE-GEN-00A0.dtbo -@ BB-WAVE-GEN-00A0.dts

After the compilation finished (with no errors), you’ll see a newly generated dtbo file. Copy that one to /lib/firmware and change into the firmware directory.

Loading the overlay

Now you have to load the overlay, so that the system can change your hardware accordingly. To load the overlay, you just have to echo it into the slots file, which is located at:

/sys/devices/bone_capemgr.*/

The “*” is a wildcard. It is there, because the number it represents changes from system to system. If you leave the “*” where it is, the system will extend the correct number for you. So to load your overlay, you have to do the following in bash:

echo ./BB-WAVE-GEN:00A0.dtbo > /sys/devices/bone_capemgr.*/slots

If there is no output after you entered the above command, the overlay was loaded successfully. If there is, check the error message with dmesg and solve eventual problems (feel free to write a command if you don’t know how). If you catentate the contents of this slots-file, you can verify if your overlay was loaded correctly, or not:

Bildschirmfoto 2016-05-23 um 20.39.32

As you can see, the last line (overlay #6) shows our loaded overlay. You can unload this overlay by echoing “-6” into the slots file.

Further readings

A very nice guide about device tree overlays (Adafruit)
About the theory behind device trees and the syntax
Official devicetree.org webpage
Very well written article about gpios and device trees on the bbb with discussions
About the PRU Sub System

Table of contents

Part 0 – Introduction
Part 1 – About device trees and overlays (You are here)
Part 2 – Programming

comment-banner

Advertisements

27 thoughts on “BeagleBone Black programmable realtime unit (PRU) Hello World – Part 1

  1. Hi, it was a great tutotrial to start with but I’m stuck with the final proceedure where you run the binaries generated by pasm assembler code for the code you have written. I’m expereincing the follwing issue when I run this-

    ./pwm or sudo ./pwm
    Segmentation fault

    Im really struggling to cope up with other tutorials with one or the other dead ends. Can you please help me with this?

    Thank you.

    Like

    1. Hello!
      Unfortunately the segmentation fault error can mean a lot of things in this context and it’s usually the only error that I get when programming the PRU, regardless of what goes wrong.

      But usually it either means that you try to access an invalid address, which we don’t in this example, or that the overlay was not loaded properly or the PRU program did not end the last time and the system is now unable to halt it, or the PRU code wrong.

      So when I get this error, I usually check, if I already loaded the necessary overlay:

      echo ./BB-WAVE-GEN:00A0.dtbo > /sys/devices/bone_capemgr.*/slots

      and if it shows up, I restart the system and then I reload the overlay right after the reboot.

      Just one quick thing to note: If you change the PRU code, so that it uses different GPIO pins or devices, you will also have to change the device-tree overlay.

      Hope this helps so far, feel free to let me know or to write me an email if you need further help (see the contact page for the address)!

      Like

      1. HI, Thanks for making time to reply. I did figure a way out thanks for your help. Turns out that the proceedure quoted above worked for me when i downgraded to kernel version 3.8 (from 4.9). To add to this –

        1.does the device tree binary or .dtbs varies for different kernel versions ?

        It may be insesitive of me to ask this but DTC version is all that metters right ?(for generation of .dtbs from .dts) Having asked this since the syntax of some .dts file changed in lower version of kerneal compared to that of one I used I’m bit confuded about the working nature of DTC(DEVICE TREE COMPILER).

        Loads of thanks.

        Liked by 1 person

      2. Good to hear, that it’s working now! You have an interesting point here. I personally never thought that it makes a difference, what Kernel version one uses. I’d also say, that the compiler version is the only thing that matters, but I have to admit that I don’t know exactly, but I will look into this at some point, because this is something I haven’t thought about so far. But from what I’ve read so far, the newer kernel versions seem to occupy some other GPIOs that then can not be used which then leads to a segmentation fault. But huge thanks for pointing this issue out, I’ll have to further investigate this and I might update the articles according to what I find out!

        Like

  2. I really like what you guys tend to be up too. This type of clever work and coverage! Keep up the terrific works guys I’ve added you guys to our blogroll.

    Liked by 1 person

  3. Your style is very unique in comparison to other folks I have read stuff from.
    Many thanks for posting when you’ve got the opportunity, Guess I’ll just
    bookmark this page.

    Like

  4. Pretty nice post. I simply stumbled upon your
    weblog and wished to mention that I have really enjoyed surfing around your
    weblog posts. In any case I will be subscribing for your feed and I hope you write once
    more soon!

    Liked by 1 person

  5. I don’t know whether it’s just me or if everybody else encountering problems
    with your site. It appears as though some of the written text within your content are running off the screen. Can somebody else
    please comment and let me know if this is happening to them as well?
    This may be a issue with my internet browser because I’ve had this happen before.

    Like

Leave your two cents, comment here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s