summaryrefslogtreecommitdiff
path: root/avr/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/timer.c')
-rw-r--r--avr/timer.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/avr/timer.c b/avr/timer.c
new file mode 100644
index 0000000..1b15985
--- /dev/null
+++ b/avr/timer.c
@@ -0,0 +1,61 @@
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include "timer.h"
+#include <avr/interrupt.h>
+#include <util/atomic.h>
+#include "time.h"
+
+/* timer interrupt/overflow counter */
+/* counts up every ms. */
+static volatile
+uint32_t timestamp;
+
+/*
+ * 1000Hz timer interrupt generated by OC4A
+ */
+ISR(TIMER4_COMPA_vect)
+{
+ static int_fast8_t tick_10ms;
+ static int_fast8_t tick_1s;
+ int_fast8_t i, j;
+
+ extern void disk_timerproc(void);
+
+ OCR4A += F_CPU / 1000; /* 1000Hz interval */
+
+ timestamp++;
+
+ i = tick_10ms + 1;
+ if (i == 10) {
+ Stat |= S_10MS_TO;
+
+ /* Drive timer procedure of low level disk I/O module */
+ disk_timerproc();
+
+ j = tick_1s - 1;
+ if (j == 0) {
+ system_tick();
+ j = 100;
+ }
+ tick_1s = j;
+ i = 0;
+ }
+ tick_10ms = i;
+}
+
+
+/*--------------------------------------------------------------------------*/
+
+uint32_t get_timer(uint32_t base)
+{
+ uint32_t ret;
+ ATOMIC_BLOCK(ATOMIC_FORCEON)
+ {
+ ret = timestamp;
+ }
+ return ret - base;
+}