From: Mikko Rasa Date: Tue, 12 Nov 2013 13:57:18 +0000 (+0200) Subject: Add a clock facility X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7efd4244ff0fc8a4d7aa43079e16d5311e01203b;p=model-railway-devices.git Add a clock facility --- 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