From 7efd4244ff0fc8a4d7aa43079e16d5311e01203b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 12 Nov 2013 15:57:18 +0200 Subject: [PATCH] Add a clock facility --- common/clock.c | 34 ++++++++++++++++++++++++++++++++++ common/clock.h | 14 ++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 common/clock.c create mode 100644 common/clock.h diff --git a/common/clock.c b/common/clock.c new file mode 100644 index 0000000..1e43e87 --- /dev/null +++ b/common/clock.c @@ -0,0 +1,34 @@ +#include "clock.h" +#include "timer.h" + +#ifndef CLOCK_TIMER +#error CLOCK_TIMER must be defined +#endif + +static volatile uint32_t time = 0; + +void clock_start() +{ + timer_start_hz(CLOCK_TIMER, CLOCK_RATE, 1); +} + +void clock_stop() +{ + timer_stop(CLOCK_TIMER); +} + +uint32_t clock_get() +{ + cli(); + uint32_t t = time; + sei(); + return t; +} + +void tick() +{ + ++time; +} + +#define SET_CLOCK_CALLBACK(n) TIMER_SET_CALLBACK(n, tick) +SET_CLOCK_CALLBACK(CLOCK_TIMER) diff --git a/common/clock.h b/common/clock.h new file mode 100644 index 0000000..134cdfe --- /dev/null +++ b/common/clock.h @@ -0,0 +1,14 @@ +#ifndef CLOCK_H_ +#define CLOCK_H_ + +#include + +#ifndef CLOCK_RATE +#define CLOCK_RATE 1000 +#endif + +void clock_start(); +void clock_stop(); +uint32_t clock_get(); + +#endif -- 2.45.2