libdragon
Data Structures | Files | Defines | Typedefs | Functions | Variables
Timer Subsystem
libdragon

Interface to the timer module in the MIPS r4300 processor. More...

Data Structures

struct  timer_link
 Timer structure. More...

Files

file  timer.c
 

Timer Subsystem.


file  timer.h
 

Timer Subsystem.


Defines

#define read_count(x)   asm volatile("mfc0 %0,$9\n\t nop \n\t" : "=r" (x) : )
 Read the count out of the count register.
#define write_count(x)   asm volatile("mtc0 %0,$9\n\t nop \n\t" : : "r" (x) )
 Write the count to the count register.
#define write_compare(x)   asm volatile("mtc0 %0,$11\n\t nop \n\t" : : "r" (x) )
 Set the compare register.
#define TF_ONE_SHOT   0
 Timer should fire only once.
#define TF_CONTINUOUS   1
 Timer should fire at a regular interval.
#define TIMER_TICKS(us)   ((int)((long long)(us) * 46875LL / 1000LL))
 Calculate timer ticks based on microseconds.
#define TIMER_MICROS(tk)   ((int)((long long)(tk) * 1000LL / 46875LL))
 Calculate microseconds based on timer ticks.
#define TIMER_TICKS_LL(us)   ((long long)(us) * 46875LL / 1000LL)
 Calculate timer ticks based on microseconds.
#define TIMER_MICROS_LL(tk)   ((long long)(tk) * 1000LL / 46875LL)
 Calculate microseconds based on timer ticks.

Typedefs

typedef struct timer_link timer_link_t
 Timer structure.

Functions

static int __proc_timers (timer_link_t *head)
 Process linked list of timers.
static void timer_callback (void)
 Timer callback function.
void timer_init (void)
 Initialize the timer subsystem.
timer_link_tnew_timer (int ticks, int flags, void(*callback)(int ovfl))
 Create a new timer and add to list.
void start_timer (timer_link_t *timer, int ticks, int flags, void(*callback)(int ovfl))
 Start a timer not currently in the list.
void stop_timer (timer_link_t *timer)
 Stop a timer and remove it from the list.
void delete_timer (timer_link_t *timer)
 Remove a timer from the list and delete it.
void timer_close (void)
 Free and close the timer subsystem.
long long timer_ticks (void)
 Return total ticks since timer was initialized.

Variables

static timer_link_tTI_timers = 0
 Internal linked list of timers.
static long long total_ticks
 Total ticks elapsed since timer subsystem initialization.

Detailed Description

Interface to the timer module in the MIPS r4300 processor.

The timer subsystem allows code to receive a callback after a specified number of ticks or microseconds. It interfaces with the MIPS coprocessor 0 to handle the timer interrupt and provide useful timing services.

Before attempting to use the timer subsystem, code should call timer_init. After the timer subsystem has been initialized, a new one-shot or continuous timer can be created with new_timer. To remove an expired one-shot timer or a recurring timer, use delete_timer. To temporarily stop a timer, use stop_timer. To restart a stopped timer or an expired one-shot timer, use start_timer. Once code no longer needs the timer subsystem, a call to timer_close will free all continuous timers and shut down the timer subsystem. Note that timers removed with stop_timer or expired one-short timers will not be removed automatically and are the responsibility of the calling code to be freed, regardless of a call to timer_close.


Define Documentation

#define read_count (   x)    asm volatile("mfc0 %0,$9\n\t nop \n\t" : "=r" (x) : )

Read the count out of the count register.

Parameters:
[out]xVariable to place count into
#define TIMER_MICROS (   tk)    ((int)((long long)(tk) * 1000LL / 46875LL))

Calculate microseconds based on timer ticks.

Parameters:
[in]tkTicks to convert to microseconds
Returns:
Microseconds
#define TIMER_MICROS_LL (   tk)    ((long long)(tk) * 1000LL / 46875LL)

Calculate microseconds based on timer ticks.

Parameters:
[in]tkTicks to convert to microseconds
Returns:
Microseconds as a long long
#define TIMER_TICKS (   us)    ((int)((long long)(us) * 46875LL / 1000LL))

Calculate timer ticks based on microseconds.

Parameters:
[in]usMicroseconds to convert to ticks
Returns:
Timer ticks
#define TIMER_TICKS_LL (   us)    ((long long)(us) * 46875LL / 1000LL)

Calculate timer ticks based on microseconds.

Parameters:
[in]usMicroseconds to convert to ticks
Returns:
Timer ticks as a long long
#define write_compare (   x)    asm volatile("mtc0 %0,$11\n\t nop \n\t" : : "r" (x) )

Set the compare register.

This sets up the compare register so that when the count register equals the compare register, an interrupt will be generated.

Parameters:
[in]xValue to write into the compare register
#define write_count (   x)    asm volatile("mtc0 %0,$9\n\t nop \n\t" : : "r" (x) )

Write the count to the count register.

Parameters:
[in]xValue to write into the count register

Function Documentation

static int __proc_timers ( timer_link_t head) [static]

Process linked list of timers.

Walk the linked list of timers and call the callbacks of any that have expired.

Note:
This function will remove one-shot timers from the list after they have fired.
Parameters:
[in]headHead of the linked list of timers
Return values:
1The list needs reprocessing
0All timer operations were handled successfully
void delete_timer ( timer_link_t timer)

Remove a timer from the list and delete it.

Parameters:
[in]timerTimer structure to stop, remove and free
timer_link_t* new_timer ( int  ticks,
int  flags,
void(*)(int ovfl)  callback 
)

Create a new timer and add to list.

Parameters:
[in]ticksNumber of ticks before the timer should fire
[in]flagsTimer flags. See TF_ONE_SHOT and TF_CONTINUOUS
[in]callbackCallback function to call when the timer expires
Returns:
A pointer to the timer structure created
void start_timer ( timer_link_t timer,
int  ticks,
int  flags,
void(*)(int ovfl)  callback 
)

Start a timer not currently in the list.

Parameters:
[in]timerPointer to timer structure to reinsert and start
[in]ticksNumber of ticks before the timer should fire
[in]flagsTimer flags. See TF_ONE_SHOT and TF_CONTINUOUS
[in]callbackCallback function to call when the timer expires
void stop_timer ( timer_link_t timer)

Stop a timer and remove it from the list.

Note:
This function does not free a timer structure, use delete_timer to do this.
Parameters:
[in]timerTimer structure to stop and remove
static void timer_callback ( void  ) [static]

Timer callback function.

This function is called by the interrupt controller whenever compare == count.

void timer_close ( void  )

Free and close the timer subsystem.

This function will ensure all recurring timers are deleted from the list before closing. One-shot timers that have expired will need to be manually deleted with delete_timer.

long long timer_ticks ( void  )

Return total ticks since timer was initialized.

Returns:
Then number of ticks since the timer was initialized
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines