Andrew B. Wright, S. M. ’88, Ph. D.
You should create a subdirectory under your $(HOME) called hello (e.g., /root/hello) and put your code files and the Makefile in that directory. You will have to create subdirectories pru0 and pru1 in your code directory for this example to work.
You should set the variable, CODE_ROOT, in the Makefile to the directory containing the code. The archive (below) is set for /root/code/hello/hello, which may not be desirable in all cases.
The code, including the Makefile, is packed up in this little archive.
Download the archive to a directory (/root/code/hello if you want the code to work out of the box) and unpack it using the command “tar -xzf ‘archive name’ “. This will create an extra directory /root/code/hello/hello in which the files will be located. The archive should create the pru0 and pru1 subdirectories as well. If you go to the directory, typing “make hello” and “make install” will create the executable files. Typing “./hello” should give you the output (100 iterations of “Hello, world!” echoed back from PRU1).
There were some big changes in the latest image after November when I started this. Type uname -a. I’m updating this for
4.4.30-ti-r64 4.4.54-ti-r93 4.9.82-ti-r102.
With the latest beaglebone debian kernel, the method of communicating with the pru is the remoteproc system. There is an overview of the structure here.
Here’s the debian project page, from which you can link to the source code. Carry on from here and you get to the kernel source. Which is already at
4.4.41 (1/15/2017) 4.4.68 (5/29/2017) 4.9.82 (5/26/2018)!!!! I’m not sure how to take this, but, it seems that development is moving forward at hyperspeed. If you look in the drivers sub-directory, you find a folder remoteproc and another folder rpmsg. These are the pieces of the kernel that interact with the pru. In remoteproc, you find a few files (pru_rproc.c, pru_rproc.h, pruss_intc.c, pruss.c, pruss.h) that directly interact with the pru. The good news is that this development is yielding an easier to understand, easier to use remoteproc system. When this is all done, this will be a great system.
If you perform a lsmod, you should see the following drivers running (pru_rproc, pruss_intc, pruss, rpmsg_pru, rpmsg_core, virtio_rpmsg_bus). These are the drivers which implement ARM:PRU communication.
Important variables that you need to know:
maximum number of messages (32)
size of vring (512)
The hand’s on training from TI does have some useful information in conjunction with the code in
/opt/source/… /usr/lib/ti/pru-software-support-package. And, while the labs are useful, the place to dig is the “examples/am335x” directory. This contains completed code that can be tested and adapted quickly. The programs that follow in this blog are adapted from the code in PRU_Halt, PRU_RPMsg_Echo_Interrupt0, PRU_RPMsg_Echo_Interrupt1. If those files are not present, then “apt-get install pru-software-support-package”.
Other functions that look interesting for exploration are PRU_Direct_Connect0, PRU_Direct_Connect1, PruArm_to_Pru_Interrupt, and Pru_toPruArm_Interrupt. This should give a full suite of Arm-Pru0-Pru1 interconnects. With that enabled, debugging and interaction across all devices should be possible.
In the latest kernel, there is an entry in /sys/class called remoteproc which allows the pru to be stopped and started by writing to the file /sys/class/remoteproc/remoteprocX/state, where X=1 will talk to PRU0 and X=2 will talk to PRU1.
The removes the need to load/unload the rpmsg module (which was a horrible hack FWIW).
The Makefile in the examples gives a hint at how to compile for the PRU. Here is a stripped down version for the “Hello, world!” example given in the examples. I have set it up so that both pru0 and pru1 are involved, so adaptions can be made relative to either unit.
On the latest debian, the PRU compiler, clpru, is installed by default. The compiler is located in /usr/bin. The libc-dev header files are located in /usr/include/arm-linux-gnueabihf/. The header files for rpmsg are located in
/usr/share/ti/cgt-pru/include /usr/lib/ti/pru-software-support-package/include/. the only thing that seems to be needed in arm-linux-gnueabihf are the stdint cdefs specific to the arm.
Communication with the remoteproc kernel driver from the pru requires that pru-code is linked with the library, rpmsg_lib.lib. The header files are located in /usr/lib/ti/pru-software-support-package/include/.
The structure of communications using INTC can be seen in the Technical Reference Manual, Figure 4-17.
PRU0 is connected to Host-0 using R31 bit 30.
PRU1 is connected to Host-1 using R31 bit 31.
System events (SYS_EVT) are connected to one of the ten channels (0..9). This channel connection is done through the resource table.
Figure 4-21 demonstrates the Interrupt number and connection to a pin. Interrupt number 16, 17, 18, and 19 are connected to signals pr1_pru_mst_intr_intr_req, pr1_pru_mst_intr_intr_req, pr1_pru_mst_intr_intr_req, and pr1_pru_mst_intr_intr_req.
So, the numbers for TO_ARM_HOST and FROM_ARM_HOST correspond to these interrupts and pins. When the INTC controller is initialized, the information in the resource_table is loaded into the registers in the INTC controller that allow it to establish the appropriate interrupt connections.
The registers where this is stored are Channel Map Registers (CMR0, CMR1, CMR2 ,… CMR15) and Host Map Registers (HRM0, HRM1, HRM2) in the PRU_ICSS_INTC (see Table 4-102 of the TRM). If you’re using the remoteproc driver, you really, really, really do not want to write anything to these registers.
As an aside, the PRU is not interruptible. This is part of its charm. So, the INTC ‘interrupt’ is not really an interrupt from the PRU side of things. In order to see if the ARM has placed information in the buffer, the PRU has to know to check. To tell the PRU that it’s done something, the ARM sets a bit in R31. The PRU has to poll this bit to determine if something has happened.
This allows the PRU to run in a very deterministic manner and provide an extremely repeatable timed loop.
Unless you code an infinite loop (see below) …
PRU side responses (from pru_rpmsg):
PRU_RPMSG_INVALID_EVENT (pru_rpmsg_init) – means that to_arm_event or from_arm_event is not within MIN_VALID_EVENT or MAX_VALID_EVENT
PRU_RPMSG_SUCCESS (pru_rpmsg_init, pru_rpmsg_send, pru_rpmsg_receive) – function returned successfully
PRU_RPMSG_NO_BUF_AVAILABLE (pru_rpmsg_send, pru_rpmsg_receive) – response from pru_virtqueue_get_avail_buf was less than zero, which occurs if vq->last_avail_idx == avail->idx.
PRU_RPMSG_BUF_TOO_SMALL (pru_rpmsg_send) – the size of the data + header is greater than RPMSG_BUF_SIZE (512)
PRU_RPMSG_INVALID_HEAD (pru_rpmsg_send, pru_rpmsg_receive) – the result of pru_virtqueue_add_used_buf was less than zero, which occurs if head > vq->vring.num.
PRU_RPMSG_NO_KICK (pru_virtqueue_kick) means that no kick was sent (the VRING_AVAIL_F_NO_INTERRUPT bit is set in vq->vring.avail->flags).
ARM side responses (available through dev_err -> dmesg) obtained from kernel source rpmsg_pru.c driver code
Message length table is full – MAX_FIFO_MSG (32) has been exceeded
Unable to allocate fifo for the rpmsg_pru device – MAX_FIFO_MSG * FIFO_MSG_SIZE bytes are not available in rpmsg_pru_probe
Device already open – the PRU is locked (mutex_lock)
Data too large for RPMsg Buffer (rpmsg_pru_write) – same as PRU_RPMSG_BUF_TOO_SMALL, but from the ARM side
Error copying buffer from user space – not sure what gives rise to this, but, it’s not good.
rpmsg_send failed – what it says. Not sure where rpmsg_send is defined or why it would fail.
Failed to get a minor number for the rpmsg_pru device
Unable to add cdev for the rpmsg_pru device
Unable to create the rpmsg_pru device
In the Makefile, the compiler directive, -D PRU0 and -D PRU1, works with the #ifdef statements in the resource_table file to provide the different sys_evt -> channel -> host definitions required by rpmsg_pru30 and rpmsg_pru31 devices.
There are two places in main.c that would be undesirable in an embedded, real-time application. They’re fine in a test like this.
while (!(*status & VIRTIO_CONFIG_S_DRIVER_OK));
could lock up the PRU forever, waiting until a system resource becomes available. It would be better to check this status and then make sure that all the PRU controlled resources remain in a safe holding pattern state. Imagine if the PRU were controlling motors. Ideally, you would want the system to make sure those motors were in the ‘safe’ state while awaiting the ARM to PRU communication channel to start up, and to keep making sure they stayed in that state.
The second line is
while (pru_rpmsg_channel(RPMSG_NS_CREATE, &transport, CHAN_NAME, CHAN_DESC, CHAN_PORT) != PRU_RPMSG_SUCCESS);
The problem is the same as with the first line; however, this one could really stall for a while if all the buffers had been taken by another process.
There are two instructions in the linker command file, hello.cmd, and in the header files, pru_cfg.h and pru_intc.h, which interact with the linker allocations and symbol definitions. The header files are located in /usr/lib/ti/pru-software-support-package/include.
PRU_INTC : org = 0x00020000 len = 0x00001504 CREGISTER=0
volatile __far pruIntc CT_INTC __attribute__((cregister(“PRU_INTC”, far), peripheral));
PRU_CFG : org = 0x00026000 len = 0x00000044 CREGISTER=4
volatile __far pruCfg CT_CFG __attribute__((cregister(“PRU_CFG”, near), peripheral));
The statement, cregister (“PRU_INTC” … , causes the linker to look for a symbol definition, PRU_INTC … CREGISTER=0. There is a section on cregisters in the Technical Reference Manual which documents all the PRU registers. Access is faster using this method. The __far keywords in this example make sure that 32 bit pointers are used.
The volatile and peripheral keywords are also necessary. From the PRU C/C++ Language Implementation Guide:
“Any variable which might be modified by something external to the obvious control flow of the program must be declared volatile. This tells the compiler that a function might modify the value at any time, so the compiler should not perform optimizations which will change the number or order of accesses of that variable. This is the primary purpose of the volatile keyword.”
“The peripheral attribute can only be used with the cregister attribute and has two effects. First, it puts the object in a section that will not be loaded onto the device. This prevents initialization of the peripheral at runtime. Second, it allows the same object to be defined in multiple source files without a linker error. The intent is that peripherals can be completely described in a header file.”
The near and far keywords in the cregister statements mean that the compiler will use either 16 bit or 32 bit offsets relative to the cregister.
Clearing STANDBY_INIT in pruCfg initializes the OCP controller, which is necessary for communicating with L3 and L4 interconnects.
The ARM-side code to communicate with the PRU is called hello.c.
Once the executable file has been compiled, the executable must be copied to the /lib/firmware directory.
The name of the firmware (for instance, am335x-pru0-fw) must be placed into /sys/class/remoteproc/remoteprocX/firmware (X is either 1 or 2 depending on whether the processor is pru0 or pru1) file.
The convention is to use the names am335x-pru0-fw and am335x-pru1-fw, although those names are no longer mandatory.
Remoteproc will load those files into the appropriate pru (0 or 1) when you send ‘start’ to /sys/class/remoteproc/remoteprocX/state.
In the latest debian, remoteproc should be enabled by default. Check by running:
lsmod | grep pru
If pru_rproc shows up, then remoteproc is actively communicating with the pru.
In order to load new code, the module has to be removed and reinserted. Although this is awkward in development, it makes sense for production where this would happen at boot time. rmmod -f pru_rproc will remove the module. modprobe pru_rproc will load the firmware into the appropriate pru.
Run ‘dmesg’ to see if everything went smoothly.
The Makefile and hello.c have all of these steps automated. Once a program has been compiled, type make install and the steps will be executed in sequence.
What follows below the line is old stuff that is increasingly out of date.
The “Hello, world!” code is called main0.c. The resource table is contained in a header file, resource_table0.h.
The resource table (resource_table0.h) is:
The linker command file, HELLO.cmd, defines the location of resources:
The code for pru1 is called main1.c (adapted from TI’s code):
and its resource table is located in resource_table1.h.
The code to listen to the pru is called hello.c (adapted from TI’s rpmsg_pru_user_space_echo.c) and runs on the arm processor.
Compile all three files using the Makefile:
Run (from the arm command line) using
You should see “Hello, world from pru0!” and “Hello, world from pru1!” at the command prompt.
Something to investigate further related to assigning pins (related to device tree overlay) is the utility:
config-pin P9.30 pruout
Another useful linux command … grep -r “pattern” … lets you see pattern within any files in the current directory.
A potentially useful command … dtc -I
Just trying out something new.
Look to see if you have /sys/kernel/debug.
If not, try “mount -t debugfs none /sys/kernel/debug”.
There is a subdirectory, remoteproc,
More importantly, there is dynamic_debug/control, which allows you to control the info that is dumped from a driver on load.
For instance, check “cat /sys/kernel/debug/dynamic_debug/control | grep pru_rproc” to see what is being dumped by rproc.
These messages appear in the dmesg ringbuffer and can be viewed by typing “dmesg.”
In order to view a clean set of messages after an operation, type “dmesg –clear” to clear the ringbuffer and then run the command (e.g. modprobe pru_rproc). Then, dmesg will show only the messages that were logged after that command.
For pru_rproc, the function pru_rproc_probe has a few dev_dbg statements:
For instance, to add the dev_dbg circa line 786, type “echo ‘file pru_rproc.c line 786 +p’ > /sys/kernel/debug/dynamic_debug/control”
To get the dynamic info, need to invoke “modprobe pru_rproc dyndbg==p” where the p is the same as the p in the line in control above.
There are other flags (plftm).
The following is a bizarre twist that occurred when TI was switching between pruss and rproc. As of the latest kernel, it appears that both options coexist without the need for this kludge.
Earlier implementation of the remoteproc PRU-Arm interface used the sys_mailbox. The current implementation uses INTC interrupt flags directly to pass events back-and-forth.
Here’s a document from another person who is trying to keep track of the remoteproc updating. This has a couple of extra steps from the standard internet descriptions that you need to do before getting things to work.
If pru_rproc doesn’t show up (as it won’t in 4.4 and later), you probably need to activate it. (NOT TESTED) Here’s a page with some instructions on enabling rproc. Change to /opt/source/dtb-4.4-ti. Presumably, you want the version of dtb that goes along with your version. Edit src/arm/arm335x-boneblack-emmc-overlay.dtb and uncomment the line /*#include “am33xx-pruss-rproc.dtsi”*/. From the command line, make, make install. Blacklist uio_pruss by echo “blacklist uio_pruss” > /etc/modprobe.d/pruss-blacklist.conf Reboot and try again. I had to manually run “modprobe pru_rproc” to get the pru_rproc working the first time. There is almost certainly a way to get it to run automatically on boot. As of right now, I can get pru_rproc to load, but it is not automatically loading the fw for the pru. More to come.