Wednesday 27 August 2014

Developing for Nordic Semiconductor nRF51822


What 

This post describes installation and first simple example running dev kit nRF51822 on Mac OS X 10.9.4.

Why

Its either you who will rule Internet of Things, or Internet of Things will rule you. Pick a side and spot wining.
I got my hands on nRF51822 and since I have switched to Mac, the installation instructions for mac are confusing... there was no obvious way to run it from thus this is a mental note to my self how to set up environment. So here you go, after tutorial you will be up and running in no time :) with multiple options.


How

First install needed shit, its all below. The instructions are minimal and assumes you can figure out some simple stuff. If not ask, Google, change carrier.

1. Eclipse

I have many copies of eclipse for different developing purpose containing different language. The reason is that plugins do brake each other and I rather reinstall single plugin then have all my "life"dependent on one eclipse.
  1. Download Eclipse http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunar (I used eclipse-cpp-luna-R-macosx-cocoa-x86_64.tar.gz)
  2. Extract it
  3. Rename it to eclipseARM
  4. Enter folder and rename eclipse.app to ArmLipse.app
  5. Move folder to /Applications/ or /User/Applications/ 

1.1 Plugins

  1. Lauch ArmLipse
  2. Create new workspace nRF51 (keep it clean)
  3. Help->Install new software
  4. Add http://embsysregview.sourceforge.net/update 
  5. and install embsysregview
  6. Go back, select all available sites, search and install GDB Hardware Debugging
  7. Go back and add site http://gnuarmeclipse.sourceforge.net/updates and install plug in for arm
  8. Go back and add http://nrf51osx.sourceforge.net/updates and install nordic plugin

2.  GNU Tools

  • Get tar for mac from here https://launchpad.net/gcc-arm-embedded/ (I got gcc-arm-none-eabi-4_8-2014q2)
  • Extract it.

3. Jlink

  1. Get libusb to start with brew install libusb
  2. Get Jlink from http://www.segger.com/jlink-software.html
  3. You will need to enter number from the biggest chips on the board
  4. I got JLink_MacOSX_V490b.pkg, double tap and follow instructions

4. Libraries

  1. You will need firmware either s110, s120, s130 depending on the purpose. You can get then form nordic Semiconductor site.
  2. Create folder development in /User/your_name/ and create subfolder nrf51
  3. Copy, SDK, Firmware and GNU tools to it
  4. Mine looks: 
    ->tree -CL 1 nrf51
    nrf51
    ├── gcc-arm-none-eabi-4_8-2014q2
    ├── nrf51_sdk_v6_0_0_43681
    ├── s110_nrf51822_7.0.0
    ├── s120_nrf51822_1.0.0
    └── s130_nrf51822_0.5.0-1.alpha
    
    5 directories, 0 files
    

5. Configure

5.1 Eclipse - hardway

In case you are insane and have no life play here:
  1. embsysregview: Copy sdks/nrf51/nrf51_sdk_v6_0_0_43681/nrf51822/SVD/nrf51.xml to Applications/eclipseARM/plugins/org.eclipse.cdt.embsysregview.data_0.2.4.r168/data/cortex-m0/Nordic
    cd ~
    mkdir Applications/eclipseARM/plugins/org.eclipse.cdt.embsysregview.data_0.2.4.r168/data/cortex-m0/Nordic
    cp development/sdks/nrf51/nrf51_sdk_v6_0_0_43681/nrf51822/SVD/nrf51.xml Applications/eclipseARM/plugins/org.eclipse.cdt.embsysregview.data_0.2.4.r168/data/cortex-m0/Nordic/ 
    
  2. Configure Environment, add this to the .bashrc (!!!change username to your username!!!)
    cd ~
    export GNU_INSTALL_ROOT=/Users/username/development/sdks/nrf51/gcc-arm-none-eabi-4_8-2014q2
    export GNU_VERSION=4.8.4
    
  3. Now, lets remove Nordics bug from Makefile.posix, just delete these lines (why are they there???)
    GNU_INSTALL_ROOT := /usr/local/gcc-arm-none-eabi-4_8-2014q1
    GNU_VERSION := 4.8.3
    
  4. Now follow the rest of configuration described in n29 v1.0 from 1.2 and make sure you can compile blinky.
  5. Dont rewrite, c-c c-v, into startup window
    mon speed 10000
    mon endian little
    mon flash download = 1 
    mon flash device = NRF51822
    mon reset 0

