summaryrefslogtreecommitdiff
path: root/include/arch/atmega2560/driver/counter.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/arch/atmega2560/driver/counter.h')
-rw-r--r--include/arch/atmega2560/driver/counter.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/include/arch/atmega2560/driver/counter.h b/include/arch/atmega2560/driver/counter.h
new file mode 100644
index 0000000..06ff73a
--- /dev/null
+++ b/include/arch/atmega2560/driver/counter.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#ifndef COUNTER_H
+#define COUNTER_H
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+typedef uint16_t counter_value_t;
+typedef uint8_t counter_overflow_t;
+
+class Counter {
+ private:
+ Counter(const Counter &copy);
+
+ public:
+ uint16_t value;
+ volatile uint8_t overflow;
+
+ Counter() : overflow(0) {}
+
+ inline void start() {
+ overflow = 0;
+ TCNT3 = 0;
+ TCCR3A = 0;
+ TCCR3B = _BV(CS10); // no prescaler
+ TIMSK3 = _BV(TOIE3);
+ }
+
+ inline void stop() {
+ TCCR3B = 0;
+ value = TCNT3;
+ }
+};
+
+extern Counter counter;
+
+#endif