BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

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