Add a platform
Needs first: How a build becomes an image
You have an RTOS or a bare-metal environment that nano-ros has no platform
for. A platform is the layer under the RMW backend: the clock, sleep, heap,
threads and sockets, exposed as free C functions named nros_platform_*. By
the end of this page your port implements those functions, a board names the
platform, and an image runs on it.
Today a platform is registered inside nano-ros itself, so the last part of this task is a contribution to nano-ros, not a package you keep on your own. Step 5 says what that contribution touches.
Before you start
- Check that you need a platform. If your RTOS is one nano-ros supports (FreeRTOS, Zephyr, NuttX, ThreadX, bare metal, POSIX), you need a board instead: Add a board.
- Know what your RMW backend asks of the platform. XRCE-DDS needs a clock and its transport. zenoh-pico needs the whole surface: a heap of about 64 KB, threads (or single-thread stubs), sockets, a random source and wall time. Cyclone DDS binds the platform’s own socket stack rather than these functions.
- Have the headers that specify the ABI at hand. They live in
packages/platform/nros-platform-api/include/nros/of nano-ros:platform.h(clock, sleep, heap, threads, random, time, critical section),platform_net.h(sockets) andplatform_timer.h(periodic timers). The Platform API and C ABI reference links their rendered documentation.
Steps
-
Scaffold the platform package:
nros new platform myoscreated .../nros-platform-myos/Cargo.toml created .../nros-platform-myos/src/lib.rs created .../nros-platform-myos/nros-platform.toml platform package `myos` scaffolded — next: fill in clock/sleep, then a board package: `nros new board <board> --for-platform myos`src/lib.rsholds stubs for the first three functions a port needs, a monotonicclock_ns, itsclock_resolution_ns, andsleep_ms. -
Choose how you implement the ABI. Both ways produce the same symbols, and the code above the platform cannot tell them apart.
- In C. Write the bodies against your kernel’s own API, as the
FreeRTOS, NuttX, ThreadX, Zephyr and POSIX ports do, in
src/platform.c,src/net.candsrc/timer.c. This is the natural fit when the kernel ships C headers and you build with its C toolchain. - In Rust. Implement the traits of
nros-platform-api(PlatformClock,PlatformAlloc,PlatformSleep,PlatformThreading, …) on a marker type, then export them with the macros ofnros-platform-cffi:nros_platform_export!(MyOsPlatform), andnros_platform_export_net!andnros_platform_export_timer!for the network and timer headers. The bare-metal ports do this, because there is no kernel to write C against.
- In C. Write the bodies against your kernel’s own API, as the
FreeRTOS, NuttX, ThreadX, Zephyr and POSIX ports do, in
-
Implement every function the three headers declare. A port that leaves one out fails to link. When your kernel lacks a primitive, return the value the header documents for it:
nros_platform_task_initreturnsNROS_PLATFORM_RET_UNSUPPORTEDon a port with no tasks, andnros_platform_epoch_usreturns0with no wall clock. Four rules catch most ports:- The clock reads a hardware timer or the kernel tick, never a counter that only moves when something polls it.
- Both mutex kinds inherit priority, as every supported port’s do. The recursive kind lets the same thread lock again: zenoh-pico needs it.
- The random source is seeded from something that differs between two boards (entropy, an address, the wall clock), not from a constant.
- Nothing panics or throws across the C boundary. Return the error codes the header names.
-
State the platform’s facts in
nros-platform.toml.[capabilities]holds what the software stack can do (ip_stack,serial,threads, …): a transport that needs a capability the platform declaresfalseis refused when the image is configured. Settings any C build on the platform needs go at the top of the file, before any table:arch_profiles,compileandrequired_env. Leave the[knobs.*]defaults alone until you have measured a reason to change one. An empty file is valid. -
Register the platform in nano-ros. This is the part that changes nano-ros, and it is a pull request, built as Contributing describes. It touches:
nros-platform: aplatform-myosfeature that pulls in your crate, and its entry in the list that resolvesConcretePlatform. A port that ships only C can use the existingplatform-cffifeature, which takes the symbols from whatever library the image links.- zenoh-pico, when the platform carries it: an ABI marker feature on
zpico-sys(myos = []), and a[port.myos]row in the zenoh backend’snros-rmw.tomlthat says which zenoh-pico system sources, defines and include paths build there. Start from the nearest platform’s row. XRCE-DDS needs no row: it selects its sources by target. - CMake, for C and C++ images:
cmake/platform/nano-ros-myos.cmake, which defines theNanoRos::Platformalias, thenros_platform_<plat>_ifacetarget behind it, thenros_platform_link_app(target)function for per-image link fixups, and theNROS_PLATFORM_LINK_FEATURESdefault.
-
Add a board that names the platform, as Add a board describes, with
platform = "myos"in its descriptor. -
Build and run the talker on that board, and check it against a stock ROS 2 peer:
nros new sensor_ws --board my-board --template talker cd sensor_ws nros run sensor_nodeOn hardware, use
nros flash --monitorinstead ofnros run.
Example
The first functions of a C port, for a kernel called MyOS. Each body is one kernel call; the opaque storage the runtime passes in holds the kernel’s own handle:
#include <nros/platform.h>
#include "myos.h"
uint64_t nros_platform_clock_ns(void) {
return (uint64_t)myos_tick_count() * 1000000u; /* 1 kHz tick */
}
uint64_t nros_platform_clock_resolution_ns(void) {
return 1000000u; /* say the tick honestly */
}
uint64_t nros_platform_epoch_us(void) {
return 0; /* no wall clock */
}
void *nros_platform_alloc(size_t size) { return myos_malloc(size); }
void *nros_platform_realloc(void *ptr, size_t size) {
return myos_realloc(ptr, size);
}
void nros_platform_dealloc(void *ptr) { myos_free(ptr); }
void nros_platform_sleep_ms(size_t ms) { myos_task_delay(ms); }
int8_t nros_platform_mutex_rec_init(void *m) {
myos_mutex_t *mx = (myos_mutex_t *)m;
*mx = myos_mutex_create_recursive();
return *mx ? NROS_PLATFORM_RET_OK : NROS_PLATFORM_RET_ERROR;
}
/* ... every other function declared in platform.h, platform_net.h and
* platform_timer.h ... */
The complete ports are the working examples. The POSIX port,
nros-platform-posix,
is the reference C port. The FreeRTOS port,
nros-platform-freertos,
shows tasks, recursive mutexes and condition variables on an RTOS, and
nros-platform-mps2-an385
is a Rust port for bare metal.
If it doesn’t work
- Timeouts never fire, or fire all at once. The clock advances only when something polls it. Read a free-running hardware timer.
- The session opens, then deadlocks. A mutex zenoh-pico takes twice is not recursive.
- Two boards on one network cannot talk. Both seeded the random source with the same constant, so they opened sessions with the same id.
- Silent memory corruption on an RTOS. A task stack is too small; an RTOS has no guard page. Size it from a measured overflow, not by copying a number.
- Duplicate
z_*symbols at link. The RTOS vendor’s own zenoh-pico system layer already defines them, and so does nano-ros’s alias layer. Turn theplatform-aliasesfeature ofzpico-sysoff for that platform. - Under QEMU, timeouts fire before packets arrive. Run the machine with
-icount shift=auto, so virtual time keeps wall-clock pace while the guest sleeps.
Internals keeps the longer list of lessons from the in-tree ports: Platform porting pitfalls.
See also
- Platform API and C ABI: the headers, the capability groups and the ports to copy.
- Platform differences: the clock, allocator, threading and network of each supported platform.
- Add a transport: for a link that needs no platform of its own.
- Internals → Platform API design: why the ABI is free symbols, and why threads and sockets stay per kernel.
Design background
- RFC-0034: platform layer split: the scalar and opaque-struct services
- RFC-0049: hierarchical platform and board config:
nros-platform.tomland its resolution ladder - RFC-0054: C header ABI single source of truth: why the headers are canonical
- RFC-0073: platform clock in nanoseconds:
clock_nsand its resolution