libnds
Macros | Enumerations | Functions
timers.h File Reference

Contains defines, macros and functions for ARM7 and ARM9 timer operation. More...

#include <nds/ndstypes.h>

Macros

#define BUS_CLOCK   (33513982)
 The speed in which the timer ticks in Hz.
 
#define TIMER0_CR   (*(vu16 *)0x04000102)
 Same as TIMER_CR(0).
 
#define TIMER0_DATA   (*(vu16 *)0x04000100)
 Same as TIMER_DATA(0).
 
#define TIMER1_CR   (*(vu16 *)0x04000106)
 Same as TIMER_CR(1).
 
#define TIMER1_DATA   (*(vu16 *)0x04000104)
 Same as TIMER_DATA(1).
 
#define TIMER2_CR   (*(vu16 *)0x0400010A)
 Same as TIMER_CR(2).
 
#define TIMER2_DATA   (*(vu16 *)0x04000108)
 Same as TIMER_DATA(2).
 
#define TIMER3_CR   (*(vu16 *)0x0400010E)
 Same as TIMER_CR(3).
 
#define TIMER3_DATA   (*(vu16 *)0x0400010C)
 Same as TIMER_DATA(3).
 
#define TIMER_CASCADE   (1 << 2)
 When set will cause the timer to count when the timer below overflows (unavailable for timer 0).
 
#define TIMER_CR(n)   (*(vu16 *)(0x04000102 + ((n) << 2)))
 Returns a dereferenced pointer to the data register for timer control register. More...
 
#define TIMER_DATA(n)   (*(vu16 *)(0x04000100 + ((n) << 2)))
 Returns a dereferenced pointer to the data register for timer number "n". More...
 
#define TIMER_DIV_1   (0)
 Causes the timer to count at 33.514 MHz.
 
#define TIMER_DIV_1024   (3)
 Causes the timer to count at (33.514 / 1024) MHz.
 
#define TIMER_DIV_256   (2)
 Causes the timer to count at (33.514 / 256) MHz.
 
#define TIMER_DIV_64   (1)
 Causes the timer to count at (33.514 / 64) MHz.
 
#define TIMER_ENABLE   (1 << 7)
 Enables the timer.
 
#define TIMER_FREQ(n)   TIMER_FREQ_SHIFT(n, 1, 0)
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. More...
 
#define TIMER_FREQ_1024(n)   TIMER_FREQ_SHIFT(n, 1, 10)
 Macro that calculates TIMER_DATA(n) settings for a given frequency of n. More...
 
#define TIMER_FREQ_256(n)   TIMER_FREQ_SHIFT(n, 1, 8)
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. More...
 
#define TIMER_FREQ_64(n)   TIMER_FREQ_SHIFT(n, 1, 6)
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. More...
 
#define TIMER_FREQ_SHIFT(n, divisor, shift)   ((-((BUS_CLOCK >> (shift)) * (divisor)) - ((((n) + 1)) >> 1)) / (n))
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n and shift. Correctly rounds the timer to the closest available frequency. More...
 
#define TIMER_IRQ_REQ   (1 << 6)
 Causes the timer to request an Interrupt on overflow.
 

Enumerations

enum  ClockDivider {
  ClockDivider_1 = 0 ,
  ClockDivider_64 = 1 ,
  ClockDivider_256 = 2 ,
  ClockDivider_1024 = 3
}
 Allowable timer clock dividers. More...
 

Functions

u32 cpuEndTiming (void)
 Ends CPU timing. More...
 
u32 cpuGetTiming (void)
 Returns the number of ticks which have elapsed since cpuStartTiming. More...
 
void cpuStartTiming (int timer)
 Begins CPU timing using two timers for 32bit resolution. More...
 
u16 timerElapsed (int timer)
 Returns the ticks elapsed since the last call to timerElapsed(). More...
 
u16 timerPause (int timer)
 Pauses the specified timer. More...
 
void timerStart (int timer, ClockDivider divider, u16 ticks, VoidFn callback)
 Start a hardware timer. More...
 
u16 timerStop (int timer)
 Stops the specified timer. More...
 
static u16 timerTick (int timer)
 Returns the raw ticks of the specified timer. More...
 
static void timerUnpause (int timer)
 Unpauses the specified timer. More...
 

Detailed Description

