timer.h File Reference

timer support function More...

Data Structures

struct  timeval
 
struct  T_TimeOutTimer
 
struct  T_SchedulerRecord
 

Macros

#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 */
 
#define GetTimeStamp()    RecentEpoch
 
#define TR_CONTROL_ACTIVE   0x0001
 
#define TIMER_RANGE_ERROR_STRING   TimerMessages[0]
 
#define TIMER_NOT_RUNNING   TimerMessages[1]
 

Typedefs

typedef void(* T_TimerEventHandler) (uint32_t *)
 

Functions

time_t GetSystemTimerRes (void)
 
FUNC_RETURN ResetTimeOutTimer (T_TimeOutTimer *tot, char symbol, time_t period, char *text)
 
int tv_elapsed (timeval *t, time_t s, time_t u, uint16_t retrigger)
 check if given time has elapsed More...
 
void tv_interval (timeval *e, timeval *t1, timeval *t2)
 compute elapsed time More...
 
void SetupSystemTime (int32_t cpuint, int port, time_t resolution)
 Setup System Time. More...
 
void StartSystemTimer (void)
 start system timer More...
 
void StopSystemTimer (void)
 stop system timer More...
 
void RegisterTimerEvent (T_TimerEventHandler EventHandlerFunc, time_t sec, time_t usec)
 register a new timer event More...
 

Variables

timeval RecentEpoch
 
const char * TimerMessages []
 
 TIMER_HANDLER_INTERRUPT
 timer interrupt function More...
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Version
2.91
Date
2019-04-17

Macro Definition Documentation

#define RES_USECONDS   1 /* mikro seconds */
#define RES_10USECONDS   10 /* 10 mikro seconds */
#define RES_100USECONDS   100 /* 100 mikro seconds */
#define RES_10MSECONDS   10000 /* 10 milli seconds */
Examples:
SMTP.c.
#define RES_100MSECONDS   100000 /* 100 milli seconds */
#define TR_CONTROL_ACTIVE   0x0001
#define TIMER_RANGE_ERROR_STRING   TimerMessages[0]
Examples:
BoardSpecific.c.
#define TIMER_NOT_RUNNING   TimerMessages[1]

Typedef Documentation

typedef void(* T_TimerEventHandler) (uint32_t *)

Function Documentation

time_t GetSystemTimerRes ( void  )
FUNC_RETURN ResetTimeOutTimer ( T_TimeOutTimer tot,
char  symbol,
time_t  period,
char *  text 
)
Examples:
SMTP.c.
176 {
177  /***************************************************************************
178  assign initialization parameters
179  ***************************************************************************/
180  tot-> Symbol = symbol;
181  tot-> Period = period;
182  tot-> Text = text;
183  if ((text!=NULL) && *text)
184  tot-> Length = strlen(text)-1;
185  else
186  tot-> Length = 0;
187 
188  /***************************************************************************
189  limit text length
190  ***************************************************************************/
191  if (tot-> Length < CPRINTF_PROGRESS_COL)
192  {
193  tot-> Length = CPRINTF_PROGRESS_COL - tot-> Length;
194  }
195  else
196  {
197  tot-> Length = CPRINTF_PROGRESS_COL;
198  }
199 
200  /***************************************************************************
201  reset time stamp
202  ***************************************************************************/
203  tot-> Timer = GetTimeStamp();
204  return(FR_OK);
205 }
#define NULL
Definition: net.h:126
#define CPRINTF_PROGRESS_COL
Definition: cprintf.h:218
#define GetTimeStamp()
Definition: timer.h:81
Definition: Common.h:280
int tv_elapsed ( timeval t,
time_t  s,
time_t  u,
uint16_t  retrigger 
)

Use this function to determine if a certain period of time has elapsed since a given time stamp. The desired time period can be specified in micro second or seconds or a combination of both.

