PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
esos_pc_tick.c
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 
31 /************************************************************************
32  * esos_pc_tick.c
33  ************************************************************************
34  * User provided (HW-specific) ES_OS routines for IRQs
35  */
36 
37 #ifdef _WIN32
38 #include <windows.h>
39 #else
40 #include <unistd.h>
41 #include <sys/time.h>
42 #endif
43 
44 #include "all_generic.h"
45 
46 // PROTOTYPE our private little helper functions
47 uint32_t clock_time(void);
48 
49 // create a variable to save the initial clock time in
50 static uint32_t initClockCount;
51 
52 /*
53  * User must provide the HW-specific routine to setup a system
54  * tick for ES_OS.
55  *
56 
57  */
58 void __esos_hw_InitSystemTick(void) {
59  /*
60  * save the time.h clock() count when the system inits
61  * so we can compute elapsed time when the user apps
62  * and the ES_OS request it later via esos_GetSystemTick()
63  */
64  initClockCount = clock_time();
65 
66 } // end _esos_hw_InitSystemTick()
67 
68 /*
69  * User must provide the HW-specific routine to return the 32 bit
70  * system tick to ES_OS and user tasks.
71  *
72  * get the current time and compute delta back to init time
73  */
74 uint32_t __esos_hw_GetSystemTickCount(void) {
75  return (clock_time()-initClockCount);
76 } // end _esos_hw_GetSystemTickCount()
77 
78 uint32_t clock_time(void) {
79  struct timeval tv;
80  struct timezone tz;
81 
82  // gettimeofday() returns time since the epoch.....
83  // in sec and usec members
84  gettimeofday(&tv, &tz);
85  // give the user time count in milliseconds
86  return ((tv.tv_sec * 1000) +(tv.tv_usec / 1000));
87 }