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

Timer Management

Asynchronous timer support. More...

Functions

 NutTimerThread (void *arg)
 System timer thread. More...

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

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

void NutTimerStop (HANDLE handle)
 Stop a specified timer. More...

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

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

void NutTimerInit (void)
 Initialize system timer. More...


Variables

NUTTIMERINFO* volatile nutTimerList
 Linked list of timers.


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.

Version:
2.0.4
Author:
Harald Kipp, egnite Software GmbH

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.
Examples:
timers/timers.c.

u_long NutGetCpuClock ( void )
 

Return the CPU clock in Hertz.

If called for the first time, this function determines the CPU clock by running a counter loop between two timer interrupts. Subsequent calls will return the previously calculated value.

Returns:
3686400, 4000000, 5000000 or 6000000.

Bug:
Due to the asynchronous nature of timer 0 and this experimental code, rounding errors have not been fully checked. On the other hand, the exact result is needed for proper baudrate calculation. Therefore this function returns four fixed values based on the loop count result.
Examples:
timers/timers.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, that the thread 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 more exact timing, use NutDelay().

Parameters:
ms   Milliseconds to sleep. Granularity is 62.5 ms. If 0, the current thread will not sleep, but may give up the CPU.

Bug:
Can't sleep on parallel application threads. Threads example fails.
Examples:
tcpc/tcpc.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 from a system thread at a very high priority and must return as soon as possible and must not call any potentially blocking function.

Parameters:
ms   Specifies the timer interval in milliseconds.
callback   Identifies the function after 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.

Bug:
One-shot timer not working. Use periodic timer and call NutTimerStop() during callback.
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.

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

NutTimerThread ( void * arg )
 

System timer thread.

Woken up periodically by timer interrupts. Tick counters of all timers are decremented by one. If a counter reaches zero, the timer callback function is called and one-shot timers are removed from the list.


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