Building kernel for RPi2 with bluetooth_6lowpan for Raspbian

Building kernel for RPi2 with bluetooth_6lowpan

In this guide, you will have two choices:

  • Cross-compilation build

  • Native build

Choose the one that fits your needs. Cross-compilation is the fastest choice.

Dependencies:

In addition to the already installed tools in Raspbian, you'll need

  • Git
  • kernel-package

These can be installed using apt:

sudo apt-get install git kernel-package -y

“kernel-package” is needed for generating the .deb package, and git is used to fetch the repository from Raspberry Pi’s github account. We'll fetch the default branch, which holds 3.18.11 at the time of writing this blog post.

Obtaining the kernel

Open a terminal and maneuver to a clean directory, I made a directory ~/raspbian.

Now we need to clone the kernel:

git clone https://github.com/raspberrypi/linux.git

Grab a coffee. This will take a while.

Option 1: Cross-compilation on x86/x64

The easiest option is to grab the toolchain from RaspberryPi. Go into a clean directory and clone:

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

Now we’ll have to define this path for easier building. For building on x86 (32-bit) environment:

export CCPREFIX=/path/to/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-

For building on x64 (64-bit) environment:

export CCPREFIX=/path/to/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-

Go to the kernel directory that you just cloned. We’ll have to ensure that the directory is cleaned

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ make mrproper

Set the default configuration to bcm2709, which is the chipset for Raspberry Pi 2

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX}
 make bcm2709_defconfig

Now we’ll start compiling

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make –j5

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make –j5 modules

On multi-core CPUs, you can utilize the cores via the “-j” operation. On my PC, I have 4 cores, therefore I add “-j 5” to the above command to speed things up. You should set the number to “NUM_OF_CPU_CORES + 1” to ensure that your cores are working full-time.  

Option 2: Native build

It’s recommended to have 10 GB free space on your Raspberry for kernel compilation.

The steps here are very similar to the section about cross-compilation, just that we remove a couple of defines.

First we need to ensure that you have your build essentials in order:

pi@raspberry:~/$ sudo apt-get install build-essential –y

Set the default configuration to bcm2709, which is the chipset for Raspberry Pi 2

pi@raspberry:~/raspbian/kernel/linux$ make bcm2709_defconfig

Now we’ll start compiling

pi@raspberry:~/raspbian/kernel/linux$ make –j5 && make –j5 modules

Generate .deb packages

For cross-compilation approach:

hkn@hkn-VirtualBox:~/raspbian/kernel/linux$ CONCURRENCY_LEVEL=5 DEB_HOST_ARCH=armhf fakeroot make-kpkg --append-to-version –name_of_your_choice --revision `date +%Y%m%d%H%M%S` --ARCH=arm --cross-compile ${CCPREFIX} kernel_image kernel_headers

CONCURRENCY_LEVEL sets the number of jobs. I used 5 since I have four CPU cores.

You will see warnings similar to this when generating the .deb files:

dpkg-architecture: warning: specified GNU system type arm-linux-gnu does not match gcc system type x86_64-linux-gnu, try setting a correct CC environment variable

Don’t worry; this is normal when cross-compiling, and actually reassuring that we did compile the kernel for ARM architecture.

For native build approach:

pi@raspberry:~/raspbian/kernel/linux$ CONCURRENCY_LEVEL=5 DEB_HOST_ARCH=armhf fakeroot make-kpkg --append-to-version –name_of_your_choice --revision `date +%Y%m%d%H%M%S` --initrd kernel_image kernel_headers

The .deb files will be placed in the parent directory. Write “ls ../*.deb” and you should have two files:

../linux-headers-*.deb
../linux-image-*.deb

Installing the kernel on your Raspberry Pi 2

If you cross-compiled, you’ll need to transfer your kernel header and kernel image to the Pi. This can be done via scp for instance.

scp linux-header*.deb linux-image*.deb pi@pi-ip:~

This will transfer the files to your home directory.

Now login to your raspberry pi and update your system, and install the new kernel:

sudo dpkg –i linux-header*.deb linux-image*.deb

The kernel is now installed, but we need to specify in our boot settings that it should use this new kernel.

pi@raspberry ~ $ cd /boot
pi@raspberry /boot $ ls | grep vmlinuz
vmlinuz-3.18.11-rpi2-v7+

Here we found the name of our new kernel image. For simplicity, we will make a symbolic link (shortcut) to ‘vmlinuz’:

pi@raspberry /boot $ sudo ln –s vmlinuz-3.18.11-rpi2-v7+ vmlinuz

Now we specify the kernel and append it to config.txt:

pi@raspberry /boot $ sudo sh –c ‘echo “kernel=vmlinuz” >> config.txt’

Reboot and enjoy a fresh new kernel with the bluetooth_6lowpan module.

Parents
  • It seems that Bluetooth 6lowpan is now natively supported on Raspbian 2015-05-05.

    pi@raspberrypi ~ $ uname -a 
    Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux
     
    pi@raspberrypi ~ $ ls /lib/modules/3.18.11-v7+/kernel/net/bluetooth/
    bluetooth_6lowpan.ko  bluetooth.ko bnep  hidp  rfcomm
    

    Just load "bluetooth_6lowpan" in /etc/modules.

    BR, zou

Comment
  • It seems that Bluetooth 6lowpan is now natively supported on Raspbian 2015-05-05.

    pi@raspberrypi ~ $ uname -a 
    Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux
     
    pi@raspberrypi ~ $ ls /lib/modules/3.18.11-v7+/kernel/net/bluetooth/
    bluetooth_6lowpan.ko  bluetooth.ko bnep  hidp  rfcomm
    

    Just load "bluetooth_6lowpan" in /etc/modules.

    BR, zou

Children
No Data