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
timer.c
- Committer:
- andrewboyson
- Date:
- 2018-11-29
- Revision:
- 31:f6ff7fdb9c67
- Parent:
- 30:212ca42b8779
- Child:
- 33:b9e3c06e7dab
File content as of revision 31:f6ff7fdb9c67:
#include <stdint.h>
#include <stdbool.h>
#include "timer.h"
#define TCR (*((volatile unsigned *) 0x40004004))
#define TC (*((volatile unsigned *) 0x40004008))
#define PR (*((volatile unsigned *) 0x4000400C))
#define MCR (*((volatile unsigned *) 0x40004014))
#define CTCR (*((volatile unsigned *) 0x40004070))
uint32_t TimerNowCount()
{
return TC;
}
uint32_t TimerIntervalCount(uint32_t* pLastCount)
{
uint32_t thisCount = TC;
uint32_t period = thisCount - *pLastCount;
*pLastCount = thisCount;
return period;
}
uint32_t TimerSinceCount(uint32_t startCount)
{
return TC - startCount;
}
uint32_t TimerSinceMs(uint32_t startCount)
{
uint32_t count = TC - startCount;
return count / TIMER_COUNT_PER_MS;
}
static uint32_t secondsBaseCount = 0;
uint32_t TimerCountSinceLastSecond()
{
return TC - secondsBaseCount;
}
int32_t TimerMultiplyFractionalPart(int32_t value, uint32_t timerCountSinceLastSecond)
{
int64_t fraction;
fraction = timerCountSinceLastSecond;
fraction <<= 32;
fraction /= TIMER_COUNT_PER_SECOND;
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.
CTCR = 0; // 21.6.3 Count Control Register - Timer mode
PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1).
MCR = 0; // 21.6.8 Match Control Register - no interrupt or reset
TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
}