From 6edd1b9de2c7705557490f86fda4d6f99d1c76b6 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 20 Feb 2024 06:51:29 +0100 Subject: [PATCH] added a ticks delta function, handles overflow situations much better --- common_arm/ticks.c | 9 +++++++++ common_arm/ticks.h | 1 + 2 files changed, 10 insertions(+) diff --git a/common_arm/ticks.c b/common_arm/ticks.c index 297e507a4..019c3b10c 100644 --- a/common_arm/ticks.c +++ b/common_arm/ticks.c @@ -309,6 +309,14 @@ uint32_t GetTicks(void) { return (hi << 16) | lo; } +uint32_t RAMFUNC GetTicksDelta(uint32_t start) { + uint32_t stop = GetTicks(); + if (stop >= start) { + return stop - start; + } + return (UINT32_MAX - start) + stop; +} + // Wait - Spindelay in ticks. // if called with a high number, this will trigger the WDT... void WaitTicks(uint32_t ticks) { @@ -328,3 +336,4 @@ void StopTicks(void) { AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; } + diff --git a/common_arm/ticks.h b/common_arm/ticks.h index 4a2276268..67d8dceed 100644 --- a/common_arm/ticks.h +++ b/common_arm/ticks.h @@ -28,6 +28,7 @@ void StartTicks(void); uint32_t GetTicks(void); +uint32_t RAMFUNC GetTicksDelta(uint32_t start); void WaitUS(uint32_t us); void WaitTicks(uint32_t ticks); void StartCountUS(void);