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


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:33