PRU firmware can be written in C or assembly. Firmware written in C can be compiled by the TI ‘clpru’ compiler which is part of the ‘ti-cgt-pru’ package. Firmware written in assembly can be compiled by the ‘pasm’ tool which is part of ‘am335x-pru-package’. When using the Buildroot system, these packages are integrated into menuconfig, so enabling their options will install them as host tools. After installing the compiler, it can be used to generate PRU firmware images from source code. The following is a minimal example of C source code for the PRU co-processor:
pru0.c
#include <stdint.h> #include <rsc_types.h> /* struct resource_table */
#define CYCLES_PER_SECOND 200000000 /* 200MHz clock */ #define PR1_PRU0_PRU_R30_14 (1 << 14) /* Pin to change (GPIO1_12) */
volatile register uint32_t __R30; /* GPIO output register */
void main(void) { while (1) { /* Switch the GPIO with one cycle every millisecond. This can be * verified with an oscilloscope. */ __R30 |= PR1_PRU0_PRU_R30_14; __delay_cycles(CYCLES_PER_SECOND / 2000); __R30 &= ~PR1_PRU0_PRU_R30_14; __delay_cycles(CYCLES_PER_SECOND / 2000); } }
/* Required by the Linux remoteproc driver. This is an empty resource table * which is good enough if no resources are required. */ #pragma DATA_SECTION(my_resource_table, “.resource_table”) #pragma RETAIN(my_resource_table) struct resource_table my_resource_table = {1, 0, 0, 0}; |
This example will switch the AM3358 pin for GPIO1_12 on and off with a cycle period of 1KHz. An oscilloscope can be used to verify that this works. Building the firmware can be done using the following commands:
build.sh
#!/bin/sh
BR_DIR=/buildroot HOST_DIR=$BR_DIR/output/host PATH=$PATH:$HOST_DIR/bin:$HOST_DIR/share/ti-cgt-pru/bin PRU_TOOLS=$HOST_DIR/share/ti-cgt-pru PRU_INC=“-I$PRU_TOOLS/include -I$PRU_TOOLS/usr/include -I$PRU_TOOLS/usr/include/am335x” PRU_LIB=“-i$PRU_TOOLS/lib”
clpru $PRU_INC -c pru0.c lnkpru $PRU_LIB AM335x_PRU.cmd pru0.obj -o am335x-pru0-fw |
These commands first setup paths for the compiler. In this case, the compiler is installed as a host tool in a Buildroot system. After the paths are setup, the ‘clpru’ command compiles the source, and the ‘lnkpru’ command links it. The result is the file ‘am335x-pru0-fw’, which is the default firmware name for PRU0. The link command requires the file ‘AM335x_PRU.cmd’. This file is part of the ‘pru-software-support’ package, or it can be found online.