Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Diff: timer.c
- Revision:
- 33:b9e3c06e7dab
- Parent:
- 31:f6ff7fdb9c67
- Child:
- 34:aeb58975e61a
--- a/timer.c Thu Nov 29 18:43:27 2018 +0000
+++ b/timer.c Fri Nov 30 18:28:04 2018 +0000
@@ -2,6 +2,7 @@
#include <stdbool.h>
#include "timer.h"
+#include "tick.h"
#define TCR (*((volatile unsigned *) 0x40004004))
#define TC (*((volatile unsigned *) 0x40004008))
@@ -30,43 +31,27 @@
return count / TIMER_COUNT_PER_MS;
}
-static uint32_t secondsBaseCount = 0;
-
-uint32_t TimerCountSinceLastSecond()
+bool TimerIntervalHasElapsed(uint32_t* pBaseCount, uint32_t intervalCount)
{
- return TC - secondsBaseCount;
+ if (TC > *pBaseCount + intervalCount) //All unsigned wrap around arithmetic
+ {
+ *pBaseCount += intervalCount;
+ return true;
+ }
+ return false;
}
-int32_t TimerMultiplyFractionalPart(int32_t value, uint32_t timerCountSinceLastSecond)
+
+int32_t TimerMultiplyFractionalPart(int32_t value, uint32_t timerCountSinceStart, uint32_t interval)
{
int64_t fraction;
- fraction = timerCountSinceLastSecond;
+ fraction = timerCountSinceStart;
fraction <<= 32;
- fraction /= TIMER_COUNT_PER_SECOND;
+ fraction /= interval;
return (value * fraction) >> 32;
}
-bool TimerHadSecond = false;
-
-//Counts from zero to 2^32 and wraps around after:
-// 13.7 years if 10 per second - scan time must be less than 100mS
-// 1.37 years if 100 per second - scan time must be less than 10mS
-uint32_t TimerTicks = 0;
-
-void TimerMain()
-{
- TimerHadSecond = TimerCountSinceLastSecond() > TIMER_COUNT_PER_SECOND;
- if (TimerHadSecond) secondsBaseCount += TIMER_COUNT_PER_SECOND;
-
- static uint32_t tickBaseCount = 0;
-
- if (TC - tickBaseCount > TIMER_COUNT_PER_SECOND / TIMER_TICKS_PER_SECOND)
- {
- TimerTicks++;
- tickBaseCount += TIMER_COUNT_PER_SECOND / TIMER_TICKS_PER_SECOND;
- }
-}
void TimerInit()
{
TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.