.. Instructions to set up the embedded toolchain and to build an example project.
.. Author: Thomas Van Hoof

.. STATUS: production


################################################
Setting Up the Toolchain for Embedded Developers
################################################

Instructions to set up the embedded toolchain and to build an example project.

.. rubric:: Quick Navigation
.. contents::
   :local:
   :depth: 2

.. hint:: The tutorials that were followed to make this guide can be found :ref:`at the bottom of this document <external-tutorials>`.

----

************************
Installing the Toolchain
************************

GCC for ARM
===========

:abbr:`GCC (GNU Compiler collection)` is the compiler.

GCC for ARM can be downloaded from `launchpad <https://launchpad.net/gcc-arm-embedded/+download>`_ or `arm_developer <https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads>`_.

To install GCC, open a terminal and go to the directory where you downloaded the GCC archive. ::

    $ sudo mkdir /usr/local/gcc-arm/
    $ sudo chown <USERNAME> /usr/local/gcc-arm/
    $ tar -xjf gcc-arm-none-eabi-<VERSION>.tar.bz2 -C usr/local/gcc-arm/

.. _gcc-folder:

Add GCC to your system path. The system path can be modified by editing the ``.bash_profile`` file in your home directory. To find the folder where GCC was extracted run the following command ::

    $ ls /usr/local/gcc-arm | grep gcc-arm-none-eabi

Add the following line at the end of ``.bash_profile`` ::

    export PATH=/usr/local/gcc-arm/gcc-arm-none-eabi-<VERSION>/bin:$PATH

Finally, test your installation ::

    $ arm-none-eabi-gcc --version
    arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 5.4.1 20160919 (release) [ARM/embedded-5-branch revision 240496]
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

openOCD
=======

:abbr:`OpenOCD (open on-chip-debugger)` is the software that provides a link between the debugging software (`gdb`) and the hardware debugger.

OpenOCD can be downloaded from `github <https://github.com/gnuarmeclipse/openocd/releases>`_. To install it, open ``gnuarmeclipse-openocd-osx-<VERSION>.pkg``.
The package will be installed in ``/Applications/GNU\ ARM\ Eclipse/OpenOCD/``.

Test your installation ::

    $ openocd --version
    Open On-Chip Debugger 0.10.0+dev-g0ecee83 (2017-02-10-15:11)
    Licensed under GNU GPL v2
    For bug reports, read
          http://openocd.org/doc/doxygen/bugs.html

nRF5X Command Line Tools
========================

The nRF5X Command Line Tools are a set of tools provided by Nordic to easily flash a target.

To install the nRF5X Command Line Tools you first need to install `brew <https://brew.sh>`_.

Then open a terminal and type ::

    $ brew cask install nrf5x-command-line-tools

Test your installation ::

    $ nrfjprog --version
    nrfjprog version: 9.3.1
    JLinkARM.dll version: 6.12g

For programming multiple targets in parallel, it is advised to install ``pynrfjprog``. It is an python API for nrfjprog and can be installed via pip3 (if you do not have python 3 installed first install it ``brew install python3``) ::

    $ pip3 install pynrfjprog

Nordic SDK
==========

The Nordic :abbr:`SDK (Software Development kit)` is a a set of libraries and drivers.

The Nordic SDK can be downloaded from `Nordic <http://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF52832>`_. Extract the downloaded content, and place it in a convenient directory for you to work in.

Since Nordic does not know what compiler is used, it is necessary to change the default makefile. This makefile is in ``<SDK directory>/components/toolchain/gcc/Makefile.possix``.
Set the value of ``GNU_INSTALL_ROOT`` to ``/usr/local/gcc-arm/gcc-arm-none-eabi-VERSION``. (`VERSION` should be set according to :ref:`your gcc installation <gcc-folder>`.)


***************************
Building an Example Project
***************************

Compiling an Example Project without BLE
========================================

In the SDK there are a few examples that we can use to test the complete toolchain. These can be found in ``<SDK directory>/examples``. In this tutorial, we will compile a blinky project with FreeRTOS. In ``<SDK directory>/examples/peripheral/``, you will find a lot examples. For now we are interested in ``blinky_freertos``.

Since our development board uses the pca10040 we will go in the corresponding directory. There is only one ``blank`` directory in it. Now we need to choose how to build, so we will go further within armgcc. This is where you will choose the makefile for the project. The directory we are now in is ``<SDK directory>/examples/peripheral/blinky_freertos/pca10040/blank/armgcc``.

To compile the project, type ::

    $ make
    mkdir _build
    Compiling file: nrf_log_backend_serial.c
    ....
    Compiling file: SEGGER_RTT_printf.c
    Assembling file: gcc_startup_nrf52.S
    Compiling file: system_nrf52.c
    Linking target: _build/nrf52832_xxaa.out

      text	   data	    bss	    dec	    hex	filename
      12112	    116	   4580	  16808	   41a8	_build/nrf52832_xxaa.out

    Preparing: _build/nrf52832_xxaa.hex
    Preparing: _build/nrf52832_xxaa.bin

