summaryrefslogtreecommitdiff
path: root/firmware/src/peripheral/timer.c
diff options
context:
space:
mode:
authordmlunar <root@lunar.sh>2025-01-22 16:47:21 +0200
committerdmlunar <root@lunar.sh>2025-10-15 23:42:50 +0200
commit729f2a2c3ebfb2612d873caf453a1d7ca02180d9 (patch)
tree7bab2fcc0c7f50eab3013348697bc06ddd71d551 /firmware/src/peripheral/timer.c
downloadvarpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.tar.gz
varpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.zip
varpa: initial public commit
Diffstat (limited to 'firmware/src/peripheral/timer.c')
-rw-r--r--firmware/src/peripheral/timer.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/firmware/src/peripheral/timer.c b/firmware/src/peripheral/timer.c
new file mode 100644
index 0000000..1caff1c
--- /dev/null
+++ b/firmware/src/peripheral/timer.c
@@ -0,0 +1,57 @@
+/**
+ *
+ * Author: Dylan Muller
+ * Copyright (c) 2025
+ * All rights reserved.
+ *
+ * - Commercial/IP use prohibited.
+ * - Attribution required.
+ * See License.txt
+ *
+ */
+
+#include "setup.h"
+
+#include "peripheral/timer.h"
+
+#include <stdint.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/atomic.h>
+
+uint32_t millis_count = 0;
+
+void timer_init(void)
+{
+ uint32_t ctc_overflow;
+
+ ctc_overflow = ((F_CPU / 1000) / 8);
+ TCCR1B |= (1 << WGM12) | (1 << CS11);
+
+ OCR1AH = (ctc_overflow >> 8);
+ OCR1AL = ctc_overflow;
+
+ TIMSK1 |= (1 << OCIE1A);
+}
+
+void timer_reset(void)
+{
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ millis_count = 0;
+ }
+}
+
+uint32_t timer_millis(void)
+{
+ uint32_t millis;
+
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ millis = millis_count;
+ }
+ return millis;
+}
+
+ISR(TIMER1_COMPA_vect)
+{
+ millis_count++;
+}