libdragon
Loading...
Searching...
No Matches
Macros | Functions
timer.c File Reference

Timer Subsystem. More...

Macros

#define TF_CONTEXT   0x20
 Timer callback expects a context parameter.
 
#define TF_OVERFLOW   0x40
 Timer is the special overflow timer.
 
#define TF_CALLED   0x80
 Timer has been called once in this interrupt.
 

Functions

void timer_init (void)
 Initialize the timer subsystem.
 
timer_link_tnew_timer (int ticks, int flags, timer_callback1_t callback)
 Create a new timer and add to list.
 
timer_link_tnew_timer_context (int ticks, int flags, timer_callback2_t callback, void *ctx)
 Create a new timer with context and add to list.
 
void start_timer (timer_link_t *timer, int ticks, int flags, timer_callback1_t callback)
 Start a timer (not currently in the list)
 
void start_timer_context (timer_link_t *timer, int ticks, int flags, timer_callback2_t callback, void *ctx)
 Start a timer (not currently in the list) with context.
 
void restart_timer (timer_link_t *timer)
 Reset a timer and add to 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, as a 64-bit counter.
 

Detailed Description

Timer Subsystem.

Function Documentation

◆ timer_init()

void timer_init ( void  )

Initialize the timer subsystem.

This function will reset the COP0 ticks counter to 0. Even if you later access the hardware counter directly (via TICKS_READ()), it should not be a problem if you call timer_init() early in the application main.

Do not modify the COP0 ticks counter after calling this function. Doing so will impede functionality of the timer module.

The timer subsystem tracks the number of times timer_init is called and will only initialize the subsystem on the first call. This reference count also applies to timer_close, which will only close the subsystem if it is called the same number of times as timer_init.

◆ new_timer()

timer_link_t * new_timer ( int  ticks,
int  flags,
timer_callback1_t  callback 
)

Create a new timer and add to list.

If you need to associate some data with the timer, consider using new_timer_context to include a pointer in the callback.

Parameters
[in]ticksNumber of ticks before the timer should fire
[in]flagsTimer flags. See TF_ONE_SHOT, TF_CONTINUOUS and TF_DISABLED
[in]callbackCallback function to call when the timer expires
Returns
A pointer to the timer structure created

◆ new_timer_context()

timer_link_t * new_timer_context ( int  ticks,
int  flags,
timer_callback2_t  callback,
void *  ctx 
)

Create a new timer with context and add to list.

If you don't need the context, consider using new_timer instead.

Parameters
[in]ticksNumber of ticks before the timer should fire
[in]flagsTimer flags. See TF_ONE_SHOT, TF_CONTINUOUS and TF_DISABLED
[in]callbackCallback function to call when the timer expires
[in]ctxOpaque pointer to pass as an argument to callback
Returns
A pointer to the timer structure created

◆ start_timer()

void start_timer ( timer_link_t timer,
int  ticks,
int  flags,
timer_callback1_t  callback 
)

Start a timer (not currently in the list)

If you need to associate some data with the timer, consider using start_timer_context to include a pointer in the callback.

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, TF_CONTINUOUS, and TF_DISABLED
[in]callbackCallback function to call when the timer expires

◆ start_timer_context()

void start_timer_context ( timer_link_t timer,
int  ticks,
int  flags,
timer_callback2_t  callback,
void *  ctx 
)

Start a timer (not currently in the list) with context.

If you don't need the context, consider using start_timer instead.

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, TF_CONTINUOUS, and TF_DISABLED
[in]callbackCallback function to call when the timer expires
[in]ctxOpaque pointer to pass as an argument to callback

◆ restart_timer()

void restart_timer ( timer_link_t timer)

Reset a timer and add to list.

Parameters
[in]timerPointer to timer structure to reinsert and start

◆ stop_timer()

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.
It is safe to call this function from a timer callback, including to stop a timer from its own callback.
Parameters
[in]timerTimer structure to stop and remove

◆ delete_timer()

void delete_timer ( timer_link_t timer)

Remove a timer from the list and delete it.

Note
It is not safe to call this function from a timer callback.
Parameters
[in]timerTimer structure to stop, remove and free

◆ timer_close()

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.

The timer subsystem tracks the number of times timer_init is called and will only close the subsystem if timer_close is called the same number of times.

◆ timer_ticks()

long long timer_ticks ( void  )

Return total ticks since timer was initialized, as a 64-bit counter.

Returns
Then number of ticks since the timer was initialized