nRF52840 Advanced Development With the nRF5 SDK

Pages
Contributors: jimblom
Favorited Favorite 5

Building Blinky for the SparkFun nRF52840 Mini Breakout

There's one last bit of setup to do before you can compile and upload the Blinky sketch to your SparkFun nRF52840 Mini Breakout. We need to modify the Makefile so it builds for our board and we need to ensure that, when the code is linked, it doesn't try to overwrite the SoftDevice or the bootloader's home in memory.

Get Used to It

These modifications will need to be made for every example you test out in the nRF5. Don't worry! You'll fall into a rhythm as you continue test out examples in the SDK.

Add a SparkFun_nRF52840_Mini Target

The standard "blinky" example includes build options for the Nordic nRF52832 (pca10040) and nRF52840 (pca10056) development kits. We want to add a build option for the SparkFun nRF52840 Mini Breakout. The easiest way to do this is to copy and duplicate the pca10056 folder and rename it to something like sparkfun_nrf52840_mini.

Copying pca10056 to sparkfun_nrf52840_mini

Once you've copied pca10056 to sparkfun_nrf52840_mini, click into your new folder, then click into the blank and armgcc folders. Then open Makefile.

The most obvious change we need to make to this file is the board definition. Look for CFLAGS and ASMFLAGS definitions for -DBOARD_PCA10056, and modify them to -DBOARD_SPARKFUN_NRF52840_MINI:

language:c
CFLAGS += -DBOARD_SPARKFUN_NRF52840_MINI

and

language:c
ASMFLAGS += -DBOARD_SPARKFUN_NRF52840_MINI

(Make sure to delete any ... += -DBOARD_PCA1056, or similar lines, to ensure the Nordic nRF52840 development kit is not enabled.)

Adding DFU-Programming Targets

We can also use this makefile to call adafruit-nrfutil and load code via serial DFU. To do that, add the following targets to the end of your Makefile:

language:c
dfu-package: $(OUTPUT_DIRECTORY)/nrf52840_xxaa.hex
    @echo Packaging $<
    adafruit-nrfutil dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $<  _build/dfu-package.zip

bootload: $(OUTPUT_DIRECTORY)/nrf52840_xxaa.hex dfu-package
    @echo Flashing: $<
    adafruit-nrfutil --verbose dfu serial --package _build/dfu-package.zip -p $(SERIAL_PORT) -b 115200 --singlebank --touch 1200
Tab and Space Pains

Copying the above text will probably paste four spaces instead of tabs. If you get a compilation error like:

Makefile:305: *** missing separator. Stop.

replace the four spaces before each of the four indented lines with an actual tab.

These two targets provide easy, command-line access to nrfutil's DFU-packaging and uploading features.

Revising the Linker Script's Memory Organization

One last big change you'll need to make is to the memory organization. This example doesn't know we have a bootloader living at address 0x00 in flash.

Open blinky_gcc_nrf52.ld. Then replace the MEMORY section of the linker script with the below memory origin/length combinations:

language:c
MEMORY
{
  FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xce000
  RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 0x40000
}

That will move the flash origin from 0x0 to 0x26000 and limit the available space accordingly. (Still nearly 1MB -- 892kB -- to play with!)

SparkFun Pro nRF52840 Mini Flash Memory Map

As you delve further into nRF52840 development, it may help to know a bit about how the flash memory is mapped. Here's a quick rundown:

DescriptionStart AddressEnd AddressSize (KB)
Bootloader Settings0x000FF0000x000FFFFF4
Master Boot Record Params0x000FE0000x000FEFFF4
Bootloader0x000F40000x000FDFFF40
Application Code0x000260000x000F3FFF824
SoftDevice0x000010000x00025FFF148
Master Boot Record0x000000000x00000FFF4

For more information on the nRF52840's memory map with the S140 programmed, check out Nordic's S140 Memory resource map and usage documentation.

Build the Example

Whew! That's it. To compile the code simply type:

language:makefile
make

Or, if you want to build and program in one swift command:

language:makefile
make bootload SERIAL_PORT=COM7

Make sure your nRF52840 board is in bootloader mode (double-tap reset). (Also make sure to replace "COM7" with your nRF52840's DFU serial port.)

gif showing added build packages and compile

Having a hard time seeing the code? Click the image for a closer look.