mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 22:40:27 +08:00
Add Zephyr as a PX4 platform, so that PX4 can run on the boards and drivers Zephyr supports, and be built and tested with west and twister.
The repository is a Zephyr module (zephyr/module.yml) and a west manifest (platforms/zephyr/west.yml) pinned to Zephyr v4.5.0. An application that enables CONFIG_PX4 gets the PX4 build nested in its Zephyr build: the PX4 modules still come from a .px4board file (boards/zephyr/<board>, or boards/zephyr/generic), and uORB, parameters and the module Kconfig are generated as on the other platforms. Only the submodules this build uses are fetched. Dynamic modules are rejected until they can be built as Zephyr LLEXT extensions.
The platform layer builds on Zephyr's POSIX API, defines __PX4_POSIX like QuRT does, and reuses the POSIX parts that apply:
- hrt counts the cycles of the system timer, with a resolution of a microsecond, and runs its callouts on the kernel tick, which defaults to 100 kHz where the system timer counts that fast, in a k_timer handler in interrupt context.
- PX4 is built as on NuttX: with the -Os of Zephyr, its estimators and controllers with -O2 (MAX_CUSTOM_OPT_LEVEL), and the modules marked UNITY_BUILD as a single compilation unit. The Zephyr compile options go before the options of each target, so that MAX_CUSTOM_OPT_LEVEL overrides the optimization of Zephyr. A board with flash to spare can build it for speed with CONFIG_SPEED_OPTIMIZATIONS.
- The shell has a 4 KiB stack, as PX4 commands run on it, and leaves out the devmem, date and device commands and the wildcards, which PX4 doesn't use. The main thread, which runs the startup script, has a 4 KiB stack.
- Thread priorities stay those of NuttX, and are mapped to those of Zephyr in pthread_attr_setschedparam() and pthread_setschedparam(), which the platform's pthread.h replaces in PX4 code only, leaving the rest of the image on Zephyr's: the top 64, those of the work queues, one to one, and the others below them. Scaled down instead, the offsets of PX4 put the commander above the controllers and the estimator. New thread attributes get explicit scheduling, as Zephyr threads otherwise inherit the priority of the thread that creates them.
- A Zephyr thread runs on the stack of its attributes. Tasks keep their attributes until they are joined, and the platform's pthread.h also replaces pthread_attr_destroy() in PX4 code, to keep the stack of a thread that still runs on it, and to free it once the thread has exited: PX4 destroys the attributes right after creating the work queues, the MAVLink receiver, the log writer and the commander worker thread.
- The common code of PX4 handles where Zephyr differs from the other POSIX platforms, under __PX4_ZEPHYR: tasks and work queues use SCHED_RR, as SCHED_FIFO threads are cooperative on Zephyr, and the work queue priorities are counted from the PX4 SCHED_PRIORITY_MAX; px4_sem_t is a k_sem and the work queue lock masks the interrupts, as on NuttX, since hrt callouts schedule work items from interrupt context; the uORB node lock (ATOMIC_ENTER) masks them too, as on NuttX, since a k_sem has no priority inheritance: a low priority thread preempted while it copies a topic would block the higher priority threads that use it; the work queue threads are started with px4_task_spawn_cmd(), as in the NuttX protected build, so that their attributes are kept; 64-bit atomics, which are not lock-free on Cortex-M, mask the interrupts as on NuttX, with the plain perf counters of the other embedded targets; and PX4_STACK_OVERHEAD comes from Kconfig instead of the 24 KiB of Linux.
- A few headers wrap the Zephyr libc: the Zephyr struct device clashes with PX4's namespace device in C++, so PX4 C++ files see it as struct zephyr_device and the Zephyr drivers are used from C files, and _IO, F_OK and dprintf() are missing.
- The builtin commands and the board's init/rc.px4 lines are compiled into a table. The startup script runs from the application's main(), and the "px4" shell command runs any builtin and returns its exit code. A task that is joined keeps running: it is listed, and px4_task_delete() still stops it. Task ids carry a generation: a task that exits before it is joined is released by the next spawn and keeps its exit code until px4_task_join(), and its id never names the task that reuses its slot.
- The version library reports Zephyr and its kernel version.
platforms/zephyr/app is the reference application, run in QEMU with:
west build -b mps2/an386 PX4-Autopilot/platforms/zephyr/app -t run
or "make zephyr_qemu". "make zephyr" builds it for ZEPHYR_BOARD, and "make zephyr_tests" runs twister. The Zephyr boards are left out of the PX4 config targets and of the CI build matrix, as they are built with west.
Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
69 lines
3.1 KiB
C
69 lines
3.1 KiB
C
/****************************************************************************
|
|
*
|
|
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
// <zephyr/kernel.h> is included here, and its struct device conflicts with namespace device in C++.
|
|
// As visibility.h includes this header first, PX4 C++ files always see struct zephyr_device, and can't
|
|
// use the Zephyr device drivers: C files can.
|
|
#ifdef __cplusplus
|
|
#define device zephyr_device
|
|
#include_next <pthread.h>
|
|
#undef device
|
|
#else
|
|
#include_next <pthread.h>
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// PX4 sets the thread priorities of NuttX, which px4_sched.c maps to those of Zephyr, and destroys the attributes of
|
|
// threads that still run, which px4_pthread.c allows. Only PX4 code, built with this header, calls these functions:
|
|
// the rest of the image keeps the functions of Zephyr.
|
|
int px4_pthread_attr_init(pthread_attr_t *attr);
|
|
int px4_pthread_attr_destroy(pthread_attr_t *attr);
|
|
int px4_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
|
|
int px4_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
|
|
int px4_pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#define pthread_attr_init px4_pthread_attr_init
|
|
#define pthread_attr_destroy px4_pthread_attr_destroy
|
|
#define pthread_attr_setschedpolicy px4_pthread_attr_setschedpolicy
|
|
#define pthread_attr_setschedparam px4_pthread_attr_setschedparam
|
|
#define pthread_setschedparam px4_pthread_setschedparam
|