Wednesday, May 4, 2016

Developing Applications for Nordic nRF51 on Gentoo

These days embedded computer platforms are everywhere and affordable for hobbyists--such as the BLE Nano (based off of Nordic nRF51822 SoC and 32-bit ARM Cortex M0) for around $20. Lets get started with BLE development on Gentoo!


Compile Requirements for Nordic SDK


Previously I used Linux Mint and Ubuntu as my development platform of convenience but Gentoo has always been my favorite (especially for coding and compiling). On Ubuntu the embedded development toolchain is provided by the Launchpad GCC ARM Embedded Project. According to Nordic SDK documentation this is the preferred development environment when using GCC.

Gentoo Cross Platform Toolchain (w/ crossdev)


Having finally installed Gentoo on my workstation I found that there some issues using crossdev for compiling my BLE Nano project with the Nordic SDK. Specifically, crossdev profiles such as "arm-none-eabi" (recommended for ARM Cortex M0) or "armv6j-hardfloat-linux-gnueabi" (recommended for RaspberryPi) fail to compile the Nordic SDK examples. This is because Gentoo does not provide the modified newlib (nano) libraries which optimizes the size of the binary by using the specs LDFLAG option "--specs=nano.specs". Compiling without this LDFLAG produces binaries which do not run properly on the BLE Nano.

Providing the Newlib (Nano) Requirement


Initially I tried to resolve this problem by reviewing the crossdev shell code to provide newlib (nano) using the following configuration flags copied from GCC ARM Embedded "build-toolchain.sh":
   --disable-newlib-supplied-syscalls
   --enable-newlib-reent-small
   --disable-newlib-fvwrite-in-streamio
   --disable-newlib-fseek-optimization
   --disable-newlib-wide-orient
   --enable-newlib-nano-malloc
   --disable-newlib-unbuf-stream-opt
   --enable-lite-exit
   --enable-newlib-global-atexit
   --enable-newlib-nano-formatted-io
   --disable-nls
Alas, the code for crossdev is a bit hard for me to understand as well as the newlib ebuilds it creates and uses from the portage tree. I was not able to provide newlib (nano) from source using crossdev or ebuild without breaking my portage toolchain. Until somebody figures this part out there is a binary ebuild called "gcc-arm-embedded-bin" on an unofficial portage overlay.

Installing the Binary Toolchain from Launchpad


The latest overlay ebuild at the time of this writing gcc-arm-embedded-bin-4.9_p20150306.ebuild can be updated to install the most recent GCC release by simply providing an updated SRC_URI download location or local file and changing the ebuild filename to match. For my toolchain I changed the filename to gcc-arm-embedded-bin-5.3_p20160330.ebuild and updated SRC_URI to gcc-arm-none-eabi-5_3-2016q1-20160330-linux.tar.bz2. Be sure to run the ebuild manifest command after updating your portage overlay.

You can download the updated ebuild here if the unofficial overlay disappears for some reason.