mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 22:40:27 +08:00
feat(zephyr): add Zephyr RTOS platform
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>
This commit is contained in:
1 parent
8351cc0046
commit
04ac816ab0
48 files changed
+2598
-58
No files matched your search
@@ -19,6 +19,8 @@ menu "Toolchain"
|
||||
bool "qurt"
|
||||
config PLATFORM_ROS2
|
||||
bool "ros2"
|
||||
config PLATFORM_ZEPHYR
|
||||
bool "zephyr"
|
||||
endchoice
|
||||
|
||||
config BOARD_PLATFORM
|
||||
@@ -27,6 +29,7 @@ menu "Toolchain"
|
||||
default "posix" if PLATFORM_POSIX
|
||||
default "qurt" if PLATFORM_QURT
|
||||
default "ros2" if PLATFORM_ROS2
|
||||
default "zephyr" if PLATFORM_ZEPHYR
|
||||
|
||||
config BOARD_LOCKSTEP
|
||||
bool "Force enable lockstep"
|
||||
@@ -291,7 +294,7 @@ source "src/templates/Kconfig"
|
||||
endmenu
|
||||
|
||||
menu "platforms"
|
||||
depends on PLATFORM_QURT || PLATFORM_POSIX || PLATFORM_NUTTX
|
||||
depends on PLATFORM_QURT || PLATFORM_POSIX || PLATFORM_NUTTX || PLATFORM_ZEPHYR
|
||||
source "platforms/Kconfig"
|
||||
endmenu
|
||||
|
||||
|
||||
@@ -233,8 +233,8 @@ define colorecho
|
||||
+@echo -e '${COLOR_BLUE}${1} ${NO_COLOR}'
|
||||
endef
|
||||
|
||||
# Get a list of all config targets boards/*/*.px4board
|
||||
ALL_CONFIG_TARGETS := $(shell find boards -maxdepth 3 -mindepth 3 -name '*.px4board' -print | sed -e 's|boards\/||' | sed -e 's|\.px4board||' | sed -e 's|\/|_|g' | sort)
|
||||
# Get a list of all config targets boards/*/*.px4board (Zephyr boards are built with west)
|
||||
ALL_CONFIG_TARGETS := $(shell find boards -maxdepth 3 -mindepth 3 -name '*.px4board' -not -path 'boards/zephyr/*' -print | sed -e 's|boards\/||' | sed -e 's|\.px4board||' | sed -e 's|\/|_|g' | sort)
|
||||
|
||||
# ADD CONFIGS HERE
|
||||
# --------------------------------------------------------------------
|
||||
@@ -268,6 +268,22 @@ all_default_targets: $(CONFIG_TARGETS_DEFAULT)
|
||||
updateconfig:
|
||||
@./Tools/kconfig/updateconfig.py
|
||||
|
||||
# Zephyr targets, built with west in a Zephyr workspace (see platforms/zephyr)
|
||||
# --------------------------------------------------------------------
|
||||
.PHONY: zephyr zephyr_qemu zephyr_tests
|
||||
|
||||
ZEPHYR_BOARD ?= mps2/an386
|
||||
ZEPHYR_BUILD_DIR = $(SRC_DIR)/build/zephyr_$(subst /,_,$(ZEPHYR_BOARD))
|
||||
|
||||
zephyr:
|
||||
@west build -b $(ZEPHYR_BOARD) -d "$(ZEPHYR_BUILD_DIR)" "$(SRC_DIR)/platforms/zephyr/app"
|
||||
|
||||
zephyr_qemu:
|
||||
@west build -b mps2/an386 -d "$(SRC_DIR)/build/zephyr_mps2_an386" "$(SRC_DIR)/platforms/zephyr/app" -t run
|
||||
|
||||
zephyr_tests:
|
||||
@west twister -T "$(SRC_DIR)/platforms/zephyr" -p mps2/an386 -O "$(SRC_DIR)/build/zephyr_twister"
|
||||
|
||||
# board reorganization deprecation warnings (2018-11-22)
|
||||
define deprecation_warning
|
||||
$(warning $(1) has been deprecated and will be removed, please use $(2)!)
|
||||
|
||||
@@ -45,7 +45,7 @@ grouped_targets = {}
|
||||
# which the CI image does not have.
|
||||
excluded_boards = ['px4_ros2', 'espressif_esp32', 'aifoundry_erbium']
|
||||
excluded_manufacturers = ['atlflight']
|
||||
excluded_platforms = []
|
||||
excluded_platforms = ['zephyr'] # built with west, see platforms/zephyr
|
||||
|
||||
# Container overrides for platforms/boards that need a non-default container
|
||||
platform_container_overrides = {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
CONFIG_PLATFORM_ZEPHYR=y
|
||||
CONFIG_BOARD_ROMFSROOT=""
|
||||
CONFIG_SYSTEMCMDS_PARAM=y
|
||||
CONFIG_SYSTEMCMDS_PERF=y
|
||||
CONFIG_SYSTEMCMDS_REBOOT=y
|
||||
CONFIG_SYSTEMCMDS_TOPIC_LISTENER=y
|
||||
CONFIG_SYSTEMCMDS_UORB=y
|
||||
CONFIG_SYSTEMCMDS_VER=y
|
||||
CONFIG_SYSTEMCMDS_WORK_QUEUE=y
|
||||
@@ -0,0 +1 @@
|
||||
ver all
|
||||
@@ -0,0 +1,36 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
add_library(drivers_board
|
||||
board_shutdown.cpp
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file board_config.h
|
||||
*
|
||||
* Generic Zephyr board definitions
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define BOARD_OVERRIDE_UUID "ZEPHYRGENERIC000" // must be of length 16
|
||||
#define PX4_SOC_ARCH_ID PX4_SOC_ARCH_ID_UNUSED
|
||||
|
||||
#define BOARD_HAS_POWER_CONTROL 1
|
||||
#define CONFIG_BOARDCTL_RESET
|
||||
|
||||
#define PX4_NUMBER_I2C_BUSES 1
|
||||
|
||||
#include <system_config.h>
|
||||
#include <px4_platform_common/board_common.h>
|
||||
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <board_config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <zephyr/sys/poweroff.h>
|
||||
|
||||
int board_register_power_state_notification_cb(power_button_state_notification_t cb)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int board_power_off(int status)
|
||||
{
|
||||
#if defined(CONFIG_POWEROFF)
|
||||
sys_poweroff();
|
||||
#endif
|
||||
return -ENOTSUP;
|
||||
}
|
||||
@@ -91,7 +91,7 @@ function(px4_add_module)
|
||||
REQUIRED MODULE MAIN
|
||||
ARGN ${ARGN})
|
||||
|
||||
if(UNITY_BUILD AND (${PX4_PLATFORM} STREQUAL "nuttx"))
|
||||
if(UNITY_BUILD AND (${PX4_PLATFORM} MATCHES "^(nuttx|zephyr)$"))
|
||||
# build standalone test library to catch compilation errors and provide sane output
|
||||
add_library(${MODULE}_original STATIC EXCLUDE_FROM_ALL ${SRCS})
|
||||
if(DEPENDS)
|
||||
@@ -134,6 +134,9 @@ function(px4_add_module)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
elseif(DYNAMIC AND (${PX4_PLATFORM} STREQUAL "zephyr"))
|
||||
message(FATAL_ERROR "${MODULE}: dynamic modules are not supported on Zephyr yet")
|
||||
|
||||
elseif(DYNAMIC AND MAIN AND (${PX4_PLATFORM} STREQUAL "posix"))
|
||||
add_library(${MODULE} SHARED ${SRCS})
|
||||
target_compile_definitions(${MODULE} PRIVATE ${MAIN}_main=px4_module_main)
|
||||
|
||||
@@ -74,7 +74,9 @@
|
||||
#if defined(__PX4_NUTTX)
|
||||
# include <nuttx/config.h>
|
||||
# include <nuttx/irq.h>
|
||||
#endif // __PX4_NUTTX
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
# include <zephyr/irq.h>
|
||||
#endif
|
||||
|
||||
// On a single-core NuttX build the hardware memory barrier (`dmb`) a seq_cst atomic
|
||||
// emits is unnecessary for inter-thread ordering; a compiler-only fence is sufficient
|
||||
@@ -93,7 +95,7 @@ class atomic
|
||||
{
|
||||
public:
|
||||
|
||||
#if defined(__PX4_POSIX)
|
||||
#if defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR)
|
||||
// Ensure that all operations are lock-free, so that 'atomic' can be used from
|
||||
// IRQ handlers. This might not be required everywhere though.
|
||||
static_assert(__atomic_always_lock_free(sizeof(T), 0), "atomic is not lock-free for the given type T");
|
||||
@@ -107,18 +109,18 @@ public:
|
||||
*/
|
||||
inline T load() const
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
// Not lock-free (e.g. 64-bit on a 32-bit core): a critical section
|
||||
// provides both atomicity and full ordering.
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T val = _value;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
// Single core: a relaxed load (a plain, non-tearing ldr) plus a
|
||||
@@ -139,16 +141,16 @@ public:
|
||||
*/
|
||||
inline void store(T value)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
_value = value;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -164,17 +166,17 @@ public:
|
||||
*/
|
||||
inline T fetch_add(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T ret = _value;
|
||||
_value += num;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -192,17 +194,17 @@ public:
|
||||
*/
|
||||
inline T fetch_sub(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T ret = _value;
|
||||
_value -= num;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -220,17 +222,17 @@ public:
|
||||
*/
|
||||
inline T fetch_and(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T val = _value;
|
||||
_value &= num;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -248,17 +250,17 @@ public:
|
||||
*/
|
||||
inline T fetch_xor(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T val = _value;
|
||||
_value ^= num;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -276,17 +278,17 @@ public:
|
||||
*/
|
||||
inline T fetch_or(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T val = _value;
|
||||
_value |= num;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -304,17 +306,17 @@ public:
|
||||
*/
|
||||
inline T fetch_nand(T num)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
T ret = _value;
|
||||
_value = ~(_value & num);
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -336,24 +338,24 @@ public:
|
||||
*/
|
||||
inline bool compare_exchange(T *expected, T desired)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_ZEPHYR)
|
||||
|
||||
if (!__atomic_always_lock_free(sizeof(T), 0)) {
|
||||
irqstate_t flags = enter_critical_section();
|
||||
const auto flags = lock();
|
||||
|
||||
if (_value == *expected) {
|
||||
_value = desired;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
*expected = _value;
|
||||
leave_critical_section(flags);
|
||||
unlock(flags);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __PX4_NUTTX
|
||||
#endif // __PX4_NUTTX || __PX4_ZEPHYR
|
||||
|
||||
#if PX4_ATOMIC_SINGLE_CORE
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
@@ -367,6 +369,14 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
#if defined(__PX4_NUTTX)
|
||||
static irqstate_t lock() { return enter_critical_section(); }
|
||||
static void unlock(irqstate_t flags) { leave_critical_section(flags); }
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
static unsigned int lock() { return irq_lock(); }
|
||||
static void unlock(unsigned int key) { irq_unlock(key); }
|
||||
#endif
|
||||
|
||||
T _value {};
|
||||
};
|
||||
|
||||
|
||||
@@ -76,7 +76,11 @@ typedef pollevent_t px4_pollevent_t;
|
||||
|
||||
#elif defined(__PX4_POSIX)
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
#define PX4_STACK_OVERHEAD CONFIG_PX4_STACK_OVERHEAD
|
||||
#else
|
||||
#define PX4_STACK_OVERHEAD (1024 * 24)
|
||||
#endif
|
||||
|
||||
#define px4_cache_aligned_data()
|
||||
#define px4_cache_aligned_alloc malloc
|
||||
|
||||
@@ -98,6 +98,11 @@ private:
|
||||
void work_lock() { _flags = enter_critical_section(); }
|
||||
void work_unlock() { leave_critical_section(_flags); }
|
||||
irqstate_t _flags;
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
// In Zephyr work can be enqueued from an ISR (hrt callouts)
|
||||
void work_lock() { _flags = irq_lock(); }
|
||||
void work_unlock() { irq_unlock(_flags); }
|
||||
unsigned int _flags;
|
||||
#else
|
||||
// loop as the wait may be interrupted by a signal
|
||||
void work_lock() { do {} while (px4_sem_wait(&_qlock) != 0); }
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
|
||||
#include <semaphore.h>
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
#include <zephyr/kernel.h>
|
||||
#endif
|
||||
|
||||
#if !defined(__PX4_NUTTX)
|
||||
/* Values for protocol attribute */
|
||||
|
||||
@@ -54,11 +58,16 @@
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
// k_sem, so that it can be posted from interrupt context
|
||||
typedef struct k_sem px4_sem_t;
|
||||
#else
|
||||
typedef struct {
|
||||
pthread_mutex_t lock;
|
||||
pthread_cond_t wait;
|
||||
int value;
|
||||
} px4_sem_t;
|
||||
#endif
|
||||
|
||||
__EXPORT int px4_sem_init(px4_sem_t *s, int pshared, unsigned value);
|
||||
__EXPORT int px4_sem_setprotocol(px4_sem_t *s, int protocol);
|
||||
|
||||
@@ -65,7 +65,12 @@ typedef int px4_task_t;
|
||||
#include <sched.h>
|
||||
|
||||
/** Default scheduler type */
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
// SCHED_FIFO threads are cooperative on Zephyr
|
||||
#define SCHED_DEFAULT SCHED_RR
|
||||
#else
|
||||
#define SCHED_DEFAULT SCHED_FIFO
|
||||
#endif
|
||||
|
||||
#if defined(__PX4_LINUX) || defined(__PX4_DARWIN) || defined(__PX4_CYGWIN) || defined(__PX4_ROS2)
|
||||
|
||||
@@ -79,6 +84,13 @@ typedef int px4_task_t;
|
||||
#define SCHED_PRIORITY_MIN 0
|
||||
#define SCHED_PRIORITY_DEFAULT 20
|
||||
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
|
||||
// the priorities of NuttX, which the platform maps to those of Zephyr in the same order
|
||||
#define SCHED_PRIORITY_MAX 255
|
||||
#define SCHED_PRIORITY_MIN 1
|
||||
#define SCHED_PRIORITY_DEFAULT 100
|
||||
|
||||
#else
|
||||
#error "No target OS defined"
|
||||
#endif
|
||||
|
||||
@@ -60,9 +60,9 @@ WorkQueue::WorkQueue(const wq_config_t &config) :
|
||||
pthread_setname_np(pthread_self(), _config.name);
|
||||
#endif
|
||||
|
||||
#ifndef __PX4_NUTTX
|
||||
#if !defined(__PX4_NUTTX) && !defined(__PX4_ZEPHYR)
|
||||
px4_sem_init(&_qlock, 0, 1);
|
||||
#endif /* __PX4_NUTTX */
|
||||
#endif
|
||||
|
||||
px4_sem_init(&_process_lock, 0, 0);
|
||||
px4_sem_setprotocol(&_process_lock, SEM_PRIO_NONE);
|
||||
@@ -83,9 +83,9 @@ WorkQueue::~WorkQueue()
|
||||
px4_sem_destroy(&_process_lock);
|
||||
work_unlock();
|
||||
|
||||
#ifndef __PX4_NUTTX
|
||||
#if !defined(__PX4_NUTTX) && !defined(__PX4_ZEPHYR)
|
||||
px4_sem_destroy(&_qlock);
|
||||
#endif /* __PX4_NUTTX */
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WorkQueue::Attach(WorkItem *item)
|
||||
|
||||
@@ -49,6 +49,13 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
// SCHED_FIFO threads are cooperative on Zephyr
|
||||
#define PX4_WQ_SCHED_POLICY SCHED_RR
|
||||
#else
|
||||
#define PX4_WQ_SCHED_POLICY SCHED_FIFO
|
||||
#endif
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
namespace px4
|
||||
@@ -265,7 +272,7 @@ WorkQueueRunner(void *context)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT)
|
||||
#if (defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT)) || defined(__PX4_ZEPHYR)
|
||||
// Wrapper for px4_task_spawn_cmd interface
|
||||
inline static int
|
||||
WorkQueueRunner(int argc, char *argv[])
|
||||
@@ -291,8 +298,8 @@ WorkQueueManagerRun(int, char **)
|
||||
// create new work queue
|
||||
|
||||
// stack size
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_QURT)
|
||||
const size_t stacksize = math::max(PTHREAD_STACK_MIN, PX4_STACK_ADJUSTED(wq->stacksize));
|
||||
#if defined(__PX4_NUTTX) || defined(__PX4_QURT) || defined(__PX4_ZEPHYR)
|
||||
const size_t stacksize = math::max((int)PTHREAD_STACK_MIN, PX4_STACK_ADJUSTED(wq->stacksize));
|
||||
#elif defined(__PX4_POSIX)
|
||||
// On posix system , the desired stacksize round to the nearest multiplier of the system pagesize
|
||||
// It is a requirement of the pthread_attr_setstacksize* function
|
||||
@@ -302,10 +309,15 @@ WorkQueueManagerRun(int, char **)
|
||||
#endif
|
||||
|
||||
// priority
|
||||
int sched_priority = sched_get_priority_max(SCHED_FIFO) + wq->relative_priority;
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
// a PX4 priority, which the platform maps to Zephyr's
|
||||
int sched_priority = SCHED_PRIORITY_MAX + wq->relative_priority;
|
||||
#else
|
||||
int sched_priority = sched_get_priority_max(PX4_WQ_SCHED_POLICY) + wq->relative_priority;
|
||||
#endif
|
||||
|
||||
// use pthreads for NuttX flat and posix builds. For NuttX protected build, use tasks or kernel threads
|
||||
#if !defined(__PX4_NUTTX) || defined(CONFIG_BUILD_FLAT)
|
||||
// use pthreads for NuttX flat and posix builds. For NuttX protected build and Zephyr, use tasks or kernel threads
|
||||
#if (!defined(__PX4_NUTTX) || defined(CONFIG_BUILD_FLAT)) && !defined(__PX4_ZEPHYR)
|
||||
pthread_attr_t attr;
|
||||
int ret_attr_init = pthread_attr_init(&attr);
|
||||
|
||||
@@ -376,7 +388,7 @@ WorkQueueManagerRun(int, char **)
|
||||
const char *arg[2] = {arg1, nullptr};
|
||||
|
||||
int pid = px4_task_spawn_cmd(wq->name,
|
||||
SCHED_FIFO,
|
||||
PX4_WQ_SCHED_POLICY,
|
||||
sched_priority,
|
||||
stacksize,
|
||||
WorkQueueRunner,
|
||||
|
||||
@@ -237,7 +237,7 @@ public:
|
||||
|
||||
// _data is read (and lazily allocated in write()) under the node lock. On POSIX
|
||||
// ATOMIC_ENTER is that same lock, so reading it here serializes against the
|
||||
// allocation; on NuttX it is a set-once, word-aligned pointer.
|
||||
// allocation; on NuttX and Zephyr it is a set-once, word-aligned pointer.
|
||||
ATOMIC_ENTER;
|
||||
|
||||
if (_data == nullptr) {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
get_property(module_libraries GLOBAL PROPERTY PX4_MODULE_LIBRARIES)
|
||||
|
||||
set(builtin_apps_string)
|
||||
set(builtin_apps_decl_string)
|
||||
foreach(module ${module_libraries})
|
||||
get_target_property(MAIN ${module} MAIN)
|
||||
get_target_property(STACK_MAIN ${module} STACK_MAIN)
|
||||
get_target_property(PRIORITY ${module} PRIORITY)
|
||||
|
||||
if(MAIN)
|
||||
string(APPEND builtin_apps_string "\t{\"${MAIN}\", ${MAIN}_main, ${STACK_MAIN}, ${PRIORITY}},\n")
|
||||
string(APPEND builtin_apps_decl_string "int ${MAIN}_main(int argc, char *argv[]);\n")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(startup_script ${PX4_BOARD_DIR}/init/rc.px4)
|
||||
set(startup_script_string)
|
||||
if(EXISTS ${startup_script})
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${startup_script})
|
||||
file(STRINGS ${startup_script} startup_script_lines)
|
||||
foreach(line ${startup_script_lines})
|
||||
string(STRIP "${line}" line)
|
||||
if(line AND NOT line MATCHES "^#")
|
||||
string(REPLACE "\\" "\\\\" line "${line}")
|
||||
string(REPLACE "\"" "\\\"" line "${line}")
|
||||
string(APPEND startup_script_string "\t\"${line}\",\n")
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
configure_file(px4_zephyr_apps.c.in ${CMAKE_CURRENT_BINARY_DIR}/px4_zephyr_apps.c)
|
||||
|
||||
add_library(px4 STATIC
|
||||
${CMAKE_CURRENT_BINARY_DIR}/px4_zephyr_apps.c
|
||||
src/px4/common/px4_zephyr.cpp
|
||||
)
|
||||
target_compile_definitions(px4 PRIVATE MODULE_NAME="px4")
|
||||
target_link_libraries(px4 PUBLIC
|
||||
${module_libraries}
|
||||
parameters
|
||||
px4_layer
|
||||
px4_platform
|
||||
uORB
|
||||
work_queue
|
||||
px4_work_queue
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(px4_zephyr)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
@@ -0,0 +1,3 @@
|
||||
CONFIG_PX4=y
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_LOG=y
|
||||
@@ -0,0 +1,19 @@
|
||||
sample:
|
||||
name: PX4 Autopilot
|
||||
description: PX4 running on Zephyr
|
||||
common:
|
||||
tags:
|
||||
- px4
|
||||
tests:
|
||||
px4.app:
|
||||
platform_allow:
|
||||
- mps2/an386
|
||||
integration_platforms:
|
||||
- mps2/an386
|
||||
harness: console
|
||||
harness_config:
|
||||
type: multi_line
|
||||
ordered: true
|
||||
regex:
|
||||
- "HW arch: ZEPHYR_GENERIC"
|
||||
- "OS: Zephyr"
|
||||
@@ -0,0 +1,39 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_zephyr.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return px4_zephyr_start();
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
if(NOT DEFINED ZEPHYR_BASE)
|
||||
message(FATAL_ERROR "Zephyr targets are built with west, see platforms/zephyr/app")
|
||||
endif()
|
||||
|
||||
function(px4_os_add_flags)
|
||||
|
||||
add_definitions(
|
||||
-D__PX4_POSIX
|
||||
-D__PX4_ZEPHYR
|
||||
-Dnoreturn_function=__attribute__\(\(noreturn\)\)
|
||||
)
|
||||
|
||||
include_directories(
|
||||
platforms/zephyr/include
|
||||
platforms/posix/include
|
||||
platforms/posix/src/px4/common/include
|
||||
platforms/posix/src/px4/generic/generic/include
|
||||
)
|
||||
|
||||
# the Zephyr compile options come before the options of each target, so that MAX_CUSTOM_OPT_LEVEL
|
||||
# builds the estimators and controllers with -O2 instead of the -Os of Zephyr, as on NuttX
|
||||
add_compile_options($<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_OPTIONS>)
|
||||
|
||||
link_libraries(px4_zephyr_interface)
|
||||
|
||||
endfunction()
|
||||
|
||||
function(px4_os_determine_build_chip)
|
||||
|
||||
set(PX4_CHIP "generic" CACHE STRING "PX4 Chip" FORCE)
|
||||
set(PX4_CHIP_MANUFACTURER "generic" CACHE STRING "PX4 Chip Manufacturer" FORCE)
|
||||
|
||||
endfunction()
|
||||
|
||||
# arguments are not parsed, Zephyr does not allow setting BOARD
|
||||
function(px4_os_prebuild_targets)
|
||||
|
||||
# Zephyr compile settings, with the Zephyr headers treated as system headers
|
||||
add_library(px4_zephyr_interface INTERFACE)
|
||||
target_include_directories(px4_zephyr_interface SYSTEM INTERFACE
|
||||
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
|
||||
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
|
||||
)
|
||||
target_compile_definitions(px4_zephyr_interface INTERFACE
|
||||
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
|
||||
)
|
||||
target_compile_options(px4_zephyr_interface INTERFACE
|
||||
# PX4 uses GNU language extensions, as on the other platforms
|
||||
$<$<COMPILE_LANGUAGE:C>:-std=gnu11>
|
||||
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>
|
||||
)
|
||||
add_dependencies(px4_zephyr_interface zephyr_generated_headers)
|
||||
|
||||
add_library(prebuild_targets INTERFACE)
|
||||
target_link_libraries(prebuild_targets INTERFACE px4_layer drivers_board px4_zephyr_interface)
|
||||
add_dependencies(prebuild_targets DEPENDS uorb_headers)
|
||||
|
||||
endfunction()
|
||||
@@ -0,0 +1,68 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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
|
||||
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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
|
||||
|
||||
#include_next <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int dprintf(int fd, const char *format, ...) __attribute__((format(printf, 2, 3)));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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
|
||||
|
||||
#include_next <sys/ioctl.h>
|
||||
|
||||
#define IOCPARM_MASK 0x1fff /* parameter length, at most 13 bits */
|
||||
#define IOC_VOID 0x20000000 /* no parameters */
|
||||
|
||||
#define _IOC(inout,group,num,len) ((unsigned long) \
|
||||
((inout) | (((len) & IOCPARM_MASK) << 16) | ((group) << 8) | (num)))
|
||||
#define _IO(g,n) _IOC(IOC_VOID, (g), (n), 0)
|
||||
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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
|
||||
|
||||
#include_next <unistd.h>
|
||||
|
||||
#ifndef F_OK
|
||||
#define F_OK 0
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
if(NOT CONFIG_PX4)
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(px4_source_dir ${ZEPHYR_CURRENT_MODULE_DIR})
|
||||
|
||||
if(NOT CONFIG_PX4_BOARD_CONFIG STREQUAL "")
|
||||
set(px4_config ${CONFIG_PX4_BOARD_CONFIG})
|
||||
elseif(EXISTS ${px4_source_dir}/boards/zephyr/${BOARD}/default.px4board)
|
||||
set(px4_config zephyr_${BOARD})
|
||||
else()
|
||||
set(px4_config zephyr_generic)
|
||||
endif()
|
||||
|
||||
set(CONFIG ${px4_config} CACHE STRING "PX4 board configuration" FORCE)
|
||||
unset(PX4_CONFIG_FILE CACHE)
|
||||
|
||||
set(PX4_GIT_SUBMODULES
|
||||
src/lib/events/libevents
|
||||
src/lib/heatshrink/heatshrink
|
||||
)
|
||||
|
||||
zephyr_include_directories(include)
|
||||
|
||||
add_subdirectory(${px4_source_dir} ${CMAKE_CURRENT_BINARY_DIR}/px4)
|
||||
|
||||
target_link_libraries(app PRIVATE px4)
|
||||
|
||||
# compile settings for application code using the PX4 API
|
||||
get_directory_property(px4_include_dirs DIRECTORY ${px4_source_dir} INCLUDE_DIRECTORIES)
|
||||
get_directory_property(px4_compile_definitions DIRECTORY ${px4_source_dir} COMPILE_DEFINITIONS)
|
||||
add_library(px4_interface INTERFACE)
|
||||
target_include_directories(px4_interface INTERFACE ${px4_include_dirs})
|
||||
target_compile_definitions(px4_interface INTERFACE ${px4_compile_definitions})
|
||||
target_compile_options(px4_interface INTERFACE
|
||||
$<$<COMPILE_LANGUAGE:C>:-std=gnu11>
|
||||
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>
|
||||
-include visibility.h
|
||||
)
|
||||
add_dependencies(px4_interface uorb_headers)
|
||||
|
||||
zephyr_library_named(px4_pthread)
|
||||
# px4_pthread_attr_destroy() keeps the stacks that threads run on, see px4_pthread.c
|
||||
zephyr_library_sources(${px4_source_dir}/platforms/zephyr/src/px4/common/px4_pthread.c)
|
||||
|
||||
if(CONFIG_PX4_SHELL)
|
||||
zephyr_library_named(px4_shell)
|
||||
zephyr_library_sources(${px4_source_dir}/platforms/zephyr/src/px4/common/px4_shell.c)
|
||||
endif()
|
||||
@@ -0,0 +1,115 @@
|
||||
menuconfig PX4
|
||||
bool "PX4 Autopilot"
|
||||
select CPP
|
||||
select REQUIRES_FULL_LIBCPP
|
||||
select POSIX_API
|
||||
select POSIX_THREADS
|
||||
select POSIX_SEMAPHORES
|
||||
select POSIX_TIMERS
|
||||
select POSIX_PRIORITY_SCHEDULING
|
||||
select THREAD_STACK_INFO
|
||||
select THREAD_MONITOR
|
||||
select DYNAMIC_THREAD
|
||||
select DYNAMIC_THREAD_ALLOC
|
||||
select THREAD_NAME
|
||||
select REBOOT
|
||||
select FPU if CPU_HAS_FPU
|
||||
select FPU_SHARING if CPU_HAS_FPU
|
||||
help
|
||||
Build PX4 Autopilot into this application. The PX4 modules are
|
||||
selected by the PX4 board configuration, see PX4_BOARD_CONFIG.
|
||||
|
||||
if PX4
|
||||
|
||||
config PX4_BOARD_CONFIG
|
||||
string "PX4 board configuration"
|
||||
help
|
||||
PX4 configuration to build, for example "zephyr_generic". When empty,
|
||||
boards/zephyr/<board>/default.px4board is used if it exists, and
|
||||
boards/zephyr/generic/default.px4board otherwise.
|
||||
|
||||
config PX4_SHELL
|
||||
bool "px4 shell command"
|
||||
default y
|
||||
depends on SHELL
|
||||
help
|
||||
Run PX4 commands from the Zephyr shell, e.g. "px4 uorb status".
|
||||
|
||||
config PX4_STACK_OVERHEAD
|
||||
int "Stack overhead"
|
||||
default 1024
|
||||
help
|
||||
Bytes added to the stack size of every PX4 task and work queue, for
|
||||
the kernel, the C library and the floating point context.
|
||||
|
||||
endif # PX4
|
||||
|
||||
choice STD_CPP
|
||||
default STD_CPP17 if PX4
|
||||
endchoice
|
||||
|
||||
choice PICOLIBC_IO_LEVEL
|
||||
default PICOLIBC_IO_FLOAT if PX4
|
||||
endchoice
|
||||
|
||||
if PX4
|
||||
|
||||
# the PX4 priorities, mapped in order: the top 64 one to one, the others below them
|
||||
configdefault NUM_PREEMPT_PRIORITIES
|
||||
default 127
|
||||
|
||||
# PX4 threads run FIFO among equal priorities, as on NuttX (RR_INTERVAL=0): time slicing would only
|
||||
# re-arm a timeout on every context switch
|
||||
configdefault TIMESLICING
|
||||
default n
|
||||
|
||||
# the longest PX4 thread name, wq:nav_and_controllers, has 22 characters (NuttX: TASK_NAME_SIZE 24)
|
||||
configdefault THREAD_MAX_NAME_LEN
|
||||
default 24
|
||||
|
||||
# 10 us ticks, where the system timer counts at least that fast
|
||||
configdefault SYS_CLOCK_TICKS_PER_SEC
|
||||
default 100000 if TICKLESS_KERNEL && SYS_CLOCK_HW_CYCLES_PER_SEC >= 100000
|
||||
|
||||
# hrt_absolute_time() counts the cycles of the system timer, which wrap after minutes in 32 bits
|
||||
configdefault CORTEX_M_SYSTICK_64BIT_CYCLE_COUNTER
|
||||
default y
|
||||
|
||||
configdefault POSIX_THREAD_THREADS_MAX
|
||||
default 32
|
||||
|
||||
configdefault MAX_PTHREAD_MUTEX_COUNT
|
||||
default 64
|
||||
|
||||
configdefault MAX_PTHREAD_COND_COUNT
|
||||
default 64
|
||||
|
||||
configdefault HEAP_MEM_POOL_SIZE
|
||||
default 131072
|
||||
|
||||
# the startup script runs on the main thread, which then exits: it uses 1.5 KiB of the stack in
|
||||
# the SIH configuration
|
||||
configdefault MAIN_STACK_SIZE
|
||||
default 4096
|
||||
|
||||
# debugging commands and features of the Zephyr shell that PX4 doesn't use
|
||||
configdefault DEVMEM_SHELL
|
||||
default n
|
||||
|
||||
configdefault DATE_SHELL
|
||||
default n
|
||||
|
||||
configdefault DEVICE_SHELL
|
||||
default n
|
||||
|
||||
configdefault SHELL_WILDCARD
|
||||
default n
|
||||
|
||||
configdefault SHELL_STATS
|
||||
default n
|
||||
|
||||
# PX4 commands run on the shell thread
|
||||
configdefault SHELL_STACK_SIZE
|
||||
default 4096 if PX4_SHELL
|
||||
|
||||
endif # PX4
|
||||
@@ -0,0 +1,64 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file px4_sched.h
|
||||
*
|
||||
* PX4 uses the thread priorities of NuttX on Zephyr, from 1 to 255, which
|
||||
* the POSIX layer maps to the preemptible priorities of Zephyr in the same
|
||||
* order.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Get the POSIX priority of Zephyr for a PX4 priority
|
||||
*
|
||||
* The top PX4_ZEPHYR_PRIORITY_BAND PX4 priorities, those of the work queues,
|
||||
* map one to one. The others are compressed below them.
|
||||
*
|
||||
* @param priority PX4 priority, from SCHED_PRIORITY_MIN to SCHED_PRIORITY_MAX
|
||||
* @return POSIX priority of SCHED_RR, from 0 to CONFIG_NUM_PREEMPT_PRIORITIES - 1
|
||||
*/
|
||||
int px4_zephyr_posix_priority(int priority);
|
||||
|
||||
/** Number of the highest PX4 priorities that map one to one */
|
||||
#define PX4_ZEPHYR_PRIORITY_BAND 64
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file px4_zephyr.h
|
||||
*
|
||||
* Entry points of PX4 for Zephyr applications.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef int (*px4_zephyr_main_t)(int argc, char *argv[]);
|
||||
|
||||
struct px4_zephyr_builtin {
|
||||
const char *name;
|
||||
px4_zephyr_main_t main;
|
||||
int stack_size;
|
||||
int priority;
|
||||
};
|
||||
|
||||
extern const struct px4_zephyr_builtin px4_zephyr_builtins[];
|
||||
extern const size_t px4_zephyr_builtins_count;
|
||||
|
||||
extern const char *const px4_zephyr_startup_script[];
|
||||
|
||||
/**
|
||||
* Initialize the PX4 platform: hrt, work queues, parameters, uORB and logging.
|
||||
*/
|
||||
int px4_zephyr_init(void);
|
||||
|
||||
/**
|
||||
* Run a PX4 command in a task of its own and wait for it to exit.
|
||||
* argv[0] is the command name.
|
||||
*/
|
||||
int px4_zephyr_run_command(int argc, char *argv[]);
|
||||
|
||||
/**
|
||||
* Wait for a PX4 task to exit, as px4_task_join(), and get the value its main
|
||||
* function returned in exit_code, if not NULL.
|
||||
* @return 0, or a negative error code if the task could not be joined
|
||||
*/
|
||||
int px4_zephyr_task_join(int id, int *exit_code);
|
||||
|
||||
/**
|
||||
* Initialize PX4 and run the board startup script.
|
||||
*/
|
||||
int px4_zephyr_start(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
/* builtin PX4 commands and startup script, generated by platforms/zephyr/CMakeLists.txt */
|
||||
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <px4_zephyr.h>
|
||||
|
||||
${builtin_apps_decl_string}
|
||||
const struct px4_zephyr_builtin px4_zephyr_builtins[] = {
|
||||
${builtin_apps_string}};
|
||||
|
||||
const size_t px4_zephyr_builtins_count = sizeof(px4_zephyr_builtins) / sizeof(px4_zephyr_builtins[0]);
|
||||
|
||||
const char *const px4_zephyr_startup_script[] = {
|
||||
${startup_script_string} NULL
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
empy>=3.3,<4
|
||||
jinja2>=2.8
|
||||
jsonschema
|
||||
kconfiglib
|
||||
packaging
|
||||
pyros-genmsg
|
||||
pyyaml
|
||||
@@ -0,0 +1,34 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
add_subdirectory(common)
|
||||
@@ -0,0 +1,45 @@
|
||||
############################################################################
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
add_library(px4_layer
|
||||
${PX4_SOURCE_DIR}/platforms/common/module_base.cpp
|
||||
${PX4_SOURCE_DIR}/platforms/posix/src/px4/common/lib_crc32.c
|
||||
${PX4_SOURCE_DIR}/platforms/posix/src/px4/common/px4_init.cpp
|
||||
drv_hrt.cpp
|
||||
px4_sched.c
|
||||
px4_sem.cpp
|
||||
px4_zephyr_impl.cpp
|
||||
tasks.cpp
|
||||
)
|
||||
target_compile_definitions(px4_layer PRIVATE MODULE_NAME="px4")
|
||||
target_link_libraries(px4_layer PRIVATE work_queue px4_work_queue drivers_board)
|
||||
@@ -0,0 +1,258 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file drv_hrt.cpp
|
||||
*
|
||||
* High-resolution timer on top of the Zephyr kernel clock. Callouts are
|
||||
* invoked from the k_timer expiry function, in interrupt context.
|
||||
*/
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
static sq_queue_t callout_queue;
|
||||
static struct k_timer hrt_timer;
|
||||
|
||||
const uint16_t latency_bucket_count = LATENCY_BUCKET_COUNT;
|
||||
const uint16_t latency_buckets[LATENCY_BUCKET_COUNT] = { 1, 2, 5, 10, 20, 50, 100, 1000 };
|
||||
__EXPORT uint32_t latency_counters[LATENCY_BUCKET_COUNT + 1];
|
||||
|
||||
static void hrt_call_reschedule()
|
||||
{
|
||||
struct hrt_call *next = (struct hrt_call *)(void *)sq_peek(&callout_queue);
|
||||
|
||||
if (next == nullptr) {
|
||||
k_timer_stop(&hrt_timer);
|
||||
return;
|
||||
}
|
||||
|
||||
k_timer_start(&hrt_timer, K_TIMEOUT_ABS_TICKS(k_us_to_ticks_ceil64(next->deadline)), K_NO_WAIT);
|
||||
}
|
||||
|
||||
static void hrt_call_enter(struct hrt_call *entry)
|
||||
{
|
||||
struct hrt_call *call = (struct hrt_call *)(void *)sq_peek(&callout_queue);
|
||||
|
||||
if ((call == nullptr) || (entry->deadline < call->deadline)) {
|
||||
sq_addfirst(&entry->link, &callout_queue);
|
||||
hrt_call_reschedule();
|
||||
return;
|
||||
}
|
||||
|
||||
struct hrt_call *next;
|
||||
|
||||
while ((next = (struct hrt_call *)(void *)sq_next(&call->link)) != nullptr) {
|
||||
if (entry->deadline < next->deadline) {
|
||||
break;
|
||||
}
|
||||
|
||||
call = next;
|
||||
}
|
||||
|
||||
sq_addafter(&call->link, &entry->link, &callout_queue);
|
||||
}
|
||||
|
||||
static void hrt_latency_update(hrt_abstime latency)
|
||||
{
|
||||
for (unsigned index = 0; index < LATENCY_BUCKET_COUNT; index++) {
|
||||
if (latency <= latency_buckets[index]) {
|
||||
latency_counters[index]++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
latency_counters[LATENCY_BUCKET_COUNT]++;
|
||||
}
|
||||
|
||||
static void hrt_timer_expiry(struct k_timer *timer)
|
||||
{
|
||||
// interrupts of a higher priority than the system timer can schedule calls too
|
||||
unsigned int key = irq_lock();
|
||||
struct hrt_call *call;
|
||||
|
||||
while ((call = (struct hrt_call *)(void *)sq_peek(&callout_queue)) != nullptr) {
|
||||
const hrt_abstime now = hrt_absolute_time();
|
||||
|
||||
if (call->deadline > now) {
|
||||
break;
|
||||
}
|
||||
|
||||
sq_rem(&call->link, &callout_queue);
|
||||
hrt_latency_update(now - call->deadline);
|
||||
|
||||
const hrt_abstime deadline = call->deadline;
|
||||
|
||||
// zero the deadline, as the call has occurred
|
||||
call->deadline = 0;
|
||||
|
||||
const hrt_callout callout = call->callout;
|
||||
void *const arg = call->arg;
|
||||
|
||||
// the callout runs with interrupts enabled, as on NuttX
|
||||
irq_unlock(key);
|
||||
|
||||
if (callout) {
|
||||
callout(arg);
|
||||
}
|
||||
|
||||
key = irq_lock();
|
||||
|
||||
// periodic calls are re-entered, unless the callout cancelled them
|
||||
if (call->period != 0) {
|
||||
// the callout may have rescheduled the call with hrt_call_delay()
|
||||
if (call->deadline <= now) {
|
||||
call->deadline = deadline + call->period;
|
||||
}
|
||||
|
||||
// or queued it again with hrt_call_every(): enter it once
|
||||
sq_rem(&call->link, &callout_queue);
|
||||
hrt_call_enter(call);
|
||||
}
|
||||
}
|
||||
|
||||
hrt_call_reschedule();
|
||||
|
||||
irq_unlock(key);
|
||||
}
|
||||
|
||||
static void hrt_call_internal(struct hrt_call *entry, hrt_abstime deadline, hrt_abstime interval,
|
||||
hrt_callout callout, void *arg)
|
||||
{
|
||||
const unsigned int key = irq_lock();
|
||||
|
||||
// the entry may be uninitialised, sq_rem() only dereferences queued entries
|
||||
if (entry->deadline != 0) {
|
||||
sq_rem(&entry->link, &callout_queue);
|
||||
}
|
||||
|
||||
entry->deadline = deadline;
|
||||
entry->period = interval;
|
||||
entry->callout = callout;
|
||||
entry->arg = arg;
|
||||
|
||||
hrt_call_enter(entry);
|
||||
|
||||
irq_unlock(key);
|
||||
}
|
||||
|
||||
hrt_abstime hrt_absolute_time()
|
||||
{
|
||||
#if defined(CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER)
|
||||
// the cycles of the system timer, which also counts the kernel ticks, of 10 us by default
|
||||
return k_cyc_to_us_floor64(k_cycle_get_64());
|
||||
#else
|
||||
return k_ticks_to_us_floor64(k_uptime_ticks());
|
||||
#endif
|
||||
}
|
||||
|
||||
void hrt_store_absolute_time(volatile hrt_abstime *time)
|
||||
{
|
||||
*time = hrt_absolute_time();
|
||||
}
|
||||
|
||||
void hrt_init()
|
||||
{
|
||||
sq_init(&callout_queue);
|
||||
k_timer_init(&hrt_timer, hrt_timer_expiry, nullptr);
|
||||
}
|
||||
|
||||
void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg)
|
||||
{
|
||||
hrt_call_internal(entry, hrt_absolute_time() + delay, 0, callout, arg);
|
||||
}
|
||||
|
||||
void hrt_call_every(struct hrt_call *entry, hrt_abstime delay, hrt_abstime interval, hrt_callout callout, void *arg)
|
||||
{
|
||||
hrt_call_internal(entry, hrt_absolute_time() + delay, interval, callout, arg);
|
||||
}
|
||||
|
||||
void hrt_call_at(struct hrt_call *entry, hrt_abstime calltime, hrt_callout callout, void *arg)
|
||||
{
|
||||
hrt_call_internal(entry, calltime, 0, callout, arg);
|
||||
}
|
||||
|
||||
bool hrt_called(struct hrt_call *entry)
|
||||
{
|
||||
// the 64-bit deadline is zeroed in interrupt context, by hrt_timer_expiry()
|
||||
const unsigned int key = irq_lock();
|
||||
const bool called = (entry->deadline == 0);
|
||||
irq_unlock(key);
|
||||
|
||||
return called;
|
||||
}
|
||||
|
||||
void hrt_cancel(struct hrt_call *entry)
|
||||
{
|
||||
const unsigned int key = irq_lock();
|
||||
|
||||
sq_rem(&entry->link, &callout_queue);
|
||||
entry->deadline = 0;
|
||||
|
||||
// prevent a periodic call from being re-entered when cancelled from its own callout
|
||||
entry->period = 0;
|
||||
|
||||
irq_unlock(key);
|
||||
}
|
||||
|
||||
void hrt_call_init(struct hrt_call *entry)
|
||||
{
|
||||
memset(entry, 0, sizeof(*entry));
|
||||
}
|
||||
|
||||
void hrt_call_delay(struct hrt_call *entry, hrt_abstime delay)
|
||||
{
|
||||
const unsigned int key = irq_lock();
|
||||
entry->deadline = hrt_absolute_time() + delay;
|
||||
irq_unlock(key);
|
||||
}
|
||||
|
||||
int px4_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
||||
{
|
||||
if (tp == nullptr) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (clk_id == CLOCK_MONOTONIC) {
|
||||
abstime_to_ts(tp, hrt_absolute_time());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return system_clock_gettime(clk_id, tp);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file px4_pthread.c
|
||||
*
|
||||
* A thread of the Zephyr POSIX API runs on the stack of the attributes it was
|
||||
* created with, which pthread_attr_destroy() frees. PX4 destroys the
|
||||
* attributes once the thread is created, as POSIX allows, so PX4 code calls
|
||||
* px4_pthread_attr_destroy() instead, through the pthread.h of the platform,
|
||||
* to keep the stack of a thread that still runs on it, and to free it once
|
||||
* the thread has exited.
|
||||
*
|
||||
* Zephyr only refuses to free the stack of a running thread, and leaks it then.
|
||||
* Its check misses a thread whose stack starts after an MPU or FPU guard, as on
|
||||
* Cortex-M7, and frees the stack while the thread runs on it (Zephyr issue
|
||||
* 103856).
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
struct kept_stack {
|
||||
void *stack;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
struct stack_use {
|
||||
uintptr_t start;
|
||||
uintptr_t end;
|
||||
bool used;
|
||||
};
|
||||
|
||||
static struct kept_stack kept_stacks[CONFIG_POSIX_THREAD_THREADS_MAX];
|
||||
static K_MUTEX_DEFINE(kept_stacks_lock);
|
||||
|
||||
static void find_thread(const struct k_thread *thread, void *user_data)
|
||||
{
|
||||
struct stack_use *use = user_data;
|
||||
|
||||
// the stack starts after the guard that the MPU carves out of it
|
||||
if ((thread->stack_info.start >= use->start) && (thread->stack_info.start < use->end)) {
|
||||
use->used = true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool stack_used(void *stack, size_t size)
|
||||
{
|
||||
struct stack_use use = {
|
||||
.start = (uintptr_t)stack,
|
||||
.end = (uintptr_t)stack + size,
|
||||
};
|
||||
|
||||
k_thread_foreach(find_thread, &use);
|
||||
|
||||
return use.used;
|
||||
}
|
||||
|
||||
int px4_pthread_attr_destroy(pthread_attr_t *attr)
|
||||
{
|
||||
void *stack = NULL;
|
||||
size_t size = 0;
|
||||
bool keep = false;
|
||||
|
||||
k_mutex_lock(&kept_stacks_lock, K_FOREVER);
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(kept_stacks); i++) {
|
||||
if ((kept_stacks[i].stack != NULL) && !stack_used(kept_stacks[i].stack, kept_stacks[i].size)) {
|
||||
k_thread_stack_free(kept_stacks[i].stack);
|
||||
kept_stacks[i].stack = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if ((pthread_attr_getstack(attr, &stack, &size) == 0) && (stack != NULL) && stack_used(stack, size)) {
|
||||
keep = true;
|
||||
|
||||
// a stack that can't be freed later is still kept for its thread
|
||||
for (size_t i = 0; i < ARRAY_SIZE(kept_stacks); i++) {
|
||||
if (kept_stacks[i].stack == NULL) {
|
||||
kept_stacks[i].stack = stack;
|
||||
kept_stacks[i].size = size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
k_mutex_unlock(&kept_stacks_lock);
|
||||
|
||||
if (keep) {
|
||||
memset(attr, 0, sizeof(*attr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return pthread_attr_destroy(attr);
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file px4_sched.c
|
||||
*
|
||||
* PX4 sets thread priorities for NuttX, from 1 to 255, with offsets from
|
||||
* SCHED_PRIORITY_MAX and SCHED_PRIORITY_DEFAULT. Scaled down to the
|
||||
* preemptible priorities of Zephyr, the offsets would change the order of
|
||||
* the threads, so PX4 code calls the POSIX scheduling functions here, through
|
||||
* the pthread.h of the platform, to map PX4 priorities in order. The rest of
|
||||
* the image calls those of Zephyr.
|
||||
*
|
||||
* New thread attributes also get explicit scheduling, as PX4 expects: the
|
||||
* POSIX threads of Zephyr otherwise inherit the priority of the thread that
|
||||
* creates them, whatever priority the attributes set. SCHED_FIFO becomes
|
||||
* SCHED_RR, as SCHED_FIFO threads are cooperative on Zephyr.
|
||||
*
|
||||
* The priorities that PX4 reads back are those of Zephyr: PX4 only reads them
|
||||
* to set its own.
|
||||
*/
|
||||
|
||||
#include <px4_sched.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/toolchain.h>
|
||||
|
||||
#include <px4_platform_common/tasks.h>
|
||||
|
||||
// the functions of Zephyr, which pthread.h of the platform replaces in PX4 code
|
||||
#undef pthread_attr_init
|
||||
#undef pthread_attr_setschedpolicy
|
||||
#undef pthread_attr_setschedparam
|
||||
#undef pthread_setschedparam
|
||||
|
||||
#define POSIX_PRIORITY_MAX (CONFIG_NUM_PREEMPT_PRIORITIES - 1)
|
||||
|
||||
// the PX4 priorities below the band
|
||||
#define PX4_COMPRESSED_MAX (SCHED_PRIORITY_MAX - PX4_ZEPHYR_PRIORITY_BAND)
|
||||
#define POSIX_COMPRESSED_MAX (POSIX_PRIORITY_MAX - PX4_ZEPHYR_PRIORITY_BAND)
|
||||
|
||||
BUILD_ASSERT(POSIX_COMPRESSED_MAX > 0, "too few preemptible priorities for the PX4 priorities");
|
||||
|
||||
int px4_zephyr_posix_priority(int priority)
|
||||
{
|
||||
priority = CLAMP(priority, SCHED_PRIORITY_MIN, SCHED_PRIORITY_MAX);
|
||||
|
||||
if (priority > PX4_COMPRESSED_MAX) {
|
||||
return POSIX_PRIORITY_MAX - (SCHED_PRIORITY_MAX - priority);
|
||||
}
|
||||
|
||||
return (priority - SCHED_PRIORITY_MIN) * POSIX_COMPRESSED_MAX / (PX4_COMPRESSED_MAX - SCHED_PRIORITY_MIN);
|
||||
}
|
||||
|
||||
static int zephyr_policy(int policy)
|
||||
{
|
||||
return (policy == SCHED_FIFO) ? SCHED_RR : policy;
|
||||
}
|
||||
|
||||
int px4_pthread_attr_init(pthread_attr_t *attr)
|
||||
{
|
||||
int ret = pthread_attr_init(attr);
|
||||
|
||||
if (ret == 0) {
|
||||
const struct sched_param param = {
|
||||
.sched_priority = px4_zephyr_posix_priority(SCHED_PRIORITY_DEFAULT),
|
||||
};
|
||||
|
||||
(void)pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
|
||||
(void)pthread_attr_setschedpolicy(attr, SCHED_RR);
|
||||
(void)pthread_attr_setschedparam(attr, ¶m);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int px4_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
|
||||
{
|
||||
return pthread_attr_setschedpolicy(attr, zephyr_policy(policy));
|
||||
}
|
||||
|
||||
int px4_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
|
||||
{
|
||||
if (param == NULL) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
const struct sched_param zephyr_param = {
|
||||
.sched_priority = px4_zephyr_posix_priority(param->sched_priority),
|
||||
};
|
||||
|
||||
return pthread_attr_setschedparam(attr, &zephyr_param);
|
||||
}
|
||||
|
||||
int px4_pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
|
||||
{
|
||||
if (param == NULL) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
const struct sched_param zephyr_param = {
|
||||
.sched_priority = px4_zephyr_posix_priority(param->sched_priority),
|
||||
};
|
||||
|
||||
return pthread_setschedparam(thread, zephyr_policy(policy), &zephyr_param);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_platform_common/sem.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
int px4_sem_init(px4_sem_t *s, int pshared, unsigned value)
|
||||
{
|
||||
return k_sem_init(s, value, K_SEM_MAX_LIMIT);
|
||||
}
|
||||
|
||||
int px4_sem_setprotocol(px4_sem_t *s, int protocol)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int px4_sem_wait(px4_sem_t *s)
|
||||
{
|
||||
return k_sem_take(s, K_FOREVER);
|
||||
}
|
||||
|
||||
int px4_sem_trywait(px4_sem_t *s)
|
||||
{
|
||||
if (k_sem_take(s, K_NO_WAIT) != 0) {
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int px4_sem_timedwait(px4_sem_t *s, const struct timespec *abstime)
|
||||
{
|
||||
const hrt_abstime deadline = ts_to_abstime(abstime);
|
||||
const hrt_abstime now = hrt_absolute_time();
|
||||
const k_timeout_t timeout = (deadline > now) ? K_USEC(deadline - now) : K_NO_WAIT;
|
||||
|
||||
if (k_sem_take(s, timeout) != 0) {
|
||||
errno = ETIMEDOUT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int px4_sem_post(px4_sem_t *s)
|
||||
{
|
||||
k_sem_give(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int px4_sem_getvalue(px4_sem_t *s, int *sval)
|
||||
{
|
||||
*sval = k_sem_count_get(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int px4_sem_destroy(px4_sem_t *s)
|
||||
{
|
||||
k_sem_reset(s);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_zephyr.h>
|
||||
|
||||
#include <zephyr/shell/shell.h>
|
||||
|
||||
static int cmd_px4_builtin(const struct shell *sh, size_t argc, char **argv)
|
||||
{
|
||||
return px4_zephyr_run_command(argc, argv);
|
||||
}
|
||||
|
||||
static void px4_builtin_get(size_t idx, struct shell_static_entry *entry)
|
||||
{
|
||||
if (idx < px4_zephyr_builtins_count) {
|
||||
entry->syntax = px4_zephyr_builtins[idx].name;
|
||||
entry->handler = cmd_px4_builtin;
|
||||
entry->help = NULL;
|
||||
entry->subcmd = NULL;
|
||||
entry->args.mandatory = 1;
|
||||
entry->args.optional = SHELL_OPT_ARG_MAX;
|
||||
|
||||
} else {
|
||||
entry->syntax = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
SHELL_DYNAMIC_CMD_CREATE(px4_builtins, px4_builtin_get);
|
||||
|
||||
SHELL_CMD_REGISTER(px4, &px4_builtins, "PX4 commands", NULL);
|
||||
@@ -0,0 +1,141 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_zephyr.h>
|
||||
|
||||
#include <parameters/param.h>
|
||||
#include <px4_platform_common/init.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/posix.h>
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <px4_platform_common/workqueue.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
static constexpr int MAX_ARGS = 16;
|
||||
static constexpr size_t MAX_LINE_LENGTH = 128;
|
||||
|
||||
int px4_zephyr_init(void)
|
||||
{
|
||||
static bool initialized = false;
|
||||
|
||||
if (initialized) {
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
|
||||
work_queues_init();
|
||||
|
||||
const int ret = px4_platform_init();
|
||||
|
||||
// parameters are not persistent yet
|
||||
param_control_autosave(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int px4_zephyr_run_command(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 1) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < px4_zephyr_builtins_count; i++) {
|
||||
const px4_zephyr_builtin &builtin = px4_zephyr_builtins[i];
|
||||
|
||||
if (strcmp(builtin.name, argv[0]) == 0) {
|
||||
char *args[MAX_ARGS + 1] {};
|
||||
|
||||
for (int arg = 1; arg < argc && arg <= MAX_ARGS; arg++) {
|
||||
args[arg - 1] = argv[arg];
|
||||
}
|
||||
|
||||
px4_task_t task = px4_task_spawn_cmd(builtin.name, SCHED_DEFAULT, builtin.priority,
|
||||
PX4_STACK_ADJUSTED(builtin.stack_size),
|
||||
builtin.main, args);
|
||||
|
||||
if (task < 0) {
|
||||
PX4_ERR("failed to start %s (%d)", builtin.name, task);
|
||||
return task;
|
||||
}
|
||||
|
||||
int exit_code = PX4_ERROR;
|
||||
const int ret = px4_zephyr_task_join(task, &exit_code);
|
||||
|
||||
return (ret == 0) ? exit_code : ret;
|
||||
}
|
||||
}
|
||||
|
||||
PX4_ERR("unknown command: %s", argv[0]);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
static int run_line(const char *line)
|
||||
{
|
||||
char buffer[MAX_LINE_LENGTH];
|
||||
strncpy(buffer, line, sizeof(buffer) - 1);
|
||||
buffer[sizeof(buffer) - 1] = '\0';
|
||||
|
||||
char *argv[MAX_ARGS + 1] {};
|
||||
int argc = 0;
|
||||
char *saveptr = nullptr;
|
||||
|
||||
for (char *token = strtok_r(buffer, " \t", &saveptr); token != nullptr && argc < MAX_ARGS;
|
||||
token = strtok_r(nullptr, " \t", &saveptr)) {
|
||||
argv[argc++] = token;
|
||||
}
|
||||
|
||||
if (argc == 0) {
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
return px4_zephyr_run_command(argc, argv);
|
||||
}
|
||||
|
||||
int px4_zephyr_start(void)
|
||||
{
|
||||
int ret = px4_zephyr_init();
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
PX4_ERR("platform init failed (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (const char *const *line = px4_zephyr_startup_script; *line != nullptr; line++) {
|
||||
run_line(*line);
|
||||
}
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_platform_common/defines.h>
|
||||
#include <px4_daemon/server_io.h>
|
||||
|
||||
#include <sys/boardctl.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/reboot.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
long PX4_TICKS_PER_SEC = CONFIG_SYS_CLOCK_TICKS_PER_SEC;
|
||||
|
||||
int boardctl(unsigned int cmd, uintptr_t arg)
|
||||
{
|
||||
if (cmd == BOARDIOC_RESET) {
|
||||
sys_reboot(SYS_REBOOT_COLD);
|
||||
}
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
int dprintf(int fd, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
const int len = vsnprintf(nullptr, 0, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (len <= 0) {
|
||||
return len;
|
||||
}
|
||||
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
|
||||
if (buf == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, len + 1, format, args);
|
||||
va_end(args);
|
||||
|
||||
const int ret = write(fd, buf, len);
|
||||
free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__END_DECLS
|
||||
|
||||
FILE *get_stdout(bool *isatty_)
|
||||
{
|
||||
if (isatty_) {
|
||||
*isatty_ = false;
|
||||
}
|
||||
|
||||
return stdout;
|
||||
}
|
||||
@@ -0,0 +1,467 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file tasks.cpp
|
||||
* Implementation of the task API for Zephyr
|
||||
*/
|
||||
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/posix.h>
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <px4_zephyr.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PX4_MAX_TASKS CONFIG_POSIX_THREAD_THREADS_MAX
|
||||
|
||||
// a task id is the slot of the task in taskmap, and the generation of the slot above it, so that the id of a task
|
||||
// that was released doesn't name the task that reuses its slot
|
||||
static constexpr int TASK_SLOT_BITS = 8;
|
||||
static constexpr int TASK_SLOT_MASK = (1 << TASK_SLOT_BITS) - 1;
|
||||
static constexpr uint32_t TASK_GENERATION_MASK = INT32_MAX >> TASK_SLOT_BITS;
|
||||
static_assert(PX4_MAX_TASKS <= TASK_SLOT_MASK + 1, "too many tasks for the task ids");
|
||||
|
||||
// Zephyr thread attributes own the thread stack, so they are only destroyed after the thread has been joined
|
||||
enum class TaskState : uint8_t {
|
||||
Unused,
|
||||
Running,
|
||||
Exited,
|
||||
Released, // the thread was joined by the next spawn, the exit code waits for px4_task_join()
|
||||
};
|
||||
|
||||
struct task_entry {
|
||||
pthread_t pid;
|
||||
pthread_attr_t attr;
|
||||
void *data; // the arguments, freed with the task, as px4_task_delete() may stop it at any point
|
||||
char name[16];
|
||||
TaskState state;
|
||||
bool joining; // the thread joining the task releases it
|
||||
bool releasing; // a spawn is releasing the task, which then keeps its exit code
|
||||
uint32_t generation;
|
||||
int exit_code; // of a released task
|
||||
};
|
||||
|
||||
static task_entry taskmap[PX4_MAX_TASKS] {};
|
||||
static pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t task_released = PTHREAD_COND_INITIALIZER;
|
||||
|
||||
static px4_task_t task_id(int slot)
|
||||
{
|
||||
return (px4_task_t)((taskmap[slot].generation << TASK_SLOT_BITS) | slot);
|
||||
}
|
||||
|
||||
// the slot of a task that is not unused, or -1, with task_mutex held
|
||||
static int task_slot(px4_task_t id)
|
||||
{
|
||||
const int slot = id & TASK_SLOT_MASK;
|
||||
|
||||
if ((id < 0) || (slot >= PX4_MAX_TASKS) || (taskmap[slot].state == TaskState::Unused) || (task_id(slot) != id)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
px4_main_t entry;
|
||||
int slot; // in taskmap
|
||||
int argc;
|
||||
char *argv[];
|
||||
// strings are allocated after the struct data
|
||||
} pthdata_t;
|
||||
|
||||
static void *entry_adapter(void *ptr)
|
||||
{
|
||||
pthdata_t *data = (pthdata_t *)ptr;
|
||||
|
||||
// px4_task_delete() is expected to stop the task immediately, as task_delete() on NuttX, see px4_task_delete()
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
|
||||
pthread_setname_np(pthread_self(), taskmap[data->slot].name);
|
||||
|
||||
px4_task_exit(data->entry(data->argc, data->argv));
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// join the thread of a task and free its resources, then keep its exit code for px4_task_join(), or free the slot
|
||||
static int task_release(task_entry &task, bool keep_exit_code, int *exit_code)
|
||||
{
|
||||
void *retval = nullptr;
|
||||
const int rv = pthread_join(task.pid, &retval);
|
||||
pthread_attr_destroy(&task.attr);
|
||||
free(task.data);
|
||||
|
||||
// a task stopped by px4_task_delete() returns PTHREAD_CANCELED, -1
|
||||
const int code = (rv == 0) ? (int)(intptr_t)retval : PX4_ERROR;
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
task.data = nullptr;
|
||||
task.joining = false;
|
||||
task.releasing = false;
|
||||
task.exit_code = code;
|
||||
task.state = keep_exit_code ? TaskState::Released : TaskState::Unused;
|
||||
pthread_cond_broadcast(&task_released);
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
if ((rv == 0) && (exit_code != nullptr)) {
|
||||
*exit_code = code;
|
||||
}
|
||||
|
||||
return -rv;
|
||||
}
|
||||
|
||||
static void task_release_exited()
|
||||
{
|
||||
for (task_entry &task : taskmap) {
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
const bool exited = (task.state == TaskState::Exited) && !task.joining && !task.releasing;
|
||||
|
||||
if (exited) {
|
||||
task.releasing = true;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
if (exited) {
|
||||
task_release(task, true, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, px4_main_t entry,
|
||||
char *const argv[])
|
||||
{
|
||||
task_release_exited();
|
||||
|
||||
int argc = 0;
|
||||
size_t len = strlen(name) + 1;
|
||||
|
||||
while (argv != nullptr && argv[argc] != nullptr) {
|
||||
len += strlen(argv[argc]) + 1;
|
||||
argc++;
|
||||
}
|
||||
|
||||
const size_t structsize = sizeof(pthdata_t) + (argc + 2) * sizeof(char *);
|
||||
|
||||
pthdata_t *taskdata = (pthdata_t *)calloc(1, structsize + len);
|
||||
|
||||
if (taskdata == nullptr) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
taskdata->entry = entry;
|
||||
taskdata->argc = argc + 1;
|
||||
|
||||
// argv[0] is the task name, as on NuttX
|
||||
char *offset = (char *)taskdata + structsize;
|
||||
taskdata->argv[0] = offset;
|
||||
strcpy(offset, name);
|
||||
offset += strlen(name) + 1;
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
taskdata->argv[i + 1] = offset;
|
||||
strcpy(offset, argv[i]);
|
||||
offset += strlen(argv[i]) + 1;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
int slot = -1;
|
||||
|
||||
for (int i = 0; i < PX4_MAX_TASKS; i++) {
|
||||
if (taskmap[i].state == TaskState::Unused) {
|
||||
slot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// then the slot of a task that was released and never joined, whose id then names no task
|
||||
for (int i = 0; (slot < 0) && (i < PX4_MAX_TASKS); i++) {
|
||||
if (taskmap[i].state == TaskState::Released) {
|
||||
slot = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (slot < 0) {
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
free(taskdata);
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
task_entry &task = taskmap[slot];
|
||||
task.generation = (task.generation + 1) & TASK_GENERATION_MASK;
|
||||
const px4_task_t id = task_id(slot);
|
||||
taskdata->slot = slot;
|
||||
task.data = taskdata;
|
||||
task.joining = false;
|
||||
task.releasing = false;
|
||||
strncpy(task.name, name, sizeof(task.name) - 1);
|
||||
task.name[sizeof(task.name) - 1] = '\0';
|
||||
|
||||
if (stack_size < (int)PTHREAD_STACK_MIN) {
|
||||
stack_size = PTHREAD_STACK_MIN;
|
||||
}
|
||||
|
||||
sched_param param{};
|
||||
param.sched_priority = priority;
|
||||
|
||||
int rv = pthread_attr_init(&task.attr);
|
||||
|
||||
if (rv == 0) {
|
||||
rv = pthread_attr_setstacksize(&task.attr, stack_size);
|
||||
|
||||
if (rv == 0) {
|
||||
rv = pthread_attr_setinheritsched(&task.attr, PTHREAD_EXPLICIT_SCHED);
|
||||
}
|
||||
|
||||
if (rv == 0) {
|
||||
rv = pthread_attr_setschedpolicy(&task.attr, scheduler);
|
||||
}
|
||||
|
||||
if (rv == 0) {
|
||||
rv = pthread_attr_setschedparam(&task.attr, ¶m);
|
||||
}
|
||||
|
||||
if (rv == 0) {
|
||||
task.state = TaskState::Running;
|
||||
rv = pthread_create(&task.pid, &task.attr, &entry_adapter, taskdata);
|
||||
}
|
||||
|
||||
if (rv != 0) {
|
||||
pthread_attr_destroy(&task.attr);
|
||||
}
|
||||
}
|
||||
|
||||
if (rv != 0) {
|
||||
PX4_ERR("failed to create thread for %s (%i)", name, rv);
|
||||
task.data = nullptr;
|
||||
task.state = TaskState::Unused;
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
free(taskdata);
|
||||
return -rv;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
int px4_zephyr_task_join(int id, int *exit_code)
|
||||
{
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
int slot = task_slot(id);
|
||||
|
||||
// a task that exited can be released by a spawn before it is joined: wait for its exit code
|
||||
while ((slot >= 0) && taskmap[slot].releasing) {
|
||||
pthread_cond_wait(&task_released, &task_mutex);
|
||||
slot = task_slot(id);
|
||||
}
|
||||
|
||||
if (slot < 0) {
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
return -ESRCH;
|
||||
}
|
||||
|
||||
task_entry &task = taskmap[slot];
|
||||
|
||||
if (task.state == TaskState::Released) {
|
||||
if (exit_code != nullptr) {
|
||||
*exit_code = task.exit_code;
|
||||
}
|
||||
|
||||
task.state = TaskState::Unused;
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// another thread joins the task
|
||||
if (task.joining) {
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
// the task keeps running while it is joined: it can still be listed and deleted
|
||||
task.joining = true;
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return task_release(task, false, exit_code);
|
||||
}
|
||||
|
||||
int px4_task_join(px4_task_t id)
|
||||
{
|
||||
return px4_zephyr_task_join(id, nullptr);
|
||||
}
|
||||
|
||||
int px4_task_delete(px4_task_t id)
|
||||
{
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
const int slot = task_slot(id);
|
||||
|
||||
if ((slot < 0) || (taskmap[slot].state != TaskState::Running)) {
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
const pthread_t pid = taskmap[slot].pid;
|
||||
|
||||
if (pthread_equal(pid, pthread_self())) {
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
px4_task_exit(0);
|
||||
}
|
||||
|
||||
// The task cancels asynchronously: Zephyr aborts its thread wherever it runs, as Zephyr's blocking calls are not
|
||||
// cancellation points. A lock the task holds then, such as the heap's or that of a uORB topic, is never released,
|
||||
// and the threads that wait for it block forever. MAVLink deletes its task only once it did not stop when asked to.
|
||||
const int rv = pthread_cancel(pid);
|
||||
char name[sizeof(taskmap[slot].name)];
|
||||
strcpy(name, taskmap[slot].name);
|
||||
|
||||
if (rv == 0) {
|
||||
taskmap[slot].state = TaskState::Exited;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
if (rv == 0) {
|
||||
PX4_WARN("task %s deleted", name);
|
||||
}
|
||||
|
||||
return -rv;
|
||||
}
|
||||
|
||||
void px4_task_exit(int ret)
|
||||
{
|
||||
const pthread_t pid = pthread_self();
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
for (task_entry &task : taskmap) {
|
||||
if ((task.state == TaskState::Running) && pthread_equal(task.pid, pid)) {
|
||||
task.state = TaskState::Exited;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
pthread_exit((void *)(intptr_t)ret);
|
||||
}
|
||||
|
||||
int px4_task_kill(px4_task_t id, int sig)
|
||||
{
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
void px4_show_tasks()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
PX4_INFO("Active Tasks:");
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
for (const task_entry &task : taskmap) {
|
||||
if (task.state == TaskState::Running) {
|
||||
PX4_INFO(" %-10s %lu", task.name, (unsigned long)task.pid);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
if (count == 0) {
|
||||
PX4_INFO(" No running tasks");
|
||||
}
|
||||
}
|
||||
|
||||
bool px4_task_is_running(const char *taskname)
|
||||
{
|
||||
bool running = false;
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
for (const task_entry &task : taskmap) {
|
||||
if ((task.state == TaskState::Running) && (strcmp(task.name, taskname) == 0)) {
|
||||
running = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
px4_task_t px4_getpid()
|
||||
{
|
||||
const pthread_t pid = pthread_self();
|
||||
px4_task_t ret = -1;
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
|
||||
// released tasks have no thread, whose pthread_t Zephyr gives to new threads
|
||||
for (int i = 0; i < PX4_MAX_TASKS; i++) {
|
||||
if (((taskmap[i].state == TaskState::Running) || (taskmap[i].state == TaskState::Exited))
|
||||
&& pthread_equal(taskmap[i].pid, pid)) {
|
||||
ret = task_id(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char *px4_get_taskname()
|
||||
{
|
||||
const px4_task_t id = px4_getpid();
|
||||
return (id >= 0) ? taskmap[id & TASK_SLOT_MASK].name : "UnknownApp";
|
||||
}
|
||||
|
||||
int px4_prctl(int option, const char *arg2, px4_task_t pid)
|
||||
{
|
||||
if (option == PR_SET_NAME) {
|
||||
return pthread_setname_np(pthread_self(), arg2);
|
||||
}
|
||||
|
||||
PX4_WARN("FAILED SETTING TASK NAME");
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
manifest:
|
||||
remotes:
|
||||
- name: zephyrproject-rtos
|
||||
url-base: https://github.com/zephyrproject-rtos
|
||||
|
||||
projects:
|
||||
- name: zephyr
|
||||
remote: zephyrproject-rtos
|
||||
revision: v4.5.0
|
||||
import:
|
||||
name-allowlist:
|
||||
- cmsis
|
||||
- cmsis_6
|
||||
- hal_nxp
|
||||
- picolibc
|
||||
@@ -4,10 +4,21 @@
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
#include <zephyr/irq.h>
|
||||
|
||||
// The node lock masks the interrupts, as on NuttX: with a semaphore, a low priority thread
|
||||
// preempted while it copies a topic would block the higher priority threads that use it
|
||||
#define ATOMIC_ENTER unsigned int atomic_key = irq_lock()
|
||||
#define ATOMIC_LEAVE irq_unlock(atomic_key)
|
||||
#define ATOMIC_PAUSE irq_unlock(atomic_key)
|
||||
#define ATOMIC_RESUME atomic_key = irq_lock()
|
||||
#else
|
||||
#define ATOMIC_ENTER lock()
|
||||
#define ATOMIC_LEAVE unlock()
|
||||
#define ATOMIC_PAUSE
|
||||
#define ATOMIC_RESUME
|
||||
#endif
|
||||
|
||||
namespace cdev
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
//
|
||||
// On POSIX/SITL we always use real atomics: ThreadSanitizer needs them to stay
|
||||
// clean, native 64-bit atomics are cheap there, and we don't care about the
|
||||
// overhead. On flight hardware (NuttX/QURT) these are single-writer diagnostic
|
||||
// overhead. On flight hardware (NuttX/QURT/Zephyr) these are single-writer diagnostic
|
||||
// counters, and a 64-bit atomic lowers to a locked (interrupt-disabling)
|
||||
// sequence on Cortex-M (~2x cost on this hot path). There a concurrent reader
|
||||
// can at worst observe an off-by-one torn read of a 64-bit field, which is
|
||||
@@ -64,7 +64,7 @@
|
||||
// PerfVar mirrors the subset of the px4::atomic API used below so the call
|
||||
// sites are identical in both configurations; on embedded the accessors are
|
||||
// trivial and inline to bare loads/stores.
|
||||
#if defined(__PX4_POSIX) || defined(__SANITIZE_THREAD__)
|
||||
#if (defined(__PX4_POSIX) && !defined(__PX4_ZEPHYR)) || defined(__SANITIZE_THREAD__)
|
||||
template <typename T> using PerfVar = px4::atomic<T>;
|
||||
#else
|
||||
template <typename T>
|
||||
|
||||
@@ -45,6 +45,10 @@
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
|
||||
#if defined(__PX4_ZEPHYR)
|
||||
#include <zephyr/version.h>
|
||||
#endif
|
||||
|
||||
// dev >= 0
|
||||
// alpha >= 64
|
||||
// beta >= 128
|
||||
@@ -322,6 +326,8 @@ uint32_t px4_os_version(void)
|
||||
|
||||
#elif defined(__PX4_NUTTX)
|
||||
return version_tag_to_number(NUTTX_GIT_TAG_STR);
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
return version_tag_to_number(KERNEL_VERSION_STRING);
|
||||
#else
|
||||
# error "px4_os_version not implemented for current OS"
|
||||
#endif
|
||||
@@ -331,6 +337,8 @@ const char *px4_os_version_string(void)
|
||||
{
|
||||
#if defined(__PX4_NUTTX)
|
||||
return NUTTX_GIT_VERSION_STR;
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
return STRINGIFY(BUILD_VERSION);
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
@@ -348,6 +356,8 @@ const char *px4_os_name(void)
|
||||
return "NuttX";
|
||||
#elif defined(__PX4_CYGWIN)
|
||||
return "Cygwin";
|
||||
#elif defined(__PX4_ZEPHYR)
|
||||
return "Zephyr";
|
||||
#else
|
||||
# error "px4_os_name not implemented for current OS"
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
name: px4
|
||||
build:
|
||||
cmake: platforms/zephyr/module
|
||||
kconfig: platforms/zephyr/module/Kconfig
|
||||
settings:
|
||||
dts_root: platforms/zephyr
|
||||
samples:
|
||||
- platforms/zephyr/app
|
||||
tests:
|
||||
- platforms/zephyr/tests
|
||||
package-managers:
|
||||
pip:
|
||||
requirement-files:
|
||||
- platforms/zephyr/requirements.txt
|
||||
Reference in new issue
Block a user