00001 /*************************************************************************** 00002 tag: 00003 00004 fosi.hpp - description 00005 ------------------- 00006 begin : Jan 21 2006 00007 copyright : (C) 2006 Klaas Gadeyne 00008 email : firstname lastname at fmtc be 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU Lesser General Public * 00013 * License as published by the Free Software Foundation; either * 00014 * version 2.1 of the License, or (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00019 * Lesser General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Lesser General Public * 00022 * License along with this library; if not, write to the Free Software * 00023 * Foundation, Inc., 59 Temple Place, * 00024 * Suite 330, Boston, MA 02111-1307 USA * 00025 * * 00026 ***************************************************************************/ 00027 00028 #ifndef RTT_ECOS__FOSI_H 00029 #define RTT_ECOS__FOSI_H 00030 00031 #define HAVE_FOSI_API 00032 00033 #ifdef __cplusplus 00034 extern "C" 00035 { 00036 #endif 00037 00038 #include <stdio.h> 00039 #include <cyg/kernel/kapi.h> 00040 // #include <errno.h> 00041 #include "os_ecos.h" 00042 #include <pkgconf/kernel.h> 00043 #include <cyg/infra/diag.h> // for diag_printf 00044 00045 // Own implementation of recursive mutexes 00046 #include "ecos_rec_mutex.h" 00047 00048 #define SCHED_ECOS_FIFO 0 00049 #define ORO_SCHED_RT 0 00050 #define ORO_SCHED_OTHER 0 00051 00052 #define ORO_WAIT_ABS 0 00053 #define ORO_WAIT_REL 1 00055 typedef long long NANO_TIME; 00056 typedef cyg_tick_count_t TICK_TIME; 00057 00058 const TICK_TIME InfiniteTicks = ULONG_LONG_MAX; 00059 const NANO_TIME InfiniteNSecs = LONG_LONG_MAX; 00060 const double InfiniteSeconds = DBL_MAX; 00061 00062 typedef struct { 00063 // the thread 00064 cyg_thread thread; 00065 // its name 00066 char * name; 00067 00068 // And its handle 00069 cyg_handle_t handle; 00070 00071 // Stack pointer 00072 char * stack; 00073 00074 /* bool to fake soft or hard RT behaviour (ecos does not 00075 differentiate between hard and soft realtime) 00076 */ 00077 bool hrt; 00078 00079 // STUFF for periodic threads (ecos has no native API for creating 00080 // periodic threads) 00081 // Next firetime 00082 NANO_TIME periodMark; 00083 // the period 00084 NANO_TIME period; 00085 cyg_handle_t counter_hdl; 00086 cyg_handle_t sys_clk_hdl; 00087 cyg_handle_t alarm_hdl; 00088 cyg_alarm alarm_obj; 00089 cyg_sem_t wakeup_sem; 00090 } RTOS_TASK; 00091 00092 00093 // Time Related 00094 #include <time.h> 00095 #include <unistd.h> 00096 00097 typedef struct timespec TIME_SPEC; 00098 00099 inline 00100 TICK_TIME nano2ticks( NANO_TIME nano ) 00101 { 00102 // FIXME need more efficient calculation... 00103 return (CYGNUM_HAL_RTC_DENOMINATOR*nano)/CYGNUM_HAL_RTC_NUMERATOR; 00104 } 00105 00106 inline 00107 NANO_TIME ticks2nano( TICK_TIME count ) 00108 { 00109 // FIXME need more efficient calculation... 00110 return CYGNUM_HAL_RTC_NUMERATOR/CYGNUM_HAL_RTC_DENOMINATOR*count; 00111 } 00112 00113 inline NANO_TIME rtos_get_time_ns( void ) 00114 { 00115 return ticks2nano(cyg_current_time()); 00116 } 00117 00118 inline TICK_TIME rtos_get_time_ticks( void ) 00119 { 00120 return cyg_current_time(); 00121 } 00122 00123 /* 00124 inline int rtos_nanosleep( const TIME_SPEC * rqtp, TIME_SPEC * rmtp ) 00125 { 00126 // return usleep(rqtp->tv_nsec/1000L); 00127 return nanosleep( rqtp, rmtp ); 00128 } 00129 */ 00130 00131 typedef cyg_sem_t rt_sem_t; 00132 00133 static inline int rtos_sem_init(rt_sem_t* m, int value ) 00134 { 00135 cyg_semaphore_init(m, value); 00136 return 0; 00137 } 00138 00139 static inline int rtos_sem_destroy(rt_sem_t* m ) 00140 { 00141 cyg_semaphore_destroy(m); 00142 return 0; 00143 } 00144 00145 static inline int rtos_sem_signal(rt_sem_t* m ) 00146 { 00147 cyg_semaphore_post(m); 00148 return 0; 00149 } 00150 00151 static inline int rtos_sem_wait(rt_sem_t* m ) 00152 { 00153 cyg_semaphore_wait(m); 00154 return 0; 00155 } 00156 00157 // Should return 0 if no timeout occurs 00158 static inline int rtos_sem_trywait(rt_sem_t* m ) 00159 { 00160 if (cyg_semaphore_trywait(m) == true) 00161 return 0; 00162 else 00163 return -1; 00164 } 00165 00166 // Should return 0 if no timeout occurs 00167 static inline int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay ) 00168 { 00169 // cyg_semaphore_timed_wait returns true if no timeout occurs 00170 if (cyg_semaphore_timed_wait(m,cyg_current_time()+nano2ticks(delay)) == true) 00171 return 0; 00172 else 00173 return -1; 00174 } 00175 00176 static inline int rtos_sem_value(rt_sem_t* m ) 00177 { 00178 int val = 0; 00179 cyg_semaphore_peek(m, &val); 00180 return val; 00181 } 00182 00183 // Mutex functions 00184 00185 typedef cyg_mutex_t rt_mutex_t; 00186 typedef cyg_recursive_mutex_t rt_rec_mutex_t; 00187 00188 // 00189 static inline int rtos_mutex_init(rt_mutex_t* m) 00190 { 00191 cyg_mutex_init(m); 00192 return 0; 00193 } 00194 00195 static inline int rtos_mutex_destroy(rt_mutex_t* m ) 00196 { 00197 cyg_mutex_release(m); 00198 cyg_mutex_destroy(m); 00199 return 0; 00200 } 00201 00202 static inline int rtos_mutex_rec_init(rt_rec_mutex_t* m) 00203 { 00204 cyg_recursive_mutex_init(m); 00205 return 0; 00206 } 00207 00208 static inline int rtos_mutex_rec_destroy(rt_rec_mutex_t* m ) 00209 { 00210 cyg_recursive_mutex_destroy(m); 00211 return 0; 00212 } 00213 00214 static inline int rtos_mutex_lock( rt_mutex_t* m) 00215 { 00216 return cyg_mutex_lock(m); 00217 } 00218 00219 static inline int rtos_mutex_rec_lock( rt_rec_mutex_t* m) 00220 { 00221 return cyg_recursive_mutex_lock(m); 00222 } 00223 00224 static inline int rtos_mutex_trylock( rt_mutex_t* m) 00225 { 00226 if (cyg_mutex_trylock(m) == true) 00227 return 0; 00228 else 00229 return -1; 00230 } 00231 00232 static inline int rtos_mutex_rec_trylock( rt_rec_mutex_t* m) 00233 { 00234 if (cyg_recursive_mutex_trylock(m) == true) 00235 return 0; 00236 else 00237 return -1; 00238 } 00239 00240 static inline int rtos_mutex_unlock( rt_mutex_t* m) 00241 { 00242 cyg_mutex_unlock(m); 00243 return 0; 00244 } 00245 00246 static inline int rtos_mutex_rec_unlock( rt_rec_mutex_t* m) 00247 { 00248 cyg_recursive_mutex_unlock(m); 00249 return 0; 00250 } 00251 00252 static inline void rtos_enable_rt_warning() 00253 { 00254 } 00255 00256 static inline void rtos_disable_rt_warning() 00257 { 00258 } 00259 00260 #define rtos_printf diag_printf 00261 00262 #ifdef __cplusplus 00263 } 00264 00265 #endif 00266 #endif