Data Structures | Files | Defines | Typedefs | Functions

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

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.

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] x Variable to place count into
#define TIMER_MICROS (   tk  )     ((int)((long long)(tk) * 1000LL / 46875LL))

Calculate microseconds based on timer ticks.

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

Calculate microseconds based on timer ticks.

Parameters:
[in] tk Ticks 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] us Microseconds 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] us Microseconds 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] x Value 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] x Value to write into the count register

Function Documentation

void delete_timer ( timer_link_t timer  ) 

Remove a timer from the list and delete it.

Parameters:
[in] timer Timer 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] ticks Number of ticks before the timer should fire
[in] flags Timer flags. See TF_ONE_SHOT and TF_CONTINUOUS
[in] callback Callback 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] timer Pointer to timer structure to reinsert and start
[in] ticks Number of ticks before the timer should fire
[in] flags Timer flags. See TF_ONE_SHOT and TF_CONTINUOUS
[in] callback Callback 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] timer Timer structure to stop and remove
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