Skip to content
systemcoding.dev
Embedded

Build Your First nRF Connect SDK Application With west

Install the nRF Connect SDK v2.9, build a Zephyr blinky for the nRF52840 DK with west build, flash it with west flash and read printk output over serial.

6 min read
Summarize in AI
Flat illustration of RTOS tasks taking turns on a timeline, with the Arm, C and Nordic Semiconductor logos.

The nRF Connect SDK has a reputation for a steep first hour: a toolchain manager, a west workspace, devicetree, Kconfig and a board name with a slash in it. Each piece has a reason, and once you have built one application the rest follows the same pattern. This post goes from nothing installed to a blinking LED and serial output on an nRF52840 DK, explaining what each command does. It uses the nRF Connect SDK v2.9.0, which is based on Zephyr 3.7, and the hardware model v2 board names that SDK versions from 2.7 onward use.

What gets installed

Four things are needed, and the nRF Connect for VS Code extension pack installs all of them from one place.

PieceWhat it is
ToolchainThe Zephyr SDK's arm compiler, CMake, Ninja, Python and west, in a versioned bundle
The SDKA west workspace with Zephyr, nrf, nrfxlib, MCUboot and other modules at matching revisions
nRF Command Line Tools or nrfutilProgramming and the J-Link driver
An editorVS Code with the extension, or a terminal and any editor

If you prefer the terminal, install nrfutil from Nordic, add the toolchain-manager command with nrfutil install toolchain-manager, and install the toolchain bundle that matches v2.9.0. The manager then gives you a shell with the right environment through nrfutil toolchain-manager launch --shell. The extension does the same thing through its interface.

Connect the nRF52840 DK to your computer with the USB cable at the debugger connector, on the short edge by the power switch, not the nRF USB connector at the side. The kit's debugger enumerates as a J-Link device and a virtual serial port.

Get the SDK

In the toolchain shell, create a workspace at a path without spaces:

Shell
west init -m https://github.com/nrfconnect/sdk-nrf --mr v2.9.0 ncs
cd ncs
west update

west init reads the west.yml in the nrf repository at tag v2.9.0, which lists every module and its pinned revision. west update clones them. When it finishes, west list shows the projects:

Text
manifest     nrf              HEAD   ...
nrf          nrf              v2.9.0 ...
zephyr       zephyr           v3.7.99-ncs1 ...
mcuboot      bootloader/mcuboot ...
nrfxlib      nrfxlib          ...

The exact revision strings differ by release. The key point is that Zephyr is a fork pinned by Nordic, so you build with the Zephyr that goes with your nrf revision, not the upstream one.

Make an application

Keep your application outside the SDK tree, in a folder of its own. This is the minimum project:

Text
blinky_log/
  CMakeLists.txt
  prj.conf
  src/
    main.c
CMakeLists.txtcmake
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky_log)

target_sources(app PRIVATE src/main.c)

find_package(Zephyr) pulls in the entire build system, using ZEPHYR_BASE, which the toolchain shell sets. app is a CMake library target that Zephyr creates for your code.

prj.confText
CONFIG_GPIO=y
src/main.cc
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

#define SLEEP_MS 500

static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);

int main(void)
{
	if (!gpio_is_ready_dt(&led)) {
		printk("LED device not ready\n");
		return 0;
	}
	if (gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE) < 0) {
		printk("Cannot configure LED pin\n");
		return 0;
	}

	printk("Blinky running on %s\n", CONFIG_BOARD_TARGET);

	while (1) {
		gpio_pin_toggle_dt(&led);
		k_msleep(SLEEP_MS);
	}
	return 0;
}

This is the same program as Zephyr's own samples/basic/blinky. Three lines deserve a comment. DT_ALIAS(led0) asks the devicetree for the node aliased led0, which the board file defines as an on-board LED. GPIO_DT_SPEC_GET bundles the controller, pin and active level into one constant. printk is the kernel's minimal print function: it needs no logging configuration and writes to the console UART.

Build it

From the application folder, in the toolchain shell:

Shell
west build -b nrf52840dk/nrf52840

The -b flag selects the board target. The form is board/soc, and for a board with several cores a third part names the core, as in nrf5340dk/nrf5340/cpuapp. The first build creates a build/ directory, configures CMake with devicetree and Kconfig, then compiles. The final output reports the memory use:

Text
Memory region         Used Size  Region Size  %age Used
           FLASH:       23764 B       868 KB      2.67%
             RAM:        5008 B       256 KB      1.91%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from .../build/zephyr/zephyr.elf for board: nrf52840dk

The sizes belong to this sample, and your figures vary. If you change the board later and the build behaves strangely, add --pristine (or -p) so that west wipes the old build directory first:

Shell
west build -p always -b nrf52840dk/nrf52840

Inside build/zephyr/ are the files you will use: zephyr.elf for debugging, zephyr.hex and zephyr.bin for programming, .config holding the resolved Kconfig options and zephyr.dts holding the final devicetree.

Flash it and read the output

Shell
west flash

west flash finds the build directory, picks the programming runner configured for the board, erases the sectors it needs, writes the hex and resets the chip. The LED labelled LED1 on the DK starts blinking twice a second. To see the printk line, open the DK's virtual serial port at 115200 baud, 8N1. The port appears as /dev/ttyACM0 on Linux, /dev/cu.usbmodem... on macOS and a COM port on Windows. The DK exposes several ports, and the first is usually the console.

Text
*** Booting nRF Connect SDK v2.9.0-... ***
*** Using Zephyr OS v3.7.99-... ***
Blinky running on nrf52840dk/nrf52840

The two banner lines come from the kernel, and the third is your printk. If you see nothing, the terminal may be attached to the wrong port, so try the others.

Reading the build when something is wrong

Four files answer most questions, all under build/zephyr/:

  • .config shows the final value of every Kconfig symbol. Search it for CONFIG_GPIO to confirm your option took effect.

  • zephyr.dts shows the merged devicetree. Search for led0 to see which pin the alias resolves to.

  • include/generated/devicetree_generated.h holds the macros the build derived from the devicetree.

  • zephyr.map is the linker map, for finding what takes flash.

Two build targets help with size. west build -t ram_report and west build -t rom_report print the memory use per symbol, and they are the first thing to run when the image grows.

Changing configuration without editing files

west build -t menuconfig opens a text interface over the whole Kconfig tree. Navigate, press ? for the help of an option and / to search. Changes you save go to a temporary file under build/, not to prj.conf, so copy the options you want to keep into prj.conf. To pass a one-off option on the command line, append it after --:

Shell
west build -b nrf52840dk/nrf52840 -- -DCONFIG_LOG=y

Common first-hour problems

If west is not found, you are not in the toolchain shell. If the build fails on a missing board, the board name uses the old form, such as nrf52840dk_nrf52840, which hardware model v2 retired in favour of nrf52840dk/nrf52840; the old name works only on SDKs before v2.7. If west flash cannot find the J-Link, check the USB cable (a charge-only cable has no data lines) and the power switch on the DK. If the flash works but nothing blinks, confirm that the kit's LED is the one labelled LED1 and not the power LED.

When not to use west on the command line

If you debug a lot, the VS Code extension adds a debug configuration, a devicetree visual editor and one-click build, and it calls the same west commands. Use it when you want those. The command line is better for scripts and CI, where west build and west flash run unchanged.

Also inRTOS
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.