]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Add a clock facility
authorMikko Rasa <tdb@tdb.fi>
Tue, 12 Nov 2013 13:57:18 +0000 (15:57 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 12 Nov 2013 13:57:18 +0000 (15:57 +0200)
common/clock.c [new file with mode: 0644]
common/clock.h [new file with mode: 0644]

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)
diff --git a/common/clock.h b/common/clock.h
new file mode 100644 (file)
index 0000000..134cdfe
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef CLOCK_H_
+#define CLOCK_H_
+
+#include <stdint.h>
+
+#ifndef CLOCK_RATE
+#define CLOCK_RATE 1000
+#endif
+
+void clock_start();
+void clock_stop();
+uint32_t clock_get();
+
+#endif