aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x/timer.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-05-07 21:32:27 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-05-25 11:52:13 -0400
commit970831ee0d3b91897196e92270d98b2a3067427f (patch)
tree441c2b329ea669ff5e996a617d7b6bc2c315fa0d /src/lpc176x/timer.c
parent5ae22a5e51872fd3e6130a3acce68f598bee9ff0 (diff)
downloadkutter-970831ee0d3b91897196e92270d98b2a3067427f.tar.gz
kutter-970831ee0d3b91897196e92270d98b2a3067427f.tar.xz
kutter-970831ee0d3b91897196e92270d98b2a3067427f.zip
lpc176x: Add initial support for LPC176x processors
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/lpc176x/timer.c')
-rw-r--r--src/lpc176x/timer.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/lpc176x/timer.c b/src/lpc176x/timer.c
new file mode 100644
index 00000000..d6d6f9ed
--- /dev/null
+++ b/src/lpc176x/timer.c
@@ -0,0 +1,59 @@
+// lpc176x timer interrupt scheduling
+//
+// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "LPC17xx.h" // LPC_TIM0
+#include "board/irq.h" // irq_disable
+#include "board/misc.h" // timer_read_time
+#include "board/timer_irq.h" // timer_dispatch_many
+#include "sched.h" // DECL_INIT
+
+// Set the next irq time
+static void
+timer_set(uint32_t value)
+{
+ LPC_TIM0->MR0 = value;
+ LPC_TIM0->IR = 0x01;
+}
+
+// Return the current time (in absolute clock ticks).
+uint32_t
+timer_read_time(void)
+{
+ return LPC_TIM0->TC;
+}
+
+// Activate timer dispatch as soon as possible
+void
+timer_kick(void)
+{
+ timer_set(timer_read_time() + 50);
+}
+
+void
+timer_init(void)
+{
+ // Disable timer
+ LPC_TIM0->TCR = 0x02;
+ // Enable interrupts
+ NVIC_SetPriority(TIMER0_IRQn, 1);
+ NVIC_EnableIRQ(TIMER0_IRQn);
+ LPC_TIM0->MCR = 0x01;
+ // Clear counter value
+ LPC_TIM0->TC = 0;
+ timer_kick();
+ // Start timer
+ LPC_TIM0->TCR = 0x01;
+}
+DECL_INIT(timer_init);
+
+void __visible __aligned(16) // aligning helps stabilize perf benchmarks
+TIMER0_IRQHandler(void)
+{
+ irq_disable();
+ uint32_t next = timer_dispatch_many();
+ timer_set(next);
+ irq_enable();
+}