Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/osal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/freertos/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/freertos/sys/osal_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,8 @@ typedef struct os_timer
uint32_t us;
} os_timer_t;

typedef TickType_t os_tick_t;

#ifdef __cplusplus
}
#endif
Expand Down
30 changes: 30 additions & 0 deletions src/linux/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/linux/sys/osal_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,8 @@ typedef struct os_timer
bool oneshot;
} os_timer_t;

typedef uint64_t os_tick_t;

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions src/rt-kernel/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/rt-kernel/sys/osal_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ 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;
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
}
Expand Down
37 changes: 29 additions & 8 deletions src/windows/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (&currentCount);
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 (&currentCount);
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;
Expand Down
3 changes: 3 additions & 0 deletions src/windows/sys/osal_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,8 @@ typedef struct os_timer
bool oneshot;
} os_timer_t;

typedef uint64_t os_tick_t;

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions test/test_osal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}