From 587e35a37f26fbeabd8d9469ec3831e299874961 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 14 Feb 2023 13:17:13 +0100 Subject: [PATCH] Add tick api to osal --- include/osal.h | 8 ++++++++ src/freertos/osal.c | 15 +++++++++++++++ src/freertos/sys/osal_sys.h | 3 +++ src/linux/osal.c | 30 +++++++++++++++++++++++++++++ src/linux/sys/osal_sys.h | 3 +++ src/rt-kernel/osal.c | 15 +++++++++++++++ src/rt-kernel/sys/osal_sys.h | 2 ++ src/windows/osal.c | 37 ++++++++++++++++++++++++++++-------- src/windows/sys/osal_sys.h | 3 +++ test/test_osal.cpp | 15 +++++++++++++++ 10 files changed, 123 insertions(+), 8 deletions(-) diff --git a/include/osal.h b/include/osal.h index cac4bfb..b136e08 100644 --- a/include/osal.h +++ b/include/osal.h @@ -76,12 +76,20 @@ typedef void os_mbox_t; typedef void os_timer_t; #endif +#ifndef OS_TICK +typedef void os_tick_t; +#endif + void * os_malloc (size_t size); void os_free (void * ptr); void os_usleep (uint32_t us); uint32_t os_get_current_time_us (void); +os_tick_t os_tick_current (void); +os_tick_t os_tick_from_us (uint32_t us); +void os_tick_sleep (os_tick_t tick); + os_thread_t * os_thread_create ( const char * name, uint32_t priority, diff --git a/src/freertos/osal.c b/src/freertos/osal.c index 037f901..26d83e9 100644 --- a/src/freertos/osal.c +++ b/src/freertos/osal.c @@ -86,6 +86,21 @@ uint32_t os_get_current_time_us (void) return 1000 * (xTaskGetTickCount() / portTICK_PERIOD_MS); } +os_tick_t os_tick_current (void) +{ + return xTaskGetTickCount(); +} + +os_tick_t os_tick_from_us (uint32_t us) +{ + return us / (1000u * portTICK_PERIOD_MS); +} + +void os_tick_sleep (os_tick_t tick) +{ + vTaskDelay (tick); +} + os_sem_t * os_sem_create (size_t count) { SemaphoreHandle_t handle = xSemaphoreCreateCounting (UINT32_MAX, count); diff --git a/src/freertos/sys/osal_sys.h b/src/freertos/sys/osal_sys.h index 4d6abfe..8e4370f 100644 --- a/src/freertos/sys/osal_sys.h +++ b/src/freertos/sys/osal_sys.h @@ -33,6 +33,7 @@ extern "C" { #define OS_EVENT #define OS_MBOX #define OS_TIMER +#define OS_TICK typedef SemaphoreHandle_t os_mutex_t; typedef TaskHandle_t os_thread_t; @@ -48,6 +49,8 @@ typedef struct os_timer uint32_t us; } os_timer_t; +typedef TickType_t os_tick_t; + #ifdef __cplusplus } #endif diff --git a/src/linux/osal.c b/src/linux/osal.c index 63acf0f..90847e6 100644 --- a/src/linux/osal.c +++ b/src/linux/osal.c @@ -234,6 +234,36 @@ uint32_t os_get_current_time_us (void) return ts.tv_sec * 1000 * 1000 + ts.tv_nsec / 1000; } +os_tick_t os_tick_current (void) +{ + struct timespec ts; + os_tick_t tick; + + clock_gettime (CLOCK_MONOTONIC, &ts); + tick = ts.tv_sec; + tick *= NSECS_PER_SEC; + tick += ts.tv_nsec; + return tick; +} + +os_tick_t os_tick_from_us (uint32_t us) +{ + return (os_tick_t)us * 1000; +} + +void os_tick_sleep (os_tick_t tick) +{ + struct timespec ts; + struct timespec remain; + + ts.tv_sec = tick / NSECS_PER_SEC; + ts.tv_nsec = tick % NSECS_PER_SEC; + while (clock_nanosleep (CLOCK_MONOTONIC, 0, &ts, &remain) != 0) + { + ts = remain; + } +} + os_event_t * os_event_create (void) { os_event_t * event; diff --git a/src/linux/sys/osal_sys.h b/src/linux/sys/osal_sys.h index 0f81219..870fad6 100644 --- a/src/linux/sys/osal_sys.h +++ b/src/linux/sys/osal_sys.h @@ -29,6 +29,7 @@ extern "C" { #define OS_EVENT #define OS_MBOX #define OS_TIMER +#define OS_TICK typedef pthread_t os_thread_t; typedef pthread_mutex_t os_mutex_t; @@ -70,6 +71,8 @@ typedef struct os_timer bool oneshot; } os_timer_t; +typedef uint64_t os_tick_t; + #ifdef __cplusplus } #endif diff --git a/src/rt-kernel/osal.c b/src/rt-kernel/osal.c index 2b82d0a..140815a 100644 --- a/src/rt-kernel/osal.c +++ b/src/rt-kernel/osal.c @@ -65,6 +65,21 @@ uint32_t os_get_current_time_us (void) return 1000 * tick_to_ms (tick_get()); } +os_tick_t os_tick_current (void) +{ + return tick_get(); +} + +os_tick_t os_tick_from_us (uint32_t us) +{ + return tick_from_ms (us / 1000); +} + +void os_tick_sleep (os_tick_t tick) +{ + task_delay (tick); +} + os_sem_t * os_sem_create (size_t count) { return sem_create (count); diff --git a/src/rt-kernel/sys/osal_sys.h b/src/rt-kernel/sys/osal_sys.h index d8974ee..f9a3eca 100644 --- a/src/rt-kernel/sys/osal_sys.h +++ b/src/rt-kernel/sys/osal_sys.h @@ -28,6 +28,7 @@ extern "C" { #define OS_EVENT #define OS_MBOX #define OS_TIMER +#define OS_TICK typedef task_t os_thread_t; typedef mtx_t os_mutex_t; @@ -35,6 +36,7 @@ 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 } diff --git a/src/windows/osal.c b/src/windows/osal.c index 6b6718b..124bff1 100644 --- a/src/windows/osal.c +++ b/src/windows/osal.c @@ -74,24 +74,45 @@ os_thread_t * os_thread_create ( return handle; } -uint32_t os_get_current_time_us (void) +static uint64_t os_get_frequency_tick (void) { - static LARGE_INTEGER performanceFrequency = {0}; - LARGE_INTEGER currentCount; - uint64_t currentTime; - - if (performanceFrequency.QuadPart == 0) + static uint64_t frequency; + if (frequency == 0) { + LARGE_INTEGER performanceFrequency; timeBeginPeriod (URESOLUTION); QueryPerformanceFrequency (&performanceFrequency); - performanceFrequency.QuadPart = performanceFrequency.QuadPart / (1000 * 1000); + frequency = performanceFrequency.QuadPart; } + return frequency; +} +uint32_t os_get_current_time_us (void) +{ + LARGE_INTEGER currentCount; + uint64_t currentTime; QueryPerformanceCounter (¤tCount); - currentTime = currentCount.QuadPart / performanceFrequency.QuadPart; + currentTime = 1000000 * currentCount.QuadPart / os_get_frequency_tick(); return (uint32_t)(currentTime & UINT32_MAX); } +os_tick_t os_tick_current (void) +{ + LARGE_INTEGER currentCount; + QueryPerformanceCounter (¤tCount); + return currentCount.QuadPart; +} + +os_tick_t os_tick_from_us (uint32_t us) +{ + return os_get_frequency_tick() * us / 1000000; +} + +void os_tick_sleep (os_tick_t tick) +{ + Sleep ((DWORD)(1000u * tick / os_get_frequency_tick())); +} + os_sem_t * os_sem_create (size_t count) { os_sem_t * sem; diff --git a/src/windows/sys/osal_sys.h b/src/windows/sys/osal_sys.h index a18af2c..d2f336e 100644 --- a/src/windows/sys/osal_sys.h +++ b/src/windows/sys/osal_sys.h @@ -30,6 +30,7 @@ extern "C" { #define OS_EVENT #define OS_MBOX #define OS_TIMER +#define OS_TICK typedef HANDLE os_thread_t; typedef CRITICAL_SECTION os_mutex_t; @@ -68,6 +69,8 @@ typedef struct os_timer bool oneshot; } os_timer_t; +typedef uint64_t os_tick_t; + #ifdef __cplusplus } #endif diff --git a/test/test_osal.cpp b/test/test_osal.cpp index a0f190a..9f1cd59 100644 --- a/test/test_osal.cpp +++ b/test/test_osal.cpp @@ -209,3 +209,18 @@ TEST_F (Osal, CurrentTime) EXPECT_NEAR (100 * 1000, t1 - t0, 1000); } + + +TEST_F (Osal, Tick) +{ + os_tick_t t0, t1, sleep; + + sleep = os_tick_from_us (100 * 1000); + t0 = os_tick_current(); + os_tick_sleep (sleep); + t1 = os_tick_current(); + + EXPECT_NEAR ((double)sleep, + (double)(t1 - t0), + (double)os_tick_from_us (1000)); +}