5.2 Eclipse easy way

If you want time over for your girlfriend just follow these steps:
  1. Create a project how its is described in http://sourceforge.net/p/nrf51osx/wiki/create%20a%20project/
  2. You done!

6 Make that F*** blink

So all done and you happy, almost. Go get this awesome programmer http://sourceforge.net/projects/rknrfgo/. Install, fire up terminal and run:

cdccontrol disable


Then fire up rknrfgo (fancy name) and program your device :) 

6.1 Bug fixing

Everybody is changing everything and fixing bugs all over the places. Which leads that there is HUGE amount of inconsistency in EVERY single post you will read or software you install. Trust no one.
  1. When you created project and if you are using SDK 6+ (and you should) uploading ldscripts/mem.ld will be faulty. To make it work just c-c c-v following (assuming you select softdevice :))
    /* 
    ** Linker script to configure memory regions. 
    **
    ** Device type: xxaa, 256K flash, 16K RAM
    ** Device type: xxab, 128K flash, 16K RAM
    **
    ** Softdevice:  none, all memory available to device,  Code start: 0x0000 , xxaa:0x40000, xxbb:0x20000, RAM start: 0x20000000 
    ** Softdevice:  s110, uses 80K code, 8K RAM,   Code start: 0x16000, xxaa:0x2c000, xxbb:0x0c000, RAM start: 0x20002000 
    ** Softdevice:  s120, uses 96K code, 10K RAM,   Code start: 0x18000, xxaa:0x28000, xxbb:0x08000, RAM start: 0x20002800 
    */ 
    
    GROUP(-lgcc -lc -lnosys)
    
    MEMORY
    {
      FLASH (rx) : ORIGIN = 0x00016000, LENGTH = 0x2C000
      RAM (rwx)  : ORIGIN = 0x20002000, LENGTH = 0x2000 
    }
    
    /*
    ** All permutations, copy one into the MEMORY section above, removing the leading '**'s
    **
    ** xxaa, no softdevice
    **  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x40000  
    **  RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 0x4000 
    **
    ** xxab, no softdevice
    **  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x20000 
    **  RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 0x4000 
    **
    ** xxaa s110
    **  FLASH (rx) : ORIGIN = 0x00016000, LENGTH = 0x2C000
    **  RAM (rwx)  : ORIGIN = 0x20002000, LENGTH = 0x2000 
    **
    ** xxab s110
    **  FLASH (rx) : ORIGIN = 0x00016000, LENGTH = 0x0C000
    **  RAM (rwx)  : ORIGIN = 0x20002000, LENGTH = 0x2000 
    **
    ** xxaa s120
    **  FLASH (rx) : ORIGIN = 0x00018000, LENGTH = 0x28000
    **  RAM (rwx)  : ORIGIN = 0x20002800, LENGTH = 0x1800 
    **
    ** xxab s120
    **  FLASH (rx) : ORIGIN = 0x00018000, LENGTH = 0x08000
    **  RAM (rwx)  : ORIGIN = 0x20002800, LENGTH = 0x1800 
    **
    */
    

7 What you get?! This beauty! :)

Dat Feeling of the #blink! #nordic #ble #internetofthings #iot #sanfrancisco #arduino #california #oneplus #summershots #SoC #nrf51822

Acknowledgment

The biggest thanks goes to the people who spread they knowledge all over the internet, and did load of coding. Go check them out, donate, send flower or fruits!

Roland King (King indeed)
http://sourceforge.net/projects/rknrfgo/
http://sourceforge.net/projects/nrf51osx/

Other sources of ideas
http://hg.cmason.com/nrf
http://robolabwiki.sdu.dk/mediawiki/index.php/Nordic_Semiconductor_nRF51822_development
http://erlblog.lewin.nu/2013/12/setting-up-eclipse-under-mac-os-x-for.html