sys_arch interface for lwIP 2.0.3

Author: Adam Dunkels
Simon Goldschmidt

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, mailboxes and mutexes 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. Timer scheduling is implemented in lwIP, but can be implemented
by the sys_arch port (LWIP_TIMERS_CUSTOM==1).

Sys_arch需要为LwIP提供信号量和邮箱两种进程间的服务(通信方式)。如果想获得完整的LwIP 功能,需要在sys_arch 中实现多线程的支持,但对于基本的LwIP 功能,sys_arch 并不需要这些支持。
定时器调度是在lwIP中实现的,但是可以实现通过sysarch端口(LWIP_TIMERS_CUSTOM==1)。

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功能的源文件外,操作系统模拟层必须提供在LwIP中所使用全部宏定义的少数头文件。所需的文件及必须定义的宏定义列在对sys_arch的描述之后。

Semaphores can be either counting or binary - lwIP works with both
kinds. Mailboxes should be implemented as a queue which allows multiple messages
to be posted (implementing as a rendez-vous point where only one message can be
posted at a time can have a highly negative impact on performance). 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". Mutexes are represented by the type "sys_mutex_t".
lwIP does not place any restrictions on how these types are represented
internally.

信号量用"sys_sem_t"类型来表示,该类型在sys_arch.h文件中定义。相应地,邮箱用"sys_mbox_t"类型来表示。
对于sys_sem_t和sys_mbox_t内在如何表示这两种不同类型,LwIP没有任何限制。

Since lwIP 1.4.0, semaphore, mutexes 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开始,信号量、互斥量和邮箱函数都是原型化的
允许使用指针或实际操作系统结构。这种方式,记忆
这种类型的需求可以被分配到适当的位置(全局的或者是在
堆栈)或在堆上(在“new()内部分配”函数)。

/* 2018年1月22日13:05:14 suozhang lwip sys_arch.c by FreeRTOS 驱动 */

The following functions must be implemented by the sys_arch:

以下函数必须由sys_arch实现:

- void sys_init(void)

Is called to initialize the sys_arch layer.

/*********************************************************************
* @fn sys_init
*
* @brief 用以初始化系统接口模拟层。
*
* @param void
*
* @return void
*/
void sys_init(void)
{
  /* nothing on FreeRTOS porting */
}

- 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”的内存中。
  指向(既可以是指针,也可以是实际操作系统的结构)。
  “count”参数指定了这个信号量的初始状态(它是不是0就是1)。
  如果已经创建了这个信号量,那么应该返回ERR_OK。返回任何
  其他错误将提供一个提示,说明哪里出了问题,但除了断言,
  没有实现真正的错误处理。

/*********************************************************************
* @fn sys_sem_new
*
* @brief creates a new semaphore.
*
* @param *sem: semaphore is allocated to the memory , count: either 0 or 1
*
* @return the operation status, ERR_OK on OK; others on error
*/
err_t sys_sem_new( sys_sem_t *sem, u8_t count )
{ vSemaphoreCreateBinary( *sem ); if(*sem == NULL)
{
return ERR_MEM;
} if(count == ) // Means it can't be taken
{
xSemaphoreTake(*sem,);
} return ERR_OK; }

- void sys_sem_free(sys_sem_t *sem)

  Deallocates a semaphore.
  解除分配一个信号量。

/*********************************************************************
* @fn sys_sem_free
*
* @brief Deallocates a semaphore.
*
* @param *sem: semaphore is allocated to the memory
*
* @return void
*/
void sys_sem_free(sys_sem_t *sem)
{
vSemaphoreDelete(*sem);
}

- 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.

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.

Notice that lwIP implements a function with a similar name,
sys_sem_wait(), that uses the sys_arch_sem_wait() function.

等待指定的信号并阻塞线程。timeout参数为0,线程会一直被阻塞至收到指定的信号;非0,则线程仅被阻塞至指定的timeout时间(单位为毫秒)。
在timeout参数值非0的情况下,返回值为等待指定的信号所消耗的毫秒数。如果在指定的时间内并没有收到信号,返回值为SYS_ARCH_TIMEOUT。如果线程不必再等待这个信号(也就是说,已经收到信号),返回值也可以为0。
注意,LwIP 实现了一个名称与之相似的函数来调用这个函数,sys_sem_wait(),在sys_arch 中使用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.

如果信号量是有效的,则返回1,如果它无效的话。当使用指针时,一个简单的方法就是检查指针!=零。

当直接使用OS结构时,实现这个可能会更加复杂。这也可能是一个定义,在这种情况下,函数不是原型。

- 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.

使一个信号量无效,使syssem有效性()返回0。注意:这并不意味着信号量应该被释放:

syssemfree()在调用这个函数之前总是被调用的!这也可能是一个定义,在这种情况下,函数不是原型。

- void sys_mutex_new(sys_mutex_t *mutex)

Creates a new mutex. The mutex is allocated to the memory that 'mutex'
points to (which can be both a pointer or the actual OS structure).
If the mutex 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.

创建一个新的互斥。互斥对象被分配到“互斥”的内存中指向(既可以是指针,也可以是实际操作系统的结构)。

如果已经创建了互斥锁,则应该返回ERR_OK。返回任何其他错误将提供一个提示,说明哪里出了问题,但除了断言,没有实现真正的错误处理。

- void sys_mutex_free(sys_mutex_t *mutex)

Deallocates a mutex.

- void sys_mutex_lock(sys_mutex_t *mutex)

Blocks the thread until the mutex can be grabbed.

- void sys_mutex_unlock(sys_mutex_t *mutex)

Releases the mutex previously locked through 'sys_mutex_lock()'.

