diff --git a/CMakeLists.txt b/CMakeLists.txt index 59b538a..15fe1f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,49 +52,31 @@ configure_file ( ${OSAL_BINARY_DIR}/src/version.h ) -# Add platform-dependent targets early, so they can be configured by -# platform -add_library(osal "") - -# Suppress certain warnings when building with MSVC -if (WINDOWS_MONO) -target_compile_options (osal - PRIVATE - /wd4820 # padding added - ) -endif() +add_library(osal-interface INTERFACE) -# Use position independent code if platform supports shared libraries -get_property(SUPPORTS_SHARED GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS) -if (SUPPORTS_SHARED) - set_property(TARGET osal PROPERTY POSITION_INDEPENDENT_CODE ON) -endif() +target_include_directories(osal-interface + INTERFACE + $ + $ + ) if (CMAKE_PROJECT_NAME STREQUAL OSAL AND BUILD_TESTING) add_executable(osal_test "") endif() +# The default system implementation of the OSAL +# is defined by the build platform, but we provide +# a common help string here. +set (OSAL_DEFAULT_SYSTEM_HELP_STRING "The default Osal system implementation to use") + # Platform configuration include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${CMAKE_SYSTEM_NAME}.cmake) -set_target_properties (osal - PROPERTIES - C_STANDARD 99 - ) - -target_compile_features(osal PUBLIC c_std_99) - -target_include_directories(osal - PUBLIC - $ - $ - PRIVATE - src - ${OSAL_BINARY_DIR}/src - ) +# Add alias target for the default system +add_library(osal ALIAS ${OSAL_DEFAULT_SYSTEM}) install( - TARGETS osal + TARGETS osal-interface EXPORT OsalTargets ) @@ -123,6 +105,17 @@ install(FILES DESTINATION include ) +install(FILES + include/sys/osal_cc_clang.h + include/sys/osal_cc_gcc.h + include/sys/osal_cc_msvc.h + include/sys/osal_cc_rtk.h + include/sys/osal_cc.h + include/osal_log.h + DESTINATION include + ) + + if (CMAKE_PROJECT_NAME STREQUAL OSAL AND BUILD_TESTING) add_subdirectory (test) include(AddGoogleTest) diff --git a/cmake/Linux.cmake b/cmake/Linux.cmake index 311e96d..bcf0793 100644 --- a/cmake/Linux.cmake +++ b/cmake/Linux.cmake @@ -12,46 +12,14 @@ # license. See the file LICENSE distributed with this software for # full license information. #*******************************************************************/ - find_package(Threads) -option (USE_SCHED_FIFO - "Use SCHED_FIFO policy. May require extra privileges to run" - OFF) - -target_sources(osal PRIVATE - src/linux/osal.c - src/linux/osal_log.c - ) - -target_compile_options(osal - PRIVATE - -Wall - -Wextra - -Werror - -Wno-unused-parameter - $<$:-DUSE_SCHED_FIFO> - INTERFACE - $<$:--coverage> - ) - -target_include_directories(osal PUBLIC - $ - ) - -target_link_libraries(osal PUBLIC - Threads::Threads - rt - INTERFACE - $<$:--coverage> - ) - -install(FILES - src/linux/sys/osal_cc.h - src/linux/sys/osal_sys.h - DESTINATION include/sys - ) +add_subdirectory(src/linux) if (BUILD_TESTING) set(GOOGLE_TEST_INDIVIDUAL TRUE) endif() + +set (OSAL_DEFAULT_SYSTEM + osal-linux + CACHE STRING ${OSAL_DEFAULT_SYSTEM_HELP_STRING}) diff --git a/cmake/OsalConfig.cmake.in b/cmake/OsalConfig.cmake.in index 87890f3..1348634 100644 --- a/cmake/OsalConfig.cmake.in +++ b/cmake/OsalConfig.cmake.in @@ -7,3 +7,11 @@ endif() @PACKAGE_INIT@ include ( "${CMAKE_CURRENT_LIST_DIR}/OsalTargets.cmake" ) + +set (OSAL_DEFAULT_SYSTEM @OSAL_DEFAULT_SYSTEM@ CACHE STRING "@OSAL_DEFAULT_SYSTEM_HELP_STRING@") + +if (TARGET ${OSAL_DEFAULT_SYSTEM}) + add_library (osal ALIAS ${OSAL_DEFAULT_SYSTEM}) +else() + message (VERBOSE "Osal default system not available yet") +endif() diff --git a/cmake/STM32Cube.cmake b/cmake/STM32Cube.cmake index d4370e8..c570635 100644 --- a/cmake/STM32Cube.cmake +++ b/cmake/STM32Cube.cmake @@ -13,32 +13,18 @@ # full license information. #*******************************************************************/ -target_sources(osal PRIVATE - src/freertos/osal.c - src/freertos/osal_log.c - ) - -target_compile_options(osal - PRIVATE - -Wall - -Wextra - -Werror - -Wno-unused-parameter - ) - -target_include_directories(osal PUBLIC - $ - ) - -install(FILES - src/freertos/sys/osal_cc.h - src/freertos/sys/osal_sys.h - DESTINATION include/sys - ) - add_library(cube) -target_link_libraries(osal cube) install(TARGETS cube EXPORT OsalTargets) set(CORE_DIR src/freertos/${BOARD}) include(STM32Cube/${BOARD}) + +add_subdirectory(src/freertos) +target_link_libraries(osal-freertos + PRIVATE + cube + ) + +set (OSAL_DEFAULT_SYSTEM + osal-freertos + CACHE STRING ${OSAL_DEFAULT_SYSTEM_HELP_STRING}) diff --git a/cmake/Windows.cmake b/cmake/Windows.cmake index b16bf4f..97c7cdc 100644 --- a/cmake/Windows.cmake +++ b/cmake/Windows.cmake @@ -13,46 +13,12 @@ # full license information. #*******************************************************************/ -target_sources(osal PRIVATE - src/windows/osal.c - src/windows/osal_log.c - ) - -target_compile_options(osal - PRIVATE - $<$: - /W4 - /WX - /wd4100 - /wd4152 - > - - $<$: - -Wall - -Wextra - -Werror - -Wno-unused-parameter - > - - PUBLIC - $<$: - /wd4200 - > - ) - -target_link_libraries(osal - winmm) - -target_include_directories(osal PUBLIC - $ - ) - -install(FILES - src/windows/sys/osal_cc.h - src/windows/sys/osal_sys.h - DESTINATION include/sys - ) +add_subdirectory(src/windows) if (BUILD_TESTING) set(GOOGLE_TEST_INDIVIDUAL TRUE) endif() + +set (OSAL_DEFAULT_SYSTEM + osal-windows + CACHE STRING ${OSAL_DEFAULT_SYSTEM_HELP_STRING}) diff --git a/cmake/iMX8MM.cmake b/cmake/iMX8MM.cmake index e9eb45e..5158a8d 100644 --- a/cmake/iMX8MM.cmake +++ b/cmake/iMX8MM.cmake @@ -14,25 +14,18 @@ # full license information. #*******************************************************************/ -target_sources(osal PRIVATE - src/freertos/osal.c - src/freertos/osal_log.c - ) - -target_include_directories(osal PUBLIC - $ - ) - -install(FILES - src/freertos/sys/osal_cc.h - src/freertos/sys/osal_sys.h - DESTINATION include/sys - ) - add_library(mcuxsdk) -target_link_libraries(osal mcuxsdk) install(TARGETS mcuxsdk EXPORT OsalTargets) - set(CORE_DIR src/freertos/${BOARD}) include(MCUXSDK/${BOARD}) + +add_subdirectory(src/freertos) +target_link_libraries(osal-freertos + PRIVATE + cube + ) + +set (OSAL_DEFAULT_SYSTEM + osal-freertos + CACHE STRING ${OSAL_DEFAULT_SYSTEM_HELP_STRING}) diff --git a/cmake/rt-kernel.cmake b/cmake/rt-kernel.cmake index 816142e..ce8a1ed 100644 --- a/cmake/rt-kernel.cmake +++ b/cmake/rt-kernel.cmake @@ -13,29 +13,8 @@ # full license information. #*******************************************************************/ -target_sources(osal PRIVATE - src/rt-kernel/osal.c - src/rt-kernel/osal_log.c - ) +add_subdirectory(src/rt-kernel) -target_compile_options(osal - PRIVATE - -Wall - -Wextra - -Werror - -Wno-unused-parameter - ) - -target_link_libraries(osal - kern - ) - -target_include_directories(osal PUBLIC - $ - ) - -install(FILES - src/rt-kernel/sys/osal_cc.h - src/rt-kernel/sys/osal_sys.h - DESTINATION include/sys - ) +set (OSAL_DEFAULT_SYSTEM + osal-rt-kernel + CACHE STRING ${OSAL_DEFAULT_SYSTEM_HELP_STRING}) diff --git a/include/osal.h b/include/osal.h index b136e08..2caa7ab 100644 --- a/include/osal.h +++ b/include/osal.h @@ -25,7 +25,6 @@ extern "C" { #include #include -#include "sys/osal_sys.h" #include "sys/osal_cc.h" #ifndef MIN @@ -44,41 +43,19 @@ extern "C" { #define NELEMENTS(a) (sizeof (a) / sizeof ((a)[0])) #endif -#ifndef OS_WAIT_FOREVER #define OS_WAIT_FOREVER 0xFFFFFFFF -#endif #ifndef OS_MAIN #define OS_MAIN int main #endif -#ifndef OS_MUTEX -typedef void os_mutex_t; -#endif - -#ifndef OS_SEM -typedef void os_sem_t; -#endif - -#ifndef OS_THREAD -typedef void os_thread_t; -#endif - -#ifndef OS_EVENT -typedef void os_event_t; -#endif - -#ifndef OS_MBOX -typedef void os_mbox_t; -#endif - -#ifndef OS_TIMER -typedef void os_timer_t; -#endif - -#ifndef OS_TICK -typedef void os_tick_t; -#endif +typedef struct os_mutex os_mutex_t; +typedef struct os_sem os_sem_t; +typedef struct os_thread os_thread_t; +typedef struct os_event os_event_t; +typedef struct os_mbox os_mbox_t; +typedef struct os_timer os_timer_t; +typedef uint64_t os_tick_t; void * os_malloc (size_t size); void os_free (void * ptr); diff --git a/include/sys/osal_cc.h b/include/sys/osal_cc.h new file mode 100644 index 0000000..11ea001 --- /dev/null +++ b/include/sys/osal_cc.h @@ -0,0 +1,26 @@ +/********************************************************************* + * _ _ _ + * _ __ | |_ _ | | __ _ | |__ ___ + * | '__|| __|(_)| | / _` || '_ \ / __| + * | | | |_ _ | || (_| || |_) |\__ \ + * |_| \__|(_)|_| \__,_||_.__/ |___/ + * + * www.rt-labs.com + * Copyright 2017 rt-labs AB, Sweden. + * + * This software is licensed under the terms of the BSD 3-clause + * license. See the file LICENSE distributed with this software for + * full license information. + ********************************************************************/ + +#if defined(__rtk__) +#include "osal_cc_rtk.h" +#elif defined(__GNUC__) || defined(__GNUG__) +#include "osal_cc_gcc.h" +#elif defined(__clang__) +#include "osal_cc_clang.h" +#elif defined(_MSC_VER) +#include "osal_cc_msvc.h" +#else +#error "Unsupported compiler" +#endif diff --git a/src/linux/sys/osal_cc.h b/include/sys/osal_cc_clang.h similarity index 96% rename from src/linux/sys/osal_cc.h rename to include/sys/osal_cc_clang.h index fd51af0..6b84baf 100644 --- a/src/linux/sys/osal_cc.h +++ b/include/sys/osal_cc_clang.h @@ -22,17 +22,10 @@ extern "C" { #include -#if defined(__clang__) -#if !defined(CLANG_ANALYZER_NORETURN) #if __has_feature(attribute_analyzer_noreturn) #define CLANG_ANALYZER_NORETURN __attribute__ ((analyzer_noreturn)) #else #define CLANG_ANALYZER_NORETURN -#endif -#endif -#else -#define CLANG_ANALYZER_NORETURN -#endif static inline void cc_assert (int exp) CLANG_ANALYZER_NORETURN { diff --git a/src/windows/sys/osal_cc.h b/include/sys/osal_cc_gcc.h similarity index 67% rename from src/windows/sys/osal_cc.h rename to include/sys/osal_cc_gcc.h index 5c2b7fc..de81942 100644 --- a/src/windows/sys/osal_cc.h +++ b/include/sys/osal_cc_gcc.h @@ -20,13 +20,17 @@ extern "C" { #endif -#if defined(__GNUC__) || defined(__GNUG__) - #include +static inline void cc_assert (int exp) +{ + assert (exp); // LCOV_EXCL_LINE +} + #define CC_PACKED_BEGIN #define CC_PACKED_END #define CC_PACKED __attribute__ ((packed)) +#define CC_ALIGNED(n) __attribute__((aligned (n))) #define CC_FORMAT(str, arg) __attribute__ ((format (printf, str, arg))) @@ -78,60 +82,6 @@ extern "C" { #define CC_UNUSED(var) (void)(var) -static inline void cc_assert (int exp) -{ - assert (exp); // LCOV_EXCL_LINE -} - -#elif defined(_MSC_VER) - -#include - -#define CC_PACKED_BEGIN __pragma (pack (push, 1)) -#define CC_PACKED_END __pragma (pack (pop)) -#define CC_PACKED - -#define CC_FORMAT(str, arg) - -#define CC_TO_LE16(x) ((uint16_t)(x)) -#define CC_TO_LE32(x) ((uint32_t)(x)) -#define CC_TO_LE64(x) ((uint64_t)(x)) -#define CC_FROM_LE16(x) ((uint16_t)(x)) -#define CC_FROM_LE32(x) ((uint32_t)(x)) -#define CC_FROM_LE64(x) ((uint64_t)(x)) -#define CC_TO_BE16(x) ((uint16_t)_byteswap_ushort (x)) -#define CC_TO_BE32(x) ((uint32_t)_byteswap_ulong (x)) -#define CC_TO_BE64(x) ((uint64_t)_byteswap_uint64 (x)) -#define CC_FROM_BE16(x) ((uint16_t)_byteswap_ushort (x)) -#define CC_FROM_BE32(x) ((uint32_t)_byteswap_ulong (x)) -#define CC_FROM_BE64(x) ((uint64_t)_byteswap_uint64 (x)) - -/* TODO */ -#define CC_ATOMIC_GET8(p) (*p) -#define CC_ATOMIC_GET16(p) (*p) -#define CC_ATOMIC_GET32(p) (*p) -#define CC_ATOMIC_GET64(p) (*p) - -/* TODO */ -#define CC_ATOMIC_SET8(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET16(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET32(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET64(p, v) ((*p) = (v)) - -static uint8_t __inline cc_ctz (uint32_t x) -{ - DWORD n = 0; - _BitScanForward (&n, x); - return (uint8_t)n; -} - -#define __builtin_ctz(x) cc_ctz (x) - -#define CC_ASSERT(exp) assert (exp) -#define CC_STATIC_ASSERT(exp) static_assert ((exp), "") - -#endif - #ifdef __cplusplus } #endif diff --git a/include/sys/osal_cc_msvc.h b/include/sys/osal_cc_msvc.h new file mode 100644 index 0000000..7fdf5f6 --- /dev/null +++ b/include/sys/osal_cc_msvc.h @@ -0,0 +1,72 @@ +/********************************************************************* + * _ _ _ + * _ __ | |_ _ | | __ _ | |__ ___ + * | '__|| __|(_)| | / _` || '_ \ / __| + * | | | |_ _ | || (_| || |_) |\__ \ + * |_| \__|(_)|_| \__,_||_.__/ |___/ + * + * www.rt-labs.com + * Copyright 2017 rt-labs AB, Sweden. + * + * This software is licensed under the terms of the BSD 3-clause + * license. See the file LICENSE distributed with this software for + * full license information. + ********************************************************************/ + +#ifndef CC_H +#define CC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define CC_PACKED_BEGIN __pragma (pack (push, 1)) +#define CC_PACKED_END __pragma (pack (pop)) +#define CC_PACKED + +#define CC_FORMAT(str, arg) + +#define CC_TO_LE16(x) ((uint16_t)(x)) +#define CC_TO_LE32(x) ((uint32_t)(x)) +#define CC_TO_LE64(x) ((uint64_t)(x)) +#define CC_FROM_LE16(x) ((uint16_t)(x)) +#define CC_FROM_LE32(x) ((uint32_t)(x)) +#define CC_FROM_LE64(x) ((uint64_t)(x)) +#define CC_TO_BE16(x) ((uint16_t)_byteswap_ushort (x)) +#define CC_TO_BE32(x) ((uint32_t)_byteswap_ulong (x)) +#define CC_TO_BE64(x) ((uint64_t)_byteswap_uint64 (x)) +#define CC_FROM_BE16(x) ((uint16_t)_byteswap_ushort (x)) +#define CC_FROM_BE32(x) ((uint32_t)_byteswap_ulong (x)) +#define CC_FROM_BE64(x) ((uint64_t)_byteswap_uint64 (x)) + +/* TODO */ +#define CC_ATOMIC_GET8(p) (*p) +#define CC_ATOMIC_GET16(p) (*p) +#define CC_ATOMIC_GET32(p) (*p) +#define CC_ATOMIC_GET64(p) (*p) + +/* TODO */ +#define CC_ATOMIC_SET8(p, v) ((*p) = (v)) +#define CC_ATOMIC_SET16(p, v) ((*p) = (v)) +#define CC_ATOMIC_SET32(p, v) ((*p) = (v)) +#define CC_ATOMIC_SET64(p, v) ((*p) = (v)) + +static uint8_t __inline cc_ctz (uint32_t x) +{ + DWORD n = 0; + _BitScanForward (&n, x); + return (uint8_t)n; +} + +#define __builtin_ctz(x) cc_ctz (x) + +#define CC_ASSERT(exp) assert (exp) +#define CC_STATIC_ASSERT(exp) static_assert ((exp), "") + +#ifdef __cplusplus +} +#endif + +#endif /* CC_H */ diff --git a/src/rt-kernel/sys/osal_cc.h b/include/sys/osal_cc_rtk.h similarity index 100% rename from src/rt-kernel/sys/osal_cc.h rename to include/sys/osal_cc_rtk.h diff --git a/src/freertos/CMakeLists.txt b/src/freertos/CMakeLists.txt new file mode 100644 index 0000000..4c6ba7c --- /dev/null +++ b/src/freertos/CMakeLists.txt @@ -0,0 +1,43 @@ +#******************************************************************** +# _ _ _ +# _ __ | |_ _ | | __ _ | |__ ___ +# | '__|| __|(_)| | / _` || '_ \ / __| +# | | | |_ _ | || (_| || |_) |\__ \ +# |_| \__|(_)|_| \__,_||_.__/ |___/ +# +# www.rt-labs.com +# Copyright 2021 rt-labs AB, Sweden. +# Copyright 2023 NXP +# +# This software is licensed under the terms of the BSD 3-clause +# license. See the file LICENSE distributed with this software for +# full license information. +#*******************************************************************/ + +set(_osal_sys osal-freertos) +add_library(${_osal_sys}) + +target_include_directories(${_osal_sys} PRIVATE + sys +) + +target_compile_definitions(${_osal_sys} + PUBLIC + "OS_MAIN=int _main" +) + +target_sources(${_osal_sys} + PRIVATE + osal.c + osal_log.c + ) + +target_link_libraries(${_osal_sys} + PUBLIC + osal-interface + ) + +install( + TARGETS ${_osal_sys} + EXPORT OsalTargets + ) diff --git a/src/freertos/osal.c b/src/freertos/osal.c index a8d4625..397627f 100644 --- a/src/freertos/osal.c +++ b/src/freertos/osal.c @@ -14,6 +14,7 @@ ********************************************************************/ #include "osal.h" +#include "osal_sys.h" #include @@ -131,7 +132,7 @@ void os_sem_destroy (os_sem_t * sem) os_event_t * os_event_create (void) { EventGroupHandle_t handle = xEventGroupCreate(); - CC_ASSERT (handle); + CC_ASSERT (handle != NULL); return (os_event_t *)handle; } @@ -166,7 +167,7 @@ void os_event_destroy (os_event_t * event) os_mbox_t * os_mbox_create (size_t size) { QueueHandle_t handle = xQueueCreate (size, sizeof (void *)); - CC_ASSERT (handle); + CC_ASSERT (handle != NULL); return (os_mbox_t *)handle; } @@ -255,3 +256,8 @@ void os_timer_destroy (os_timer_t * timer) CC_ASSERT (status == pdPASS); free (timer); } + +void os_init (void * (main_fn)(int argc, char * argv[])) +{ + +} diff --git a/src/freertos/osal_log.c b/src/freertos/osal_log.c index 4bcaf0b..f24fe16 100644 --- a/src/freertos/osal_log.c +++ b/src/freertos/osal_log.c @@ -14,11 +14,12 @@ ********************************************************************/ #include "osal_log.h" +#include "osal_sys.h" #include #include -void os_log (uint8_t type, const char * fmt, ...) +static void os_log_impl (uint8_t type, const char * fmt, ...) { va_list list; @@ -47,3 +48,5 @@ void os_log (uint8_t type, const char * fmt, ...) vprintf (fmt, list); va_end (list); } + +os_log_t os_log = os_log_impl; diff --git a/src/freertos/sys/osal_cc.h b/src/freertos/sys/osal_cc.h deleted file mode 100644 index ab19a79..0000000 --- a/src/freertos/sys/osal_cc.h +++ /dev/null @@ -1,92 +0,0 @@ -/********************************************************************* - * _ _ _ - * _ __ | |_ _ | | __ _ | |__ ___ - * | '__|| __|(_)| | / _` || '_ \ / __| - * | | | |_ _ | || (_| || |_) |\__ \ - * |_| \__|(_)|_| \__,_||_.__/ |___/ - * - * www.rt-labs.com - * Copyright 2021 rt-labs AB, Sweden. - * - * This software is licensed under the terms of the BSD 3-clause - * license. See the file LICENSE distributed with this software for - * full license information. - ********************************************************************/ - -#ifndef CC_H -#define CC_H - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define CC_PACKED_BEGIN -#define CC_PACKED_END -#define CC_PACKED __attribute__((packed)) -#define CC_ALIGNED(n) __attribute__((aligned (n))) - -#define CC_FORMAT(str,arg) __attribute__((format (printf, str, arg))) - - -#if BYTE_ORDER == LITTLE_ENDIAN -#define CC_TO_LE16(x) ((uint16_t)(x)) -#define CC_TO_LE32(x) ((uint32_t)(x)) -#define CC_TO_LE64(x) ((uint64_t)(x)) -#define CC_FROM_LE16(x) ((uint16_t)(x)) -#define CC_FROM_LE32(x) ((uint32_t)(x)) -#define CC_FROM_LE64(x) ((uint64_t)(x)) -#define CC_TO_BE16(x) ((uint16_t)__builtin_bswap16 (x)) -#define CC_TO_BE32(x) ((uint32_t)__builtin_bswap32 (x)) -#define CC_TO_BE64(x) ((uint64_t)__builtin_bswap64 (x)) -#define CC_FROM_BE16(x) ((uint16_t)__builtin_bswap16 (x)) -#define CC_FROM_BE32(x) ((uint32_t)__builtin_bswap32 (x)) -#define CC_FROM_BE64(x) ((uint64_t)__builtin_bswap64 (x)) -#else -#define CC_TO_LE16(x) ((uint16_t)__builtin_bswap16 (x)) -#define CC_TO_LE32(x) ((uint32_t)__builtin_bswap32 (x)) -#define CC_TO_LE64(x) ((uint64_t)__builtin_bswap64 (x)) -#define CC_FROM_LE16(x) ((uint16_t)__builtin_bswap16 (x)) -#define CC_FROM_LE32(x) ((uint32_t)__builtin_bswap32 (x)) -#define CC_FROM_LE64(x) ((uint64_t)__builtin_bswap64 (x)) -#define CC_TO_BE16(x) ((uint16_t)(x)) -#define CC_TO_BE32(x) ((uint32_t)(x)) -#define CC_TO_BE64(x) ((uint64_t)(x)) -#define CC_FROM_BE16(x) ((uint16_t)(x)) -#define CC_FROM_BE32(x) ((uint32_t)(x)) -#define CC_FROM_BE64(x) ((uint64_t)(x)) -#endif - -#define CC_ATOMIC_GET8(p) (*p) -#define CC_ATOMIC_GET16(p) (*p) -#define CC_ATOMIC_GET32(p) (*p) -#define CC_ATOMIC_GET64(p) \ -({ \ - uint64_t v; \ - /*int_lock();*/ \ - v = *p; \ - /*int_unlock();*/ \ - v; \ -}) - -#define CC_ATOMIC_SET8(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET16(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET32(p, v) ((*p) = (v)) -#define CC_ATOMIC_SET64(p, v) \ -({ \ - /*int_lock();*/ \ - *p = v; \ - /*int_unlock();*/ \ -}) - -#define CC_ASSERT(exp) assert (exp) -#define CC_STATIC_ASSERT(exp) _Static_assert (exp, "") -#define CC_UNUSED(var) (void)(var) - -#ifdef __cplusplus -} -#endif - -#endif /* CC_H */ diff --git a/src/freertos/sys/osal_sys.h b/src/freertos/sys/osal_sys.h index 3b06a6f..e08e334 100644 --- a/src/freertos/sys/osal_sys.h +++ b/src/freertos/sys/osal_sys.h @@ -25,22 +25,6 @@ extern "C" { #endif -#define OS_MAIN extern "C" int _main - -#define OS_THREAD -#define OS_MUTEX -#define OS_SEM -#define OS_EVENT -#define OS_MBOX -#define OS_TIMER -#define OS_TICK - -typedef SemaphoreHandle_t os_mutex_t; -typedef TaskHandle_t os_thread_t; -typedef SemaphoreHandle_t os_sem_t; -typedef EventGroupHandle_t os_event_t; -typedef QueueHandle_t os_mbox_t; - typedef struct os_timer { TimerHandle_t handle; @@ -49,8 +33,6 @@ typedef struct os_timer uint32_t us; } os_timer_t; -typedef TickType_t os_tick_t; - #ifdef __cplusplus } #endif diff --git a/src/linux/CMakeLists.txt b/src/linux/CMakeLists.txt new file mode 100644 index 0000000..c1054f8 --- /dev/null +++ b/src/linux/CMakeLists.txt @@ -0,0 +1,65 @@ +#******************************************************************** +# _ _ _ +# _ __ | |_ _ | | __ _ | |__ ___ +# | '__|| __|(_)| | / _` || '_ \ / __| +# | | | |_ _ | || (_| || |_) |\__ \ +# |_| \__|(_)|_| \__,_||_.__/ |___/ +# +# www.rt-labs.com +# Copyright 2017 rt-labs AB, Sweden. +# +# This software is licensed under the terms of the BSD 3-clause +# license. See the file LICENSE distributed with this software for +# full license information. +#*******************************************************************/ + +find_package(Threads) + +option (USE_SCHED_FIFO + "Use SCHED_FIFO policy. May require extra privileges to run" + OFF) + +set(_osal_sys osal-linux) +add_library(${_osal_sys}) + +target_compile_features(${_osal_sys} + PUBLIC + c_std_99 + ) + +# Use position independent code if platform supports shared libraries +get_property(SUPPORTS_SHARED GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS) +if (SUPPORTS_SHARED) + set_property(TARGET ${_osal_sys} PROPERTY POSITION_INDEPENDENT_CODE ON) +endif() + +target_sources(${_osal_sys} + PRIVATE + osal.c + osal_log.c + ) + +target_compile_options(${_osal_sys} + PRIVATE + -Wall + -Wextra + -Werror + -Wno-unused-parameter + $<$:-DUSE_SCHED_FIFO> + ) + +target_include_directories(${_osal_sys} PRIVATE + sys +) + +target_link_libraries(${_osal_sys} + PUBLIC + osal-interface + Threads::Threads + rt + ) + +install( + TARGETS ${_osal_sys} + EXPORT OsalTargets + ) diff --git a/src/linux/osal.c b/src/linux/osal.c index fe8008c..f60dd8d 100644 --- a/src/linux/osal.c +++ b/src/linux/osal.c @@ -16,7 +16,7 @@ #define _GNU_SOURCE /* For pthread_setname_mp() */ #include "osal.h" -/* #include "options.h" */ +#include "osal_sys.h" #include #include @@ -57,7 +57,7 @@ os_thread_t * os_thread_create ( void * arg) { int result; - pthread_t * thread; + os_thread_t * thread; pthread_attr_t attr; thread = malloc (sizeof (*thread)); @@ -74,20 +74,20 @@ os_thread_t * os_thread_create ( pthread_attr_setschedparam (&attr, ¶m); #endif - result = pthread_create (thread, &attr, (void *)entry, arg); + result = pthread_create (&thread->thread, &attr, (void *)entry, arg); CC_ASSERT (result == 0); - pthread_setname_np (*thread, name); + pthread_setname_np (thread->thread, name); return thread; } os_mutex_t * os_mutex_create (void) { int result; - pthread_mutex_t * mutex; + os_mutex_t * mutex; pthread_mutexattr_t mattr; - mutex = malloc (sizeof (*mutex)); + mutex = malloc (sizeof (os_mutex_t)); CC_ASSERT (mutex != NULL); CC_STATIC_ASSERT (_POSIX_THREAD_PRIO_INHERIT > 0); @@ -95,28 +95,25 @@ os_mutex_t * os_mutex_create (void) pthread_mutexattr_setprotocol (&mattr, PTHREAD_PRIO_INHERIT); pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE); - result = pthread_mutex_init (mutex, &mattr); + result = pthread_mutex_init (&mutex->mutex, &mattr); CC_ASSERT (result == 0); return mutex; } -void os_mutex_lock (os_mutex_t * _mutex) +void os_mutex_lock (os_mutex_t * mutex) { - pthread_mutex_t * mutex = _mutex; - pthread_mutex_lock (mutex); + pthread_mutex_lock (&mutex->mutex); } -void os_mutex_unlock (os_mutex_t * _mutex) +void os_mutex_unlock (os_mutex_t * mutex) { - pthread_mutex_t * mutex = _mutex; - pthread_mutex_unlock (mutex); + pthread_mutex_unlock (&mutex->mutex); } -void os_mutex_destroy (os_mutex_t * _mutex) +void os_mutex_destroy (os_mutex_t * mutex) { - pthread_mutex_t * mutex = _mutex; - pthread_mutex_destroy (mutex); + pthread_mutex_destroy (&mutex->mutex); free (mutex); } @@ -571,7 +568,7 @@ void os_timer_stop (os_timer_t * timer) void os_timer_destroy (os_timer_t * timer) { timer->exit = true; - pthread_join (*timer->thread, NULL); + pthread_join (timer->thread->thread, NULL); timer_delete (timer->timerid); free (timer); } diff --git a/src/linux/sys/osal_sys.h b/src/linux/sys/osal_sys.h index 870fad6..7c2e87b 100644 --- a/src/linux/sys/osal_sys.h +++ b/src/linux/sys/osal_sys.h @@ -22,17 +22,15 @@ extern "C" { #include #include +#include -#define OS_THREAD -#define OS_MUTEX -#define OS_SEM -#define OS_EVENT -#define OS_MBOX -#define OS_TIMER -#define OS_TICK +typedef struct os_thread { + pthread_t thread; +} os_thread_t; -typedef pthread_t os_thread_t; -typedef pthread_mutex_t os_mutex_t; +typedef struct os_mutex { + pthread_mutex_t mutex; +} os_mutex_t; typedef struct os_sem { diff --git a/src/rt-kernel/CMakeLists.txt b/src/rt-kernel/CMakeLists.txt new file mode 100644 index 0000000..1ca566e --- /dev/null +++ b/src/rt-kernel/CMakeLists.txt @@ -0,0 +1,52 @@ +#******************************************************************** +# _ _ _ +# _ __ | |_ _ | | __ _ | |__ ___ +# | '__|| __|(_)| | / _` || '_ \ / __| +# | | | |_ _ | || (_| || |_) |\__ \ +# |_| \__|(_)|_| \__,_||_.__/ |___/ +# +# www.rt-labs.com +# Copyright 2017 rt-labs AB, Sweden. +# +# This software is licensed under the terms of the BSD 3-clause +# license. See the file LICENSE distributed with this software for +# full license information. +#*******************************************************************/ + +set(_osal_sys osal-rt-kernel) +add_library(${_osal_sys}) + +target_compile_features(${_osal_sys} + PUBLIC + c_std_99 + ) + +target_sources(${_osal_sys} + PRIVATE + osal.c + osal_log.c + ) + +target_compile_options(${_osal_sys} + PRIVATE + -Wall + -Wextra + -Werror + -Wno-unused-parameter + ) + +target_link_libraries(${_osal_sys} + PRIVATE + osal-interface + kern + ) + +target_include_directories(${_osal_sys} + PRIVATE + src/rt-kernel + ) + +install( + TARGETS ${_osal_sys} + EXPORT OsalTargets + ) diff --git a/src/rt-kernel/osal.c b/src/rt-kernel/osal.c index 140815a..c264f5c 100644 --- a/src/rt-kernel/osal.c +++ b/src/rt-kernel/osal.c @@ -14,6 +14,7 @@ ********************************************************************/ #include "osal.h" +#include "sys/osal_sys.h" void * os_malloc (size_t size) { @@ -32,27 +33,27 @@ os_thread_t * os_thread_create ( void (*entry) (void * arg), void * arg) { - return task_spawn (name, entry, priority, stacksize, arg); + return (os_thread_t *)task_spawn (name, entry, priority, stacksize, arg); } os_mutex_t * os_mutex_create (void) { - return mtx_create(); + return (os_mutex_t *)mtx_create(); } void os_mutex_lock (os_mutex_t * mutex) { - mtx_lock (mutex); + mtx_lock ((mtx_t *)mutex); } void os_mutex_unlock (os_mutex_t * mutex) { - mtx_unlock (mutex); + mtx_unlock ((mtx_t *)mutex); } void os_mutex_destroy (os_mutex_t * mutex) { - mtx_destroy (mutex); + mtx_destroy ((mtx_t *)mutex); } void os_usleep (uint32_t us) @@ -82,12 +83,13 @@ void os_tick_sleep (os_tick_t tick) os_sem_t * os_sem_create (size_t count) { - return sem_create (count); + return (os_sem_t *)sem_create (count); } -bool os_sem_wait (os_sem_t * sem, uint32_t time) +bool os_sem_wait (os_sem_t * sem_, uint32_t time) { int tmo = 0; + sem_t * sem = (sem_t *)sem_; if (time != OS_WAIT_FOREVER) { @@ -101,24 +103,27 @@ bool os_sem_wait (os_sem_t * sem, uint32_t time) return tmo; } -void os_sem_signal (os_sem_t * sem) +void os_sem_signal (os_sem_t * sem_) { + sem_t * sem = (sem_t *)sem_; sem_signal (sem); } -void os_sem_destroy (os_sem_t * sem) +void os_sem_destroy (os_sem_t * sem_) { + sem_t * sem = (sem_t *)sem_; sem_destroy (sem); } os_event_t * os_event_create (void) { - return flags_create (0); + return (os_event_t *)flags_create (0); } -bool os_event_wait (os_event_t * event, uint32_t mask, uint32_t * value, uint32_t time) +bool os_event_wait (os_event_t * event_, uint32_t mask, uint32_t * value, uint32_t time) { int tmo = 0; + flags_t * event = (flags_t *)event_; if (time != OS_WAIT_FOREVER) { @@ -133,29 +138,33 @@ bool os_event_wait (os_event_t * event, uint32_t mask, uint32_t * value, uint32_ return tmo; } -void os_event_set (os_event_t * event, uint32_t value) +void os_event_set (os_event_t * event_, uint32_t value) { + flags_t * event = (flags_t *)event_; flags_set (event, value); } -void os_event_clr (os_event_t * event, uint32_t value) +void os_event_clr (os_event_t * event_, uint32_t value) { + flags_t * event = (flags_t *)event_; flags_clr (event, value); } -void os_event_destroy (os_event_t * event) +void os_event_destroy (os_event_t * event_) { + flags_t * event = (flags_t *)event_; flags_destroy (event); } os_mbox_t * os_mbox_create (size_t size) { - return mbox_create (size); + return (os_mbox_t *)mbox_create (size); } -bool os_mbox_fetch (os_mbox_t * mbox, void ** msg, uint32_t time) +bool os_mbox_fetch (os_mbox_t * mbox_, void ** msg, uint32_t time) { int tmo = 0; + mbox_t * mbox = (mbox_t *)mbox_; if (time != OS_WAIT_FOREVER) { @@ -169,9 +178,10 @@ bool os_mbox_fetch (os_mbox_t * mbox, void ** msg, uint32_t time) return tmo; } -bool os_mbox_post (os_mbox_t * mbox, void * msg, uint32_t time) +bool os_mbox_post (os_mbox_t * mbox_, void * msg, uint32_t time) { int tmo = 0; + mbox_t * mbox = (mbox_t *)mbox_; if (time != OS_WAIT_FOREVER) { @@ -185,40 +195,46 @@ bool os_mbox_post (os_mbox_t * mbox, void * msg, uint32_t time) return tmo; } -void os_mbox_destroy (os_mbox_t * mbox) +void os_mbox_destroy (os_mbox_t * mbox_) { + mbox_t * mbox = (mbox_t *)mbox_; mbox_destroy (mbox); } os_timer_t * os_timer_create ( uint32_t us, - void (*fn) (os_timer_t *, void * arg), + void (*fn_) (os_timer_t *, void * arg), void * arg, bool oneshot) { - return tmr_create ( + void (*fn)(tmr_t *, void *) = (void (*)(tmr_t *, void *))fn_; + return (os_timer_t *)tmr_create ( tick_from_ms (us / 1000), fn, arg, (oneshot) ? TMR_ONCE : TMR_CYCL); } -void os_timer_set (os_timer_t * timer, uint32_t us) +void os_timer_set (os_timer_t * timer_, uint32_t us) { + tmr_t * timer = (tmr_t *)timer_; tmr_set (timer, tick_from_ms (us / 1000)); } -void os_timer_start (os_timer_t * timer) +void os_timer_start (os_timer_t * timer_) { + tmr_t * timer = (tmr_t *)timer_; tmr_start (timer); } -void os_timer_stop (os_timer_t * timer) +void os_timer_stop (os_timer_t * timer_) { + tmr_t * timer = (tmr_t *)timer_; tmr_stop (timer); } -void os_timer_destroy (os_timer_t * timer) +void os_timer_destroy (os_timer_t * timer_) { + tmr_t * timer = (tmr_t *)timer_; tmr_destroy (timer); } diff --git a/src/rt-kernel/sys/osal_sys.h b/src/rt-kernel/sys/osal_sys.h index f9a3eca..d58185a 100644 --- a/src/rt-kernel/sys/osal_sys.h +++ b/src/rt-kernel/sys/osal_sys.h @@ -22,22 +22,6 @@ extern "C" { #include -#define OS_THREAD -#define OS_MUTEX -#define OS_SEM -#define OS_EVENT -#define OS_MBOX -#define OS_TIMER -#define OS_TICK - -typedef task_t os_thread_t; -typedef mtx_t os_mutex_t; -typedef sem_t os_sem_t; -typedef flags_t os_event_t; -typedef mbox_t os_mbox_t; -typedef tmr_t os_timer_t; -typedef tick_t os_tick_t; - #ifdef __cplusplus } #endif diff --git a/src/windows/CMakeLists.txt b/src/windows/CMakeLists.txt new file mode 100644 index 0000000..b2ef641 --- /dev/null +++ b/src/windows/CMakeLists.txt @@ -0,0 +1,71 @@ +#******************************************************************** +# _ _ _ +# _ __ | |_ _ | | __ _ | |__ ___ +# | '__|| __|(_)| | / _` || '_ \ / __| +# | | | |_ _ | || (_| || |_) |\__ \ +# |_| \__|(_)|_| \__,_||_.__/ |___/ +# +# www.rt-labs.com +# Copyright 2017 rt-labs AB, Sweden. +# +# This software is licensed under the terms of the BSD 3-clause +# license. See the file LICENSE distributed with this software for +# full license information. +#*******************************************************************/ + +set(_osal_sys osal-windows) +add_library(${_osal_sys}) + +target_compile_features(${_osal_sys} PUBLIC + c_std_99 + ) + +# Use position independent code if platform supports shared libraries +get_property(SUPPORTS_SHARED GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS) +if (SUPPORTS_SHARED) + set_property(TARGET ${_osal_sys} PROPERTY POSITION_INDEPENDENT_CODE ON) +endif() + +target_sources(${_osal_sys} PRIVATE + osal.c + osal_log.c + ) + +target_compile_options(${_osal_sys} + PRIVATE + $<$: + /W4 + /WX + /wd4100 + /wd4152 + /wd4820 # padding added + > + + $<$: + -Wall + -Wextra + -Werror + -Wno-unused-parameter + > + + PUBLIC + $<$: + /wd4200 + > + ) + +target_link_libraries(${_osal_sys} + PUBLIC + osal-interface + winmm + ) + +target_include_directories(${_osal_sys} + PRIVATE + sys + ) + +install( + TARGETS ${_osal_sys} + EXPORT OsalTargets + ) diff --git a/src/windows/osal.c b/src/windows/osal.c index a8dd495..e844d06 100644 --- a/src/windows/osal.c +++ b/src/windows/osal.c @@ -14,6 +14,7 @@ ********************************************************************/ #include "osal.h" +#include "sys/osal_sys.h" #define URESOLUTION 10 @@ -32,22 +33,22 @@ os_mutex_t * os_mutex_create (void) CRITICAL_SECTION * handle; handle = CreateMutex (NULL, FALSE, NULL); CC_ASSERT (handle != INVALID_HANDLE_VALUE); - return handle; + return (os_mutex_t *)handle; } void os_mutex_lock (os_mutex_t * mutex) { - WaitForSingleObject (mutex, INFINITE); + WaitForSingleObject ((HANDLE)mutex, INFINITE); } void os_mutex_unlock (os_mutex_t * mutex) { - ReleaseMutex (mutex); + ReleaseMutex ((HANDLE)mutex); } void os_mutex_destroy (os_mutex_t * mutex) { - CloseHandle (mutex); + CloseHandle ((HANDLE)mutex); } void os_usleep (uint32_t usec) @@ -75,7 +76,7 @@ os_thread_t * os_thread_create ( { SetThreadPriority (handle, THREAD_PRIORITY_TIME_CRITICAL); } - return handle; + return (os_thread_t *)handle; } static uint64_t os_get_frequency_tick (void) diff --git a/src/windows/sys/osal_sys.h b/src/windows/sys/osal_sys.h index c5cf209..31fd7e3 100644 --- a/src/windows/sys/osal_sys.h +++ b/src/windows/sys/osal_sys.h @@ -28,18 +28,7 @@ extern "C" { #include #include -#define OS_WAIT_FOREVER INFINITE - -#define OS_THREAD -#define OS_MUTEX -#define OS_SEM -#define OS_EVENT -#define OS_MBOX -#define OS_TIMER -#define OS_TICK - -typedef HANDLE os_thread_t; -typedef CRITICAL_SECTION os_mutex_t; +CC_STATIC_ASSERT(OS_WAIT_FOREVER == INFINITE); typedef struct os_sem { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c26581b..90fdb17 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,12 +14,26 @@ #*******************************************************************/ # Get osal properties +get_target_property(OSAL_SOURCE_DIR osal SOURCE_DIR) get_target_property(OSAL_SOURCES osal SOURCES) get_target_property(OSAL_OPTIONS osal COMPILE_OPTIONS) +get_target_property(OSAL_COMPILE_DEFINITIONS osal COMPILE_DEFINITIONS) get_target_property(OSAL_INCLUDES osal INCLUDE_DIRECTORIES) get_target_property(OSAL_LIBS osal LINK_LIBRARIES) -if (OSAL_LIBS STREQUAL "OSAL_LIBS-NOTFOUND") +if (NOT OSAL_OPTIONS) + set(OSAL_OPTIONS "") +endif() + +if (NOT OSAL_COMPILE_DEFINITIONS) + set(OSAL_COMPILE_DEFINITIONS "") +endif() + +if (NOT OSAL_INCLUDES) + set(OSAL_INCLUDES "") +endif() + +if (NOT OSAL_LIBS) set(OSAL_LIBS "") endif() @@ -43,9 +57,14 @@ target_sources(osal_test PRIVATE osal_test.cpp ) +target_compile_definitions(osal_test + PRIVATE + UNIT_TEST + ${OSAL_COMPILE_DEFINITIONS} + ) + target_compile_options(osal_test PRIVATE - -DUNIT_TEST ${OSAL_OPTIONS} )