![]() |
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.
Functions | |
void | NutTimerInit (void) |
Initialize system timer. | |
void | NutMicroDelay (uint32_t us) |
Loop for a specified number of microseconds. | |
void | NutDelay (uint8_t ms) |
Loop for a specified number of milliseconds. | |
void | NutTimerInsert (NUTTIMERINFO *tn) |
Insert a new timer in the global timer list. | |
void | NutTimerProcessElapsed (void) |
Process elapsed timers. | |
NUTTIMERINFO * | NutTimerCreate (uint32_t ticks, void(*callback)(HANDLE, void *), void *arg, uint8_t flags) |
Create a new system timer. | |
HANDLE | NutTimerStartTicks (uint32_t ticks, void(*callback)(HANDLE, void *), void *arg, uint8_t flags) |
Start a system timer. | |
HANDLE | NutTimerStart (uint32_t ms, void(*callback)(HANDLE, void *), void *arg, uint8_t flags) |
Start a system timer. | |
void | NutSleep (uint32_t ms) |
Temporarily suspends the current thread. | |
void | NutTimerStop (HANDLE handle) |
Stop a specified timer. | |
uint32_t | NutGetTickCount (void) |
Return the number of system timer ticks. | |
uint32_t | NutGetSeconds (void) |
Return the seconds counter value. | |
uint32_t | NutGetMillis (void) |
Return the milliseconds counter value. | |
int | NutClockSet (int idx, uint32_t freq) |
Return the specified clock frequency. | |
uint32_t | NutGetCpuClock (void) |
Return the CPU clock frequency. | |
Variables | |
NUTTIMERINFO * | nutTimerList |
Double linked list of all system timers. | |
volatile uint32_t | nut_ticks |
System tick counter. | |
volatile uint32_t | nut_delay_loops |
Loops per microsecond. |
void NutTimerInit | ( | void | ) |
Initialize system timer.
This function is automatically called by Nut/OS during system initialization. It calls the hardware dependent layer to initialze the timer hardware and register a timer interrupt handler.
Definition at line 289 of file timer.c.
References nut_delay_loops, NutEnableTimerIrq, NutGetTickCount(), and NutRegisterTimer().
void NutMicroDelay | ( | uint32_t | us | ) |
Loop for a specified number of microseconds.
This call will not release the CPU and will not switch to another thread. However, interrupts are not disabled and introduce some jitter. Furthermore, unless NUT_DELAYLOOPS is not defined, the deviation may be greater than 10%.
If you need exact timing, use timer/counter hardware instead.
us | Delay time in microseconds. Values above 255 milliseconds may not work. |
Definition at line 338 of file timer.c.
References _NOP, and nut_delay_loops.
void NutDelay | ( | uint8_t | ms | ) |
Loop for a specified number of milliseconds.
This call will not release the CPU and will not switch to another thread. Because of absent thread switching, the delay time is more exact than with NutSleep().
ms | Delay time in milliseconds, maximum is 255. |
Definition at line 362 of file timer.c.
References NutMicroDelay().
void NutTimerInsert | ( | NUTTIMERINFO * | tn | ) |
Insert a new timer in the global timer list.
Applications should not call this function.
tn | Pointer to the timer structure to insert. |
Definition at line 376 of file timer.c.
References _NUTTIMERINFO::tn_next, _NUTTIMERINFO::tn_prev, and _NUTTIMERINFO::tn_ticks_left.
void NutTimerProcessElapsed | ( | void | ) |
Process elapsed timers.
This routine is called during context switch processing. Applications should not use this function.
Definition at line 407 of file timer.c.
References NutGetTickCount(), NutHeapFree(), NutTimerInsert(), _NUTTIMERINFO::tn_arg, _NUTTIMERINFO::tn_callback, _NUTTIMERINFO::tn_next, _NUTTIMERINFO::tn_prev, _NUTTIMERINFO::tn_ticks, and _NUTTIMERINFO::tn_ticks_left.
NUTTIMERINFO* NutTimerCreate | ( | uint32_t | ticks, | |
void(*)(HANDLE, void *) | callback, | |||
void * | arg, | |||
uint8_t | flags | |||
) |
Create a new system timer.
Applications should not call this function.
ticks | Specifies the timer interval in system ticks. | |
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. |
Definition at line 474 of file timer.c.
References NutGetTickCount(), NutHeapAlloc(), TM_ONESHOT, _NUTTIMERINFO::tn_arg, _NUTTIMERINFO::tn_callback, _NUTTIMERINFO::tn_ticks, and _NUTTIMERINFO::tn_ticks_left.
HANDLE NutTimerStartTicks | ( | uint32_t | ticks, | |
void(*)(HANDLE, void *) | callback, | |||
void * | arg, | |||
uint8_t | flags | |||
) |
Start a system timer.
The function returns immediately, while the timer runs asynchronously in the background.
The timer counts for a specified number of ticks, then calls the callback routine with a given argument.
Even after the timer elapsed, the callback function is not executed before the currently running thread is ready to give up the CPU. Thus, system timers may not fulfill the required accuracy. For precise or high resolution timing, native timer interrupt routines are a better choice.
ticks | Specifies the timer interval in system ticks. | |
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. |
Definition at line 525 of file timer.c.
References NutTimerCreate(), and NutTimerInsert().
Start a system 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.
Even after the timer elapsed, the callback function is not executed before the currently running thread is ready to give up the CPU. Thus, system timers may not fulfill the required accuracy. For precise or high resolution timing, native timer interrupt routines are a better choice.
ms | Specifies the timer interval in milliseconds. The resolution is limited to the granularity of the system timer. | |
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. |
Definition at line 565 of file timer.c.
References NutTimerMillisToTicks(), and NutTimerStartTicks().
void NutSleep | ( | uint32_t | 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.
ms | Milliseconds to sleep. If 0, the current thread will not sleep, but may give up the CPU. The resolution is limited to the granularity of the system timer. |
Definition at line 591 of file timer.c.
References NutThreadRemoveQueue(), NutThreadResume(), NutThreadWake(), NutThreadYield(), NutTimerStart(), runningThread, runQueue, _NUTTHREADINFO::td_qnxt, _NUTTHREADINFO::td_queue, _NUTTHREADINFO::td_state, _NUTTHREADINFO::td_timer, TDS_RUNNING, TDS_SLEEP, TM_ONESHOT, TRACE_ADD_ITEM, and TRACE_TAG_THREAD_SLEEP.
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.
handle | Identifies the timer to be stopped. This handle must have been created by calling NutTimerStart() or NutTimerStartTicks(). |
Definition at line 628 of file timer.c.
References NutTimerInsert(), _NUTTIMERINFO::tn_callback, _NUTTIMERINFO::tn_next, _NUTTIMERINFO::tn_prev, _NUTTIMERINFO::tn_ticks, and _NUTTIMERINFO::tn_ticks_left.
uint32_t NutGetTickCount | ( | void | ) |
Return the number of system timer ticks.
This function returns the number of system ticks since the system was started.
Definition at line 660 of file timer.c.
References nut_ticks, NutEnterCritical, and NutExitCritical.
uint32_t 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.
Definition at line 693 of file timer.c.
References NutGetTickClock(), and NutGetTickCount().
uint32_t 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 with the 32 bit tick counter (4294967296). With the default 1024 ticks/s this will happen after 7.9 years. The resolution is also given by the system ticks.
Definition at line 714 of file timer.c.
References NutGetTickClock(), and NutGetTickCount().
int NutClockSet | ( | int | idx, | |
uint32_t | freq | |||
) |
Return the specified clock frequency.
If only 1 hardware clock is defined, then this function is mapped to NutGetCpuClock().
The number of available hardware clocks depends on the target harware and is specified by NUT_HWCLK_MAX + 1.
idx | Index of the hardware clock. Can be any of the following:
|
In the future this may be used to set any hardware clock. For now, this simply clears the clock value cache and must be called after changing any clock frequency.
idx | Index of the hardware clock, currently ignored. Set to -1 (all clocks) to maintain upward compatibility. | |
freq | Clock frequency in Hertz, currently ignored. Set to NUT_CACHE_LVALID (release cached value) to maintain upward compatibility. |
Definition at line 763 of file timer.c.
References memset().
uint32_t NutGetCpuClock | ( | void | ) |
Return the CPU clock frequency.
Same as NutClockGet(NUT_HWCLK_CPU) but faster, specially when NUT_CPU_FREQ is defined.
Definition at line 780 of file timer.c.
References NUT_CACHE_LVALID, NUT_CPU_FREQ, NUT_HWCLK_CPU, NutArchClockGet(), and NutClockGet.
volatile uint32_t nut_delay_loops |
Loops per microsecond.
Definition at line 258 of file timer.c.
Referenced by NutMicroDelay(), and NutTimerInit().