- void sys_mutex_valid(sys_mutex_t *mutex)

Returns 1 if the mutes 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_mutex_set_invalid(sys_mutex_t *mutex)

Invalidate a mutex so that sys_mutex_valid() returns 0.
ATTENTION: This does NOT mean that the mutex shall be deallocated:
sys_mutex_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.

- 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.

- 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.

- 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.

- 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.

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.

Note that a function with a similar name, sys_mbox_fetch(), is
implemented 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 not
present in the mailbox, it immediately returns with the code
SYS_MBOX_EMPTY. On success 0 is returned.

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:
#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.

When lwIP is used from more than one context (e.g. from multiple threads OR from
main-loop and from interrupts), the SYS_LIGHTWEIGHT_PROT protection SHOULD be enabled!

- 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.

sys_arch_protect() is only required if your port is supporting an operating
system.

- 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.

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 careful 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.

-------------------------------------------------------------------------------
Additional files required for the "OS support" emulation layer:
-------------------------------------------------------------------------------

cc.h - Architecture environment, some compiler specific, some
environment specific (probably should move env stuff
to sys_arch.h.)

Typedefs for the types used by lwip -
u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_t

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.

This file must either include a system-local <errno.h> 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.

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.c

Arch dependent types for the following objects:
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_NULL NULL
SYS_SEM_NULL NULL

sys_arch interface for lwIP 2.0.3的更多相关文章

  1. PPP interface for lwIP

    //原文 地址 :http://www.nongnu.org/lwip/2_0_x/group__ppp.html /* //协议说明,2017年6月29日14:19:18,suozhang PPP ...

  2. lwip 2.0.2 snmp mib ipv6

    1.3.6.1.2.1 - SNMP MIB-2 Submitted by Harald.T.Alvestrand at uninett.no from host aun.uninett.no (12 ...

  3. lwIP 2.0.3 移植笔记(基于 STM32 + μC/OS-II)

    本次实验参考自原子已经移植好的 LWIP(版本:1.4.1)模板来进行的,感谢! 由于要做的一个小项目要用到网络通信,而且想要加上 UC/OS-II 跑个系统,感觉 LWIP 这个轻量级的 IP 协议 ...

  4. LWIP network interface 即 LWIP 的 硬件 数据 接口 移植 详解 STM32 以太网数据 到达 的第二站: void ethernetif_input( void * pvParameters )

    根据 上一篇 文章 , ETH  DMA 数据中断 会 发送 一个信号量 ,我使用 全局 搜索 这个信号量 s_xSemaphore 得到 一下 几个 值 根据 这个 分析  我们找到了   数据 的 ...

  5. LWIP network interface 即 LWIP 的 硬件 数据 接口 移植 首先 详解 STM32 以太网数据 到达 的第一站: ETH DMA 中断函数

    要 运行  LWIP  不光 要实现  OS  的 一些 接口  ,还要 有 硬件 数据 接口 移植 ,即 网线上 来的 数据 怎么个形式 传递给  LWIP ,去解析 做出相应的 应答  ,2017 ...

  6. java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0

    java 继承使用关键字extends   继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...

  7. lwip 2.0.3 DNS 域名解析 使用

    1.  在  lwipopts.h 中 #define LWIP_DNS 1 /* 使能 DNS 服务器的功能 ,2018年1月8日21:16:20,suozhang */ #define LWIP_ ...

  8. Golang接口(interface)三个特性(译文)

    The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...

  9. 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

随机推荐

  1. 创建Web项目运行时出小错误及解决方法

    1.目录结构 2.各文件内容 index.jsp <%@ page contentType="text/html;charset=UTF-8" language=" ...

  2. 记录LNMP环境彻底删除绑定域名及网站文件夹/文件的过程

    lnmp vhost del #删除绑定的域名 chattr -i /home/wwwroot/域名文件夹/.user.ini #解除文件安全限制 rm -rf /home/wwwroot/域名文件夹 ...

  3. 通过vertical-align属性实现“竖向居中”显示

    自学编程大概有大半年的时间了,从15年7月开始学习使用人数最多的JAVA,到后来喜欢上了前端,但由于之间在建筑设计院的工作加班颇为频繁,每天刨去工作,基本没有多少自己个人的时间,只能每天6,7点起床, ...

  4. javascript 获取服务时间

    用到了jquery的ajax方法,ajax自己写也可以. 具体用法 var setId = setInterval(function(){ var xhr = $.ajax({ type: 'HEAD ...

  5. 微服务架构之spring cloud zipkin

    Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...

  6. MAVLink Linux/QNX/MacOs Integration Tutorial (UDP)

    MAVLink Linux/QNX/MacOs Integration Tutorial (UDP) Overview This program was written to test the udp ...

  7. mac下安装elasticsearch报错Exception BindTransportException[Failed to bind to [9300-9400]]

    解决办法:进入 config目录下 修改 vim elasticsearch.xml network.host设置为 0.0.0.0 即可外网访问.

  8. 打通版微社区(1):PHP环境部署 for DZX3.2

    写在前面:本文参考了http://blog.sina.com.cn/s/blog_513be2630101linz.html非常感谢博主此文对我此次操作帮助很大.PHP的windows部署方案主要分为 ...

  9. Mac远程连接服务器

    方法一:ssh 方法二:command+K进入远程桌面,这种方式类似于windwos下的远程桌面

  10. HTTP协议图--HTTP 报文首部之首部字段(重点分析)

    1.首部字段概述 先来回顾一下首部字段在报文的位置,HTTP 报文包含报文首部和报文主体,报文首部包含请求行(或状态行)和首部字段. 在报文众多的字段当中,HTTP 首部字段包含的信息最为丰富.首部字 ...