3. Creating a Simple Application with the c29clang Compiler Tools¶
This section of the Getting Started Guide provides an example of how to build a simple application using the c29clang command-line interface. In addition, it provides a walk-through of how to build a simple application in a Code Composer Studio project that uses the c29clang compiler.
3.1. Source Files¶
The subsections below describe how to build a simple “Hello World!” program using either the c29clang command-line interface or the Code Composer Studio (CCS) development environment. For the purposes of these tutorial examples, it is assumed that you have a C source file containing the following C code:
1#include <stdio.h>
2
3int main() {
4 printf("Hello World\n");
5 return 0;
6}
It is also assumed that you have at your disposal a linker command file that provides a specification of the available memory and how to place compiler/linker generated output sections in that memory. For example, in the tutorials below, the following linker command file, named lnkme.cmd, could be used:
/****************************************************************************/
/* lnk.cmd - V1.00 Command file for linking C29 programs */
/****************************************************************************/
/* This linker command file assumes C/C++ model */
/****************************************************************************/
-c
-stack 0x8000 /* Software stack size */
-heap 0x2000 /* Heap area size */
/* Specify the system memory map */
MEMORY
{
ROM : org = 0x00000020 len = 0x2FFFE0 /* 1.25 GB */
FLASH : org = 0x10000000 len = 0x300000 /* 1.25 GB */
RAM : org = 0x18000000 len = 0x300000 /* 1.25 GB */
}
#define RO_CODE FLASH
#define RO_DATA FLASH
#define RW_DATA RAM
/* Specify the sections allocation into memory */
SECTIONS
{
.text : {} > RO_CODE /* Code */
.cinit : {} > RO_DATA /* Initialization tables */
.const : {} > RO_DATA /* Constant data */
.pinit : {} > RO_DATA /* C++ Constructor tables */
.data : {} > RW_DATA /* Initialized variables */
.bss : {} > RW_DATA /* Uninitialized variables */
.stack : {} > RW_DATA /* Software system stack */
.sysmem : {} > RW_DATA /* Dynamic memory allocation area */
}
3.2. Compile and Link Using Command-Line¶
You can use a single command line to compile your source files and link against C/C++ runtime libraries to create an executable file. By default, the c29clang compiler will compile and attempt to link compiler generated object files with runtime libraries. If you only want to compile your source files into object files without linking, the c29clang -c option can be added to the command line.
If you were building the “Hello World!” example program, you could use the a command like the following:
%> c29clang -mcpu=c29.c0 hello.c -o hello_world.out -Xlinker -llnkme.cmd -Xlinker -mhello_world.map
When the c29clang compiler runs, it implicitly adds the directories where the C/C++ runtime header files are installed to the include file directory search path. Likewise, when the linker is invoked by c29clang, it implicitly adds the directories where the C/C++ runtime libraries are installed to your library file directory search path. In addition, the linker implicitly includes the list of applicable C/C++ runtime libraries into a link to resolve references to C/C++ runtime and built-in functions and data objects.
The above command produces an executable file, hello_world.out, which can be loaded and run on the appropriate C29x processor.
3.3. Compile and Link Using Build Automation Tools¶
You can use build systems to automate the command line compilation and linking steps. Supported build systems include GNU Make and CMake (v3.29 or higher).
For example, the following CMakeLists.txt file contains the directives required to use CMake to build a sample c29clang application:
cmake_minimum_required(VERSION 3.29) # Minimum version for c29clang support
#------------------------------------------------------------------------------
# Set up cross compiling with TI clang compiler
#------------------------------------------------------------------------------
set(CMAKE_SYSTEM_NAME Generic) # Inform cmake of cross-compiling
# find c29clang in execution path
find_program(CMAKE_C_COMPILER c29clang)
# or set path to c29clang explicity
#set(CMAKE_C_COMPILER /Users/ti/ti-cgt-c29_1.2.0.LTS/bin/c29clang)
#------------------------------------------------------------------------------
project(DDREyeFirmware C)
add_executable(app
main.c
lib/uart_lib/src/uartConsole.c
lib/uart_lib/src/uartStdio.c
lib/uart_lib/src/uart.c
lib/pattern_gen_lib/src/pattern_gen_lib.c)
add_compile_options(-mcpu=c29.c0)
add_compile_options(-Oz -g)
include_directories(lib/uart_lib/inc)
include_directories(lib/uart_lib/src)
include_directories(lib/pattern_gen_lib/inc)
include_directories(lib/pattern_gen_lib/src)
add_link_options(LINKER:--ram_model)
add_link_options(LINKER:--warn_sections)
add_link_options(${CMAKE_SOURCE_DIR}/linker.cmd)
3.4. Compile and Link Using Code Composer Studio¶
If you use CCS as your development environment, the compiler and linker option are automatically set for you when you create a project. The build settings that are created when the project is created determine which compiler and linker command-line options are used to build the project and can be adjusted as needed.
To create and build the “Hello World!” example as a CCS project, follow these steps:
Create a project
1.1) Choose File > New > CCS Project from the “File” tab
1.2) In the “New CCS Project” wizard, if you are compiling for a specific TI C29x processor, then you can select the processor from the Target drop-down menus. For the purposes of this tutorial, the “C29 Device” setting was selected in the right-hand side Target drop-down menu.
1.3) In the Project name field, type a name for the project. For the purposes of this tutorial, we’ll refer to the project name “hello_world”.
1.4) In the Compiler version drop-down menu, select the c29clang compiler that you have installed.
1.5) Expand the Project type and tool-chain section, then select the device endianness. For this tutorial, a little-endian device is assumed.
1.6) Expand the Project templates and examples section, then select a template for your project. For this tutorial, the “Empty Project” template is assumed.
1.7) Click Finish. A new project, “hello_world”, will be added to your current workspace.
Add source files
2.1) Left click on the “hello_world” project in the current workspace to make it the active project.
2.2) Right-click on the “hello_world” project, then select Add Files…” from the drop-down menu. You can then browse to find a C source file containing the code described in “Source Files” subsection above. When the source file is found and selected in the Add files to hello_world pop-up browser, clock on Open and then copy the file into the project. For this tutorial, the source file name is assumed to be “hello.c”.
2.3) Repeat step 2.3 to find and copy an appropriate linker command file to be used in the project. For this tutorial, a linker command file named “lnkme.cmd” is assumed.
Open and adjust project build settings as needed
3.1) Right-click on the “hello_world” project, then select Show Build Settings… or Properties from the drop-down menu.
3.2) In the Properties for hello_world pop-up dialog box, walk through the categories along the left-hand side of the dialog and make necessary adjustments in each category:
3.2.1) In the General category, check that the proper compiler version, Device endianness, and Linker command file are selected
3.2.2) In the C29 Compiler > Processor Options category, select the appropriate options from each of the drop-down menus in the Processor Options window.
3.2.3) Further adjustments to other categories are not necessary for the purposes of this tutorial.
3.2.4) When adjustments to the Properties for hello_world are complete, click on Apply and Close
Build the project
4.1) Right-click on the “hello_world” project, then select Build Project from the drop-down menu.
4.2) As CCS runs the compiler and linker commands, the project build progress will appear in the CCS Console window, with a resulting output file named “hello_world.out”. This .out file can then be loaded and run on an appropriate TI C29x processor. The resulting .out file can be loaded and run on the appropriate TI C29x processor.