sys_arch.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. sys_arch interface for lwIP 0.6++
  2. Author: Adam Dunkels
  3. The operating system emulation layer provides a common interface
  4. between the lwIP code and the underlying operating system kernel. The
  5. general idea is that porting lwIP to new architectures requires only
  6. small changes to a few header files and a new sys_arch
  7. implementation. It is also possible to do a sys_arch implementation
  8. that does not rely on any underlying operating system.
  9. The sys_arch provides semaphores and mailboxes to lwIP. For the full
  10. lwIP functionality, multiple threads support can be implemented in the
  11. sys_arch, but this is not required for the basic lwIP
  12. functionality. Previous versions of lwIP required the sys_arch to
  13. implement timer scheduling as well but as of lwIP 0.5 this is
  14. implemented in a higher layer.
  15. In addition to the source file providing the functionality of sys_arch,
  16. the OS emulation layer must provide several header files defining
  17. macros used throughout lwip. The files required and the macros they
  18. must define are listed below the sys_arch description.
  19. Semaphores can be either counting or binary - lwIP works with both
  20. kinds. Mailboxes are used for message passing and can be implemented
  21. either as a queue which allows multiple messages to be posted to a
  22. mailbox, or as a rendez-vous point where only one message can be
  23. posted at a time. lwIP works with both kinds, but the former type will
  24. be more efficient. A message in a mailbox is just a pointer, nothing
  25. more.
  26. Semaphores are represented by the type "sys_sem_t" which is typedef'd
  27. in the sys_arch.h file. Mailboxes are equivalently represented by the
  28. type "sys_mbox_t". lwIP does not place any restrictions on how
  29. sys_sem_t or sys_mbox_t are represented internally.
  30. Since lwIP 1.4.0, semaphore and mailbox functions are prototyped in a way that
  31. allows both using pointers or actual OS structures to be used. This way, memory
  32. required for such types can be either allocated in place (globally or on the
  33. stack) or on the heap (allocated internally in the "*_new()" functions).
  34. The following functions must be implemented by the sys_arch:
  35. - void sys_init(void)
  36. Is called to initialize the sys_arch layer.
  37. - err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  38. Creates a new semaphore. The semaphore is allocated to the memory that 'sem'
  39. points to (which can be both a pointer or the actual OS structure).
  40. The "count" argument specifies the initial state of the semaphore (which is
  41. either 0 or 1).
  42. If the semaphore has been created, ERR_OK should be returned. Returning any
  43. other error will provide a hint what went wrong, but except for assertions,
  44. no real error handling is implemented.
  45. - void sys_sem_free(sys_sem_t *sem)
  46. Deallocates a semaphore.
  47. - void sys_sem_signal(sys_sem_t *sem)
  48. Signals a semaphore.
  49. - u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  50. Blocks the thread while waiting for the semaphore to be
  51. signaled. If the "timeout" argument is non-zero, the thread should
  52. only be blocked for the specified time (measured in
  53. milliseconds). If the "timeout" argument is zero, the thread should be
  54. blocked until the semaphore is signalled.
  55. If the timeout argument is non-zero, the return value is the number of
  56. milliseconds spent waiting for the semaphore to be signaled. If the
  57. semaphore wasn't signaled within the specified time, the return value is
  58. SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
  59. (i.e., it was already signaled), the function may return zero.
  60. Notice that lwIP implements a function with a similar name,
  61. sys_sem_wait(), that uses the sys_arch_sem_wait() function.
  62. - int sys_sem_valid(sys_sem_t *sem)
  63. Returns 1 if the semaphore is valid, 0 if it is not valid.
  64. When using pointers, a simple way is to check the pointer for != NULL.
  65. When directly using OS structures, implementing this may be more complex.
  66. This may also be a define, in which case the function is not prototyped.
  67. - void sys_sem_set_invalid(sys_sem_t *sem)
  68. Invalidate a semaphore so that sys_sem_valid() returns 0.
  69. ATTENTION: This does NOT mean that the semaphore shall be deallocated:
  70. sys_sem_free() is always called before calling this function!
  71. This may also be a define, in which case the function is not prototyped.
  72. - err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  73. Creates an empty mailbox for maximum "size" elements. Elements stored
  74. in mailboxes are pointers. You have to define macros "_MBOX_SIZE"
  75. in your lwipopts.h, or ignore this parameter in your implementation
  76. and use a default size.
  77. If the mailbox has been created, ERR_OK should be returned. Returning any
  78. other error will provide a hint what went wrong, but except for assertions,
  79. no real error handling is implemented.
  80. - void sys_mbox_free(sys_mbox_t *mbox)
  81. Deallocates a mailbox. If there are messages still present in the
  82. mailbox when the mailbox is deallocated, it is an indication of a
  83. programming error in lwIP and the developer should be notified.
  84. - void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  85. Posts the "msg" to the mailbox. This function have to block until
  86. the "msg" is really posted.
  87. - err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  88. Try to post the "msg" to the mailbox. Returns ERR_MEM if this one
  89. is full, else, ERR_OK if the "msg" is posted.
  90. - u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  91. Blocks the thread until a message arrives in the mailbox, but does
  92. not block the thread longer than "timeout" milliseconds (similar to
  93. the sys_arch_sem_wait() function). If "timeout" is 0, the thread should
  94. be blocked until a message arrives. The "msg" argument is a result
  95. parameter that is set by the function (i.e., by doing "*msg =
  96. ptr"). The "msg" parameter maybe NULL to indicate that the message
  97. should be dropped.
  98. The return values are the same as for the sys_arch_sem_wait() function:
  99. Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
  100. timeout.
  101. Note that a function with a similar name, sys_mbox_fetch(), is
  102. implemented by lwIP.
  103. - u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  104. This is similar to sys_arch_mbox_fetch, however if a message is not
  105. present in the mailbox, it immediately returns with the code
  106. SYS_MBOX_EMPTY. On success 0 is returned.
  107. To allow for efficient implementations, this can be defined as a
  108. function-like macro in sys_arch.h instead of a normal function. For
  109. example, a naive implementation could be:
  110. #define sys_arch_mbox_tryfetch(mbox,msg) \
  111. sys_arch_mbox_fetch(mbox,msg,1)
  112. although this would introduce unnecessary delays.
  113. - int sys_mbox_valid(sys_mbox_t *mbox)
  114. Returns 1 if the mailbox is valid, 0 if it is not valid.
  115. When using pointers, a simple way is to check the pointer for != NULL.
  116. When directly using OS structures, implementing this may be more complex.
  117. This may also be a define, in which case the function is not prototyped.
  118. - void sys_mbox_set_invalid(sys_mbox_t *mbox)
  119. Invalidate a mailbox so that sys_mbox_valid() returns 0.
  120. ATTENTION: This does NOT mean that the mailbox shall be deallocated:
  121. sys_mbox_free() is always called before calling this function!
  122. This may also be a define, in which case the function is not prototyped.
  123. If threads are supported by the underlying operating system and if
  124. such functionality is needed in lwIP, the following function will have
  125. to be implemented as well:
  126. - sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
  127. Starts a new thread named "name" with priority "prio" that will begin its
  128. execution in the function "thread()". The "arg" argument will be passed as an
  129. argument to the thread() function. The stack size to used for this thread is
  130. the "stacksize" parameter. The id of the new thread is returned. Both the id
  131. and the priority are system dependent.
  132. - sys_prot_t sys_arch_protect(void)
  133. This optional function does a "fast" critical region protection and returns
  134. the previous protection level. This function is only called during very short
  135. critical regions. An embedded system which supports ISR-based drivers might
  136. want to implement this function by disabling interrupts. Task-based systems
  137. might want to implement this by using a mutex or disabling tasking. This
  138. function should support recursive calls from the same task or interrupt. In
  139. other words, sys_arch_protect() could be called while already protected. In
  140. that case the return value indicates that it is already protected.
  141. sys_arch_protect() is only required if your port is supporting an operating
  142. system.
  143. - void sys_arch_unprotect(sys_prot_t pval)
  144. This optional function does a "fast" set of critical region protection to the
  145. value specified by pval. See the documentation for sys_arch_protect() for
  146. more information. This function is only required if your port is supporting
  147. an operating system.
  148. For some configurations, you also need:
  149. - u32_t sys_now(void)
  150. This optional function returns the current time in milliseconds (don't care
  151. for wraparound, this is only used for time diffs).
  152. Not implementing this function means you cannot use some modules (e.g. TCP
  153. timestamps, internal timeouts for NO_SYS==1).
  154. Note:
  155. Be carefull with using mem_malloc() in sys_arch. When malloc() refers to
  156. mem_malloc() you can run into a circular function call problem. In mem.c
  157. mem_init() tries to allcate a semaphore using mem_malloc, which of course
  158. can't be performed when sys_arch uses mem_malloc.
  159. -------------------------------------------------------------------------------
  160. Additional files required for the "OS support" emulation layer:
  161. -------------------------------------------------------------------------------
  162. cc.h - Architecture environment, some compiler specific, some
  163. environment specific (probably should move env stuff
  164. to sys_arch.h.)
  165. Typedefs for the types used by lwip -
  166. u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_t
  167. Compiler hints for packing lwip's structures -
  168. PACK_STRUCT_FIELD(x)
  169. PACK_STRUCT_STRUCT
  170. PACK_STRUCT_BEGIN
  171. PACK_STRUCT_END
  172. Platform specific diagnostic output -
  173. LWIP_PLATFORM_DIAG(x) - non-fatal, print a message.
  174. LWIP_PLATFORM_ASSERT(x) - fatal, print message and abandon execution.
  175. Portability defines for printf formatters:
  176. U16_F, S16_F, X16_F, U32_F, S32_F, X32_F, SZT_F
  177. "lightweight" synchronization mechanisms -
  178. SYS_ARCH_DECL_PROTECT(x) - declare a protection state variable.
  179. SYS_ARCH_PROTECT(x) - enter protection mode.
  180. SYS_ARCH_UNPROTECT(x) - leave protection mode.
  181. If the compiler does not provide memset() this file must include a
  182. definition of it, or include a file which defines it.
  183. This file must either include a system-local <errno.h> which defines
  184. the standard *nix error codes, or it should #define LWIP_PROVIDE_ERRNO
  185. to make lwip/arch.h define the codes which are used throughout.
  186. perf.h - Architecture specific performance measurement.
  187. Measurement calls made throughout lwip, these can be defined to nothing.
  188. PERF_START - start measuring something.
  189. PERF_STOP(x) - stop measuring something, and record the result.
  190. sys_arch.h - Tied to sys_arch.c
  191. Arch dependent types for the following objects:
  192. sys_sem_t, sys_mbox_t, sys_thread_t,
  193. And, optionally:
  194. sys_prot_t
  195. Defines to set vars of sys_mbox_t and sys_sem_t to NULL.
  196. SYS_MBOX_NULL NULL
  197. SYS_SEM_NULL NULL