summaryrefslogtreecommitdiff
path: root/arm9/src/timer3.c
diff options
context:
space:
mode:
authorAkira Akashi <rubenru09@aol.com>2021-03-21 15:52:06 +0000
committerGitHub <noreply@github.com>2021-03-21 15:52:06 +0000
commitcd9eb8c0276e74498b2ad0db2367bd6f76549f19 (patch)
treef0f81eeb1e8ad3d2a0400b72a0215c29829c2e12 /arm9/src/timer3.c
parent1cd32c047cbcdb8169e1d013922c846c374d500a (diff)
parent00ecc6db377847f7199cb3f1c443497ef3581bfe (diff)
Merge branch 'master' into asmproc-multiple
Diffstat (limited to 'arm9/src/timer3.c')
-rw-r--r--arm9/src/timer3.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/arm9/src/timer3.c b/arm9/src/timer3.c
new file mode 100644
index 00000000..1799fd24
--- /dev/null
+++ b/arm9/src/timer3.c
@@ -0,0 +1,66 @@
+#include "timer3.h"
+
+struct Timer3Data timer3_data;
+
+
+THUMB_FUNC void Init_Timer3()
+{
+ timer3_data.Timer3Counter = 0;
+ timer3_data.NeedReset = FALSE;
+
+ reg_OS_TM3CNT_H = 0;
+ reg_OS_TM3CNT_L = 0;
+ reg_OS_TM3CNT_H = 0xc1; // start timer3 with f/64 and irq enable
+
+ OS_SetIrqFunction(0x40, &CountUpTimer3);
+ OS_EnableIrqMask(0x40); // irq on timer3 overflow
+}
+
+
+THUMB_FUNC void CountUpTimer3()
+{
+ timer3_data.Timer3Counter++;
+
+ if (timer3_data.NeedReset)
+ {
+ reg_OS_TM3CNT_H = 0;
+ reg_OS_TM3CNT_L = 0;
+ reg_OS_TM3CNT_H = 0xc1;
+ timer3_data.NeedReset = FALSE;
+ }
+
+ *(vu32 *)HW_INTR_CHECK_BUF |= 0x40;
+
+ OS_SetIrqFunction(0x40, &CountUpTimer3);
+}
+
+THUMB_FUNC u64 internal_GetTimer3Count()
+{
+ OSIntrMode intr_mode = OS_DisableInterrupts();
+
+ vu16 timer3 = reg_OS_TM3CNT_L;
+ vu64 timer3_counter = timer3_data.Timer3Counter & 0x0000ffffffffffff;
+
+ if (reg_OS_IF & 0x40 && !(timer3 & 0x8000))
+ {
+ timer3_counter++;
+ }
+
+ OS_RestoreInterrupts(intr_mode);
+ return (timer3_counter << 16) | timer3;
+}
+
+THUMB_FUNC u64 GetTimer3Count()
+{
+ return internal_GetTimer3Count();
+}
+
+THUMB_FUNC u64 Timer3CountToMilliSeconds(u64 count)
+{
+ return (count *64) / 33514;
+}
+
+THUMB_FUNC u64 Timer3CountToSeconds(u64 count)
+{
+ return (count *64) / HW_SYSTEM_CLOCK;
+}