timer.c File Reference

timer support function More...

#include "..\includes.h"

Functions

 if (RecentEpoch.tv_usec >=1000000)
 
 while (pActiveRecord)
 
 AckTimerInterrupt ()
 
time_t GetSystemTimerRes (void)
 
FUNC_RETURN ResetTimeOutTimer (T_TimeOutTimer *tot, char symbol, time_t period, char *text)
 
FUNC_RETURN ProcessTimeout (T_TimeOutTimer *tot, time_t timeout)
 
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 = {(time_t)0,(time_t)0}
 
static time_t usec_inc = (time_t)1
 
static int SystemTimerRunning = 0
 
static T_SchedulerRecordgpSchedulerHead = NULL
 
uint16_t gTaskId = 1
 
const char * TimerMessages []
 
 TIMER_HANDLER_INTERRUPT
 timer interrupt function More...
 
RecentEpoch tv_usec = usec_inc
 

Detailed Description

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

Function Documentation

if ( RecentEpoch.tv_usec >=  1000000)
Examples:
Echo.c, and NetTest.c.
125  {
126  RecentEpoch.tv_usec = (time_t)0;
128  }
timeval RecentEpoch
Definition: timer.c:81
time_t tv_sec
Definition: timer.h:77
time_t tv_usec
Definition: timer.h:78
while ( pActiveRecord  )
134  {
135  if (tv_elapsed (&(pActiveRecord->timer), pActiveRecord->sec, pActiveRecord->usec, pActiveRecord->control&TR_CONTROL_ACTIVE) )
136  {
137  pActiveRecord->EventHandler((uint32_t*)pActiveRecord);
138  // if(pActiveRecord->control&TR_CONTROL_SINGLE)
139  // {
140  // }
141  }
142 
143  /***********************************************************************
144  switch to next task
145  ***********************************************************************/
146  pActiveRecord = pActiveRecord->pNext;
147 }
int tv_elapsed(timeval *t, time_t s, time_t u, uint16_t retrigger)
check if given time has elapsed
Definition: timer.c:286
unsigned int uint32_t
Definition: stdint.h:47
#define TR_CONTROL_ACTIVE
Definition: timer.h:107
AckTimerInterrupt ( )
time_t GetSystemTimerRes ( void  )
161 {
162  return (usec_inc);
163 }
static time_t usec_inc
Definition: timer.c:82
FUNC_RETURN ResetTimeOutTimer ( T_TimeOutTimer tot,
char  symbol,
time_t  period,
char *  text 
)
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
FUNC_RETURN ProcessTimeout ( T_TimeOutTimer tot,
time_t  timeout 
)
Examples:
SMTP.c.
220 {
221  FUNC_RETURN f_result = FR_OK; // test result
222 #if (defined(INC_UART) || defined(INC_CDC))
223  char c;
224 #endif
225 
226 #ifdef INC_UART
228  {
229  if ( BoardUartReadChar ( &c) ) // read from UART to skip if necessary
230  {
231  return (f_result = FR_SKIPPED);
232  }
233  }
234 #endif
235 #ifdef INC_CDC
236  if (output_device & CPRINTF_CDC_OUTPUT)
237  {
238  if ( CDC_read ( &c) ) // read from CDC to skip if necessary
239  {
240  return (f_result = FR_SKIPPED);
241  }
242  }
243 #endif
244  /***************************************************************************
245  reset timeout
246  ***************************************************************************/
247  if ( timeout == 0 )
248  {
249  tot-> Timer = GetTimeStamp(); // reset timeout
250  }
251 
252  /***************************************************************************
253  process a timeout
254  ***************************************************************************/
255  if ( timeout && tv_elapsed (&(tot-> Timer), timeout, 0, TRUE) )
256  {
257  f_result = FR_TIMEDOUT;
258  }
259  else
260  {
261  /***********************************************************************
262  perform progress bar animation
263  ***********************************************************************/
264  CPrintProgressBarText (tot-> Symbol, tot-> Period, tot-> Length, tot-> Text );
265  }
266 
267  return (f_result);
268 }
uint16_t output_device
Select active output device.
Definition: cprintf.c:110
Definition: Common.h:281
int tv_elapsed(timeval *t, time_t s, time_t u, uint16_t retrigger)
check if given time has elapsed
Definition: timer.c:286
int32_t BoardUartReadChar(char *c)
read one character from the UART
Definition: Common.h:283
#define GetTimeStamp()
Definition: timer.h:81
FUNC_RETURN
Definition: Common.h:279
Definition: Common.h:280
#define CPRINTF_UART_OUTPUT
Definition: cprintf.h:83
int32_t CPrintProgressBarText(char c, time_t period, size_t length, char *text)
Definition: cprintf.c:902
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
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
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
-
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
-
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
-
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 = {(time_t)0,(time_t)0}
time_t usec_inc = (time_t)1
static
int SystemTimerRunning = 0
static
T_SchedulerRecord* gpSchedulerHead = NULL
static
uint16_t gTaskId = 1
const char* TimerMessages[]
Initial value:
= {
" Timer out of range error",
" System timer not running!",
}
TIMER_HANDLER_INTERRUPT
Initial value:
{
Definition: timer.h:104
static T_SchedulerRecord * gpSchedulerHead
Definition: timer.c:85
Parameters
-
Returns
nothing
Note
uses global variable RecentEpoch
RecentEpoch tv_usec = usec_inc