Orocos Real-Time Toolkit  2.9.0
fosi.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag:
3 
4  fosi.hpp - description
5  -------------------
6  begin : Jan 21 2006
7  copyright : (C) 2006 Klaas Gadeyne
8  email : firstname lastname at fmtc be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 #ifndef RTT_ECOS__FOSI_H
29 #define RTT_ECOS__FOSI_H
30 
31 #define HAVE_FOSI_API
32 
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif
37 
38 #include <stdio.h>
39 #include <cyg/kernel/kapi.h>
40  // #include <errno.h>
41 #include "os_ecos.h"
42 #include <pkgconf/kernel.h>
43 #include <cyg/infra/diag.h> // for diag_printf
44 
45  // Own implementation of recursive mutexes
46 #include "ecos_rec_mutex.h"
47 
48 #define SCHED_ECOS_FIFO 0
49 #define ORO_SCHED_RT 0
50 #define ORO_SCHED_OTHER 0
51 
52 #define ORO_WAIT_ABS 0
53 #define ORO_WAIT_REL 1
54 
55  typedef long long NANO_TIME;
56  typedef cyg_tick_count_t TICK_TIME;
57 
58  const TICK_TIME InfiniteTicks = ULONG_LONG_MAX;
59  const NANO_TIME InfiniteNSecs = LONG_LONG_MAX;
60  const double InfiniteSeconds = DBL_MAX;
61 
62  typedef struct {
63  // the thread
64  cyg_thread thread;
65  // its name
66  char * name;
67 
68  // And its handle
69  cyg_handle_t handle;
70 
71  // Stack pointer
72  char * stack;
73 
74  /* bool to fake soft or hard RT behaviour (ecos does not
75  differentiate between hard and soft realtime)
76  */
77  bool hrt;
78 
79  // STUFF for periodic threads (ecos has no native API for creating
80  // periodic threads)
81  // Next firetime
82  NANO_TIME periodMark;
83  // the period
84  NANO_TIME period;
85  cyg_handle_t counter_hdl;
86  cyg_handle_t sys_clk_hdl;
87  cyg_handle_t alarm_hdl;
88  cyg_alarm alarm_obj;
89  cyg_sem_t wakeup_sem;
90  } RTOS_TASK;
91 
92 
93  // Time Related
94 #include <time.h>
95 #include <unistd.h>
96 
97  typedef struct timespec TIME_SPEC;
98 
99  inline
100  TICK_TIME nano2ticks( NANO_TIME nano )
101  {
102  // FIXME need more efficient calculation...
103  return (CYGNUM_HAL_RTC_DENOMINATOR*nano)/CYGNUM_HAL_RTC_NUMERATOR;
104  }
105 
106  inline
107  NANO_TIME ticks2nano( TICK_TIME count )
108  {
109  // FIXME need more efficient calculation...
110  return CYGNUM_HAL_RTC_NUMERATOR/CYGNUM_HAL_RTC_DENOMINATOR*count;
111  }
112 
113  inline NANO_TIME rtos_get_time_ns( void )
114  {
115  return ticks2nano(cyg_current_time());
116  }
117 
118  inline TICK_TIME rtos_get_time_ticks( void )
119  {
120  return cyg_current_time();
121  }
122 
123  /*
124  inline int rtos_nanosleep( const TIME_SPEC * rqtp, TIME_SPEC * rmtp )
125  {
126  // return usleep(rqtp->tv_nsec/1000L);
127  return nanosleep( rqtp, rmtp );
128  }
129  */
130 
131  typedef cyg_sem_t rt_sem_t;
132 
133  static inline int rtos_sem_init(rt_sem_t* m, int value )
134  {
135  cyg_semaphore_init(m, value);
136  return 0;
137  }
138 
139  static inline int rtos_sem_destroy(rt_sem_t* m )
140  {
141  cyg_semaphore_destroy(m);
142  return 0;
143  }
144 
145  static inline int rtos_sem_signal(rt_sem_t* m )
146  {
147  cyg_semaphore_post(m);
148  return 0;
149  }
150 
151  static inline int rtos_sem_wait(rt_sem_t* m )
152  {
153  cyg_semaphore_wait(m);
154  return 0;
155  }
156 
157  // Should return 0 if no timeout occurs
158  static inline int rtos_sem_trywait(rt_sem_t* m )
159  {
160  if (cyg_semaphore_trywait(m) == true)
161  return 0;
162  else
163  return -1;
164  }
165 
166  // Should return 0 if no timeout occurs
167  static inline int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay )
168  {
169  // cyg_semaphore_timed_wait returns true if no timeout occurs
170  if (cyg_semaphore_timed_wait(m,cyg_current_time()+nano2ticks(delay)) == true)
171  return 0;
172  else
173  return -1;
174  }
175 
176  static inline int rtos_sem_value(rt_sem_t* m )
177  {
178  int val = 0;
179  cyg_semaphore_peek(m, &val);
180  return val;
181  }
182 
183  // Mutex functions
184 
185  typedef cyg_mutex_t rt_mutex_t;
187 
188  //
189  static inline int rtos_mutex_init(rt_mutex_t* m)
190  {
191  cyg_mutex_init(m);
192  return 0;
193  }
194 
195  static inline int rtos_mutex_destroy(rt_mutex_t* m )
196  {
197  cyg_mutex_release(m);
198  cyg_mutex_destroy(m);
199  return 0;
200  }
201 
202  static inline int rtos_mutex_rec_init(rt_rec_mutex_t* m)
203  {
205  return 0;
206  }
207 
208  static inline int rtos_mutex_rec_destroy(rt_rec_mutex_t* m )
209  {
211  return 0;
212  }
213 
214  static inline int rtos_mutex_lock( rt_mutex_t* m)
215  {
216  return cyg_mutex_lock(m);
217  }
218 
219  static inline int rtos_mutex_rec_lock( rt_rec_mutex_t* m)
220  {
221  return cyg_recursive_mutex_lock(m);
222  }
223 
224  static inline int rtos_mutex_trylock( rt_mutex_t* m)
225  {
226  if (cyg_mutex_trylock(m) == true)
227  return 0;
228  else
229  return -1;
230  }
231 
232  static inline int rtos_mutex_rec_trylock( rt_rec_mutex_t* m)
233  {
234  if (cyg_recursive_mutex_trylock(m) == true)
235  return 0;
236  else
237  return -1;
238  }
239 
240  static inline int rtos_mutex_unlock( rt_mutex_t* m)
241  {
242  cyg_mutex_unlock(m);
243  return 0;
244  }
245 
246  static inline int rtos_mutex_rec_unlock( rt_rec_mutex_t* m)
247  {
249  return 0;
250  }
251 
252  static inline void rtos_enable_rt_warning()
253  {
254  }
255 
256  static inline void rtos_disable_rt_warning()
257  {
258  }
259 
260 #define rtos_printf diag_printf
261 
262 #ifdef __cplusplus
263 }
264 
265 #endif
266 #endif
int rtos_mutex_init(rt_mutex_t *m)
long long NANO_TIME
Definition: fosi.h:55
const double InfiniteSeconds
Definition: fosi.h:60
int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
const TICK_TIME InfiniteTicks
Definition: fosi.h:58
cyg_handle_t handle
Definition: fosi.h:69
bool hrt
Definition: fosi.h:77
NANO_TIME rtos_get_time_ns(void)
Get "system" time in nanoseconds.
Definition: fosi.h:113
cyg_handle_t sys_clk_hdl
Definition: fosi.h:86
const NANO_TIME InfiniteNSecs
Definition: fosi.h:59
char * name
Definition: fosi.h:66
int rtos_mutex_rec_init(rt_rec_mutex_t *m)
int rtos_sem_trywait(rt_sem_t *m)
int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
int rtos_sem_signal(rt_sem_t *m)
cyg_sem_t wakeup_sem
Definition: fosi.h:89
void cyg_recursive_mutex_destroy(cyg_recursive_mutex_t *mx)
Destroy.
cyg_thread thread
Definition: fosi.h:64
void cyg_recursive_mutex_unlock(cyg_recursive_mutex_t *mx)
Unlock.
cyg_tick_count_t TICK_TIME
Definition: fosi.h:56
NANO_TIME periodMark
Definition: fosi.h:82
int rtos_mutex_unlock(rt_mutex_t *m)
cyg_alarm alarm_obj
Definition: fosi.h:88
int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
void rtos_disable_rt_warning()
Disallows the RTOS to print a warning when we violate real-time constraints.
void cyg_recursive_mutex_init(cyg_recursive_mutex_t *mx)
Initialize recursive mutex.
int rtos_sem_destroy(rt_sem_t *m)
int rtos_mutex_lock(rt_mutex_t *m)
TICK_TIME rtos_get_time_ticks(void)
Get "system" time in ticks FIXME see https://proj.fmtc.be/orocos-bugzilla/show_bug.cgi?id=60
Definition: fosi.h:118
char * stack
Definition: fosi.h:72
int rtos_mutex_trylock(rt_mutex_t *m)
NANO_TIME ticks2nano(TICK_TIME count)
Time conversions from system ticks to nano seconds.
Definition: fosi.h:107
cyg_recursive_mutex_t rt_rec_mutex_t
Definition: fosi.h:186
int rtos_sem_init(rt_sem_t *m, int value)
All these should return zero in case of succes.
TICK_TIME nano2ticks(NANO_TIME nano)
Time conversions from nano seconds to system ticks.
Definition: fosi.h:100
cyg_handle_t alarm_hdl
Definition: fosi.h:87
bool cyg_recursive_mutex_lock(cyg_recursive_mutex_t *mx)
Lock recursive mutex.
int rtos_mutex_destroy(rt_mutex_t *m)
int rtos_sem_value(rt_sem_t *m)
NANO_TIME period
Definition: fosi.h:84
struct MyTask RTOS_TASK
cyg_handle_t counter_hdl
Definition: fosi.h:85
void rtos_enable_rt_warning()
Allows the RTOS to print a warning when we violate real-time constraints.
struct timespec TIME_SPEC
Definition: fosi.h:97
int rtos_sem_wait(rt_sem_t *m)
bool cyg_recursive_mutex_trylock(cyg_recursive_mutex_t *mx)
Trylock.
cyg_sem_t rt_sem_t
Definition: fosi.h:131
int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)
cyg_mutex_t rt_mutex_t
Definition: fosi.h:185