URL
https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk
Subversion Repositories openrisc_2011-10-31
[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP_130/] [doc/] [sys_arch.txt] - Rev 606
Compare with Previous | Blame | View Log
sys_arch interface for lwIP 0.6++Author: Adam DunkelsThe operating system emulation layer provides a common interfacebetween the lwIP code and the underlying operating system kernel. Thegeneral idea is that porting lwIP to new architectures requires onlysmall changes to a few header files and a new sys_archimplementation. It is also possible to do a sys_arch implementationthat does not rely on any underlying operating system.The sys_arch provides semaphores and mailboxes to lwIP. For the fulllwIP functionality, multiple threads support can be implemented in thesys_arch, but this is not required for the basic lwIPfunctionality. Previous versions of lwIP required the sys_arch toimplement timer scheduling as well but as of lwIP 0.5 this isimplemented in a higher layer.In addition to the source file providing the functionality of sys_arch,the OS emulation layer must provide several header files definingmacros used throughout lwip. The files required and the macros theymust define are listed below the sys_arch description.Semaphores can be either counting or binary - lwIP works with bothkinds. Mailboxes are used for message passing and can be implementedeither as a queue which allows multiple messages to be posted to amailbox, or as a rendez-vous point where only one message can beposted at a time. lwIP works with both kinds, but the former type willbe more efficient. A message in a mailbox is just a pointer, nothingmore.Semaphores are represented by the type "sys_sem_t" which is typedef'din the sys_arch.h file. Mailboxes are equivalently represented by thetype "sys_mbox_t". lwIP does not place any restrictions on howsys_sem_t or sys_mbox_t are represented internally.The following functions must be implemented by the sys_arch:- void sys_init(void)Is called to initialize the sys_arch layer.- sys_sem_t sys_sem_new(u8_t count)Creates and returns a new semaphore. The "count" argument specifiesthe initial state of the semaphore.- void sys_sem_free(sys_sem_t sem)Deallocates a semaphore.- void sys_sem_signal(sys_sem_t sem)Signals a semaphore.- u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)Blocks the thread while waiting for the semaphore to besignaled. If the "timeout" argument is non-zero, the thread shouldonly be blocked for the specified time (measured inmilliseconds). If the "timeout" argument is zero, the thread should beblocked until the semaphore is signalled.If the timeout argument is non-zero, the return value is the number ofmilliseconds spent waiting for the semaphore to be signaled. If thesemaphore wasn't signaled within the specified time, the return value isSYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore(i.e., it was already signaled), the function may return zero.Notice that lwIP implements a function with a similar name,sys_sem_wait(), that uses the sys_arch_sem_wait() function.- sys_mbox_t sys_mbox_new(int size)Creates an empty mailbox for maximum "size" elements. Elements storedin mailboxes are pointers. You have to define macros "_MBOX_SIZE"in your lwipopts.h, or ignore this parameter in your implementationand use a default size.- void sys_mbox_free(sys_mbox_t mbox)Deallocates a mailbox. If there are messages still present in themailbox when the mailbox is deallocated, it is an indication of aprogramming error in lwIP and the developer should be notified.- void sys_mbox_post(sys_mbox_t mbox, void *msg)Posts the "msg" to the mailbox. This function have to block untilthe "msg" is really posted.- err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg)Try to post the "msg" to the mailbox. Returns ERR_MEM if this oneis full, else, ERR_OK if the "msg" is posted.- u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)Blocks the thread until a message arrives in the mailbox, but doesnot block the thread longer than "timeout" milliseconds (similar tothe sys_arch_sem_wait() function). If "timeout" is 0, the thread shouldbe blocked until a message arrives. The "msg" argument is a resultparameter that is set by the function (i.e., by doing "*msg =ptr"). The "msg" parameter maybe NULL to indicate that the messageshould be dropped.The return values are the same as for the sys_arch_sem_wait() function:Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was atimeout.Note that a function with a similar name, sys_mbox_fetch(), isimplemented by lwIP.- u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg)This is similar to sys_arch_mbox_fetch, however if a message is notpresent in the mailbox, it immediately returns with the codeSYS_MBOX_EMPTY. On success 0 is returned.To allow for efficient implementations, this can be defined as afunction-like macro in sys_arch.h instead of a normal function. Forexample, a naive implementation could be:#define sys_arch_mbox_tryfetch(mbox,msg) \sys_arch_mbox_fetch(mbox,msg,1)although this would introduce unnecessary delays.- struct sys_timeouts *sys_arch_timeouts(void)Returns a pointer to the per-thread sys_timeouts structure. In lwIP,each thread has a list of timeouts which is repressented as a linkedlist of sys_timeout structures. The sys_timeouts structure holds apointer to a linked list of timeouts. This function is called bythe lwIP timeout scheduler and must not return a NULL value.In a single thread sys_arch implementation, this function willsimply return a pointer to a global sys_timeouts variable stored inthe sys_arch module.If threads are supported by the underlying operating system and ifsuch functionality is needed in lwIP, the following function will haveto be implemented as well:- sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)Starts a new thread named "name" with priority "prio" that will begin itsexecution in the function "thread()". The "arg" argument will be passed as anargument to the thread() function. The stack size to used for this thread isthe "stacksize" parameter. The id of the new thread is returned. Both the idand the priority are system dependent.- sys_prot_t sys_arch_protect(void)This optional function does a "fast" critical region protection and returnsthe previous protection level. This function is only called during very shortcritical regions. An embedded system which supports ISR-based drivers mightwant to implement this function by disabling interrupts. Task-based systemsmight want to implement this by using a mutex or disabling tasking. Thisfunction should support recursive calls from the same task or interrupt. Inother words, sys_arch_protect() could be called while already protected. Inthat case the return value indicates that it is already protected.sys_arch_protect() is only required if your port is supporting an operatingsystem.- void sys_arch_unprotect(sys_prot_t pval)This optional function does a "fast" set of critical region protection to thevalue specified by pval. See the documentation for sys_arch_protect() formore information. This function is only required if your port is supportingan operating system.Note:Be carefull with using mem_malloc() in sys_arch. When malloc() refers tomem_malloc() you can run into a circular function call problem. In mem.cmem_init() tries to allcate a semaphore using mem_malloc, which of coursecan't be performed when sys_arch uses mem_malloc.-------------------------------------------------------------------------------Additional files required for the "OS support" emulation layer:-------------------------------------------------------------------------------cc.h - Architecture environment, some compiler specific, someenvironment specific (probably should move env stuffto sys_arch.h.)Typedefs for the types used by lwip -u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_tCompiler hints for packing lwip's structures -PACK_STRUCT_FIELD(x)PACK_STRUCT_STRUCTPACK_STRUCT_BEGINPACK_STRUCT_ENDPlatform specific diagnostic output -LWIP_PLATFORM_DIAG(x) - non-fatal, print a message.LWIP_PLATFORM_ASSERT(x) - fatal, print message and abandon execution."lightweight" synchronization mechanisms -SYS_ARCH_DECL_PROTECT(x) - declare a protection state variable.SYS_ARCH_PROTECT(x) - enter protection mode.SYS_ARCH_UNPROTECT(x) - leave protection mode.If the compiler does not provide memset() this file must include adefinition of it, or include a file which defines it.This file must either include a system-local <errno.h> which definesthe standard *nix error codes, or it should #define LWIP_PROVIDE_ERRNOto make lwip/arch.h define the codes which are used throughout.perf.h - Architecture specific performance measurement.Measurement calls made throughout lwip, these can be defined to nothing.PERF_START - start measuring something.PERF_STOP(x) - stop measuring something, and record the result.sys_arch.h - Tied to sys_arch.cArch dependent types for the following objects:sys_sem_t, sys_mbox_t, sys_thread_t,And, optionally:sys_prot_tDefines to set vars of sys_mbox_t and sys_sem_t to NULL.SYS_MBOX_NULL NULLSYS_SEM_NULL NULL
