BeRTOS
Data Structures | Defines | Functions | Variables
Timer module
BeRTOS core functionality

Hardware independent timer driver. More...

Data Structures

struct  Timer
 The timer driver supports multiple synchronous timers that can trigger an event when they expire. More...

Defines

#define synctimer_abort(t)   timer_abort(t)

Functions

ticks_t timer_clock (void)
 Return the system tick counter (expressed in ticks)
ticks_t timer_clock_unlocked (void)
 Faster version of timer_clock(), to be called only when the timer interrupt is disabled (DISABLE_INTS) or overridden by a higher-priority or non-nesting interrupt.
ticks_t ms_to_ticks (mtime_t ms)
 Convert ms [ms] to ticks.
ticks_t us_to_ticks (utime_t us)
 Convert us [us] to ticks.
mtime_t ticks_to_ms (ticks_t ticks)
 Convert ticks [ticks] to ms.
utime_t ticks_to_us (ticks_t ticks)
 Convert ticks [ticks] to us.
hptime_t us_to_hptime (utime_t us)
 Convert us [us] to hpticks.
utime_t hptime_to_us (hptime_t hpticks)
 Convert hpticks [hptime] to usec.
void timer_delayTicks (ticks_t delay)
 Wait for the specified amount of timer ticks.
void timer_delay (mtime_t delay)
 Wait some time [ms].
void timer_busyWait (hptime_t delay)
 Busy wait until the specified amount of high-precision ticks have elapsed.
void timer_delayHp (hptime_t delay)
 Wait for the specified amount of time (expressed in microseconds).
void timer_add (Timer *timer)
 Add the specified timer to the software timer service queue.
Timertimer_abort (Timer *timer)
 Remove a timer from the timers queue before it has expired.
void timer_setSoftint (Timer *timer, Hook func, iptr_t user_data)
 Set the timer so that it calls an user hook when it expires.
void timer_setDelay (Timer *timer, ticks_t delay)
 Set the timer delay (the time before the event will be triggered)
void synctimer_add (Timer *timer, List *q)
 Add timer to queue.
void synctimer_poll (List *q)
 Simple synchronous timer based scheduler polling routine.
void timer_setEvent (Timer *timer)
 Set the timer so that it sends a event notification when it expires.
void timer_waitEvent (Timer *timer)
 Wait until the timer expires.
void timer_setSignal (Timer *timer, struct Process *proc, sigmask_t sigs)
 Set the timer so that it sends a signal when it expires.

Variables

volatile ticks_t _clock
 Master system clock (1 tick accuracy)

Detailed Description

Hardware independent timer driver.

All timer related functions are implemented in this module. You have several options to use timers:

Whenever a timer expires you need to explicitly arm it again with timer_add(). If you want to abort a timer, use timer_abort(). You can use conversion macros when using msecs to specify the delay.

Author:
Bernie Innocenti <bernie@codewiz.org>

Define Documentation

#define synctimer_abort (   t)    timer_abort(t)
See also:
timer_abort

Definition at line 309 of file timer.h.


Function Documentation

ticks_t ms_to_ticks ( mtime_t  ms) [inline]

Convert ms [ms] to ticks.

Definition at line 157 of file timer.h.

void synctimer_add ( Timer timer,
List queue 
)

Add timer to queue.

See also:
synctimer_poll() for details.

Definition at line 215 of file timer.c.

void synctimer_poll ( List queue)

Simple synchronous timer based scheduler polling routine.

Sometimes you would like to have a proper scheduler, but you can't afford it due to memory constraints.

This is a simple replacement: you can create events and call them periodically at specific time intervals. All you have to do is to set up normal timers, and call synctimer_add() instead of timer_add() to add the events to your specific queue. Then, in the main loop or wherever you want, you can call synctimer_poll() to process expired events. The associated callbacks will be executed. As this is done synchronously you don't have to worry about race conditions. You can kill an event by simply calling synctimer_abort().

Definition at line 237 of file timer.c.

mtime_t ticks_to_ms ( ticks_t  ticks) [inline]

Convert ticks [ticks] to ms.

Definition at line 181 of file timer.h.

utime_t ticks_to_us ( ticks_t  ticks) [inline]

Convert ticks [ticks] to us.

Definition at line 193 of file timer.h.

Timer* timer_abort ( Timer timer)

Remove a timer from the timers queue before it has expired.

Note:
Attempting to remove a timer already expired cause undefined behaviour.

Definition at line 176 of file timer.c.

void timer_add ( Timer timer)

Add the specified timer to the software timer service queue.

When the delay indicated by the timer expires, the timer device will execute the event associated with it.

You should not call this function on an already running timer.

Note:
Interrupt safe

Definition at line 165 of file timer.c.

void timer_busyWait ( hptime_t  delay)

Busy wait until the specified amount of high-precision ticks have elapsed.

Note:
This function is interrupt safe, the only requirement is a running hardware timer.

Definition at line 285 of file timer.c.

ticks_t timer_clock ( void  ) [inline]

Return the system tick counter (expressed in ticks)

The result is guaranteed to increment monotonically, but client code must be tolerant with respect to overflows.

The following code is safe:

   drop_teabag();
   ticks_t tea_start_time = timer_clock();

   for (;;)
   {
       if (timer_clock() - tea_start_time > TEAPOT_DELAY)
       {
           printf("Your tea, Sir.\n");
           break;
       }
       patience();
   }
Note:
This function must disable interrupts on 8/16bit CPUs because the clock variable is larger than the processor word size and can't be copied atomically.
See also:
timer_delay()

Definition at line 134 of file timer.h.

ticks_t timer_clock_unlocked ( void  ) [inline]

Faster version of timer_clock(), to be called only when the timer interrupt is disabled (DISABLE_INTS) or overridden by a higher-priority or non-nesting interrupt.

See also:
timer_clock

Definition at line 150 of file timer.h.

void timer_delay ( mtime_t  delay) [inline]

Wait some time [ms].

Note:
CPU is released while waiting so you don't have to call cpu_relax() explicitly.
Parameters:
delayTime to wait [ms].

Definition at line 231 of file timer.h.

void timer_delayHp ( hptime_t  delay)

Wait for the specified amount of time (expressed in microseconds).

Definition at line 328 of file timer.c.

void timer_delayTicks ( ticks_t  delay)

Wait for the specified amount of timer ticks.

Note:
Sleeping while preemption is disabled fallbacks to a busy wait sleep.

Definition at line 250 of file timer.c.

void timer_setDelay ( Timer timer,
ticks_t  delay 
) [inline]

Set the timer delay (the time before the event will be triggered)

Note:
It's illegal to change the delay of the timer when it's still running.

Definition at line 300 of file timer.h.

void timer_setSoftint ( Timer timer,
Hook  func,
iptr_t  user_data 
) [inline]

Set the timer so that it calls an user hook when it expires.

Sometimes you may want to use the same callback for different events, so you must have different data to operate on. The user_data parameter is such data.

Parameters:
timerTimer struct to set the callback to
funcFunction that will be called when the timer expires
user_dataAdditional data you may want to pass to the callback

Definition at line 289 of file timer.h.

ticks_t us_to_ticks ( utime_t  us) [inline]

Convert us [us] to ticks.

Definition at line 169 of file timer.h.