Contains defines, macros and functions for ARM7 and ARM9 timer operation.

It also contains a simplified API for timer use and some cpu timing functions.

The timers are fed with a 33.513982 MHz source on the ARM9 and ARM7.

Note
that DSWifi will use timer 3 on the ARM9, so don't use that if you use DSWifi.

Macro Definition Documentation

◆ TIMER_CR

#define TIMER_CR (   n)    (*(vu16 *)(0x04000102 + ((n) << 2)))

Returns a dereferenced pointer to the data register for timer control register.

Example Usage:

@ ClockDivider_64
Divides the timer clock by 64 (~523.657 kHz)
Definition: timers.h:96
#define TIMER_CR(n)
Returns a dereferenced pointer to the data register for timer control register.
Definition: timers.h:42
#define TIMER_ENABLE
Enables the timer.
Definition: timers.h:85

Possible bit defines:

See also
TIMER_ENABLE
TIMER_IRQ_REQ
TIMER_CASCADE
ClockDivider

◆ TIMER_DATA

#define TIMER_DATA (   n)    (*(vu16 *)(0x04000100 + ((n) << 2)))

Returns a dereferenced pointer to the data register for timer number "n".

See also
TIMER_CR(n)
TIMER_FREQ(n)

TIMER_DATA(n) when set will latch that value into the counter. Every time the counter rolls over, TIMER_DATA(0) will return to the latched value. This allows you to control the frequency of the timer using the following formula:

TIMER_DATA(x) = -(BUS_CLOCK / (freq * divider));
#define BUS_CLOCK
The speed in which the timer ticks in Hz.
Definition: timers.h:82
#define TIMER_DATA(n)
Returns a dereferenced pointer to the data register for timer number "n".
Definition: timers.h:70

Example Usage:

TIMER_DATA(0) = value; // 0 to 3. value is 16 bits

◆ TIMER_FREQ

#define TIMER_FREQ (   n)    TIMER_FREQ_SHIFT(n, 1, 0)

A macro that calculates TIMER_DATA(n) settings for a given frequency of n.

It will calculate the correct value for TIMER_DATA(n) given the frequency in Hz (number of times the timer should overflow per second).

Example Usage:

// Calls the timerCallBack function 5000 times per second.
timerStart(0, ClockDivider_1, TIMER_FREQ(5000), timerCallBack);
void timerStart(int timer, ClockDivider divider, u16 ticks, VoidFn callback)
Start a hardware timer.
@ ClockDivider_1
Divides the timer clock by 1 (~33513.982 kHz)
Definition: timers.h:95
#define TIMER_FREQ(n)
A macro that calculates TIMER_DATA(n) settings for a given frequency of n.
Definition: timers.h:138

Max frequency is: 33513982 Hz Min frequency is: ~511 Hz

Note
Use the appropriate macro depending on the used clock divider.

◆ TIMER_FREQ_1024

#define TIMER_FREQ_1024 (   n)    TIMER_FREQ_SHIFT(n, 1, 10)

Macro that calculates TIMER_DATA(n) settings for a given frequency of n.

It wtill calculate the correct value for TIMER_DATA(n) given the frequency in Hz (number of times the timer should overflow per second).

Example Usage:

// Calls the timerCallBack function 5 times per second.
@ ClockDivider_1024
divides the timer clock by 1024 (~32.7284 kHz)
Definition: timers.h:98
#define TIMER_FREQ_1024(n)
Macro that calculates TIMER_DATA(n) settings for a given frequency of n.
Definition: timers.h:189

Max frequency is: ~32728 Hz Min frequency is: ~0.5 Hz

Note
Use the appropriate macro depending on the used clock divider.

◆ TIMER_FREQ_256

#define TIMER_FREQ_256 (   n)    TIMER_FREQ_SHIFT(n, 1, 8)

A macro that calculates TIMER_DATA(n) settings for a given frequency of n.

It will calculate the correct value for TIMER_DATA(n) given the frequency in Hz (number of times the timer should overflow per second).

Example Usage:

// Calls the timerCallBack function 50 times per second.
timerStart(0, ClockDivider_256, TIMER_FREQ_256(50), timerCallBack);
@ ClockDivider_256
Divides the timer clock by 256 (~130.914 kHz)
Definition: timers.h:97
#define TIMER_FREQ_256(n)
A macro that calculates TIMER_DATA(n) settings for a given frequency of n.
Definition: timers.h:172

