summaryrefslogtreecommitdiff
path: root/include/arch/atmega2560/driver/timer.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/arch/atmega2560/driver/timer.h')
-rw-r--r--include/arch/atmega2560/driver/timer.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/include/arch/atmega2560/driver/timer.h b/include/arch/atmega2560/driver/timer.h
new file mode 100644
index 0000000..73a8125
--- /dev/null
+++ b/include/arch/atmega2560/driver/timer.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#define ON_TIMER_INTERRUPT_head ISR(TIMER0_COMPA_vect) {
+#define ON_TIMER_INTERRUPT_tail }
+
+class Timer {
+ private:
+ Timer(const Timer &copy);
+ unsigned char prescaler;
+
+ public:
+ Timer() {}
+
+ inline void setup_khz(uint16_t const frequency) {
+ OCR0A = frequency ? 255 / frequency : 1;
+ TCCR0A = _BV(WGM01);
+ prescaler = _BV(CS01) | _BV(CS00);
+ }
+ inline void setup_hz(uint16_t const frequency) {
+ OCR0A = frequency ? 255 / frequency : 1;
+ TCCR0A = _BV(WGM01);
+ prescaler = _BV(CS02) | _BV(CS00);
+ }
+ inline void start(unsigned char const interrupt) {
+ TCNT0 = 0;
+ TCCR0B = prescaler;
+ if (interrupt) {
+ TIMSK0 = _BV(OCIE0A);
+ }
+ }
+ inline void stop() { TCCR0B = 0; TIMSK0 = 0; }
+};
+
+extern Timer timer;