Tuesday, November 20, 2018

How to compile the Core Flight System on The Raspberry Pi

Building the core Flight System (cFS) on the Rasperry Pi

This set of instructions will allow you to download, compile, and run the core Flight System (cFS) on a Raspberry Pi, running the Raspian OS.

Notes:
  1. These instructions assume you will be compiling the code on the Raspberry Pi. It also possible to cross compile the cFS for the Raspberry Pi, but that is not covered in these instructions.
  2. The changes outlined below involve removing the “-m32” flags from the compiler options. The “-m32” flags are used to build the cFS as a 32 bit executable on 64 bit linux systems. The next release of the cFS should build and run as a 64 bit executable.
  3. The instructions take the approach of altering the “pc-linux” platform support for the cFS. This works because x86/x86_64 Linux is mostly compatible with the raspberry Pi Linux (Raspbian). A better way to support both linux on the PC and Raspberry Pi (especially if you want to cross compile) would be to copy the “psp/fsw/pc-linux” directory to “psp/fsw/rpi-linux” and make the changes there. Then you will need to specify the “rpi-linux” PSP in the file: build/cpu1/cfe/cfe-config.mak. By doing this, you can support builds on x86 linux and the Raspberry Pi at the same time. Another approach would be to use the CMake build system, which is described below. 
  4. These instructions were tested on a mid 2018 release of Raspian. cFS is version 6.5

The cFS has two build systems:

  1. A standard “Makefile” based build system
  2. A build system based on the CMake build tool : https://cmake.org/
Eventually the standard "Makefile" system will be removed, and the cFS will only support the Cmake system. 

cFS Raspberry Pi Standard Makefile Build Instructions


First, let's tackle the build using the “classic” makefile based build system. This build system is older, but is compatible with the “cFS-101” tutorial on Github.


Download the cFS:
The current open source version of the cFS, Operating System Abstraction Layer (OSAL), and all open source cFS applications are hosted on a single github repository for convenience. While the cFS community is interesting in getting feedback, the repository is not currently used to accept pull requests.

$ cd cfe
$ git submodule init
$ git submodule update


What needs to be changed for the Raspberry Pi (running Raspbian OS) ?


The PC Linux build has compiler switches to build it as a 32 bit application (-m32). To build on the Raspberry Pi, you need to comment out/edit these switches. 


Edit the file psp/fsw/pc-linux/make/link-rules.mak
In that file, and remove the -m32 switch below:
##
## Linker flags that are needed
##
LDFLAGS = -m32 -Wl,-export-dynamic


In the same file, there is one more -m32 switch in the bottom of the file.
##
## Application Link Rule
##
$(APPTARGET).$(APP_EXT): $(OBJS)
       $(COMPILER) -m32 -shared -o $@ $(OBJS)

Next, edit the file  psp/fsw/pc-linux/make/compiler-opts.mak
In that file, remove the -m32 switch below:
##
## Compiler Architecture Switches
##
ARCH_OPTS = -m32


Also, remove -melf_i386 from the line below
##
## Compiler tools
##
COMPILER=gcc
ASSEMBLER=as
LINKER=ld -melf_i386


Finally, edit the file for the elf2cfetbl utility. Edit the file:
tools/elf2cfetbl/for_build/Makefile


Again, you need to remove the -m32 option from the ARCH_DEFS line
##
## Architecture / debug defines
##
ARCH_DEFS = -m32 -g
Finally, you can run the build:


$ source setvars.sh
$ cd build/cpu1
$ make config
$ make


If everything built OK, then you can run the cFS:
$ cd exe
$ sudo ./core-cpu1.bin

Note that the cFS currently runs as sudo or root on Linux. This is to enable the "real time" pthread scheduler and allow for larger posix message queue message sizes. It is possible to adjust the posix message queue sizes and run with the standard scheduler, but that will not be covered here.

When you are finished running, hit control-c to stop the cFS process.

cFS Raspberry Pi CMake Build Instructions

Next, lets cover how to download, compile, and run the cFS on a Raspberry Pi using the CMake build system. 

Notes:
  1. These instructions assume you will be compiling the code on the Raspberry Pi. It also possible to cross compile the cFS for the Raspberry Pi, but that is not covered in these instructions.
  2. The changes outlined below involve removing the “-m32” flags from the compiler options. The “-m32” flags are used to build the cFS as a 32 bit executable on 64 bit linux systems. The next release of the cFS should build and run as a 64 bit executable.

Prerequisites:
$ sudo apt-get install cmake

Download cFS:
$ cd cfe
$ git submodule init
$ git submodule update
$ mv build legacy_build
$ cp -a cfe/cmake/sample_defs .
$ cp cfe/cmake/Makefile.sample Makefile

First, you need to edit sample_defs/toolchain-cpu1.cmake
In the file below, change the CMAKE_SYSTEM_PROCESSOR to arm
Also comment out the last line that has the “-m32” switch
--------------------------------------------------------------------------------------------------

# This example toolchain file describes the cross compiler to use for
# the target architecture indicated in the configuration file.

# In this sample application, the "cross" toolchain is configured to
# simply use the system native compiler with the "m32" switch to output
# 32-bit code on a 64-bit system.  This will not be necessary in
# future revisions.

# Basic cross system configuration
SET(CMAKE_SYSTEM_NAME           Linux)
SET(CMAKE_SYSTEM_VERSION        1)
SET(CMAKE_SYSTEM_PROCESSOR      arm)

# Specify the cross compiler executables
# Typically these would be installed in a home directory or somewhere
# in /opt.  However in this example the system compiler is used.
SET(CMAKE_C_COMPILER            "/usr/bin/gcc")
SET(CMAKE_CXX_COMPILER          "/usr/bin/g++")

# Configure the find commands
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM   NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY   NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE   NEVER)

# These variable settings are specific to cFE/OSAL and determines which
# abstraction layers are built when using this toolchain
SET(CFE_SYSTEM_PSPNAME      "pc-linux")
SET(OSAL_SYSTEM_BSPNAME     "pc-linux")
SET(OSAL_SYSTEM_OSTYPE      "posix")

# This adds the "-m32" flag to all compile commands
# SET(CMAKE_C_FLAGS_INIT "-m32" CACHE STRING "C Flags required by platform")

Next, edit the tools/elf2cfetbl/CMakelists.txt file
You need to comment out the “-m32” switch as below:
------------------------------------------------------------------------------------
# force build as 32-bit
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
add_executable(elf2cfetbl elf2cfetbl.c)

Now you can build the cFS using the cMake build system:
$ make prep
$ make install

If everything built OK, then you can run the cFS:
$ cd build/exe/cpu1
$ sudo ./core-cpu1.bin

When you are finished, hit control-c to stop the cFS