Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

Timer Management
[Nut/OS API]

Asynchronous timer support. More...

Functions

HANDLE NutTimerStart (u_long ms, void(*callback)(HANDLE, void *), void *arg, u_char flags)
 Create an asynchronous timer.

void NutSleep (u_long ms)
 Temporarily suspends the current thread.

void NutTimerStopAsync (HANDLE handle)
 Asynchronously stop a specified timer.

void NutTimerStop (HANDLE handle)
 Stop a specified timer.

void NutDelay (u_char ms)
 Loop for a specified number of milliseconds.

u_long NutGetCpuClock (void)
 Return the CPU clock in Hertz.

void NutTimerInit (void)
 Initialize system timer.

u_long NutGetTickCount (void)
 Return the number of timer ticks.

u_long NutGetSeconds (void)
 Return the seconds counter value.

u_long NutGetMillis (void)
 Return the milliseconds counter value.


Variables

NUTTIMERINFO *volatile nutTimerList
 Linked list of all system timers.

NUTTIMERINFO *volatile nutTimerPool

Detailed Description

Asynchronous timer support.

The timer management provides functions to start and stop asynchronous timers, determine the CPU speed and let a thread give up the CPU for a specified time period.


Function Documentation

void NutDelay u_char    ms
 

Loop for a specified number of milliseconds.

This call will not release the CPU and will not switch to another thread. However, because of absent thread switching, this delay time is very exact.

Use NutSleep() to avoid blocking the CPU, if no exact timing is needed.

Parameters:
ms Delay time in milliseconds, maximum is 255.

u_long NutGetCpuClock void   
 

Return the CPU clock in Hertz.

Returns:
CPU clock frequency in Hertz.
Examples:
timers/timers.c.

u_long NutGetMillis void   
 

Return the milliseconds counter value.

This function returns the value of a counter, which is incremented every system timer tick. During system start, the counter is cleared to zero and will overflow roughly after 49.7 days. The resolution is depends on system ticks.

Note:
There is intentionally no provision to modify the seconds counter. Callers can rely on a continuous update and use this value for system tick independend timeout calculations.
Returns:
Value of the seconds counter.

u_long NutGetSeconds void   
 

Return the seconds counter value.

This function returns the value of a counter, which is incremented every second. During system start, the counter is cleared to zero.

Note:
There is intentionally no provision to modify the seconds counter. Callers can rely on a continuous update and use this value for system tick independend timeout calculations. Applications, which want to use this counter for date and time functions, should use an offset value.
Returns:
Value of the seconds counter.

u_long NutGetTickCount void   
 

Return the number of timer ticks.

This function returns the TickCount since the system was started. It is limited to the resolution of the system timer.

Returns:
Number of ticks.
Examples:
inetq/inetq.c.

void NutSleep u_long    ms
 

Temporarily suspends the current thread.

Causes the current thread to wait for a specified interval or, if the specified interval is zero, to give up the CPU for another thread with higher or same priority.

This function may switch to another application thread, that got the same or a higher priority and is ready to run.

Note:
Threads may sleep longer than the specified number of milliseconds, depending on the number of threads with higher or equal priority, which are ready to run. If you need exact timing, use NutDelay().

Bug:
The system may freeze if a sleep time greater than 0 ms but lower than 63 ms is specified.
Parameters:
ms Milliseconds to sleep. Granularity is 62.5 ms. If 0, the current thread will not sleep, but may give up the CPU.
Examples:
httpd/httpserv.c, inetq/inetq.c, playmp3/playmp3.c, threads/threads.c, and timers/timers.c.

void NutTimerInit void   
 

Initialize system timer.

This function is automatically called by Nut/OS during system initialization.

Nut/OS uses on-chip timer 0 for its timer services. Applications should not modify any registers of this timer, but make use of the Nut/OS timer API. Timer 1 and timer 2 are available to applications.

HANDLE NutTimerStart u_long    ms,
void(*    callback)(HANDLE, void *),
void *    arg,
u_char    flags
 

Create an asynchronous timer.

The function returns immediately, while the timer runs asynchronously in the background.

The timer counts for a specified number of milliseconds, then calls the callback routine with a given argument.

The callback function is executed in interrupt context at a very high priority. It can call only a limited set of functions and must return as soon as possible.

Parameters:
ms Specifies the timer interval in milliseconds.
callback Identifies the function to be called on each timer interval.
arg The argument passed to the callback function.
flags If set to TM_ONESHOT, the timer will be stopped after the first interval. Set to 0 for periodic timers.
Returns:
Timer handle if successfull, 0 otherwise. The handle may be used to stop the timer by calling TimerStop.
Examples:
timers/timers.c.

void NutTimerStop HANDLE    handle
 

Stop a specified timer.

Only periodic timers need to be stopped. One-shot timers are automatically stopped by the timer management after ther first timer interval. Anyway, long running one-shot timers may be stopped to release the occupied memory.

Parameters:
handle Identifies the timer to be stopped. This handle must have been created by calling NutTimerStart().
Examples:
timers/timers.c.

void NutTimerStopAsync HANDLE    handle
 

Asynchronously stop a specified timer.

Stops one-shot and periodic timers.

Note:
It is save to call this function from within an interrupt handler. The memory occupied by the timer is not released, but added to a pool and will be re-used by the next timer being created. In any case interrupts should be disabled when calling this function.
Parameters:
handle Identifies the timer to be stopped. This handle must have been created by calling NutTimerStart().


Variable Documentation

NUTTIMERINFO* volatile nutTimerList
 

Linked list of all system timers.

NUTTIMERINFO* volatile nutTimerPool
 

This pool is used to collect released memory from elapsed timers. It's required because we can't free memory in interrupt context.


© 2000-2003 by egnite Software GmbH - visit http://www.ethernut.de/