rtk/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 <osal.h>
7 #include <kern.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <config.h>
11 
12 #define timercmp(a, b, CMP) \
13  (((a)->tv_sec == (b)->tv_sec) ? \
14  ((a)->tv_usec CMP (b)->tv_usec) : \
15  ((a)->tv_sec CMP (b)->tv_sec))
16 #define timeradd(a, b, result) \
17  do { \
18  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
19  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
20  if ((result)->tv_usec >= 1000000) \
21  { \
22  ++(result)->tv_sec; \
23  (result)->tv_usec -= 1000000; \
24  } \
25  } while (0)
26 #define timersub(a, b, result) \
27  do { \
28  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
29  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
30  if ((result)->tv_usec < 0) { \
31  --(result)->tv_sec; \
32  (result)->tv_usec += 1000000; \
33  } \
34  } while (0)
35 
36 #define USECS_PER_SEC 1000000
37 #define USECS_PER_TICK (USECS_PER_SEC / CFG_TICKS_PER_SECOND)
38 
39 
40 /* Workaround for rt-labs defect 776.
41  * Default implementation of udelay() didn't work correctly when tick was
42  * shorter than one millisecond.
43  */
44 void udelay (uint32_t us)
45 {
46  tick_t ticks = (us / USECS_PER_TICK) + 1;
47  task_delay (ticks);
48 }
49 
50 int gettimeofday(struct timeval *tp, void *tzp)
51 {
52  tick_t tick = tick_get();
53  tick_t ticks_left;
54 
55  ASSERT (tp != NULL);
56 
57  tp->tv_sec = tick / CFG_TICKS_PER_SECOND;
58 
59  ticks_left = tick % CFG_TICKS_PER_SECOND;
60  tp->tv_usec = ticks_left * USECS_PER_TICK;
61  ASSERT (tp->tv_usec < USECS_PER_SEC);
62 
63  return 0;
64 }
65 
66 int osal_usleep (uint32 usec)
67 {
68  udelay(usec);
69  return 0;
70 }
71 
72 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
73 {
74  return gettimeofday(tv, tz);
75 }
76 
78 {
79  struct timeval current_time;
80  ec_timet return_value;
81 
82  gettimeofday (&current_time, 0);
83  return_value.sec = current_time.tv_sec;
84  return_value.usec = current_time.tv_usec;
85  return return_value;
86 }
87 
88 void osal_timer_start (osal_timert * self, uint32 timeout_usec)
89 {
90  struct timeval start_time;
91  struct timeval timeout;
92  struct timeval stop_time;
93 
94  gettimeofday (&start_time, 0);
95  timeout.tv_sec = timeout_usec / USECS_PER_SEC;
96  timeout.tv_usec = timeout_usec % USECS_PER_SEC;
97  timeradd (&start_time, &timeout, &stop_time);
98 
99  self->stop_time.sec = stop_time.tv_sec;
100  self->stop_time.usec = stop_time.tv_usec;
101 }
102 
104 {
105  struct timeval current_time;
106  struct timeval stop_time;
107  int is_not_yet_expired;
108 
109  gettimeofday (&current_time, 0);
110  stop_time.tv_sec = self->stop_time.sec;
111  stop_time.tv_usec = self->stop_time.usec;
112  is_not_yet_expired = timercmp (&current_time, &stop_time, <);
113 
114  return is_not_yet_expired == false;
115 }
116 
117 void *osal_malloc(size_t size)
118 {
119  return malloc(size);
120 }
121 
122 void osal_free(void *ptr)
123 {
124  free(ptr);
125 }
126 
127 int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
128 {
129  thandle = task_spawn ("worker", func, 6,stacksize, param);
130  if(!thandle)
131  {
132  return 0;
133  }
134  return 1;
135 }
136 
137 int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
138 {
139  thandle = task_spawn ("worker_rt", func, 15 ,stacksize, param);
140  if(!thandle)
141  {
142  return 0;
143  }
144  return 1;
145 }
void udelay(uint32_t us)
Definition: rtk/osal.c:44
boolean osal_timer_is_expired(osal_timert *self)
Definition: rtk/osal.c:103
ec_timet osal_current_time(void)
Definition: rtk/osal.c:77
void osal_free(void *ptr)
Definition: rtk/osal.c:122
int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
Definition: rtk/osal.c:137
Definition: osal.h:36
unsigned int uint32_t
Definition: stdint.h:80
#define USECS_PER_SEC
Definition: rtk/osal.c:36
#define timercmp(a, b, CMP)
Definition: rtk/osal.c:12
#define timeradd(a, b, result)
Definition: rtk/osal.c:16
int osal_usleep(uint32 usec)
Definition: rtk/osal.c:66
int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
Definition: rtk/osal.c:127
void * osal_malloc(size_t size)
Definition: rtk/osal.c:117
uint32_t uint32
Definition: osal.h:30
int gettimeofday(struct timeval *tp, void *tzp)
Definition: rtk/osal.c:50
int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: rtk/osal.c:72
void osal_timer_start(osal_timert *self, uint32 timeout_usec)
Definition: rtk/osal.c:88
#define USECS_PER_TICK
Definition: rtk/osal.c:37


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 27 2020 03:48:21