Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Zephyr on mps2-an385

How-to · Level 2: Building real applications

Needs first: Run a talker on an emulated board, What you just built, Get started on mps2-an385 in QEMU (Zephyr)

Status: nros-board-mps2-an385-zephyr: Verified in emulator (daily). From the board support registry; Board Support Levels explains the words.

The starter made a west workspace for Zephyr 3.7, scaffolded a Rust talker for board = "mps2-an385-zephyr", Zephyr’s mps2/an385 Cortex-M3 under QEMU, and heard it from ROS 2. This page covers what comes after it: what is particular to the board, naming a Zephyr board nano-ros does not ship, extending the build the Zephyr way, and reading the run plan. Where an image’s nano-ros and Zephyr settings go, one image per RMW, and nros build’s hand-off to west are the same on every Zephyr board; the Zephyr native_sim page covers them.

Configure the platform

The board’s own configuration puts the image on QEMU’s user-mode network: its prj.conf gives the guest 10.0.2.15, with gateway 10.0.2.2, and the locator tcp/10.0.2.2:7447. nros build applies it, so a scaffolded image states nothing. The gateway is your host, so a router listening on the host’s 0.0.0.0:7447 is reachable, as the starter’s was. An image whose router listens elsewhere states its own locator:

[image.zephyr]
board = "mps2-an385-zephyr"
locator = "tcp/10.0.2.2:7800"

The image’s other nano-ros settings (rmw, knobs, os_config fragments) are the same keys as on every Zephyr board; see Configure the platform.

Board notes

  • Zephyr’s mps2/an385 under QEMU. The image uses Zephyr’s own IP stack on the emulated SMSC911x ethernet. nros run boots it under the QEMU the Zephyr SDK ships, qemu:mps2-an385.
  • The west workspace needs nano-ros’s patches for this board. nros setup board mps2-an385-zephyr, run from inside that workspace, applies them and adds the board’s Rust target. From elsewhere, name the workspace with --zephyr-workspace <ws>, or point $ZEPHYR_BASE into it. Without them, zephyr-lang-rust’s cargo command carries none of nano-ros’s settings for the board, among them the C cross compiler the zenoh-pico crate builds with, and a Rust image fails to build.
  • Readiness. Expect the first Publishing: line within about 15 seconds of boot: a cold QEMU boot and Zephyr’s start-up come first.
  • Nothing is flashed. mps2/an385 is a QEMU machine. A Zephyr board you can hold, with a twin that boots the same image, is the Nucleo-F767ZI.

Name a Zephyr board nano-ros does not ship

An image’s board names a board in nano-ros’s catalog, and the board’s descriptor states the Zephyr id west build -b receives. So an image writes board = "mps2-an385-zephyr", never board = "mps2/an385": an image that names a Zephyr id is refused with the id to write instead. A board the catalog does not know is refused with the list of ones it does, because the descriptor supplies more than the -b string: the platform, the toolchain and the entry kind.

A board is a package. Put an nros-board.toml beside a package’s package.xml in your workspace, and it joins the catalog:

# src/my_board/nros-board.toml
[[board]]
names = ["my-board"]              # what your images say
west_board = "qemu_cortex_m3"     # what west is given
platform = "zephyr"
toolchain = "stable"
platform_feature = "platform-zephyr"
link_kind = "none"
entry_kind = "zephyr-staticlib"

Then an image names it, board = "my-board", and builds for it. For a board shared by several workspaces, list directories shaped like packages/boards/ in $NROS_EXTRA_BOARD_PATH. nros new board <name> --for-platform zephyr scaffolds the descriptor. That is nano-ros’s descriptor only: an out-of-tree Zephyr board definition (its devicetree and board.yml) comes in the Zephyr way, through your module’s board_root. Add a board takes a board through the rest, and Flash a real board flashes it.

A board whose vendor HAL is a Zephyr module your west manifest does not list states it in its descriptor, relative to the west workspace:

[board.zephyr]
modules = ["modules/hal/nxp"]

nros build adds each one to the modules it passes. If the workspace has no such directory, the build stops and names it: add the project to the west manifest and run west update. Don’t pass -DZEPHYR_EXTRA_MODULES in an image’s cmake_args; nros build refuses it, because that variable already carries nano-ros’s own module.

Extend the build the Zephyr way

nros build runs a plain west build, so every Zephyr extension point is untouched. The second row edits the application’s CMakeLists.txt, so it needs an application you own (nros materialize <image>); the rest do not:

You wantYou do
your own Kconfig symbols, defined by a workspace packagea zephyr/module.yml in that package; see below
a module from outside the workspacelist(APPEND ZEPHYR_EXTRA_MODULES <dir>) before find_package(Zephyr)
a driver in that modulezephyr_library() and zephyr_library_sources(), behind your own Kconfig
its devicetree bindingdts/bindings/, with settings: {dts_root: .} in the module’s zephyr/module.yml
an out-of-tree board, SoC or snippetthe matching board_root, soc_root or snippet_root in that module.yml
your Kconfig on for one imagea .conf fragment in the image’s os_config, passed as EXTRA_CONF_FILE
a devicetree overlay for one imagean .overlay in the same list, passed as EXTRA_DTC_OVERLAY_FILE

