Developing applications for Nordic nRF51 with mbed

This tutorial shows how to use the mbed framework to develop applications for Nordic nRF51 devices. Before you begin, install VisualGDB 5.1 or later.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:01-prjname
  2. Proceed with the default settings on the first page. Ensure you have the “enable multi-threaded build” option enabled if you are using a machine with multiple cores:02-newprj
  3. On the next page select “Use ARM mbed” and then pick your nRF51 board from the list. In this example we will use the nRF51_DK board:03-nrf51It is recommended to select “newlib-nano” as the C library instead of “default” as it will save considerable FLASH and RAM space.
  4. On the next page select the default “LEDBlink” sample and press “Next”:04-sample
  5. On the next page select Segger J-Link as the debug method and specify the exact device type manually (the auto-filled nRF51 is too generic): 05-debug
  6. Press “Finish” to create your project and build it by pressing Ctrl-Shift-B:06-build
  7. If you try to debug the project now, you will most likely see something like this and the project won’t start properly:07-noframeThis happens because the mbed port of nRF51 does not include the softdevice into your project and expects you to program it separately. The rest of this tutorial will show how to include the softdevice binary into your project so that it gets programmed automatically.
  8. The mbed SDK contains the binaries for the softdevice, however they are stored in a format that requires some extra steps in order to be embedded into a project. VisualGDB’s package for nRF51 devices actually comes with a script that does the necessary conversion. To use it locate the %LOCALAPPDATA%\VisualGDB\EmbeddedBSPs\arm-eabi\com.sysprogs.arm.nordic.nrf5x\nRF51\SoftdeviceLibraries folder and copy the ConvertSoftdevice.bat and empty.c files to %LOCALAPPDATA%\VisualGDB\EmbeddedBSPs\arm-eabi\com.sysprogs.arm.mbed\libraries\mbed\targets\hal\TARGET_NORDIC\TARGET_MCU_NRF51822\Lib\s130_nrf51822_1_0_0.09-copyfileIf you cannot find the %LOCALAPPDATA%\VisualGDB\EmbeddedBSPs\arm-eabi\com.sysprogs.arm.nordic.nrf5x folder, create a regular nRF51 project with VisualGDB and the necessary files will be automatically downloaded.
  9. Edit the first line of the ConvertSoftdevice.bat file to reflect the location of your ARM toolchain: 10-batfile
  10. Run the bat file with the following arguments:

    11-convertEnsure that the sXXX_softdevice.o file was created. The file contains the softdevice code wrapped as an object file, so you can simply link your project with it as long as the linker script specifies where to place it.
  11. Open VisualGDB Project Properties and go to the Makefile settings page. Locate the “Linker script” field and press “Make a local copy”:08-linkerscrip
  12. The copied linker script will appear under “Make files” in Solution Explorer. Open it and add a FLASH_SOFTDEVICE section with ORIGIN = 0 and LENGTH equal to the ORIGIN of the FLASH section already present in the script: 12-memory
  13. Go to the SECTIONS section and add the following section definition to the beginning of it:

    Replace 0x0001C000 with the actual start of the FLASH memory shown in the previous step.13-section
  14. Finally go to the Makefile Settings page of VisualGDB Project Properties and add the sXXX_softdevice.o file generated by running ConvertSoftdevice.bat to the additional linker inputs. This will ensure that the contents of the softdevice file will be actually placed in the FLASH_SOFTDEVICE memory you defined in the linker script:14-input
  15. Now you can build your project and hit F5 to begin debugging it. The LED should now blink properly and you should be able to step through the code:15-bkpt
  16. If you want to use the s110 softdevice instead of s130, simply repeat the conversion steps with it and edit the FLASH and RAM sizes and offsets in the linker script to reflect the size of the S110 device as shown in this post.