sys.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. #ifndef __LWIP_SYS_H__
  33. #define __LWIP_SYS_H__
  34. #include "lwip/opt.h"
  35. #include "eagle_soc.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #if NO_SYS
  40. /* For a totally minimal and standalone system, we provide null
  41. definitions of the sys_ functions. */
  42. typedef u8_t sys_sem_t;
  43. typedef u8_t sys_mutex_t;
  44. typedef u8_t sys_mbox_t;
  45. #define sys_sem_new(s, c) ERR_OK
  46. #define sys_sem_signal(s)
  47. #define sys_sem_wait(s)
  48. #define sys_arch_sem_wait(s,t)
  49. #define sys_sem_free(s)
  50. #define sys_mutex_new(mu) ERR_OK
  51. #define sys_mutex_lock(mu)
  52. #define sys_mutex_unlock(mu)
  53. #define sys_mutex_free(mu)
  54. #define sys_mbox_new(m, s) ERR_OK
  55. #define sys_mbox_fetch(m,d)
  56. #define sys_mbox_tryfetch(m,d)
  57. #define sys_mbox_post(m,d)
  58. #define sys_mbox_trypost(m,d)
  59. #define sys_mbox_free(m)
  60. #define sys_thread_new(n,t,a,s,p)
  61. #define sys_msleep(t)
  62. #else /* NO_SYS */
  63. /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
  64. #define SYS_ARCH_TIMEOUT 0xffffffffUL
  65. /** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.
  66. * For now we use the same magic value, but we allow this to change in future.
  67. */
  68. #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
  69. #include "lwip/err.h"
  70. #include "arch/sys_arch.h"
  71. /** Function prototype for thread functions */
  72. typedef void (*lwip_thread_fn)(void *arg);
  73. /* Function prototypes for functions to be implemented by platform ports
  74. (in sys_arch.c) */
  75. /* Mutex functions: */
  76. /** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores
  77. should be used instead */
  78. #if LWIP_COMPAT_MUTEX
  79. /* for old ports that don't have mutexes: define them to binary semaphores */
  80. #define sys_mutex_t sys_sem_t
  81. #define sys_mutex_new(mutex) sys_sem_new(mutex, 1)
  82. #define sys_mutex_lock(mutex) sys_sem_wait(mutex)
  83. #define sys_mutex_unlock(mutex) sys_sem_signal(mutex)
  84. #define sys_mutex_free(mutex) sys_sem_free(mutex)
  85. #define sys_mutex_valid(mutex) sys_sem_valid(mutex)
  86. #define sys_mutex_set_invalid(mutex) sys_sem_set_invalid(mutex)
  87. #else /* LWIP_COMPAT_MUTEX */
  88. /** Create a new mutex
  89. * @param mutex pointer to the mutex to create
  90. * @return a new mutex */
  91. err_t sys_mutex_new(sys_mutex_t *mutex);
  92. /** Lock a mutex
  93. * @param mutex the mutex to lock */
  94. void sys_mutex_lock(sys_mutex_t *mutex);
  95. /** Unlock a mutex
  96. * @param mutex the mutex to unlock */
  97. void sys_mutex_unlock(sys_mutex_t *mutex);
  98. /** Delete a semaphore
  99. * @param mutex the mutex to delete */
  100. void sys_mutex_free(sys_mutex_t *mutex);
  101. #ifndef sys_mutex_valid
  102. /** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */
  103. int sys_mutex_valid(sys_mutex_t *mutex);
  104. #endif
  105. #ifndef sys_mutex_set_invalid
  106. /** Set a mutex invalid so that sys_mutex_valid returns 0 */
  107. void sys_mutex_set_invalid(sys_mutex_t *mutex);
  108. #endif
  109. #endif /* LWIP_COMPAT_MUTEX */
  110. /* Semaphore functions: */
  111. /** Create a new semaphore
  112. * @param sem pointer to the semaphore to create
  113. * @param count initial count of the semaphore
  114. * @return ERR_OK if successful, another err_t otherwise */
  115. err_t sys_sem_new(sys_sem_t *sem, u8_t count);
  116. /** Signals a semaphore
  117. * @param sem the semaphore to signal */
  118. void sys_sem_signal(sys_sem_t *sem);
  119. /** Wait for a semaphore for the specified timeout
  120. * @param sem the semaphore to wait for
  121. * @param timeout timeout in milliseconds to wait (0 = wait forever)
  122. * @return time (in milliseconds) waited for the semaphore
  123. * or SYS_ARCH_TIMEOUT on timeout */
  124. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
  125. /** Delete a semaphore
  126. * @param sem semaphore to delete */
  127. void sys_sem_free(sys_sem_t *sem);
  128. /** Wait for a semaphore - forever/no timeout */
  129. #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0)
  130. #ifndef sys_sem_valid
  131. /** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */
  132. int sys_sem_valid(sys_sem_t *sem);
  133. #endif
  134. #ifndef sys_sem_set_invalid
  135. /** Set a semaphore invalid so that sys_sem_valid returns 0 */
  136. void sys_sem_set_invalid(sys_sem_t *sem);
  137. #endif
  138. /* Time functions. */
  139. #ifndef sys_msleep
  140. void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
  141. #endif
  142. /* Mailbox functions. */
  143. /** Create a new mbox of specified size
  144. * @param mbox pointer to the mbox to create
  145. * @param size (miminum) number of messages in this mbox
  146. * @return ERR_OK if successful, another err_t otherwise */
  147. err_t sys_mbox_new(sys_mbox_t *mbox, int size);
  148. /** Post a message to an mbox - may not fail
  149. * -> blocks if full, only used from tasks not from ISR
  150. * @param mbox mbox to posts the message
  151. * @param msg message to post (ATTENTION: can be NULL) */
  152. void sys_mbox_post(sys_mbox_t *mbox, void *msg);
  153. /** Try to post a message to an mbox - may fail if full or ISR
  154. * @param mbox mbox to posts the message
  155. * @param msg message to post (ATTENTION: can be NULL) */
  156. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
  157. /** Wait for a new message to arrive in the mbox
  158. * @param mbox mbox to get a message from
  159. * @param msg pointer where the message is stored
  160. * @param timeout maximum time (in milliseconds) to wait for a message
  161. * @return time (in milliseconds) waited for a message, may be 0 if not waited
  162. or SYS_ARCH_TIMEOUT on timeout
  163. * The returned time has to be accurate to prevent timer jitter! */
  164. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
  165. /* Allow port to override with a macro, e.g. special timout for sys_arch_mbox_fetch() */
  166. #ifndef sys_arch_mbox_tryfetch
  167. /** Wait for a new message to arrive in the mbox
  168. * @param mbox mbox to get a message from
  169. * @param msg pointer where the message is stored
  170. * @param timeout maximum time (in milliseconds) to wait for a message
  171. * @return 0 (milliseconds) if a message has been received
  172. * or SYS_MBOX_EMPTY if the mailbox is empty */
  173. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
  174. #endif
  175. /** For now, we map straight to sys_arch implementation. */
  176. #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
  177. /** Delete an mbox
  178. * @param mbox mbox to delete */
  179. void sys_mbox_free(sys_mbox_t *mbox);
  180. #define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
  181. #ifndef sys_mbox_valid
  182. /** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */
  183. int sys_mbox_valid(sys_mbox_t *mbox);
  184. #endif
  185. #ifndef sys_mbox_set_invalid
  186. /** Set an mbox invalid so that sys_mbox_valid returns 0 */
  187. void sys_mbox_set_invalid(sys_mbox_t *mbox);
  188. #endif
  189. /** The only thread function:
  190. * Creates a new thread
  191. * @param name human-readable name for the thread (used for debugging purposes)
  192. * @param thread thread-function
  193. * @param arg parameter passed to 'thread'
  194. * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
  195. * @param prio priority of the new thread (may be ignored by ports) */
  196. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);
  197. #endif /* NO_SYS */
  198. /* sys_init() must be called before anthing else. */
  199. void sys_init(void)ICACHE_FLASH_ATTR;
  200. #ifndef sys_jiffies
  201. /** Ticks/jiffies since power up. */
  202. u32_t sys_jiffies(void)ICACHE_FLASH_ATTR;
  203. #endif
  204. /** Returns the current time in milliseconds,
  205. * may be the same as sys_jiffies or at least based on it. */
  206. static inline u32_t sys_now(void) ICACHE_FLASH_ATTR;
  207. static inline u32_t sys_now(void)
  208. {
  209. return NOW()/(TIMER_CLK_FREQ/1000);
  210. }
  211. /* Critical Region Protection */
  212. /* These functions must be implemented in the sys_arch.c file.
  213. In some implementations they can provide a more light-weight protection
  214. mechanism than using semaphores. Otherwise semaphores can be used for
  215. implementation */
  216. #ifndef SYS_ARCH_PROTECT
  217. /** SYS_LIGHTWEIGHT_PROT
  218. * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
  219. * for certain critical regions during buffer allocation, deallocation and memory
  220. * allocation and deallocation.
  221. */
  222. #if SYS_LIGHTWEIGHT_PROT
  223. /** SYS_ARCH_DECL_PROTECT
  224. * declare a protection variable. This macro will default to defining a variable of
  225. * type sys_prot_t. If a particular port needs a different implementation, then
  226. * this macro may be defined in sys_arch.h.
  227. */
  228. #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
  229. /** SYS_ARCH_PROTECT
  230. * Perform a "fast" protect. This could be implemented by
  231. * disabling interrupts for an embedded system or by using a semaphore or
  232. * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
  233. * already protected. The old protection level is returned in the variable
  234. * "lev". This macro will default to calling the sys_arch_protect() function
  235. * which should be implemented in sys_arch.c. If a particular port needs a
  236. * different implementation, then this macro may be defined in sys_arch.h
  237. */
  238. #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
  239. /** SYS_ARCH_UNPROTECT
  240. * Perform a "fast" set of the protection level to "lev". This could be
  241. * implemented by setting the interrupt level to "lev" within the MACRO or by
  242. * using a semaphore or mutex. This macro will default to calling the
  243. * sys_arch_unprotect() function which should be implemented in
  244. * sys_arch.c. If a particular port needs a different implementation, then
  245. * this macro may be defined in sys_arch.h
  246. */
  247. #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
  248. sys_prot_t sys_arch_protect(void)ICACHE_FLASH_ATTR;
  249. void sys_arch_unprotect(sys_prot_t pval)ICACHE_FLASH_ATTR;
  250. #else
  251. #define SYS_ARCH_DECL_PROTECT(lev)
  252. #define SYS_ARCH_PROTECT(lev) lev = os_intr_lock() //fix by ives at 2014.3.24
  253. #define SYS_ARCH_UNPROTECT(lev) lev = os_intr_unlock()
  254. #endif /* SYS_LIGHTWEIGHT_PROT */
  255. #endif /* SYS_ARCH_PROTECT */
  256. /*
  257. * Macros to set/get and increase/decrease variables in a thread-safe way.
  258. * Use these for accessing variable that are used from more than one thread.
  259. */
  260. #ifndef SYS_ARCH_INC
  261. #define SYS_ARCH_INC(var, val) do { \
  262. SYS_ARCH_DECL_PROTECT(old_level); \
  263. SYS_ARCH_PROTECT(old_level); \
  264. var += val; \
  265. SYS_ARCH_UNPROTECT(old_level); \
  266. } while(0)
  267. #endif /* SYS_ARCH_INC */
  268. #ifndef SYS_ARCH_DEC
  269. #define SYS_ARCH_DEC(var, val) do { \
  270. SYS_ARCH_DECL_PROTECT(old_level); \
  271. SYS_ARCH_PROTECT(old_level); \
  272. var -= val; \
  273. SYS_ARCH_UNPROTECT(old_level); \
  274. } while(0)
  275. #endif /* SYS_ARCH_DEC */
  276. #ifndef SYS_ARCH_GET
  277. #define SYS_ARCH_GET(var, ret) do { \
  278. SYS_ARCH_DECL_PROTECT(old_level); \
  279. SYS_ARCH_PROTECT(old_level); \
  280. ret = var; \
  281. SYS_ARCH_UNPROTECT(old_level); \
  282. } while(0)
  283. #endif /* SYS_ARCH_GET */
  284. #ifndef SYS_ARCH_SET
  285. #define SYS_ARCH_SET(var, val) do { \
  286. SYS_ARCH_DECL_PROTECT(old_level); \
  287. SYS_ARCH_PROTECT(old_level); \
  288. var = val; \
  289. SYS_ARCH_UNPROTECT(old_level); \
  290. } while(0)
  291. #endif /* SYS_ARCH_SET */
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif /* __LWIP_SYS_H__ */