欢迎查看本文所在的系列,STM32的LWIP应用,点击跳转
sys_arch interface for lwIP 0.6++
lwIP 0.6++ 的 sys_arch 接口
Author: Adam Dunkels
作者:Adam Dunkels
The operating system emulation layer provides a common interface
between the lwIP code and the underlying operating system kernel. The
general idea is that porting lwIP to new architectures requires only
small changes to a few header files and a new sys_arch
implementation. It is also possible to do a sys_arch implementation
that does not rely on any underlying operating system.
操作系统仿真层提供了一个通用的接口在lwip代码和底层操作系统之间。通常认为
将lwip移植到新的系统需要对几个头文件进行小的修改,并且实现一个新的sys_arch。
也可以创建一个与底层操作系统无关的sys_arch实现
The sys_arch provides semaphores and mailboxes to lwIP. For the full
lwIP functionality, multiple threads support can be implemented in the
sys_arch, but this is not required for the basic lwIP
functionality. Previous versions of lwIP required the sys_arch to
implement timer scheduling as well but as of lwIP 0.5 this is
implemented in a higher layer.
sys_arch提供信号量和邮箱为lwip。为了实现完整的LWIP功能,多线程支持可以被创建
在sys_arch中,但是对于LWIP的基础功能是没有必要的。在之前的版本,LWIP要求在
sys_arch实现实时调度,但是在0.5以上的版本已经由更高的层次实现
In addition to the source file providing the functionality of sys_arch,
the OS emulation layer must provide several header files defining
macros used throughout lwip. The files required and the macros they
must define are listed below the sys_arch description.
除了源文件提供sys_arch功能,OS仿真层必须提供几个头文件定义在使用LWIP的宏。
所需文件和宏必须定义如下sys_arch描述。
Semaphores can be either counting or binary - lwIP works with both
kinds. Mailboxes are used for message passing and can be implemented
either as a queue which allows multiple messages to be posted to a
mailbox, or as a rendez-vous point where only one message can be
posted at a time. lwIP works with both kinds, but the former type will
be more efficient. A message in a mailbox is just a pointer, nothing
more.
信号可以是计数或二进制-LWIP可以工作在这两个种类.
邮箱是用于消息传递可以实现或者作为一个队列,允许多个消息被发送到一个邮箱,
或作为海天盛筵的地方只有一个消息可以一次张贴。LwIP的作品与两种,但前者将更有效率。
在邮箱的消息只是一个指针,没有更多。
Semaphores are represented by the type "sys_sem_t" which is typedef'd
in the sys_arch.h file. Mailboxes are equivalently represented by the
type "sys_mbox_t". lwIP does not place any restrictions on how
sys_sem_t or sys_mbox_t are represented internally.
信号量是由”sys_sem_t”类型的定义会在sys_arch.h文件。
邮箱等价地用型”sys_mbox_t”。LwIP没有任何限制在sys_sem_t或sys_mbox_t如何在内部表示。
Since lwIP 1.4.0, semaphore and mailbox functions are prototyped in a way that
allows both using pointers or actual OS structures to be used. This way, memory
required for such types can be either allocated in place (globally or on the
stack) or on the heap (allocated internally in the "*_new()" functions).
从lwip 1.4.0,信号量和邮箱功能的原型的一种方式,既可以使用指针也可以使用实际的OS结构。
这样,申请这种类型的内存可以在适当的地方分配(全局或堆栈)、堆(在“* _new()”功能的内部分配)。
The following functions must be implemented by the sys_arch:
下面列出的函数都必需在 sys_arch 中实现
- void sys_init(void)
Is called to initialize the sys_arch layer.
用于初始化 sys_arch 层
- err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Creates a new semaphore. The semaphore is allocated to the memory that 'sem'
points to (which can be both a pointer or the actual OS structure).
The "count" argument specifies the initial state of the semaphore (which is
either 0 or 1).
If the semaphore has been created, ERR_OK should be returned. Returning any
other error will provide a hint what went wrong, but except for assertions,
no real error handling is implemented.
创建一个新的信号量。信号量分配给内存“SEM”指向(既可以是指针也可以是实际的OS结构)。
“计数”参数指定信号量的初始状态(即0或1)。
如果信号量已被创建,err_ok应该归还。
返回任何其他错误将提示出错,但断言除外,没有实现真正的错误处理。
- 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 be
signaled. If the "timeout" argument is non-zero, the thread should
only be blocked for the specified time (measured in
milliseconds). If the "timeout" argument is zero, the thread should be
blocked until the semaphore is signalled.
阻塞一个线程直到等待的信号量被标记。
如果timeout参数非0,等态的线程最长阻塞时间为指定的timeout时长(以毫秒为单位)。
如果timeout参数为0,线程会一直阻塞,直到等待的信号量被标记。
If the timeout argument is non-zero, the return value is the number of
milliseconds spent waiting for the semaphore to be signaled. If the
semaphore wasn't signaled within the specified time, the return value is
SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
(i.e., it was already signaled), the function may return zero.
当timeout参数不为零时,返回值代表等待的毫秒数。
如果等待的信号量在指定的时间内未被标记,返回值是SYS_ARCH_TIMEOUT。
如果线程无需等待(例如:信号量己经为标记状态),函数返回值为0
Notice that lwIP implements a function with a similar name,
sys_sem_wait(), that uses the sys_arch_sem_wait() function.
注意,lwIP 实现了一个有相似名字的函数叫sys_sem_wait(),它会使用 sys_arch_sem_wait() 函数。
- int sys_sem_valid(sys_sem_t *sem)
Returns 1 if the semaphore is valid, 0 if it is not valid.
When using pointers, a simple way is to check the pointer for != NULL.
When directly using OS structures, implementing this may be more complex.
This may also be a define, in which case the function is not prototyped.
- void sys_sem_set_invalid(sys_sem_t *sem)
Invalidate a semaphore so that sys_sem_valid() returns 0.
ATTENTION: This does NOT mean that the semaphore shall be deallocated:
sys_sem_free() is always called before calling this function!
This may also be a define, in which case the function is not prototyped.
- err_t sys_mbox_new(sys_mbox_t *mbox, int size)
Creates an empty mailbox for maximum "size" elements. Elements stored
in mailboxes are pointers. You have to define macros "_MBOX_SIZE"
in your lwipopts.h, or ignore this parameter in your implementation
and use a default size.
If the mailbox has been created, ERR_OK should be returned. Returning any
other error will provide a hint what went wrong, but except for assertions,
no real error handling is implemented.
创建一个最大空间(size)为size的空邮箱。存储在mailboxes中的原素都是指针。
你需要在lwipopts.h中定义宏“_MBOX_SIZE”,或在实现中忽略此参数而使用默认大小。
- void sys_mbox_free(sys_mbox_t *mbox)
Deallocates a mailbox. If there are messages still present in the
mailbox when the mailbox is deallocated, it is an indication of a
programming error in lwIP and the developer should be notified.
释放一个邮箱。
如果在释放时邮箱中仍然有消息,对lwIP而言,这说明存在程序设计上的缺陷,所以会提醒开发人员。
- void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Posts the "msg" to the mailbox. This function have to block until
the "msg" is really posted.
将msg投递给一个邮箱。这个函数会一直阻塞,直到消息被正确的投递成功。
- err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Try to post the "msg" to the mailbox. Returns ERR_MEM if this one
is full, else, ERR_OK if the "msg" is posted.
将msg投递给一个邮箱。这个函数会一直阻塞,直到消息被正确的投递成功。
- 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 does
not block the thread longer than "timeout" milliseconds (similar to
the sys_arch_sem_wait() function). If "timeout" is 0, the thread should
be blocked until a message arrives. The "msg" argument is a result
parameter that is set by the function (i.e., by doing "*msg =
ptr"). The "msg" parameter maybe NULL to indicate that the message
should be dropped.
在有消息到来前使线程一直处理阻塞状态,线程最长阻塞“timeout”毫秒(与sys_arch_sem_wait()函数相似)。
如果“timeout”为0,线程会一直阻塞直到有消息到来。
”msg“参数是一个由函数内部赋值的输出参数(例如:通过"*msg=ptr”)。
”msg“参数取值可以为NULL,这表示删除一条消息。
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 a
timeout.
函数返回值与sys_arch_sem_wait()函数一样:如果设置了超时,函数返回值为SYS_ARCH_TIMEOUT,否则为等待的毫秒数。
Note that a function with a similar name, sys_mbox_fetch(), is
implemented by lwIP.
注意,lwIP实现了一个有相似名字的函数叫sys_mbox_fetch()
- 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 not
present in the mailbox, it immediately returns with the code
SYS_MBOX_EMPTY. On success 0 is returned.
这个函数和sys_arch_mbox_fetch类似,但如果当前邮箱中没有消息,
这个函数会立刻返回,返回值为SYS_MBOX_EMPTY。如果成功取到消息,返回值为0。
To allow for efficient implementations, this can be defined as a
function-like macro in sys_arch.h instead of a normal function. For
example, a naive implementation could be:
为了实现高效率,可以在sys_arch.h中用类似函数的宏来替换常规函数。举个幼稚的实现方式:
#define sys_arch_mbox_tryfetch(mbox,msg) \
sys_arch_mbox_fetch(mbox,msg,1)
although this would introduce unnecessary delays.
这样实现将引入不必要的延时。
- int sys_mbox_valid(sys_mbox_t *mbox)
Returns 1 if the mailbox is valid, 0 if it is not valid.
When using pointers, a simple way is to check the pointer for != NULL.
When directly using OS structures, implementing this may be more complex.
This may also be a define, in which case the function is not prototyped.
- void sys_mbox_set_invalid(sys_mbox_t *mbox)
Invalidate a mailbox so that sys_mbox_valid() returns 0.
ATTENTION: This does NOT mean that the mailbox shall be deallocated:
sys_mbox_free() is always called before calling this function!
This may also be a define, in which case the function is not prototyped.
If threads are supported by the underlying operating system and if
such functionality is needed in lwIP, the following function will have
to 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 its
execution in the function "thread()". The "arg" argument will be passed as an
argument to the thread() function. The stack size to used for this thread is
the "stacksize" parameter. The id of the new thread is returned. Both the id
and the priority are system dependent.
使用“prio”优创建一个命名为“name”的新线程,这个线程可以调用“thread()”函数开始执行。
“arg”参数将作为thread()函数的一个参数。
“stacksize”参数指定了新线程需要使用的栈(stack)大小。
函数返回值为新线程的id。id和优先级(priority)都依赖于系统。
- sys_prot_t sys_arch_protect(void)
This optional function does a "fast" critical region protection and returns
the previous protection level. This function is only called during very short
critical regions. An embedded system which supports ISR-based drivers might
want to implement this function by disabling interrupts. Task-based systems
might want to implement this by using a mutex or disabling tasking. This
function should support recursive calls from the same task or interrupt. In
other words, sys_arch_protect() could be called while already protected. In
that case the return value indicates that it is already protected.
这个可选的函数快速的(fast)产生一个临界区(criticalregion)保护(protection)并返回之前保护的等级。
这个函数仅在每个很短的临界区中调用。
对于支持ISR-based驱动的嵌入式系统可能希望通过禁止中断方式实现这个函数。
基于任务的系统可能希望通过使用互拆锁(mutex)或禁用任务来实现。
这个函数必需支持能在同一个任务或中断中递归的调用,或者说,sys_arch_protect()可以在已经保护的情况下再次调用。
不管对于任何情况,只要有返回值就表明它已经受到保护了。
sys_arch_protect() is only required if your port is supporting an operating
system.
只有当你的端支持操作系统是,你才需要实现sys_arch_protect()。
- void sys_arch_unprotect(sys_prot_t pval)
This optional function does a "fast" set of critical region protection to the
value specified by pval. See the documentation for sys_arch_protect() for
more information. This function is only required if your port is supporting
an operating system.
这个可选的函数快速的(fast)设置一个临界区(criticalregion)保护(protection)为oval指定的值。
更多信息可以参考sys_arch_protect()。
只有当你的端支持操作系统是,你才需要实现sys_arch_unprotect()。
For some configurations, you also need:
- u32_t sys_now(void)
This optional function returns the current time in milliseconds (don't care
for wraparound, this is only used for time diffs).
Not implementing this function means you cannot use some modules (e.g. TCP
timestamps, internal timeouts for NO_SYS==1).
Note:
Be carefull with using mem_malloc() in sys_arch. When malloc() refers to
mem_malloc() you can run into a circular function call problem. In mem.c
mem_init() tries to allcate a semaphore using mem_malloc, which of course
can't be performed when sys_arch uses mem_malloc.
注释:
在 sys_arch中使用 mem_malloc()时须小心。当 malloc()引用 mem_malloc()时,你可能面对的是一个循环调用的问题。
在 mem.c 文件中,mem_init()试图使用 mem_malloc 函数分配一个信号量,理所当然,当 sys_arch 使用mem_malloc函数时并不能执行。
-------------------------------------------------------------------------------
Additional files required for the "OS support" emulation layer:
对于支持操作系统的仿真层(emulation layer)所需的附加文件
-------------------------------------------------------------------------------
cc.h - Architecture environment, some compiler specific, some
environment specific (probably should move env stuff
to sys_arch.h.)
基础环境,一些特定编译项,一些特定的环境项(也许应该将环境项相关的东西移到sys_arch.h中)
定义在lwIP中用到的类型
Typedefs for the types used by lwip -
u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_t
为打包(packing)lwIP的结构体进行隐式编译
Compiler hints for packing lwip's structures -
PACK_STRUCT_FIELD(x)
PACK_STRUCT_STRUCT
PACK_STRUCT_BEGIN
PACK_STRUCT_END
特定平台的诊断输出
Platform specific diagnostic output -
LWIP_PLATFORM_DIAG(x) - non-fatal, print a message.打印一些非致命的消息
LWIP_PLATFORM_ASSERT(x) - fatal, print message and abandon execution.致命的,打印消息并退出执行
Portability defines for printf formatters:
U16_F, S16_F, X16_F, U32_F, S32_F, X32_F, SZT_F
轻量级的同步机制
"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 a
definition of it, or include a file which defines it.
如果编译器没提供menset(),这个文件必需定议这个方法,如者在其它地方定议后包含进此文件。
This file must either include a system-local
which defines
the standard *nix error codes, or it should #define LWIP_PROVIDE_ERRNO
to make lwip/arch.h define the codes which are used throughout.
文个文件或者包含定义了标准*nix错误代码的本地系统文件,或者使用
#defineLWIP_PROVIDE_ERRNO
让lwip/arch.h定义出程序生命周期中需使用的编码。
perf.h - Architecture specific performance measurement.
针对性能的基础度量
Measurement calls made throughout lwip, these can be defined to nothing.
度量贯穿于整个lwIP,但可能被定义为什么都不做
PERF_START - start measuring something.开始测量
PERF_STOP(x) - stop measuring something, and record the result.结束测量并记录结果
sys_arch.h - Tied to sys_arch.c与sys_arch.c相关
Arch dependent types for the following objects:
arch依赖于下面的对象:
sys_sem_t, sys_mbox_t, sys_thread_t,
And, optionally:
还有可选的:
sys_prot_t
Defines to set vars of sys_mbox_t and sys_sem_t to NULL.
通过下面的定义将变量sys_mbox_t和sys_sem_t设置为空
SYS_MBOX_NULL NULL
SYS_SEM_NULL NULL