System Timer

Description

All netlib timers, timeouts and timing loops use simple uint32_t counters. Thus no timing interrupt is required for proper function. Especially on low-performance DSP with limited hardware timer resource all HW timers are often occupied by signal-processing tasks.

If a HW timer is still available in your application or your application uses SYS/BIOS, this module can be used to install an interrupt driven system timer or a Clock function in SYS/BIOS resp. for a high precision timing purpose. This time base can also be used to trigger the internal network timers to get a more accurate timing result for network tasks.

All Support SW examples make use of the system timer to trigger events, display progress bars or print time-stamps.


System Timer installation

Function call

installs a HW system timer and sets the current time to application build time. Choose a free timer port that is not already occupied in your application. Parameter resolution specifies the timer granularity. Use predifened values from timer.h:

#define RES_USECONDS 1 // mikro seconds //
#define RES_10USECONDS 10 // 10 mikro seconds //
#define RES_100USECONDS 100 // 100 mikro seconds //
#define RES_MSECONDS 1000 // milli seconds //
#define RES_10MSECONDS 10000 // 10 milli seconds //
#define RES_100MSECONDS 100000 // 100 milli seconds //
#define RES_SECONDS 1000000 // seconds //
Note
The smaller the resolution value, the higher timer frequency, the higher the interrupt load. Choose a reasonable value (e.g. RES_MSECONDS for a milli seconds resolution).

System Timer Format

The system timer uses a combined data structure for seconds and micro seconds:

//**************************************************************************
// high precision time structure
//**************************************************************************
typedef struct
{
time_t tv_sec; // seconds
time_t tv_usec; // micro seconds

A global accessible variable of data type timeval can be used to get the current time:

Local Time

During SetupSystemTime() the local time is set to current build time. To print the current local UTC time use:

struct tm *local_time; // structure to hold time and date
...
//*******************************************************************
// print time
//*******************************************************************
local_time = localtime ((const time_t *)&RecentEpoch);
CPrintf (" Current UTC time: %s\r", asctime (local_time));

If your system uses a RTC or is time synchronized via NTP use the following sequence to set the local time:

//***********************************************************************
// convert epoch to local time
//***********************************************************************
local_time = localtime ((const time_t *)&RecentEpoch);
//***********************************************************************
// set new time
//***********************************************************************
local_time->tm_hour = new_hour;
local_time->tm_min = new_min;
local_time->tm_sec = new_sec;
local_time->tm_year = new_year; // since 1900
local_time->tm_mon = new_month; // 0..11
local_time->tm_mday = new_day;
//***********************************************************************
// generate new epoch
//***********************************************************************
RecentEpoch.tv_sec = mktime (local_time);




Execution Time Measurement

If started, the system timer can easily be used to measure loop execution time at timer resolution granularity. Use function

timeval stamp1, stamp2, delta; // used to determine startup time
...
//**************************************************************************
// measure network initialization time
//**************************************************************************
stamp1 = GetTimeStamp();
//**************************************************************************
// CPrintfProgress (" Initialize network ");
///*************************************************************************
InitializeNetwork ( 64); // 64 bytes for ping
stamp2 = GetTimeStamp();
tv_interval (&delta, &stamp1, &stamp2);
CPrintf (" network startup time [sec]: ");
"%"PRId32".%03"PRId32"\r\n"
delta.tv_sec,
delta.tv_usec/1000);




Elapsed Time Control

  • int tv_elapsed (timeval *t, time_t s, time_t u, uint16_t retrigger )
    can be used to determine if a certain amount of time has elapsed.
//***********************************************************************
// print time once per minute
//***********************************************************************
if ( tv_elapsed (&TimeStampEvent, 60, 0, TRUE) )
{
//*******************************************************************
// print time
//*******************************************************************
local_time = localtime ((const time_t *)&RecentEpoch);
CPrintf (UTC_PRINTF, asctime (local_time));
}
//***********************************************************************
// let a LED blink once per second (50% duty cycle)
//***********************************************************************
if ( tv_elapsed (&LEDBlinkEvent, 0, 500000, TRUE) )
{
}