Only the last two rows are nano-ros’s, and they are how an image differs from its siblings:

[image.zephyr]
board = "mps2-an385-zephyr"
os_config = ["prj-user-extra.conf", "user-widget.overlay"]

There is no image key for arbitrary -D defines: something the application always needs belongs in its CMakeLists.txt. A mistake in your module surfaces as Zephyr’s own error, unchanged, because the build is a plain west build.

A workspace package that is a Zephyr module

An os_config fragment assigns Kconfig symbols, but it cannot define one: Kconfig refuses CONFIG_MY_MODE=y for a symbol nothing declares. The package that reads the symbols defines them, as a Zephyr module. Give it a zephyr/module.yml:

# src/my_support/zephyr/module.yml
name: my_support
build:
  kconfig: zephyr/Kconfig

and the definitions in src/my_support/zephyr/Kconfig, in Zephyr’s own syntax:

# src/my_support/zephyr/Kconfig
config MY_MODE
	bool "Turn on my_support's mode"
	help
	  Read by my_support at build time.

The image’s fragment then sets it, as for any Zephyr symbol:

# prj-user-extra.conf, named in the image's os_config
CONFIG_MY_MODE=y

When a node package of the image <depend>s on my_support, directly or through other workspace packages, nros build adds it to ZEPHYR_EXTRA_MODULES in the application it generates, or on the west build line of an entry you own, so its symbols are defined and the image’s os_config fragment can set them.

Who builds the package’s CMakeLists.txt depends on the module’s build: cmake: key:

  • No cmake: key, as above: the module only defines Kconfig symbols. The generated application adds the package with add_subdirectory, as it adds every workspace package the image depends on, not only the node packages the launch file names.
  • cmake: .: Zephyr builds the package’s own CMakeLists.txt as the module, so the application does not add it a second time.
  • cmake: <another directory>: Zephyr builds that directory as the module, and the application still adds the package’s own CMakeLists.txt.

The same package works unchanged in an application you build with west yourself.

Read the run plan

From the starter’s workspace, print the plan nros run follows:

nros run zm_node --dry-run --why

The plan names the QEMU runner and the qemu:mps2-an385 machine, the console, and the link to the router the board’s configuration names. --why names where each fact came from.

Drive west yourself

Print the west build line nros build runs, with the image’s overlays, without building:

nros build --dry-run

Drive west yourself explains what goes to west and what is refused.

C++ applications

A Zephyr application reaches the C++ API through the nano-ros Zephyr module. The module, the image’s system.toml and the prj.conf split are covered above, in Configure the platform; the RMW, locator and domain come from the image, never from CONFIG_NROS_* lines in prj.conf. The C++ side needs CONFIG_CPP=y and CONFIG_STD_CPP14=y in prj.conf (Zephyr’s own settings), and the application’s CMakeLists.txt generates the message bindings:

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(my_app LANGUAGES CXX)

nros_generate_interfaces(std_msgs LANGUAGE CPP)
target_sources(app PRIVATE src/main.cpp)

A minimal main() waits for the network, opens the session with the locator and domain the image rendered into Kconfig, and spins:

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

#include <nros/platform_zephyr.h>

#include <rclcpp/rclcpp.hpp>
#include "std_msgs.hpp"

LOG_MODULE_REGISTER(my_app, LOG_LEVEL_INF);

int main(void)
{
    nros_platform_zephyr_wait_network(CONFIG_NROS_INIT_DELAY_MS);

    rclcpp::Result ret =
        rclcpp::init_in(CONFIG_NROS_LOCATOR, CONFIG_NROS_DOMAIN_ID);
    if (!ret.ok()) return 1;

    rclcpp::Node node;
    if (!rclcpp::create_node(node, "my_node").ok()) return 1;

    // ... create publishers, subscriptions, etc.

    while (true) {
        rclcpp::spin_once(100);
    }
}

nros_platform_zephyr_wait_network (<nros/platform_zephyr.h>) is defined in every Zephyr image. On an image with no IP stack (CONFIG_NET_SOCKETS off, for example a serial-only zenoh image) it returns 0 at once, so the call needs no guard. CONFIG_NROS_INIT_DELAY_MS is the module’s start-up delay; your main() is its only reader.

If it doesn’t work

  1. Confirm the router listens on the host’s 0.0.0.0 at the image’s port. A router on 127.0.0.1 is unreachable from the guest.
  2. A Rust image that fails to build naming its C compiler or target: run nros setup board mps2-an385-zephyr again from inside the west workspace. Check that the workspace its first line names is the one you build in.
  3. No Publishing: line within 30 seconds of boot: check the console for the network coming up, then Troubleshoot.

Reference

Design background

  • RFC-0122: behaviour bringups, single-node witnesses, and cadence-backed CI.