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