Skip to content

Commit

Permalink
Add support for Zephyr's native test environment
Browse files Browse the repository at this point in the history
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 <alberto.escolar.piedras@nordicsemi.no>

(f) Add support for Zephyr's POSIX architecture

(f) Add support for Zephyr's native
  • Loading branch information
aescolar committed Oct 12, 2023
1 parent b91611a commit d666f16
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libmetal/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions libmetal/lib/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ using std::atomic_signal_fence;
#elif defined(__GNUC__)
# include <metal/compiler/gcc/atomic.h>
#else
#if defined(METAL_PROCESSOR_HEADERS_ON_SYSTEM)
# include <metal/system/@PROJECT_SYSTEM@/atomic.h>
#else
# include <metal/processor/@PROJECT_PROCESSOR@/atomic.h>
#endif
#endif

#endif /* __METAL_ATOMIC__H__ */
4 changes: 4 additions & 0 deletions libmetal/lib/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#ifndef __METAL_CPU__H__
#define __METAL_CPU__H__

#if defined(METAL_PROCESSOR_HEADERS_ON_SYSTEM)
# include <metal/system/@PROJECT_SYSTEM@/cpu.h>
#else
# include <metal/processor/@PROJECT_PROCESSOR@/cpu.h>
#endif

#endif /* __METAL_CPU__H__ */
3 changes: 3 additions & 0 deletions libmetal/lib/system/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions libmetal/lib/system/zephyr/cpu.h
Original file line number Diff line number Diff line change
@@ -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 <zephyr/kernel.h>

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__ */

0 comments on commit d666f16

Please sign in to comment.