From 3d4e48666e2878a722b08af646b698535fbd1157 Mon Sep 17 00:00:00 2001 From: dmknutsen Date: Mon, 6 Apr 2020 10:25:41 -0400 Subject: [PATCH] Fix #12, Add tick to microsec rounding warning, update comments --- src/os/posix/ostimer.c | 7 ++++--- src/os/shared/osapi-common.c | 9 +++++++++ src/unit-test-coverage/shared/src/coveragetest-common.c | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/os/posix/ostimer.c b/src/os/posix/ostimer.c index 54ad418fc..1b85ad79d 100644 --- a/src/os/posix/ostimer.c +++ b/src/os/posix/ostimer.c @@ -285,9 +285,10 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) } /* - * Calculate microseconds per tick - Rounding UP - * - If the ratio is not an integer, this will choose the next higher value - * - The result is guaranteed not to be zero. + * Calculate microseconds per tick + * - If the ratio is not an integer, this will round to the nearest integer value + * - This is used internally for reporting accuracy, + * - TicksPerSecond values over 2M will return zero */ OS_SharedGlobalVars.MicroSecPerTick = (1000000 + (OS_SharedGlobalVars.TicksPerSecond / 2)) / OS_SharedGlobalVars.TicksPerSecond; diff --git a/src/os/shared/osapi-common.c b/src/os/shared/osapi-common.c index dde63d3b8..d7fbb2edc 100644 --- a/src/os/shared/osapi-common.c +++ b/src/os/shared/osapi-common.c @@ -64,6 +64,7 @@ int32 OS_API_Init(void) { int32 return_code = OS_SUCCESS; uint32 idtype; + uint32 microSecPerSec; if (OS_SharedGlobalVars.Initialized != false) { @@ -161,6 +162,14 @@ int32 OS_API_Init(void) return_code = OS_ERROR; } + microSecPerSec = OS_SharedGlobalVars.MicroSecPerTick * OS_SharedGlobalVars.TicksPerSecond; + + if ( microSecPerSec != 1000000 ) + { + OS_DEBUG("Warning: Microsecs per sec value of %lu does not equal 1000000 (MicroSecPerTick: %ld TicksPerSecond: %ld)\n", + (unsigned long) microSecPerSec, (long) OS_SharedGlobalVars.MicroSecPerTick, (long) OS_SharedGlobalVars.TicksPerSecond); + } + return(return_code); } /* end OS_API_Init */ diff --git a/src/unit-test-coverage/shared/src/coveragetest-common.c b/src/unit-test-coverage/shared/src/coveragetest-common.c index b6eeb1ad9..14a6e9195 100644 --- a/src/unit-test-coverage/shared/src/coveragetest-common.c +++ b/src/unit-test-coverage/shared/src/coveragetest-common.c @@ -97,6 +97,11 @@ void Test_OS_API_Init(void) OS_SharedGlobalVars.Initialized = false; OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_SUCCESS); + Test_MicroSecPerTick = 1000; + Test_TicksPerSecond = 1001; + OS_SharedGlobalVars.Initialized = false; + OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_SUCCESS); + /* Second call should return ERROR */ OSAPI_TEST_FUNCTION_RC(OS_API_Init(), OS_ERROR);