Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of F7_Ethernet by
sys.h
00001 /* 00002 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without modification, 00006 * are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 3. The name of the author may not be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00019 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00020 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00021 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00024 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00025 * OF SUCH DAMAGE. 00026 * 00027 * This file is part of the lwIP TCP/IP stack. 00028 * 00029 * Author: Adam Dunkels <adam@sics.se> 00030 * 00031 */ 00032 #ifndef __LWIP_SYS_H__ 00033 #define __LWIP_SYS_H__ 00034 00035 #include "lwip/opt.h" 00036 00037 #ifdef __cplusplus 00038 extern "C" { 00039 #endif 00040 00041 #if NO_SYS 00042 00043 /* For a totally minimal and standalone system, we provide null 00044 definitions of the sys_ functions. */ 00045 typedef u8_t sys_sem_t; 00046 typedef u8_t sys_mutex_t; 00047 typedef u8_t sys_mbox_t; 00048 00049 #define sys_sem_new(s, c) ERR_OK 00050 #define sys_sem_signal(s) 00051 #define sys_sem_wait(s) 00052 #define sys_arch_sem_wait(s,t) 00053 #define sys_sem_free(s) 00054 #define sys_sem_valid(s) 0 00055 #define sys_sem_set_invalid(s) 00056 #define sys_mutex_new(mu) ERR_OK 00057 #define sys_mutex_lock(mu) 00058 #define sys_mutex_unlock(mu) 00059 #define sys_mutex_free(mu) 00060 #define sys_mutex_valid(mu) 0 00061 #define sys_mutex_set_invalid(mu) 00062 #define sys_mbox_new(m, s) ERR_OK 00063 #define sys_mbox_fetch(m,d) 00064 #define sys_mbox_tryfetch(m,d) 00065 #define sys_mbox_post(m,d) 00066 #define sys_mbox_trypost(m,d) 00067 #define sys_mbox_free(m) 00068 #define sys_mbox_valid(m) 00069 #define sys_mbox_set_invalid(m) 00070 00071 #define sys_thread_new(n,t,a,s,p) 00072 00073 #define sys_msleep(t) 00074 00075 #else /* NO_SYS */ 00076 00077 /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */ 00078 #define SYS_ARCH_TIMEOUT 0xffffffffUL 00079 00080 /** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate. 00081 * For now we use the same magic value, but we allow this to change in future. 00082 */ 00083 #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT 00084 00085 #include "lwip/err.h" 00086 #include "arch/sys_arch.h" 00087 00088 /** Function prototype for thread functions */ 00089 typedef void (*lwip_thread_fn)(void *arg); 00090 00091 /* Function prototypes for functions to be implemented by platform ports 00092 (in sys_arch.c) */ 00093 00094 /* Mutex functions: */ 00095 00096 /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores 00097 should be used instead */ 00098 #if LWIP_COMPAT_MUTEX 00099 /* for old ports that don't have mutexes: define them to binary semaphores */ 00100 #define sys_mutex_t sys_sem_t 00101 #define sys_mutex_new(mutex) sys_sem_new(mutex, 1) 00102 #define sys_mutex_lock(mutex) sys_sem_wait(mutex) 00103 #define sys_mutex_unlock(mutex) sys_sem_signal(mutex) 00104 #define sys_mutex_free(mutex) sys_sem_free(mutex) 00105 #define sys_mutex_valid(mutex) sys_sem_valid(mutex) 00106 #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex) 00107 00108 #else /* LWIP_COMPAT_MUTEX */ 00109 00110 /** Create a new mutex 00111 * @param mutex pointer to the mutex to create 00112 * @return a new mutex */ 00113 err_t sys_mutex_new(sys_mutex_t *mutex); 00114 /** Lock a mutex 00115 * @param mutex the mutex to lock */ 00116 void sys_mutex_lock(sys_mutex_t *mutex); 00117 /** Unlock a mutex 00118 * @param mutex the mutex to unlock */ 00119 void sys_mutex_unlock(sys_mutex_t *mutex); 00120 /** Delete a semaphore 00121 * @param mutex the mutex to delete */ 00122 void sys_mutex_free(sys_mutex_t *mutex); 00123 #ifndef sys_mutex_valid 00124 /** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */ 00125 int sys_mutex_valid(sys_mutex_t *mutex); 00126 #endif 00127 #ifndef sys_mutex_set_invalid 00128 /** Set a mutex invalid so that sys_mutex_valid returns 0 */ 00129 void sys_mutex_set_invalid(sys_mutex_t *mutex); 00130 #endif 00131 #endif /* LWIP_COMPAT_MUTEX */ 00132 00133 /* Semaphore functions: */ 00134 00135 /** Create a new semaphore 00136 * @param sem pointer to the semaphore to create 00137 * @param count initial count of the semaphore 00138 * @return ERR_OK if successful, another err_t otherwise */ 00139 err_t sys_sem_new(sys_sem_t *sem, u8_t count); 00140 /** Signals a semaphore 00141 * @param sem the semaphore to signal */ 00142 void sys_sem_signal(sys_sem_t *sem); 00143 /** Wait for a semaphore for the specified timeout 00144 * @param sem the semaphore to wait for 00145 * @param timeout timeout in milliseconds to wait (0 = wait forever) 00146 * @return time (in milliseconds) waited for the semaphore 00147 * or SYS_ARCH_TIMEOUT on timeout */ 00148 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout); 00149 /** Delete a semaphore 00150 * @param sem semaphore to delete */ 00151 void sys_sem_free(sys_sem_t *sem); 00152 /** Wait for a semaphore - forever/no timeout */ 00153 #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0) 00154 #ifndef sys_sem_valid 00155 /** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */ 00156 int sys_sem_valid(sys_sem_t *sem); 00157 #endif 00158 #ifndef sys_sem_set_invalid 00159 /** Set a semaphore invalid so that sys_sem_valid returns 0 */ 00160 void sys_sem_set_invalid(sys_sem_t *sem); 00161 #endif 00162 00163 /* Time functions. */ 00164 #ifndef sys_msleep 00165 void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */ 00166 #endif 00167 00168 /* Mailbox functions. */ 00169 00170 /** Create a new mbox of specified size 00171 * @param mbox pointer to the mbox to create 00172 * @param size (miminum) number of messages in this mbox 00173 * @return ERR_OK if successful, another err_t otherwise */ 00174 err_t sys_mbox_new(sys_mbox_t *mbox, int size); 00175 /** Post a message to an mbox - may not fail 00176 * -> blocks if full, only used from tasks not from ISR 00177 * @param mbox mbox to posts the message 00178 * @param msg message to post (ATTENTION: can be NULL) */ 00179 void sys_mbox_post(sys_mbox_t *mbox, void *msg); 00180 /** Try to post a message to an mbox - may fail if full or ISR 00181 * @param mbox mbox to posts the message 00182 * @param msg message to post (ATTENTION: can be NULL) */ 00183 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg); 00184 /** Wait for a new message to arrive in the mbox 00185 * @param mbox mbox to get a message from 00186 * @param msg pointer where the message is stored 00187 * @param timeout maximum time (in milliseconds) to wait for a message 00188 * @return time (in milliseconds) waited for a message, may be 0 if not waited 00189 or SYS_ARCH_TIMEOUT on timeout 00190 * The returned time has to be accurate to prevent timer jitter! */ 00191 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout); 00192 /* Allow port to override with a macro, e.g. special timout for sys_arch_mbox_fetch() */ 00193 #ifndef sys_arch_mbox_tryfetch 00194 /** Wait for a new message to arrive in the mbox 00195 * @param mbox mbox to get a message from 00196 * @param msg pointer where the message is stored 00197 * @param timeout maximum time (in milliseconds) to wait for a message 00198 * @return 0 (milliseconds) if a message has been received 00199 * or SYS_MBOX_EMPTY if the mailbox is empty */ 00200 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg); 00201 #endif 00202 /** For now, we map straight to sys_arch implementation. */ 00203 #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg) 00204 /** Delete an mbox 00205 * @param mbox mbox to delete */ 00206 void sys_mbox_free(sys_mbox_t *mbox); 00207 #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0) 00208 #ifndef sys_mbox_valid 00209 /** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */ 00210 int sys_mbox_valid(sys_mbox_t *mbox); 00211 #endif 00212 #ifndef sys_mbox_set_invalid 00213 /** Set an mbox invalid so that sys_mbox_valid returns 0 */ 00214 void sys_mbox_set_invalid(sys_mbox_t *mbox); 00215 #endif 00216 00217 /** The only thread function: 00218 * Creates a new thread 00219 * @param name human-readable name for the thread (used for debugging purposes) 00220 * @param thread thread-function 00221 * @param arg parameter passed to 'thread' 00222 * @param stacksize stack size in bytes for the new thread (may be ignored by ports) 00223 * @param prio priority of the new thread (may be ignored by ports) */ 00224 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio); 00225 00226 #endif /* NO_SYS */ 00227 00228 /* sys_init() must be called before anthing else. */ 00229 void sys_init(void); 00230 00231 #ifndef sys_jiffies 00232 /** Ticks/jiffies since power up. */ 00233 u32_t sys_jiffies(void); 00234 #endif 00235 00236 /** Returns the current time in milliseconds, 00237 * may be the same as sys_jiffies or at least based on it. */ 00238 u32_t sys_now(void); 00239 00240 /* Critical Region Protection */ 00241 /* These functions must be implemented in the sys_arch.c file. 00242 In some implementations they can provide a more light-weight protection 00243 mechanism than using semaphores. Otherwise semaphores can be used for 00244 implementation */ 00245 #ifndef SYS_ARCH_PROTECT 00246 /** SYS_LIGHTWEIGHT_PROT 00247 * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection 00248 * for certain critical regions during buffer allocation, deallocation and memory 00249 * allocation and deallocation. 00250 */ 00251 #if SYS_LIGHTWEIGHT_PROT 00252 00253 /** SYS_ARCH_DECL_PROTECT 00254 * declare a protection variable. This macro will default to defining a variable of 00255 * type sys_prot_t. If a particular port needs a different implementation, then 00256 * this macro may be defined in sys_arch.h. 00257 */ 00258 #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev 00259 /** SYS_ARCH_PROTECT 00260 * Perform a "fast" protect. This could be implemented by 00261 * disabling interrupts for an embedded system or by using a semaphore or 00262 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when 00263 * already protected. The old protection level is returned in the variable 00264 * "lev". This macro will default to calling the sys_arch_protect() function 00265 * which should be implemented in sys_arch.c. If a particular port needs a 00266 * different implementation, then this macro may be defined in sys_arch.h 00267 */ 00268 #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect() 00269 /** SYS_ARCH_UNPROTECT 00270 * Perform a "fast" set of the protection level to "lev". This could be 00271 * implemented by setting the interrupt level to "lev" within the MACRO or by 00272 * using a semaphore or mutex. This macro will default to calling the 00273 * sys_arch_unprotect() function which should be implemented in 00274 * sys_arch.c. If a particular port needs a different implementation, then 00275 * this macro may be defined in sys_arch.h 00276 */ 00277 #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev) 00278 sys_prot_t sys_arch_protect(void); 00279 void sys_arch_unprotect(sys_prot_t pval); 00280 00281 #else 00282 00283 #define SYS_ARCH_DECL_PROTECT(lev) 00284 #define SYS_ARCH_PROTECT(lev) 00285 #define SYS_ARCH_UNPROTECT(lev) 00286 00287 #endif /* SYS_LIGHTWEIGHT_PROT */ 00288 00289 #endif /* SYS_ARCH_PROTECT */ 00290 00291 /* 00292 * Macros to set/get and increase/decrease variables in a thread-safe way. 00293 * Use these for accessing variable that are used from more than one thread. 00294 */ 00295 00296 #ifndef SYS_ARCH_INC 00297 #define SYS_ARCH_INC(var, val) do { \ 00298 SYS_ARCH_DECL_PROTECT(old_level); \ 00299 SYS_ARCH_PROTECT(old_level); \ 00300 var += val; \ 00301 SYS_ARCH_UNPROTECT(old_level); \ 00302 } while(0) 00303 #endif /* SYS_ARCH_INC */ 00304 00305 #ifndef SYS_ARCH_DEC 00306 #define SYS_ARCH_DEC(var, val) do { \ 00307 SYS_ARCH_DECL_PROTECT(old_level); \ 00308 SYS_ARCH_PROTECT(old_level); \ 00309 var -= val; \ 00310 SYS_ARCH_UNPROTECT(old_level); \ 00311 } while(0) 00312 #endif /* SYS_ARCH_DEC */ 00313 00314 #ifndef SYS_ARCH_GET 00315 #define SYS_ARCH_GET(var, ret) do { \ 00316 SYS_ARCH_DECL_PROTECT(old_level); \ 00317 SYS_ARCH_PROTECT(old_level); \ 00318 ret = var; \ 00319 SYS_ARCH_UNPROTECT(old_level); \ 00320 } while(0) 00321 #endif /* SYS_ARCH_GET */ 00322 00323 #ifndef SYS_ARCH_SET 00324 #define SYS_ARCH_SET(var, val) do { \ 00325 SYS_ARCH_DECL_PROTECT(old_level); \ 00326 SYS_ARCH_PROTECT(old_level); \ 00327 var = val; \ 00328 SYS_ARCH_UNPROTECT(old_level); \ 00329 } while(0) 00330 #endif /* SYS_ARCH_SET */ 00331 00332 00333 #ifdef __cplusplus 00334 } 00335 #endif 00336 00337 #endif /* __LWIP_SYS_H__ */
Generated on Wed Jul 13 2022 02:45:41 by
1.7.2
