Nanokernel Timer

This document describes nanokernel timers.

Creation

NTimer is a basic relative timer provided by the nanokernel. It can generate either a single interrupt or periodic interrupts. A timeout handler is called when the timer expires, either from the timer ISR or from the nanokernel timer thread. These timer objects can manipulated from any context. The timers are driven from a periodic system tick interrupt, usually a 1ms period. The use of NTimer is as follows:

Start

The nanokernel timer can be used in either single mode or in periodic mode. Typically, the timer is started with NTimer::OneShot() for a timeout period and then is made periodic by calling NTimer::Again(). The timeout value specified for the timer is in nanokernel ticks. The time out callback function can be chosen to be called in either an ISR context or a DFC context (running in the kernel's nanokernel timer thread, called DfcThread1).

TInt NTimer::OneShot(TInt aTime, TBool aDfc);
// If aDfc is TRUE then, the timeout function is called in a DFC context; 
// if aDfc is FALSE, the timeout function is called in a ISR context.

// Starts timer in zero-drift mode, to avoid delays in re-queuing
// the timer
TInt NTimer::Again(TInt aTime); 

// NTimer::OneShot() starts a nanokernel timer in one-shot mode with 
// ISR callback. It queues the timer to expire in the specified number 
// of nanokernel ticks. The actual wait time will be at least that 
// much and may be up to one tick more. The expiry handler will be 
// called in ISR context.
//
iRxPollTimer.OneShot(KRxTimeout);

Callback function

The timeout callback function is called when a timer started with NTimer::OneShot() or NTimer::Again() expires. A driver should implement the function to process the timeout event as appropriate for the situation.

/**
 Timer callback function. Called when the NTimer expires
 typedef void(* NTimerFn)(TAny*); is the nanokernel timer callback
 function. This is used for Rx timeout.

 @params    aPtr
         pointer refernce passed during timer initialization
 */
void DExUartPhysicalChannelH4::RxPollTimerCallback(TAny* aPtr)
    {
    ... // design specific
    }

Cancellation

A timer can be cancelled using NTimer::Cancel(). It removes the timer object from the nanokernel timer queue. If the timer has already expired, or is inactive, then it does nothing.

If a timer was queued and a DFC callback was requested, then the expiry handler might run even after Cancel() has been called. This occurs when the DfcThread1 is preempted just before calling the expiry handler for this timer, and the preempting thread/ISR/IDFC calls Cancel() on the timer.