From 1973eaf67ca7f01b92e1c4528413970a9d0fb969 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Sun, 19 Mar 2017 15:07:30 -0400 Subject: [PATCH] [Simulator] Improve CoGetOSTime() for MSVC. (#4635) * [Simulator] Increase resolution of CoGetOSTime() on Windows MSVC (possible fix for #4529, ref. #4342). * Return total time, not delta time. * Improve efficiency. --- radio/src/targets/simu/simpgmspace.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/radio/src/targets/simu/simpgmspace.cpp b/radio/src/targets/simu/simpgmspace.cpp index c737df300..1353043c6 100644 --- a/radio/src/targets/simu/simpgmspace.cpp +++ b/radio/src/targets/simu/simpgmspace.cpp @@ -96,10 +96,29 @@ uint16_t getTmr2MHz() #endif } +// return 2ms resolution to match CoOS settings U64 CoGetOSTime(void) { #if defined(_MSC_VER) - return GetTickCount()/2; + static double freqScale = 0.0; + static LARGE_INTEGER firstTick; + LARGE_INTEGER newTick; + + if (!freqScale) { + LARGE_INTEGER frequency; + // get ticks per second + QueryPerformanceFrequency(&frequency); + // 2ms resolution + freqScale = 500.0 / frequency.QuadPart; + // init timer + QueryPerformanceCounter(&firstTick); + TRACE_SIMPGMSPACE("CoGetOSTime() init: first tick = %llu @ %llu Hz", firstTick.QuadPart, frequency.QuadPart); + } + // read the timer + QueryPerformanceCounter(&newTick); + // compute the elapsed time + return U64((newTick.QuadPart - firstTick.QuadPart) * freqScale); + //return GetTickCount64() / 2; // only 10-16ms typical resolution #else auto now = std::chrono::steady_clock::now(); return (U64) std::chrono::duration_cast(now.time_since_epoch()).count() / 2;