Skip to content

Instantly share code, notes, and snippets.

@tomtzook
Last active February 12, 2024 01:20
Show Gist options
  • Save tomtzook/7a9bcccc5a1341def0ff7cab969d12dd to your computer and use it in GitHub Desktop.
Save tomtzook/7a9bcccc5a1341def0ff7cab969d12dd to your computer and use it in GitHub Desktop.
Compile c++ program for Beaglebone Black PRU with CMake

To run you need to have the pru compiler and pru software support package downloaded.

When running cmake, use the following flags:

-DCMAKE_EXE_LINKER_FLAGS="-iti-cgt-pru_2.3.3/lib AM335x_PRU.cmd"
-DPRU_COMPILER_HOME=ti-cgt-pru_2.3.3
-DPRU_SOFTPKG_HOME=pru-software-support-package-5.9.0

Modify according to your local paths.

Use custom-compiler-clpru.yaml to configure CLion with the pru compiler. See this.

AM335x_PRU.cmd is a linker file provided by TI and can be found in the software package.

cmake_minimum_required(VERSION 3.16)
project(bbb_pru C CXX)
set(STACK_SIZE 0x100)
set(HEAP_SIZE 0x100)
set(CMAKE_C_COMPILER ${PRU_COMPILER_HOME}/bin/clpru)
set(CMAKE_CXX_COMPILER ${PRU_COMPILER_HOME}/bin/clpru)
set(CMAKE_LINKER ${PRU_COMPILER_HOME}/bin/lnkpru)
set(CMAKE_AR ${PRU_COMPILER_HOME}/bin/arpru)
set(CMAKE_FIND_ROOT_PATH ${PRU_COMPILER_HOME})
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -fe <OBJECT> <SOURCE>")
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES> <OBJECTS>")
set(CMAKE_C_COMPILER_ABI ELF)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C99_EXTENSION_COMPILE_OPTION --c99)
unset(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
unset(CMAKE_C_IMPLICIT_LINK_DIRECTORIES)
unset(CMAKE_C_IMPLICIT_LINK_LIBRARIES)
set(CMAKE_CXX_COMPILER_ABI ELF)
unset(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
unset(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES)
unset(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES)
include_directories(${PRU_SOFTPKG_HOME}/include)
include_directories(${PRU_SOFTPKG_HOME}/include/am335x)
add_executable(pru_program main.cpp)
target_compile_options(pru_program PRIVATE
--include_path=${CMAKE_CURRENT_SOURCE_DIR}/include
--include_path=${PRU_COMPILER_HOME}/include
--include_path=${PRU_SOFTPKG_HOME}/include
--include_path=${PRU_SOFTPKG_HOME}/include/am335x
-v3
-O2
--display_error_number
-ppd
-ppa
--asm_listing
--c_src_interlist
--endian=little
--hardware_mac=on
--obj_directory=${CMAKE_CURRENT_BINARY_DIR}
--pp_directory=${CMAKE_CURRENT_BINARY_DIR}
--c++03
--float_operations_allowed=none
)
target_link_options(pru_program PRIVATE
-i${PRU_COMPILER_HOME}/lib
-i${PRU_COMPILER_HOME}/include
--reread_libs
--warn_sections
--stack_size=${STACK_SIZE}
--heap_size=${HEAP_SIZE}
-m ${CMAKE_CURRENT_BINARY_DIR}/pru_program.map)
target_link_libraries(pru_program PRIVATE
--library=libc.a
--library=${PRU_SOFTPKG_HOME}/lib/rpmsg_lib.lib)
set_target_properties(pru_program
PROPERTIES
SUFFIX ".out"
)
add_custom_target(pru_program_arm_bin
COMMAND "${PRU_COMPILER_HOME}/bin/hexpru"
"${PRU_COMPILER_HOME}/bin.cmd"
"pru_program.out"
DEPENDS pru_test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
compilers:
- description: TI PRU
match-sources: ".*\\.c"
match-language: C
match-compiler-exe: "(.*/)?clpru(\\.exe)?"
code-insight-target-name: avr
include-dirs: ${compiler-exe-dir}/../include
defines:
_DATA_ACCESS:
defines-text: "
#define __PRU__
#define __SIZE_T_TYPE__ unsigned
#define __PTRDIFF_T_TYPE__ int
#define __far
"
- description: CPP TI PRU
match-sources: ".*\\.cpp"
match-language: CPP
match-compiler-exe: "(.*/)?clpru(\\.exe)?"
code-insight-target-name: avr
include-dirs: ["${compiler-exe-dir}/../include", "${compiler-exe-dir}/../include/stlport"]
defines:
_DATA_ACCESS:
defines-text: "
#define __PRU__
#define __STDC__ 1
#define __STDC_HOSTED__ 1
#define __cplusplus 201402L
#define __SIZE_T_TYPE__ unsigned
#define __PTRDIFF_T_TYPE__ int
#define __far
"
int main() {
return 0;
}
#pragma once
#include <stddef.h>
#include <rsc_types.h>
struct my_resource_table {
struct resource_table base;
uint32_t offset[1]; /* Should match 'num' in actual definition */
};
#pragma DATA_SECTION(".resource_table")
#pragma RETAIN
struct my_resource_table pru_remoteproc_ResourceTable = {
1, /* we're the first version that implements this */
0, /* number of entries in the table */
0, 0, /* reserved, must be zero */
0, /* offset[0] */
};
@LukeDeWaal
Copy link

Very useful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment