Skip to content
systemcoding.dev
RTOS

Flash and Debug Zephyr Boards With west flash and west debug

Program a board with west flash, pick a runner such as openocd, jlink or nrfjprog, and step through code with west debug and GDB. Includes failures.

6 min read
Summarize in AI
Flat illustration of firmware being flashed from a terminal onto a chip, with the C, Espressif and PlatformIO logos.

Building a Zephyr image is half the job. The other half is getting it onto the chip and watching it run, and west wraps that in two commands: west flash and west debug. They do not talk to hardware themselves. They call a runner, which is a thin wrapper around a vendor or open source tool such as OpenOCD, J-Link or nrfjprog. This post explains how a runner is chosen, how to override the choice, and how to start a GDB session in Zephyr 4.2. The build side is covered in the post on application structure.

What a runner is

Every board's board.cmake file declares which runners it supports and which is the default. A runner is a Python class in scripts/west_commands/runners/, and it knows how to turn zephyr.elf, zephyr.hex or zephyr.bin into the command line of an external tool. List what your build supports:

Shell
west flash --context
Text
available runners in runners.yaml:
  nrfjprog, jlink, pyocd, openocd
default runner in runners.yaml:
  nrfjprog

The exact list depends on the board. The text above is what an nRF52840 development kit prints. The data comes from build/zephyr/runners.yaml, generated at build time, so you must have built first.

Flash with the default runner

From the application directory, after a successful west build:

Shell
west flash

For the nRF52840 kit, which defaults to nrfjprog, the output ends like this:

Text
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner nrfjprog
-- runners.nrfjprog: mass erase requested: False
Using board 683123456
-- runners.nrfjprog: Flashing file: build/zephyr/zephyr.hex
[#################] 100.000%
Applying pin reset.
-- runners.nrfjprog: Board with serial number 683123456 flashed successfully.

The serial number is your probe's. west flash rebuilds first if sources changed, so you cannot flash a stale image by accident. The chip resets and starts running when flashing finishes.

Choose a runner explicitly

When a board supports more than one runner, pick one with --runner:

Shell
west flash --runner openocd
west flash --runner jlink

OpenOCD needs an interface configuration and a target configuration, and board files usually supply both. Anything runner specific goes after -- or in a named flag. For example, to program a chosen probe by its serial number with J-Link:

Shell
west flash --runner jlink --dev-id 000683123456

--dev-id is the common name for "which probe", and each runner maps it to its own option. A command that works for one runner can be silently different for another, so read west flash --help for the runner you chose.

Erase and reset behavior

Two options change what gets written:

OptionEffectUse it when
--eraseMass erase before programmingOld firmware blocks debug access, or flash is protected
--no-resetLeave the chip halted after flashYou want to attach a debugger before the first instruction
--hex-file, --bin-fileFlash a specific fileYou are shipping a pre-built image

--erase wipes the entire flash, including any bootloader and stored settings, so do not use it casually on a board that has a bootloader you rely on.

Start a debug session

west debug launches the runner's GDB server and connects GDB to it. It needs the same runner support, and it needs a build with symbols, which Zephyr produces by default as zephyr.elf:

Shell
west debug

For OpenOCD it starts the server, loads the image and stops at the reset vector:

Text
-- west debug: rebuilding
-- west debug: using runner openocd
Open On-Chip Debugger 0.12.0
Info : Listening on port 3333 for gdb connections
Reading symbols from build/zephyr/zephyr.elf...
0x0000a2c0 in z_arm_reset ()
(gdb)

The address varies between builds. From here, the usual GDB commands apply. Set a breakpoint at main, continue, and look at a thread:

Text
(gdb) break main
Breakpoint 1 at 0x1f2c: file src/main.c, line 8.
(gdb) continue
Continuing.
Breakpoint 1, main () at src/main.c:8
8		int count = 0;
(gdb) next
10		while (1) {
(gdb) info threads

If you only want the server, so you can use your own GDB or an IDE, use west debugserver. It prints the port and stays running.

Attach without resetting

To inspect a board that is already running, use west attach. It connects GDB without loading or resetting:

Shell
west attach

This is the right choice for a hang in the field that you do not want to disturb. Be aware that attaching halts the CPU, and any watchdog that keeps running will reset the chip a few seconds later.

Debug-friendly build settings

Optimization can make stepping confusing, because the compiler reorders and removes lines. Add this to prj.conf while debugging:

prj.confText
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_THREAD_NAME=y

CONFIG_DEBUG_OPTIMIZATIONS lowers the compiler's optimization level, and CONFIG_THREAD_NAME gives threads names that GDB's thread list can show with Zephyr-aware tooling. Remove them for the shipping build. The flags are listed in the Zephyr debugging documentation.

Common failures

"No J-Link device found" or the probe is not seen. Check the cable and the board's debug switch. Some kits need a jumper set to route SWD to the onboard probe.

"Error: init mode failed" from OpenOCD. The chip is in a state OpenOCD cannot attach to, often because the previous firmware put it to sleep or remapped the debug pins. Hold reset while connecting, or run west flash --erase once.

The runner is missing. west flash reports that the tool is not on PATH. Install the vendor tool, or point to it with the runner's --tool-opt argument.

Flashing works but nothing runs. The board may boot through a bootloader that expects a signed image. Read the board documentation page before assuming a bug in your code.

Permission denied on USB. On Linux, install the probe's udev rules from the vendor package, and add your user to the right group.

When these commands are not enough

For production programming across many boards, west flash is too interactive. Use the vendor's command line tool directly in a script, driven by the zephyr.hex file. For deeply embedded targets with trace needs, use the vendor IDE or a trace probe. The west commands shine for the edit, build, flash and step cycle on a development kit.

A worked session: find why a thread never runs

Suppose a worker thread never prints. A debugger answers this faster than log statements. Start west debug, then stop the target after a few seconds with Ctrl+C and list the threads Zephyr knows about. With the Zephyr-aware GDB support in the SDK, info threads shows each thread's state:

Text
(gdb) info threads
  Id   Target Id                 Frame
* 1    Thread 536871128 (main)   z_arm_pendsv () at arch/arm/core/cortex_m/swap_helper.S:80
  2    Thread 536871416 (worker) z_arm_pendsv () at arch/arm/core/cortex_m/swap_helper.S:80

Thread ids and frames will differ on your build. Switch to the worker with thread 2, then run bt to see where it is blocked. A worker sitting in z_pend_curr is waiting on a kernel object, which usually means a semaphore nobody gives. This is a cheap way to turn "it hangs" into "it waits on that object", and the thread-aware features require CONFIG_DEBUG_THREAD_INFO=y in prj.conf for some runners.

Keep the debug commands in version control

Teams waste time re-deriving the right runner arguments. Put them in a west.yml extension or a short shell script next to the application, for instance tools/flash.sh that runs west flash --runner jlink --dev-id "$PROBE_ID". Reading the probe id from an environment variable keeps one script working across desks. The build/zephyr/runners.yaml file also records the arguments west would use by default, which is useful to read when a runner behaves unexpectedly.

Also inDebugging
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.