summaryrefslogtreecommitdiff
path: root/arm9/src/timer3.c
diff options
context:
space:
mode:
authorwho-knows-who <j.williams97@outlook.com>2021-04-17 12:48:19 +0100
committerwho-knows-who <j.williams97@outlook.com>2021-04-17 12:48:19 +0100
commit996d9d78106cf4ab601815550ba77a92ab678328 (patch)
tree9a92b4f792a11466cdee719b18ed449dee2e27fc /arm9/src/timer3.c
parent267cb812e827604d4829d3afe28a82b0970d3706 (diff)
parent85a8a2bd43633e11af094d66a35f3c32dc7c8bfe (diff)
Merge branch 'master' into 0202A1E0
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;
+}