From d666f161564f2d9608be7234399d2483c63e75db Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Wed, 11 Oct 2023 10:39:51 +0200 Subject: [PATCH] Add support for Zephyr's native test environment Zephyr's native test environment is a setup which allows building the embedded SW with the Zephyr OS and HW models as a Linux executable. When building for this target the Zephyr integration will set METAL_PROCESSOR_HEADERS_ON_SYSTEM. We need to provide appropriate headers for this environment. The "metal_cpu_yield()" call, which in other architectures is handled as either a hint for the CPU to yield to another thread or do nothing, and therefore busy waiting, needs replacing with a call out to the Zephyr API which will cause the CPU to waste 1 microsecond, therefore enabling the other CPU in the system to ready whatever we are waiting for in the meanwhile. Signed-off-by: Alberto Escolar Piedras (f) Add support for Zephyr's POSIX architecture (f) Add support for Zephyr's native --- libmetal/lib/CMakeLists.txt | 7 +++++- libmetal/lib/atomic.h | 4 ++++ libmetal/lib/cpu.h | 4 ++++ libmetal/lib/system/zephyr/CMakeLists.txt | 3 +++ libmetal/lib/system/zephyr/cpu.h | 26 +++++++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 libmetal/lib/system/zephyr/cpu.h diff --git a/libmetal/lib/CMakeLists.txt b/libmetal/lib/CMakeLists.txt index e9d5276..c8dd7d2 100644 --- a/libmetal/lib/CMakeLists.txt +++ b/libmetal/lib/CMakeLists.txt @@ -41,7 +41,9 @@ collect (PROJECT_LIB_SOURCES softirq.c) collect (PROJECT_LIB_SOURCES version.c) add_subdirectory (compiler) -add_subdirectory (processor) +if (NOT DEFINED METAL_PROCESSOR_HEADERS_ON_SYSTEM) + add_subdirectory (processor) +endif() add_subdirectory (system) collector_list (_inc_dirs PROJECT_INC_DIRS) @@ -72,6 +74,9 @@ if (WITH_ZEPHYR) add_dependencies(metal offsets_h) zephyr_library_sources(${_sources}) zephyr_include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) + if (DEFINED METAL_PROCESSOR_HEADERS_ON_SYSTEM) + zephyr_compile_definitions(METAL_PROCESSOR_HEADERS_ON_SYSTEM) + endif() else (WITH_ZEPHYR) # Build a shared library if so configured. if (WITH_SHARED_LIB) diff --git a/libmetal/lib/atomic.h b/libmetal/lib/atomic.h index 44d6b9a..e0f387a 100644 --- a/libmetal/lib/atomic.h +++ b/libmetal/lib/atomic.h @@ -101,7 +101,11 @@ using std::atomic_signal_fence; #elif defined(__GNUC__) # include #else +#if defined(METAL_PROCESSOR_HEADERS_ON_SYSTEM) +# include +#else # include #endif +#endif #endif /* __METAL_ATOMIC__H__ */ diff --git a/libmetal/lib/cpu.h b/libmetal/lib/cpu.h index 9deade6..2a94cdb 100644 --- a/libmetal/lib/cpu.h +++ b/libmetal/lib/cpu.h @@ -12,6 +12,10 @@ #ifndef __METAL_CPU__H__ #define __METAL_CPU__H__ +#if defined(METAL_PROCESSOR_HEADERS_ON_SYSTEM) +# include +#else # include +#endif #endif /* __METAL_CPU__H__ */ diff --git a/libmetal/lib/system/zephyr/CMakeLists.txt b/libmetal/lib/system/zephyr/CMakeLists.txt index 43cd1b6..6605b9a 100644 --- a/libmetal/lib/system/zephyr/CMakeLists.txt +++ b/libmetal/lib/system/zephyr/CMakeLists.txt @@ -8,6 +8,9 @@ collect (PROJECT_LIB_HEADERS log.h) collect (PROJECT_LIB_HEADERS mutex.h) collect (PROJECT_LIB_HEADERS sleep.h) collect (PROJECT_LIB_HEADERS sys.h) +if (DEFINED METAL_PROCESSOR_HEADERS_ON_SYSTEM) + collect (PROJECT_LIB_HEADERS cpu.h) +endif() collect (PROJECT_LIB_SOURCES alloc.c) collect (PROJECT_LIB_SOURCES condition.c) diff --git a/libmetal/lib/system/zephyr/cpu.h b/libmetal/lib/system/zephyr/cpu.h new file mode 100644 index 0000000..364557a --- /dev/null +++ b/libmetal/lib/system/zephyr/cpu.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * @file cpu.h + * @brief Zephyr native test environment CPU specific primitives + */ + +#ifndef __METAL_ZEPHYR_CPU__H__ +#define __METAL_ZEPHYR_CPU__H__ + +#include + +static inline void metal_cpu_yield(void) +{ + /* + * In Zephyr's native test environment let 1 microsecond pass + * to allow other threads to run + */ + k_busy_wait(1); +}; + +#endif /* __METAL_ZEPHYR_CPU__H__ */