Parameters
t- timestamp
s- seconds
u- micro seconds
retrigger- if true (1) timeval *t is updated to current time stamp
Returns
TRUE - time elapsed
FALSE - time not elapsed
Examples:
Blocksend.c, Ping2.c, SMTP.c, and SNTPTest.c.
287 {
288  /***************************************************************************
289  locals
290  ***************************************************************************/
291  timeval e;
292  time_t sec = s + (u/1000000);
293  time_t usec = u - ((u/1000000)*1000000);
294 
295  if ( SystemTimerRunning == 0 )
296  {
298  } // if
299 
300  /***************************************************************************
301  tv_interval() requires RecentEpoch > t. If the RecentEpoch was modified
302  (e.g. by NTP service or manual setting), prevent tv_interval() being called
303  and return TRUE to force update
304  ***************************************************************************/
305  if ( RecentEpoch.tv_sec < t->tv_sec )
306  {
307  if (retrigger) *t = RecentEpoch;
308  return (TRUE);
309  } // if
310 
311  tv_interval (&e, t, &RecentEpoch);
312 
313  if ( ( e.tv_sec >= sec ) && (e.tv_usec >= usec) )
314  {
315  if (retrigger) *t = RecentEpoch;
316  return (TRUE);
317  } // if
318 
319  return (FALSE);
320 }
#define prg_exit(s)
Definition: Common.h:267
static int SystemTimerRunning
Definition: timer.c:83
void tv_interval(timeval *e, timeval *t1, timeval *t2)
compute elapsed time
Definition: timer.c:336
timeval RecentEpoch
Definition: timer.c:81
#define TIMER_NOT_RUNNING
Definition: timer.h:134
Definition: timer.h:75
time_t tv_sec
Definition: timer.h:77
void tv_interval ( timeval e,
timeval t1,
timeval t2 
)

tv_interval() computes the elapsed time between two time values. All time values have micro seconds resolution. A carry-over is taken into account.

Parameters
e- elapsed time [OUT]
t1- start time [IN]
t2- end time [IN]
Returns
-
Note
precondition t2 > t1
Examples:
Blocksend.c, Chat.c, DNSTest.c, Echo.c, FTPClient.c, FTPServer.c, HTTPsimple.c, Multicast.c, NetTest.c, Ping.c, Ping2.c, PServer.c, Receive.c, Send.c, SMTP.c, and Telnet.c.
337 {
338  /***************************************************************************
339  locals
340  ***************************************************************************/
341  time_t t;
342  time_t carry = (time_t)0;
343 
344  /***************************************************************************
345  compute seconds
346  ***************************************************************************/
347  t= t2->tv_sec - t1->tv_sec;
348 
349  /***************************************************************************
350  check for carry
351  ***************************************************************************/
352  if ( t2->tv_usec < t1->tv_usec )
353  {
354  /***********************************************************************
355  correct seconds, carry micro seconds
356  ***********************************************************************/
357  t--;
358  carry = 1000000;
359  }
360 
361  /***************************************************************************
362  write back seconds
363  ***************************************************************************/
364  e->tv_sec = t;
365 
366  /***************************************************************************
367  build time difference in micro seconds
368  ***************************************************************************/
369  t = carry + t2->tv_usec-t1->tv_usec;
370 
371  /***************************************************************************
372  write back micro seconds
373  ***************************************************************************/
374  e->tv_usec = t;
375 
376 }
time_t tv_sec
Definition: timer.h:77
time_t tv_usec
Definition: timer.h:78
void SetupSystemTime ( int32_t  cpuint,
int  port,
time_t  resolution 
)

The system time in this application starts at compile date (see BUILD_XXX macros). For higher precision time use NTP protocol or a Real Time Clock.

