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