From 9a8b1ef8b983822315544b20835c0956e62481f1 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Fri, 22 Jun 2018 21:50:14 +0200 Subject: [PATCH] The PIT timer is counting downward, so we need to subtract the count from the period value --- right/src/timer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/right/src/timer.c b/right/src/timer.c index afa0ae0..2a61051 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -37,7 +37,8 @@ uint32_t Timer_GetCurrentTimeMicros() { ms = CurrentTime; // Read the overflow counter EnableGlobalIRQ(primask); // Enable interrupts again if they where enabled before - this should make it interrupt safe - uint32_t us = COUNT_TO_USEC(count, timerClockFrequency); + // Calculate the counter value in microseconds - note that the PIT timer is counting downward, so we need to subtract the count from the period value + uint32_t us = 1000U * TIMER_INTERVAL_MSEC - COUNT_TO_USEC(count, timerClockFrequency); return ms * 1000U * TIMER_INTERVAL_MSEC + us; }