Parameters
cpuint- cpu interrupt to use
port- timer to use
resolution- timer resolution
Returns
-
Examples:
Blocksend.c, Chat.c, DHCPTest.c, DNSTest.c, Echo.c, FTPClient.c, FTPServer.c, HTTPdynamic.c, HTTPjava.c, HTTPsimple.c, Multicast.c, NetTest.c, Ping.c, Ping2.c, PServer.c, Receive.c, Send.c, SMTP.c, SNTPTest.c, and Telnet.c.
393 {
394  /***************************************************************************
395  locals
396  ***************************************************************************/
397  struct tm *local_time; /* structure to hold time and date */
398 
399  /***************************************************************************
400  range checking
401  ***************************************************************************/
402  if ( resolution > RES_SECONDS )
403  {
405  } // if
406 
407  /***************************************************************************
408  save desired resolution
409  ***************************************************************************/
410  usec_inc = resolution;
411 
412  /***************************************************************************
413  initialize local time (use BUILD_ macros from Common.h)
414  ***************************************************************************/
415  local_time = localtime (NULL);
416  local_time->tm_year = BUILD_YEAR-1900;
417  local_time->tm_mon = BUILD_MONTH-1;
418  local_time->tm_mday = BUILD_DAY;
419  local_time->tm_hour = BUILD_HOUR;
420  local_time->tm_min = BUILD_MINUTE;
421  local_time->tm_sec = BUILD_SECOND;
422  RecentEpoch.tv_sec = mktime (local_time);
423  local_time = localtime ((const time_t *)&RecentEpoch);
424 
425  /***************************************************************************
426  initialize Timer to desired clock period
427  ***************************************************************************/
429  {
431  }
432 
433  /***************************************************************************
434  plug in interrupt handler
435  ***************************************************************************/
437 }
#define prg_exit(s)
Definition: Common.h:267
#define BUILD_YEAR
Definition: Common.h:129
int BoardInitializeTimer(uint32_t timer_clock, int port, uint32_t utime)
Initialize timer.
#define BUILD_MONTH
Definition: Common.h:138
static time_t usec_inc
Definition: timer.c:82
void BoardPlugTimerHandler(int32_t cpuint, int port, void(*eventTimerHandler)(void))
install timer interrupt handler
#define BUILD_MINUTE
Definition: Common.h:151
#define TIMER_RANGE_ERROR_STRING
Definition: timer.h:133
#define NULL
Definition: net.h:126
uint32_t TimerClock
Definition: BoardSpecific.c:123
#define BUILD_SECOND
Definition: Common.h:152
timeval RecentEpoch
Definition: timer.c:81
#define TIMER_HANDLER_FUNCTION
Definition: BoardSpecific.h:146
#define BUILD_DAY
Definition: Common.h:134
#define BUILD_HOUR
Definition: Common.h:150
#define RES_SECONDS
Definition: timer.h:70
time_t tv_sec
Definition: timer.h:77
void StartSystemTimer ( void  )
Parameters
-
Returns
-
Examples:
Blocksend.c, Chat.c, DHCPTest.c, DNSTest.c, Echo.c, FTPClient.c, FTPServer.c, HTTPdynamic.c, HTTPjava.c, HTTPsimple.c, Multicast.c, NetTest.c, Ping.c, Ping2.c, PServer.c, Receive.c, Send.c, SMTP.c, SNTPTest.c, and Telnet.c.
448 {
450  SystemTimerRunning = 1;
451 }
static int SystemTimerRunning
Definition: timer.c:83
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
void BoardStartTimer(int port)
start timer
void StopSystemTimer ( void  )
Parameters
-
Returns
-
Examples:
BoardSpecific.c.
462 {
464  SystemTimerRunning = 0;
465 }
static int SystemTimerRunning
Definition: timer.c:83
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
void BoardStopTimer(int port)
stop timer
void RegisterTimerEvent ( T_TimerEventHandler  EventHandlerFunc,
time_t  sec,
time_t  usec 
)
Parameters
EventHandlerFunc- event handler callback function
sec- seconds the timer is fired
usec- micro seconds the timer is fired
477 {
478  /***************************************************************************
479  locals
480  ***************************************************************************/
481  T_SchedulerRecord *pNewRecord, *pActiveRecord;
482 
483  /***************************************************************************
484  Create a new task record
485  ***************************************************************************/
486  pNewRecord = (T_SchedulerRecord *)calloc(1,sizeof(T_SchedulerRecord));
487  if(pNewRecord != NULL)
488  {
489  pNewRecord->id = ++gTaskId; /* assign unique ID */
490  pNewRecord->sec = sec; /* seconds to fire the timer */
491  pNewRecord->usec = usec; /* micro seconds to fire the timer */
492  pNewRecord->control = TR_CONTROL_ACTIVE; /* switch event to active */
493 
494  /***********************************************************************
495  Assign the event handler function to the task
496  ***********************************************************************/
497  pNewRecord->EventHandler = EventHandlerFunc;
498 
499  if(gpSchedulerHead == NULL)
500  {
501  /*******************************************************************
502  first record in the event list
503  *******************************************************************/
504  gpSchedulerHead = pNewRecord;
505  }
506  else
507  {
508  /*******************************************************************
509  Move to the last event in the list
510  *******************************************************************/
511  pActiveRecord = gpSchedulerHead;
512  while(pActiveRecord->pNext != NULL)
513  {
514  pActiveRecord = pActiveRecord->pNext;
515  }
516 
517  /*******************************************************************
518  Append the new event to the end of the list
519  *******************************************************************/
520  pActiveRecord->pNext = pNewRecord;
521  }
522  }
523 }
#define NULL
Definition: net.h:126
time_t sec
Definition: timer.h:110
T_TimerEventHandler EventHandler
Definition: timer.h:113
Definition: timer.h:104
time_t usec
Definition: timer.h:111
uint16_t gTaskId
Definition: timer.c:86
static T_SchedulerRecord * gpSchedulerHead
Definition: timer.c:85
#define TR_CONTROL_ACTIVE
Definition: timer.h:107
uint16_t control
Definition: timer.h:106
struct SchedulerRecord * pNext
Definition: timer.h:114
uint16_t id
Definition: timer.h:108

Variable Documentation

timeval RecentEpoch
Examples:
Send.c, SMTP.c, SNTPTest.c, and Telnet.c.
const char* TimerMessages[]
TIMER_HANDLER_INTERRUPT
Parameters
-
Returns
nothing
Note
uses global variable RecentEpoch