How to cross-compile D-ECU kernel

From iaiaGi Wiki
Jump to: navigation, search

This document describes how to configure the Raspberry Pi Desktop 2.2 64 bit operating system development environment to cross-compile Raspberry Pi 3 kernel.

Introduction

This document describes a detailed and properly working procedure to configure the Raspberry Pi Desktop 2.2 64 bit operating system development environment to cross-compile Raspberry Pi 3 Raspbian distribution operating system kernel.

The iaiaGi Project D-ECU (Development - Electronic Control Unit) hardware is Raspberry Pi 3 based, thus we need a mean to modify and properly optimize the Raspbian operating system kernel to support our specific hardware.

Also, it is not effective compiling the Raspbian kernel on a Raspberry Pi, due to the very long time needed completing kernel compilation. In a working environment it is more efficient to think of a cross-compilation of the same kernel, which reduces compilation time to some minutes instead of hours.


Requirementes

This procedure has been tested with the following environment:

  • Raspberry Pi Desktop:
lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.3 (stretch)
Release:	9.3
Codename:	stretch

uname -a
Linux iaiaGi-wks-01 4.9.0-5-amd64 #1 SMP Debian 4.9.65-3+deb9u2 (2018-01-04) x86_64 GNU/Linux2/7
  • Raspberry Pi OS:
lsb_release -a
No LSB modules are available.
Distributor ID:	Raspbian
Description:		Raspbian GNU/Linux 9.3 (stretch)
Release:		9.3
Codename:		stretch

uname -a
Linux iaiagi-cde 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l GNU/Linux

Resources

To write this document we referred to the following Internet shared resources:

  • Kernel building - Raspberry Pi Documentation [1].
  • Configuring the kernel - Raspberry Pi Documentation [2].
  • Raspberry Pi Kernel Compilation - eLinux.org [3].


Install toolchain

Use the following command to download the toolchain to the home folder (please, note that all commands run under the home folder unless otherwise specified):

git clone https://github.com/raspberrypi/tools ~/tools

If you have just cloned the Git Raspberry Pi tools repository and you want to update its content to the latest version, run the commands below:

cd tools
git pull
cd

Updating the environment variables makes the system aware of file locations needed for cross-compilation, type of kernel and which compiler to use (the steps below are required only the first time you configure the cross-compilation environment). You should use:

echo PATH=\$PATH:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/libexec/gcc/arm-linux-gnueabihf/4.8.3 >> ~/.bashrc
echo KERNEL_SRC=~/linux/ >> ~/.bashrc
echo CCPREFIX=~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- >> ~/.bashrc
source ~/.bashrc


Get sources

To get the sources, refer to the original GitHub repository for the various branches.

git clone --depth=1 https://github.com/raspberrypi/linux

We suggest to deeply read GitHub documentation [4] before moving to the next chapters.

If you have just cloned the Git Raspberry Pi kernel source code repository and you want to update its content to the latest version, run the commands below:

cd linux
git pull
cd


Build sources

Install libncurses5-dev and libncursesw5-dev to support make menuconfig:

sudo apt-get update
sudo apt-get install libncurses5-dev libncursesw5-dev

Install GTK+ development environment to support make gconfig:

sudo apt-get install libgtk2.0-dev libglib2.0-dev libglade2-dev

Install Qt4 development environment to support make xconfig:

sudo apt-get install libqt4-dev pkg-config

Install bc tool which is required to support the Raspberry Pi kernel cross-compilation:

sudo apt-get install bc

All the above steps are required only the first time you configure the cross-compilation environment.

Start the kernel compilation procedure by moving to the kernel source directory:

cd linux

Clean the kernel source:

KERNEL=kernel7
make mrproper

Prime the kernel with the old configuration by running:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

Then configure the kernel to support Peak System PCAN USB CAN bus interface [5]:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

In the Raspberry Pi kernel config, check that the following options are enabled:

	    -> Networking support (NET [=y])
	      -> CAN bus subsystem support (CAN [=m])
	        -> CAN Device Drivers
	          -> Platform CAN drivers with Netlink support (CAN_DEV [=m])
	            -> CAN USB interfaces
		      -> PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD ([=m])

Save the kernel configuration and exit.

Build the new kernel by running:

ARCH=arm CROSS_COMPILE=${CCPREFIX} make -j6 zImage modules dtbs


Installing directly onto the SD card

Having built the kernel, you need to copy it onto your Raspberry Pi and install the modules; this is best done directly using an SD card reader.

First, use lsblk before and after plugging in your SD card to identify it. You should end up with something like this:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 698.7G  0 disk 
├─sda1        8:1    0 690.7G  0 part /
├─sda2        8:2    0     1K  0 part 
└─sda5        8:5    0     8G  0 part [SWAP]
sdb           8:16   0   1.8T  0 disk 
└─sdb1        8:17   0   1.8T  0 part /media/<user_name>/Toshiba_2GB
sr0          11:0    1  1024M  0 rom  
mmcblk0     179:0    0   7.4G  0 disk 
├─mmcblk0p1 179:1    0  41.8M  0 part /media/<user_name>/boot
└─mmcblk0p2 179:2    0   7.4G  0 part /media/<user_name>/037616fd-28fe-4652-8248-2042ea30b929

with mmcblk0p1 being the FAT (boot) partition, and mmcblk0p2 being the ext4 filesystem (root) partition.

Unmount the two partitions and mount them according to what suggested below:

umount /media/<user_name>/boot
umount /media/<user_name>/037616fd-28fe-4652-8248-2042ea30b929

Mount these first, according to the following schema:

mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/mmcblk0p1 mnt/fat32
sudo mount /dev/mmcblk0p2 mnt/ext4

Next, install the modules:

sudo make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=mnt/ext4 modules_install

Finally, copy the kernel and Device Tree blobs onto the SD card:

sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL-d-ecu.img
sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/

Edit the config.txt file in the SD card to select the kernel that the Pi will boot into:

sudo vi mnt/fat32/config.txt

Go to the end of the file and add the following lines:

# Added to load D-ECU specific kernel
kernel=kernel7-d-ecu.img

Remove the card from the PC:

sudo umount mnt/fat32
sudo umount mnt/ext4

Finally, plug the card into the D-ECU and boot it!