rtk/osal.c
Go to the documentation of this file.
1 /******************************************************************************
2  * * *** ***
3  * *** *** ***
4  * *** **** ********** *** ***** *** **** *****
5  * ********* ********** *** ********* ************ *********
6  * **** *** *** *** *** **** ***
7  * *** *** ****** *** *********** *** **** *****
8  * *** *** ****** *** ************* *** **** *****
9  * *** **** **** *** *** *** **** ***
10  * *** ******* ***** ************** ************* *********
11  * *** ***** *** ******* ** ** ****** *****
12  * t h e r e a l t i m e t a r g e t e x p e r t s
13  *
14  * http://www.rt-labs.com
15  * Copyright (C) 2009. rt-labs AB, Sweden. All rights reserved.
16  *------------------------------------------------------------------------------
17  * $Id: osal.c 452 2013-02-26 21:02:58Z smf.arthur $
18  *------------------------------------------------------------------------------
19  */
20 
21 #include <osal.h>
22 #include <kern.h>
23 #include <time.h>
24 #include <sys/time.h>
25 #include <config.h>
26 
27 #define timercmp(a, b, CMP) \
28  (((a)->tv_sec == (b)->tv_sec) ? \
29  ((a)->tv_usec CMP (b)->tv_usec) : \
30  ((a)->tv_sec CMP (b)->tv_sec))
31 #define timeradd(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 >= 1000000) \
36  { \
37  ++(result)->tv_sec; \
38  (result)->tv_usec -= 1000000; \
39  } \
40  } while (0)
41 #define timersub(a, b, result) \
42  do { \
43  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
44  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
45  if ((result)->tv_usec < 0) { \
46  --(result)->tv_sec; \
47  (result)->tv_usec += 1000000; \
48  } \
49  } while (0)
50 
51 #define USECS_PER_SEC 1000000
52 #define USECS_PER_TICK (USECS_PER_SEC / CFG_TICKS_PER_SECOND)
53 
54 
55 /* Workaround for rt-labs defect 776.
56  * Default implementation of udelay() didn't work correctly when tick was
57  * shorter than one millisecond.
58  */
59 void udelay (uint32_t us)
60 {
61  tick_t ticks = (us / USECS_PER_TICK) + 1;
62  task_delay (ticks);
63 }
64 
65 int gettimeofday(struct timeval *tp, void *tzp)
66 {
67  tick_t tick = tick_get();
68  tick_t ticks_left;
69 
70  ASSERT (tp != NULL);
71 
72  tp->tv_sec = tick / CFG_TICKS_PER_SECOND;
73 
74  ticks_left = tick % CFG_TICKS_PER_SECOND;
75  tp->tv_usec = ticks_left * USECS_PER_TICK;
76  ASSERT (tp->tv_usec < USECS_PER_SEC);
77 
78  return 0;
79 }
80 
81 int osal_usleep (uint32 usec)
82 {
83  udelay(usec);
84  return 0;
85 }
86 
87 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
88 {
89  return gettimeofday(tv, tz);
90 }
91 
93 {
94  struct timeval current_time;
95  ec_timet return_value;
96 
97  gettimeofday (&current_time, 0);
98  return_value.sec = current_time.tv_sec;
99  return_value.usec = current_time.tv_usec;
100  return return_value;
101 }
102 
103 void osal_timer_start (osal_timert * self, uint32 timeout_usec)
104 {
105  struct timeval start_time;
106  struct timeval timeout;
107  struct timeval stop_time;
108 
109  gettimeofday (&start_time, 0);
110  timeout.tv_sec = timeout_usec / USECS_PER_SEC;
111  timeout.tv_usec = timeout_usec % USECS_PER_SEC;
112  timeradd (&start_time, &timeout, &stop_time);
113 
114  self->stop_time.sec = stop_time.tv_sec;
115  self->stop_time.usec = stop_time.tv_usec;
116 }
117 
119 {
120  struct timeval current_time;
121  struct timeval stop_time;
122  int is_not_yet_expired;
123 
124  gettimeofday (&current_time, 0);
125  stop_time.tv_sec = self->stop_time.sec;
126  stop_time.tv_usec = self->stop_time.usec;
127  is_not_yet_expired = timercmp (&current_time, &stop_time, <);
128 
129  return is_not_yet_expired == false;
130 }
131 
void udelay(uint32_t us)
Definition: rtk/osal.c:59
boolean osal_timer_is_expired(osal_timert *self)
Definition: rtk/osal.c:118
ec_timet osal_current_time(void)
Definition: rtk/osal.c:92
Definition: osal.h:53
unsigned int uint32_t
Definition: stdint.h:80
#define USECS_PER_SEC
Definition: rtk/osal.c:51
#define timercmp(a, b, CMP)
Definition: rtk/osal.c:27
#define timeradd(a, b, result)
Definition: rtk/osal.c:31
int osal_usleep(uint32 usec)
Definition: rtk/osal.c:81
uint32_t uint32
Definition: osal.h:35
int gettimeofday(struct timeval *tp, void *tzp)
Definition: rtk/osal.c:65
int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: rtk/osal.c:87
void osal_timer_start(osal_timert *self, uint32 timeout_usec)
Definition: rtk/osal.c:103
#define USECS_PER_TICK
Definition: rtk/osal.c:52


youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Jun 10 2019 15:46:24