timers.h File Reference

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. More...

#include <nds/ndstypes.h>

Defines

#define BUS_CLOCK   (33513982)
 the speed in which the timer ticks in hertz.
#define TIMER0_CR   (*(vuint16*)0x04000102)
 Same as TIMER_CR(0).
#define TIMER0_DATA   (*(vuint16*)0x04000100)
 Same as TIMER_DATA(0).
#define TIMER1_CR   (*(vuint16*)0x04000106)
 Same as TIMER_CR(1).
#define TIMER1_DATA   (*(vuint16*)0x04000104)
 Same as TIMER_DATA(1).
#define TIMER2_CR   (*(vuint16*)0x0400010A)
 Same as TIMER_CR(2).
#define TIMER2_DATA   (*(vuint16*)0x04000108)
 Same as TIMER_DATA(2).
#define TIMER3_CR   (*(vuint16*)0x0400010E)
 Same as TIMER_CR(3).
#define TIMER3_DATA   (*(vuint16*)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)   (*(vuint16*)(0x04000102+((n)<<2)))
 Returns a dereferenced pointer to the data register for timer control Register.
#define TIMER_DATA(n)   (*(vuint16*)(0x04000100+((n)<<2)))
 Returns a dereferenced pointer to the data register for timer number "n".
#define TIMER_ENABLE   (1<<7)
 Enables the timer.
#define TIMER_FREQ(n)   (-BUS_CLOCK/(n))
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).
#define TIMER_FREQ_1024(n)   (-(BUS_CLOCK>>10)/(n))
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).
#define TIMER_FREQ_256(n)   (-(BUS_CLOCK>>8)/(n))
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).
#define TIMER_FREQ_64(n)   (-(BUS_CLOCK>>6)/(n))
 A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).
#define TIMER_IRQ_REQ   (1<<6)
 Causes the timer to request an Interupt 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 ()
 ends cpu Timing.
u32 cpuGetTiming ()
 returns the number of ticks which have elapsed since cpuStartTiming.
void cpuStartTiming (int timer)
 begins cpu Timing using two timers for 32bit resolution.
u16 timerElapsed (int timer)
 returns the ticks elapsed since the last call to timerElapsed().
u16 timerPause (int timer)
 pauses the specified timer.
void timerStart (int timer, ClockDivider divider, u16 ticks, VoidFn callback)
 start a hardware timer. Callback is tied directly to interupt table and called directly resulting in less latency than the attached timer.
u16 timerStop (int timer)
 Stops the specified timer.
static u16 timerTick (int timer)
 returns the raw ticks of the specified timer.
static void timerUnpause (int timer)
 unpauses the specified timer.

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.

Define Documentation

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

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

Example Usage: TIMER_CR(x) = TIMER_ENABLE | ClockDivider_64;

Possible bit defines:

See also:
TIMER_ENABLE
TIMER_IRQ_REQ
TIMER_CASCADE
ClockDivider
Examples:
Graphics/3D/BoxTest/source/main.cpp.
#define TIMER_DATA (  )     (*(vuint16*)(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. Everytime 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));

Example Usage: TIMER_DATA(0) = value; were 0 can be 0 through 3 and value is 16 bits.

Examples:
Graphics/3D/BoxTest/source/main.cpp.
#define TIMER_FREQ (  )     (-BUS_CLOCK/(n))

A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).

Example Usage:

calls the timerCallBack function 5 times per second.
		timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(5), timerCallBack);
	

Max frequency is: 33554432Hz Min frequency is: 512Hz

Note:
Use the appropriate macro depending on the used clock divider.
#define TIMER_FREQ_1024 (  )     (-(BUS_CLOCK>>10)/(n))

A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).

Example Usage:

calls the timerCallBack function 5 times per second.
		timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(5), timerCallBack);
	

Max frequency is: 32768Hz Min frequency is: 0.5Hz

Note:
Use the appropriate macro depending on the used clock divider.
Examples:
time/timercallback/source/main.c.
#define TIMER_FREQ_256 (  )     (-(BUS_CLOCK>>8)/(n))

A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).

Example Usage:

calls the timerCallBack function 5 times per second.
		timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(5), timerCallBack);
	

Max frequency is: 131072Hz Min frequency is: 2Hz

Note:
Use the appropriate macro depending on the used clock divider.
#define TIMER_FREQ_64 (  )     (-(BUS_CLOCK>>6)/(n))

A macro that calculates TIMER_DATA(n) settings for a given frequency of n. will calculate the correct value for TIMER_DATA(n) given the frequency in hertz (number of times the timer should overflow per second).

Example Usage:

calls the timerCallBack function 5 times per second.
		timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(5), timerCallBack);
	

Max frequency is: 524288Hz Min frequency is: 8Hz

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

Enumeration Type Documentation

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

u32 cpuEndTiming (  ) 

ends cpu Timing.

Returns:
The number of ticks which have elapsed since cpuStartTiming.
u32 cpuGetTiming (  ) 

returns the number of ticks which have elapsed since cpuStartTiming.

Returns:
The number of ticks which have elapsed since cpuStartTiming.
void cpuStartTiming ( int  timer  ) 

begins cpu Timing using two timers for 32bit resolution.

Parameters:
timer The base hardware timer to use (0 - 2).
u16 timerElapsed ( int  timer  ) 

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

Parameters:
timer The hardware timer to use (0 - 3).
Returns:
The number of ticks which have elapsed since the last callto timerElapsed().
Examples:
time/stopwatch/source/main.c.
u16 timerPause ( int  timer  ) 

pauses the specified timer.

Parameters:
timer The hardware timer to use (0 - 3).
Returns:
The number of ticks which have elapsed since the last callto timerElapsed().
Examples:
time/stopwatch/source/main.c.
void timerStart ( int  timer,
ClockDivider  divider,
u16  ticks,
VoidFn  callback 
)

start a hardware timer. Callback is tied directly to interupt table and called directly resulting in less latency than the attached timer.

Parameters:
timer The hardware timer to use (0 - 3).
divider The timer channel clock divider (clock will tick at 33.513982 Mhz / divider)
ticks The number of ticks which must elapse before the timer overflows
callback The callback to be called when the timer expires (if null no irq will be generated by the timer)
Examples:
time/stopwatch/source/main.c, and time/timercallback/source/main.c.
u16 timerStop ( int  timer  ) 

Stops the specified timer.

Parameters:
timer The hardware timer to use (0 - 3).
Returns:
The number of ticks which have elapsed since the last callto timerElapsed().
Examples:
time/stopwatch/source/main.c.
static u16 timerTick ( int  timer  )  [inline, static]

returns the raw ticks of the specified timer.

Parameters:
timer The hardware timer to use (0 - 3).
Returns:
the raw ticks of the specified timer data register.
static void timerUnpause ( int  timer  )  [inline, static]

unpauses the specified timer.

Parameters:
timer The hardware timer to use (0 - 3).
Examples:
time/stopwatch/source/main.c.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Generated on Sat Oct 2 12:55:13 2010 for libnds by  doxygen 1.6.3