vxworks/osal.c
Go to the documentation of this file.
1 /*
2  * Licensed under the GNU General Public License version 2 with exceptions. See
3  * LICENSE file in the project root for full license information
4  */
5 
6 #include <time.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <osal.h>
13 #include <vxWorks.h>
14 #include <taskLib.h>
15 
16 
17 #define timercmp(a, b, CMP) \
18  (((a)->tv_sec == (b)->tv_sec) ? \
19  ((a)->tv_usec CMP (b)->tv_usec) : \
20  ((a)->tv_sec CMP (b)->tv_sec))
21 #define timeradd(a, b, result) \
22  do { \
23  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
24  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
25  if ((result)->tv_usec >= 1000000) \
26  { \
27  ++(result)->tv_sec; \
28  (result)->tv_usec -= 1000000; \
29  } \
30  } while (0)
31 #define timersub(a, b, result) \
32  do { \
33  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
34  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
35  if ((result)->tv_usec < 0) { \
36  --(result)->tv_sec; \
37  (result)->tv_usec += 1000000; \
38  } \
39  } while (0)
40 
41 #define USECS_PER_SEC 1000000
42 
43 /* OBS! config worker threads must have higher prio that task running ec_configuration */
44 #define ECAT_TASK_PRIO_HIGH 20 /* Priority for high performance network task */
45 #define ECAT_TASK_PRIO_LOW 80 /* Priority for high performance network task */
46 #define ECAT_STACK_SIZE 10000 /* Stack size for high performance task */
47 static int ecatTaskOptions = VX_SUPERVISOR_MODE | VX_UNBREAKABLE;
48 static int ecatTaskIndex = 0;
49 
50 #ifndef use_task_delay
51 #define use_task_delay 1
52 #endif
53 
54 int osal_usleep (uint32 usec)
55 {
56 #if (use_task_delay == 1)
57  /* Task delay 0 only yields */
58  taskDelay(usec / 1000);
59  return 0;
60 #else
61  /* The suspension may be longer than requested due to the rounding up of
62  * the request to the timer's resolution or to other scheduling activities
63  * (e.g., a higher priority task intervenes).
64  */
65  struct timespec ts;
66  ts.tv_sec = usec / USECS_PER_SEC;
67  ts.tv_nsec = (usec % USECS_PER_SEC) * 1000;
68  return nanosleep(&ts, NULL);
69 #endif
70 
71 }
72 
73 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
74 {
75  struct timespec ts;
76  int return_value;
77  (void)tz; /* Not used */
78 
79  /* Use clock_gettime CLOCK_MONOTONIC to a avoid NTP time adjustments */
80  return_value = clock_gettime(CLOCK_MONOTONIC, &ts);
81  tv->tv_sec = ts.tv_sec;
82  tv->tv_usec = ts.tv_nsec / 1000;
83  return return_value;
84 }
85 
87 {
88  struct timeval current_time;
89  ec_timet return_value;
90 
91  osal_gettimeofday(&current_time, 0);
92  return_value.sec = current_time.tv_sec;
93  return_value.usec = current_time.tv_usec;
94  return return_value;
95 }
96 
97 void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
98 {
99  if (end->usec < start->usec) {
100  diff->sec = end->sec - start->sec - 1;
101  diff->usec = end->usec + 1000000 - start->usec;
102  }
103  else {
104  diff->sec = end->sec - start->sec;
105  diff->usec = end->usec - start->usec;
106  }
107 }
108 
109 void osal_timer_start(osal_timert * self, uint32 timeout_usec)
110 {
111  struct timeval start_time;
112  struct timeval timeout;
113  struct timeval stop_time;
114 
115  osal_gettimeofday(&start_time, 0);
116  timeout.tv_sec = timeout_usec / USECS_PER_SEC;
117  timeout.tv_usec = timeout_usec % USECS_PER_SEC;
118  timeradd(&start_time, &timeout, &stop_time);
119 
120  self->stop_time.sec = stop_time.tv_sec;
121  self->stop_time.usec = stop_time.tv_usec;
122 }
123 
125 {
126  struct timeval current_time;
127  struct timeval stop_time;
128  int is_not_yet_expired;
129 
130  osal_gettimeofday(&current_time, 0);
131  stop_time.tv_sec = self->stop_time.sec;
132  stop_time.tv_usec = self->stop_time.usec;
133  is_not_yet_expired = timercmp(&current_time, &stop_time, <);
134 
135  return is_not_yet_expired == FALSE;
136 }
137 
138 void *osal_malloc(size_t size)
139 {
140  return malloc(size);
141 }
142 
143 void osal_free(void *ptr)
144 {
145  free(ptr);
146 }
147 
148 int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
149 {
150  char task_name[20];
151  TASK_ID * tid = (TASK_ID *)thandle;
152  FUNCPTR func_ptr = func;
153  _Vx_usr_arg_t arg1 = (_Vx_usr_arg_t)param;
154 
155  snprintf(task_name,sizeof(task_name),"worker_%d",ecatTaskIndex++);
156 
157  *tid = taskSpawn (task_name, ECAT_TASK_PRIO_LOW,
159  func_ptr, arg1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
160  if(*tid == TASK_ID_ERROR)
161  {
162  return 0;
163  }
164 
165  return 1;
166 }
167 
168 int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
169 {
170  char task_name[20];
171  TASK_ID * tid = (TASK_ID *)thandle;
172  FUNCPTR func_ptr = func;
173  _Vx_usr_arg_t arg1 = (_Vx_usr_arg_t)param;
174 
175  snprintf(task_name,sizeof(task_name),"worker_rt_%d",ecatTaskIndex++);
176 
177  *tid = taskSpawn (task_name, ECAT_TASK_PRIO_HIGH,
179  func_ptr, arg1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
180 
181  if(*tid == TASK_ID_ERROR)
182  {
183  return 0;
184  }
185  return 1;
186 }
187 
static int ecatTaskIndex
Definition: vxworks/osal.c:48
void osal_free(void *ptr)
Definition: vxworks/osal.c:143
uint32 usec
Definition: osal.h:39
int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
Definition: vxworks/osal.c:168
#define timercmp(a, b, CMP)
Definition: vxworks/osal.c:17
#define timeradd(a, b, result)
Definition: vxworks/osal.c:21
int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: vxworks/osal.c:73
void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
Definition: vxworks/osal.c:97
int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
Definition: vxworks/osal.c:148
static int ecatTaskOptions
Definition: vxworks/osal.c:47
int osal_usleep(uint32 usec)
Definition: vxworks/osal.c:54
Definition: osal.h:36
ec_timet osal_current_time(void)
Definition: vxworks/osal.c:86
#define FALSE
Definition: osal.h:22
boolean osal_timer_is_expired(osal_timert *self)
Definition: vxworks/osal.c:124
#define ECAT_TASK_PRIO_LOW
Definition: vxworks/osal.c:45
void osal_timer_start(osal_timert *self, uint32 timeout_usec)
Definition: vxworks/osal.c:109
#define ECAT_STACK_SIZE
Definition: vxworks/osal.c:46
uint32 sec
Definition: osal.h:38
#define snprintf
Definition: pcap-stdinc.h:64
uint32_t uint32
Definition: osal.h:30
void * osal_malloc(size_t size)
Definition: vxworks/osal.c:138
#define ECAT_TASK_PRIO_HIGH
Definition: vxworks/osal.c:44
#define USECS_PER_SEC
Definition: vxworks/osal.c:41


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Mon Feb 28 2022 23:46:57