Max frequency is: ~130914 Hz Min frequency is: ~2 Hz

Note
Use the appropriate macro depending on the used clock divider.

◆ TIMER_FREQ_64

#define TIMER_FREQ_64 (   n)    TIMER_FREQ_SHIFT(n, 1, 6)

A macro that calculates TIMER_DATA(n) settings for a given frequency of n.

It will calculate the correct value for TIMER_DATA(n) given the frequency in Hz (number of times the timer should overflow per second).

Example Usage:

// Calls the timerCallBack function 500 times per second.
timerStart(0, ClockDivider_64, TIMER_FREQ_64(500), timerCallBack);
#define TIMER_FREQ_64(n)
A macro that calculates TIMER_DATA(n) settings for a given frequency of n.
Definition: timers.h:155

Max frequency is: ~523656 Hz Min frequency is: 8 Hz

Note
Use the appropriate macro depending on the used clock divider.

◆ TIMER_FREQ_SHIFT

#define TIMER_FREQ_SHIFT (   n,
  divisor,
  shift 
)    ((-((BUS_CLOCK >> (shift)) * (divisor)) - ((((n) + 1)) >> 1)) / (n))

A macro that calculates TIMER_DATA(n) settings for a given frequency of n and shift. Correctly rounds the timer to the closest available frequency.

Shift values:

  • ClockDivider_1 = 0
  • ClockDivider_64 = 6
  • ClockDivider_256 = 8
  • ClockDivider_1024 = 10
  • ARM7 sound timer = 1

Enumeration Type Documentation

◆ ClockDivider

Allowable timer clock dividers.

Enumerator
ClockDivider_1 

Divides the timer clock by 1 (~33513.982 kHz)

ClockDivider_64 

Divides the timer clock by 64 (~523.657 kHz)

ClockDivider_256 

Divides the timer clock by 256 (~130.914 kHz)

ClockDivider_1024 

divides the timer clock by 1024 (~32.7284 kHz)

Function Documentation

◆ cpuEndTiming()

u32 cpuEndTiming ( void  )

Ends CPU timing.

Returns
The number of ticks which have elapsed since cpuStartTiming.

◆ cpuGetTiming()

u32 cpuGetTiming ( void  )

Returns the number of ticks which have elapsed since cpuStartTiming.

Returns
The number of ticks which have elapsed since cpuStartTiming.

◆ cpuStartTiming()

void cpuStartTiming ( int  timer)

Begins CPU timing using two timers for 32bit resolution.

Parameters
timerThe base hardware timer to use (0 - 2).

◆ timerElapsed()

u16 timerElapsed ( int  timer)

Returns the ticks elapsed since the last call to timerElapsed().

Parameters
timerThe hardware timer to use (0 - 3).
Returns
The number of ticks which have elapsed since the last call to timerElapsed().

◆ timerPause()

u16 timerPause ( int  timer)

Pauses the specified timer.

Parameters
timerThe hardware timer to use (0 - 3).
Returns
The number of ticks which have elapsed since the last call to timerElapsed().

◆ timerStart()

void timerStart ( int  timer,
ClockDivider  divider,
u16  ticks,
VoidFn  callback 
)

Start a hardware timer.

Callback is tied directly to interrupt table and called directly, resulting in less latency than the attached timer.

Parameters
timerThe hardware timer to use (0 - 3).
dividerThe timer channel clock divider (clock will tick at 33.513982 MHz / divider).
ticksThe number of ticks which must elapse before the timer overflows.
callbackThe callback to be called when the timer expires (if NULL, no IRQ will be generated by the timer).

◆ timerStop()

u16 timerStop ( int  timer)

Stops the specified timer.

Parameters
timerThe hardware timer to use (0 - 3).
Returns
The number of ticks which have elapsed since the last call to timerElapsed().

◆ timerTick()

static u16 timerTick ( int  timer)
inlinestatic

Returns the raw ticks of the specified timer.

Parameters
timerThe hardware timer to use (0 - 3).
Returns
the raw ticks of the specified timer data register.

◆ timerUnpause()

static void timerUnpause ( int  timer)
inlinestatic

Unpauses the specified timer.

Parameters
timerThe hardware timer to use (0 - 3).