diff --git a/include/arm9/timer.h b/include/arm9/timer.h index 3c3231f..85e7bcc 100644 --- a/include/arm9/timer.h +++ b/include/arm9/timer.h @@ -3,7 +3,7 @@ #include "types.h" -#define TIMER_BASE_FREQ (67027964.0) +#define TIMER_BASE_FREQ (67027963.95) #define TIMER_COUNT_UP (1u<<2) // For cascading at least 2 timers #define TIMER_IRQ_ENABLE (1u<<6) @@ -58,10 +58,8 @@ void TIMER_stop(Timer timer); /** - * @brief Halts the processor for the specified number of ticks. Use the macro below. + * @brief Halts the CPU for the specified number of milliseconds. * - * @param[in] ticks The number of ticks. + * @param[in] ms The number of milliseconds to sleep. */ -void _timerSleep(u32 ticks); - -#define TIMER_sleep(ms) _timerSleep(0xFFFFFFFFu - (u32)((TIMER_BASE_FREQ / 1024.0 / 1000.0) * ms) + 1) +void TIMER_sleep(u32 ms); diff --git a/source/arm9/timer.c b/source/arm9/timer.c index 4131464..58bc3af 100644 --- a/source/arm9/timer.c +++ b/source/arm9/timer.c @@ -21,6 +21,10 @@ #define REG_TIMER_CNT(n) *((vu16*)(TIMER_REGS_BASE + 0x02 + (n * 4))) +// For TIMER_sleep() +static u32 overflows; + + void TIMER_init(void) { @@ -58,19 +62,22 @@ static void timerSleepHandler(void) { - REG_TIMER3_CNT = REG_TIMER2_CNT = 0; - IRQ_unregisterHandler(IRQ_TIMER_3); + overflows--; + if(!overflows) + { + REG_TIMER3_CNT = 0; + IRQ_unregisterHandler(IRQ_TIMER_3); + } } -void _timerSleep(u32 ticks) +void TIMER_sleep(u32 ms) { - REG_TIMER2_VAL = (u16)ticks; - REG_TIMER3_VAL = (u16)(ticks>>16); + REG_TIMER3_VAL = TIMER_FREQ_1024(1000.0); + overflows = ms; IRQ_registerHandler(IRQ_TIMER_3, timerSleepHandler); - REG_TIMER3_CNT = TIMER_ENABLE | TIMER_IRQ_ENABLE | TIMER_COUNT_UP; - REG_TIMER2_CNT = TIMER_ENABLE | TIMER_PRESCALER_1024; + REG_TIMER3_CNT = TIMER_ENABLE | TIMER_IRQ_ENABLE | TIMER_PRESCALER_1024; while(REG_TIMER3_CNT & TIMER_ENABLE) {