]> git.tdb.fi Git - model-railway-devices.git/blobdiff - common/clock.c
Add a clock facility
[model-railway-devices.git] / common / clock.c
diff --git a/common/clock.c b/common/clock.c
new file mode 100644 (file)
index 0000000..1e43e87
--- /dev/null
@@ -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)