lwip-1.4.1文件sys_arch翻譯
歡迎檢視本文所在的系列,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 <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.
文個檔案或者包含定義了標準*nix錯誤程式碼的本地系統檔案<errno.h>,或者使用
#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