diff --git a/include/arm9/timer.h b/include/arm9/timer.h index 710538d..c19b476 100644 --- a/include/arm9/timer.h +++ b/include/arm9/timer.h @@ -5,17 +5,17 @@ -#define TIMER_BASE_FREQ (67027963.9305) +#define TIMER_BASE_FREQ (67027964.0) #define TIMER_COUNT_UP (1u<<2) // For cascading at least 2 timers #define TIMER_IRQ_ENABLE (1u<<6) #define TIMER_ENABLE (1u<<7) // Convenience macros for calculating the ticks. Based on libnds -#define TIMER_FREQ(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / n)) -#define TIMER_FREQ_64(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 64.0 / n)) -#define TIMER_FREQ_256(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 256.0 / n)) -#define TIMER_FREQ_1024(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 1024.0 / n)) +#define TIMER_FREQ(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / n) + 1) +#define TIMER_FREQ_64(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 64.0 / n) + 1) +#define TIMER_FREQ_256(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 256.0 / n) + 1) +#define TIMER_FREQ_1024(n) (0xFFFFu - (u16)(TIMER_BASE_FREQ / 1024.0 / n) + 1) typedef enum @@ -35,9 +35,34 @@ } TimerPrescaler; -void TIMER_init(void); -void TIMER_start(Timer timer, TimerPrescaler prescaler, u16 ticks, bool enableIrq); -void TIMER_stop(Timer timer); -void _timerSleep(u32 ticks); // Use the macro instead -#define TIMER_sleep(ms) _timerSleep(0xFFFFFFFFu - (u32)((TIMER_BASE_FREQ / 1024.0 / 1000.0) * ms)) +/** + * @brief Resets/initializes the timer hardware. Should not be called manually. + */ +void TIMER_init(void); + +/** + * @brief Starts a timer. + * + * @param[in] timer The timer to start. + * @param[in] prescaler The prescaler to use. + * @param[in] ticks The initial number of ticks. This is also the reload value on overflow. + * @param[in] enableIrq true if this timer should fire an interrupt on overflow. + */ +void TIMER_start(Timer timer, TimerPrescaler prescaler, u16 ticks, bool enableIrq); + +/** + * @brief Stops a timer. + * + * @param[in] timer The timer to stop. + */ +void TIMER_stop(Timer timer); + +/** + * @brief Halts the processor for the specified number of ticks. Use the macro below. + * + * @param[in] ticks The number of ticks. + */ +void _timerSleep(u32 ticks); + +#define TIMER_sleep(ms) _timerSleep(0xFFFFFFFFu - (u32)((TIMER_BASE_FREQ / 1024.0 / 1000.0) * ms) + 1) diff --git a/source/arm9/dev.c b/source/arm9/dev.c index 7dc6d62..75dcd2b 100644 --- a/source/arm9/dev.c +++ b/source/arm9/dev.c @@ -136,7 +136,7 @@ if(!sdmmc_sd_is_active()) { - TIMER_sleep(250.5f); // Wait until the SD bus times out + TIMER_sleep(250.4f); // Wait until the SD bus times out if(!sdmmc_sd_is_active()) return false; // Check again if a SD card is inserted }