Nordic provides a flash functionality within the makefile.
To use this, type ::

    $ make flash
    Flashing: _build/nrf52832_xxaa.hex
    nrfjprog --program _build/nrf52832_xxaa.hex -f nrf52 --sectorerase
    Parsing hex file.
    Erasing page at address 0x0.
    Erasing page at address 0x1000.
    Erasing page at address 0x2000.
    Applying system reset.
    Checking that the area to write is not protected.
    Programing device.
    nrfjprog --reset -f nrf52
    Applying system reset.
    Run.

Congratulations, the program that you have built is now running on the microcontroller.

Compiling an Example Project with BLE
=====================================

.. note :: If you have not built the example project without BLE, do this first

In order to get BLE working, we need to flash 2 hex files. This can be done in 2 manners.


The first hex file contains the softdevice for BLE connectivity. This can be found in ``<SDK directory>/components/softdevice/``. Here you will find many softdevices. In this tutorial the ``s132`` softdevice will be used.

.. hint :: More info about softdevices can be found in `the documentation provided by Nordic <http://infocenter.nordicsemi.com/index.jsp>`_.

Manual flashing of softdevice
-----------------------------

Go in the terminal to the directory ``<SDK directory>/components/softdevice/s132/hex``. First erase the memory of the controller to avoid conflicts ::

    $ nrfjprog -f nrf52 -e

Flashing the softdevice ::

    $ nrfjprog -f nrf52 --program s132_nrf52_3.0.0_softdevice.hex
    Parsing hex file.
    Reading flash area to program to guarantee it is erased.
    Checking that the area to write is not protected.
    Programing device.


Using the makefile for softdevice flashing
------------------------------------------

The softdevice can also be flashed from within the project directory of the example project, which is ``<SDK directory>/examples/ble_peripheral/ble_app_hrs_freertos/pca10040/s132/armgcc/``.
This can be done by ::

    $ make flash_softdevice
    Flashing: s132_nrf52_3.0.0_softdevice.hex
    nrfjprog --program ../../../../../../components/softdevice/s132/hex/s132_nrf52_3.0.0_softdevice.hex -f nrf52 --sectorerase
    Parsing hex file.
    Erasing page at address 0x0.
    Erasing page at address 0x1000.
    ...
    Erasing page at address 0x1E000.
    Applying system reset.
    Checking that the area to write is not protected.
    Programing device.
    nrfjprog --reset -f nrf52
    Applying system reset.
    Run.


Flashing the main project
-------------------------

When that is done, continue by building the example project in ``<SDK directory>/examples/ble_peripheral/ble_app_hrs_freertos/``. These steps are the same as for a project without BLE ::

    $ make
    mkdir _build
    Compiling file: nrf_log_backend_serial.c
    ...
    Compiling file: softdevice_handler.c
    Linking target: _build/nrf52832_xxaa.out

      text	   data	    bss	    dec	    hex	filename
      53272	    160	   7180	  60612	   ecc4	_build/nrf52832_xxaa.out

    Preparing: _build/nrf52832_xxaa.hex
    Preparing: _build/nrf52832_xxaa.bin

This needs to be followed by ::

    $ make flash
    Flashing: _build/nrf52832_xxaa.hex
    nrfjprog --program _build/nrf52832_xxaa.hex -f nrf52 --sectorerase
    Parsing hex file.
    Erasing page at address 0x1F000.
    ...
    Erasing page at address 0x2C000.
    Applying system reset.
    Checking that the area to write is not protected.
    Programing device.
    nrfjprog --reset -f nrf52
    Applying system reset.
    Run.

Using merghex
-------------
Mergehex is a program from the Nordic nRF5X Command Line Tools, that merges two ``.hex`` files into one. ::

    merghex -m <inputfile1> <inputfile2> -o <outputfile>

Once you have the single output file you can flash it with ::

    $ nrfjprog -f nrf52 --program <outputfile>
    Parsing hex file.
    Reading flash area to program to guarantee it is erased.
    Checking that the area to write is not protected.
    Programing device.

.. _external-tutorials:

******************
External Tutorials
******************

* `programming an nrf52 on a mac <https://aaroneiche.com/2016/06/01/programming-an-nrf52-on-a-mac/>`_
* `embedded soft dev <http://embeddedsoftdev.blogspot.be/p/ehal-nrf51.html>`_
* `gnu arm eclipse <http://gnuarmeclipse.github.io/openocd/install/>`_
* `Nordic devzone <https://devzone.nordicsemi.com/blogs/840/nrfjprog-pynrfjprog-intro-mac-os-x-linux-now-suppo/>`_
