nRF52840 Advanced Development With the nRF5 SDK
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.
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.
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
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:
Description | Start Address | End Address | Size (KB) |
---|---|---|---|
Bootloader Settings | 0x000FF000 | 0x000FFFFF | 4 |
Master Boot Record Params | 0x000FE000 | 0x000FEFFF | 4 |
Bootloader | 0x000F4000 | 0x000FDFFF | 40 |
Application Code | 0x00026000 | 0x000F3FFF | 824 |
SoftDevice | 0x00001000 | 0x00025FFF | 148 |
Master Boot Record | 0x00000000 | 0x00000FFF | 4 |
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.)