NRF51822 gcc arm export

@0xc0170 

What follows is my description of the work I've done in order have a GCC_ARM exporter for the nRF51822 target board.  I'm able to successfully export the test export project, unzip it, and run make without any errors.  The .bin file created after running make doesn't seem to do anything once flashed on the device, so there's obviously something wrong (probably many many other things wrong as well), but the work so far should be a good starting point in order to get this exporter out to the public.
I welcome any input, recommendations, and tips.  
I hadn't heard of the word 'mbed' until 2 weeks ago, I received my nRF51822-mbed kit 3 days ago, so I'm very very new to all this, but I'm a professional python developer by day, and a hobbyist embedded developer at night.  It's been quite wonderful working with the python tooling around this project, and it's the only way I could have gotten this far in such a small amount of time.
Anyway, onto the description of my work so far....

On ubuntu 14.04, do the following, otherwise probably skip this step
Install gcc-arm-none-eabi v4.7.4 from here:  https://launchpadlibrarian.net/151487636/gcc-arm-none-eabi-4_7-2013q3-20130916-linux.tar.bz2
The default gcc included in the ubuntu 14.04 repo is 4.8, and does not include nano.specs files needed for compiling.



Add 'GCC_ARM' to NRF51822 supported_toolchains in workspace_tools/targets.py
Build toolchain: python workspace_tools/build.py -m NRF51822 -t GCC_ARM
Looks like it built successfully

Run the basic test:
python workspace_tools/make.py -m NRF51822 -t GCC_ARM -p 0 -v
Looks like it compiles successfully, but linking fails because linker file does not exist

You can see GCC linker scripts in the official SDK from Nordic.
Copy sdk/nrf51822/Source/templates/gcc/gcc_nrf51_blank_xxaa.ld to libraries/mbed/targets/cmsis/TARGET_NORDIC/TARGET_NRF51822/TOOLCHAIN_GCC_ARM/NRF51822.ld 
- 'blank', because I'll worry about adding BLE softdevice later.  I'm just trying to get the most basic thing working. 
- 'xaaa' because that's the 256kb of the chip used in the nrf51822-mbed kit.

Also, don't forget to copy sdk/nrf51822/Source/templates/gcc/gcc_nrf51_common.ld to libraries/mbed/targets/cmsis/TARGET_NORDIC/TARGET_NRF51822/TOOLCHAIN_GCC_ARM/gcc_nrf51_common.ld

Delete build/ and run build.py and make.py again, and it looks like that worked.   basic.bin file created.  Auto flashing/copying .bin to mass storage device failed because it's looking for a .hex file instead.  I added a check to NRF51822 Target, and if the toolchain is GCC_ARM, it sets the OUTPUT_EXT to .bin

At this point you can compile, but the resulting .bin file doesn't seem to do anything when flashed to the device.  I think you need to convert .bin to intel .hex, and probably lots more.

Now I want to get the export 'working'

Add ('gcc_arm', 'NRF51822') to workspace_tools/export_test.py
Run workspace_tools/export_test.py, and notice that it says that NRF51822 is not supported by the toolchain

Add 'NRF51822' to TARGETS list in workspace_tools/export/gccarm.py  run export tests again, and now it'll complain that workspace_tools/export/gcc_arm_nrf51822.tmpl file is missing

Copy workspace_tools/export/gcc_arm_kl25z.tmpl to workspace_tools/export/gcc_arm_nrf51822.tmpl, and change the mcpu from 'cortex-m0plus' to 'cortex-m0'
Now after running the export test again, there will be a build/export/export_gcc_arm_NRF51822.zip file.  SUCCESS.

You can extract this file, and run make, but you'll get a bunch of errors because of some things we haven't taken care of yet.

The test export project seems to use the RTOS library, and that's where most of the errors are coming from.

First fix the missing #defines because of undefined targets #ifndefs in rtos/rtx/RTX_Conf_CM.c
I've no idea if these numbers are correct, but I've added checks for TARGET_NRF51822 which sets the following:
- Make OS_TASKCNT 14
- Make OS_SCHEDULERSTKSIZE 256
- Make OS_CLOCK 16000000  (not sure if it's supposed to be this, or 32768000)

Delete build/export, run export tests again, extract zip, run make, and notice the RTX_Conf_CM.c errors are gone.  

Now take care of the errors in RTX_CM_lib.h.  Define INITIAL_SP to 0x20004000UL.  No idea if that's correct, but I found it googling somewhere, I can't remember.

Again, delete build/export, run export tests again, extract zip, run make, and notice the RTX_CM_lib.h errors are gone, and this time the compilation takes a bit longer than before because it's actually able to compile rtos library.  But you'll notice that there's a warning about not being able to find the symbol Reset_Handler.  That's because we're missing a startup file where those are defined.

Nordic makes startup files available in their official SDK.  Copy sdk/nrf51822/Source/templates/gcc/gcc_startup_nrf51.s to libraries/mbed/targes/cmsis/TARGET_NORDIC/TARGET_NRF51822/TOOLCHAIN_GCC_ARM/startup_NRF51822.s

Delete everything in build , run build, run export tests, extract zip, run make, and notice that a .bin file is created.  SUCCESS.
Flashing that .bin doesn't do anything, and the compilation gave warnings about memory region FLASH and RAM not being declared in gcc_nrf51_common.ld script, even though they're defined in NRF51822.ld

Merge NRF51822.ld and gcc_nrf51_common.ld into a single NRF51822.ld file, so those warnings go away.

Still the .bin doesn't seem to do anything when flashed to the device, but at least something is being exported, and what's exported can be compiled and linked without any errors.
I'm sure you meant 16MHz, not 160MHz :)