1 準備工作
在查看此博客之前,建議讀者先閱讀:AURIX TriCore學習筆記四:LwIP裸機移植,文章鏈接如下所示:https://blog.csdn.net/weixin_43986229/article/details/105651842,此博客是在其基礎上進行擴展和完善,增加基於FreeRTOS移植LwIP協議時應當注意的事項。
2 移植LwIP
2.1 文件移植
基於AURIX TriCore學習筆記四:LwIP裸機移植中介紹創建工程路徑後,注意“port”目錄,這是基於FreeRTOS移植LwIP的關鍵,主要工作便是對此目錄下文件的修改以及文件的添加。首先完善工程文件,在“port\include\arch”目錄下添加“sys_arch.h”文件,在“port”目錄下添加“sys_arch.c”文件。通常稱“sys_arch.h”和“sys_arch.c”爲系統模擬接口層文件,其內容定義如下所示。
sys_arch.h文件內容:
/**
* @file
*
* lwIP sys arch configuration. This file is part of the lwIP TCP/IP stack.
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#ifndef LWIP_SYS_ARCH_H
#define LWIP_SYS_ARCH_H
/*
* The stack be used either in a bare metal environment (NO_SYS=1) or in an OS environment (NO_SYS=0).
* This implementation supports FreeRTOS as operating system (USING_OS_FREERTOS)
*/
#include "lwip/opt.h"
/* Sanity checks */
#if NO_SYS
#if defined(USING_OS_FREERTOS)
#error NO_SYS cannot be used with USING_OS_FREERTOS
#endif /* defined(USING_OS_FREERTOS) */
#else /* NO_SYS */
#if defined(USING_OS_FREERTOS)
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#else /* defined(USING_OS_FREERTOS) */
#error USING_OS_FREERTOS needs to be enabled when NO_SYS=0
#endif /* defined(USING_OS_FREERTOS) */
#endif /* NO_SYS */
#if defined(USING_OS_FREERTOS)
typedef QueueHandle_t sys_mbox_t;
typedef SemaphoreHandle_t sys_sem_t;
typedef SemaphoreHandle_t sys_mutex_t;
typedef TaskHandle_t sys_thread_t;
#define SYS_MBOX_NULL (QueueHandle_t)0
#define SYS_SEM_NULL (SemaphoreHandle_t)0
#define SYS_MUTEX_NULL SYS_SEM_NULL
#define sys_mbox_valid(mbox) (((uintptr_t)(mbox) != (uintptr_t)NULL) && (*(mbox) != SYS_SEM_NULL))
#define sys_mbox_set_invalid(mbox) do { if ((mbox) != NULL) { *(mbox) = SYS_SEM_NULL; }} while(0)
#define sys_sem_valid(sem) (((sem) != NULL) && (*(sem) != SYS_SEM_NULL))
#define sys_sem_set_invalid(sem) do {if ((sem) != NULL) { *(sem) = SYS_SEM_NULL; }} while(0)
#define sys_mutex_valid(sem) sys_sem_valid(sem)
#define sys_mutex_set_invalid(sem) sys_sem_set_invalid(sem)
/* These functions are not part of the lwIP sys_arch API
But since the functionality is support by underlying FreeRTOS, they may be useful for implementing netifs */
void sys_mbox_post_to_front(sys_mbox_t *mbox, void *data);
void sys_thread_delete(sys_thread_t thread);
#if LWIP_SOCKET_SET_ERRNO
#define set_errno(err) do { if (err) { vTaskSetThreadLocalStoragePointer(NULL, 0, (void *)(err)); } } while(0)
#define errno ((int)pvTaskGetThreadLocalStoragePointer(NULL, 0))
#endif /* LWIP_SOCKET_SET_ERRNO */
#if LWIP_NETCONN_SEM_PER_THREAD
#define LWIP_NETCONN_THREAD_SEM_GET() ((sys_sem_t*)pvTaskGetThreadLocalStoragePointer(NULL, 1))
#define LWIP_NETCONN_THREAD_SEM_ALLOC() do { \
sys_sem_t* pSem = (sys_sem_t*)pvPortMalloc(sizeof(sys_sem_t)); \
if (ERR_OK == sys_sem_new(pSem, 0)) { \
vTaskSetThreadLocalStoragePointer(NULL, 1, (void*)pSem); \
} \
} while(0)
#define LWIP_NETCONN_THREAD_SEM_FREE() do { \
sys_sem_t* pSem = LWIP_NETCONN_THREAD_SEM_GET(); \
sys_sem_free(pSem); \
vPortFree(pSem); \
vTaskSetThreadLocalStoragePointer(NULL, 1, NULL); \
} while(0)
#endif /* LWIP_NETCONN_SEM_PER_THREAD */
#endif /* defined(USING_OS_FREERTOS) */
#endif /* LWIP_FREERTOS_ARCH_SYS_ARCH_H */
sys_arch.c文件內容:
/**
* @file
*
* lwIP sys arch configuration. This file is part of the lwIP TCP/IP stack.
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
/* lwIP includes. */
#include "Global.h"
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/stats.h"
#include "lwip/debug.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include "arch/sys_arch.h"
#if defined(USING_OS_FREERTOS)
/*
* Checks validity of a mutex/semaphore, based on pointer value
*/
static err_t check_synchronization_value(const sys_sem_t *synchronization_value)
{
err_t xResult = ERR_OK;
if (NULL == *synchronization_value)
{
xResult = ERR_MEM;
}
return xResult;
}
/*
* Returns the time elapsed (in milliseconds) since a given time
* param: startTime - the time since we want to calculate how much time has passed
*/
static u32_t get_elapsed_time(u32_t startTime)
{
u32_t elapsed;
elapsed = sys_now() - startTime;
if (0U == elapsed)
{
elapsed = 1U;
}
return elapsed;
}
/*
* Creates an empty mailbox for maximum "size" elements.
*/
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
{
LWIP_ASSERT("mbox != NULL", mbox != NULL);
*mbox = xQueueCreate(size, sizeof(void*));
return ERR_OK;
}
/*
* 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_free(sys_mbox_t *mbox)
{
LWIP_ASSERT("mbox != NULL", mbox != NULL);
LWIP_ASSERT("mbox not empty", !uxQueueMessagesWaiting(*mbox));
vQueueDelete(*mbox);
}
/*
* Posts the "msg" to the mailbox. This function have to block until
* the "msg" is really posted.
*/
void sys_mbox_post(sys_mbox_t *mbox, void *data)
{
LWIP_ASSERT("mbox != NULL", mbox != NULL);
while (pdTRUE != xQueueSend(*mbox, &data, 10000)) /* time is arbitrary */
{}
}
/*
* Posts the "msg" to the front of the mailbox. This function have to block until
* the "msg" is really posted.
*/
void sys_mbox_post_to_front(sys_mbox_t *mbox, void *data)
{
LWIP_ASSERT("mbox != NULL", mbox != NULL);
while (pdTRUE != xQueueSendToFront(*mbox, &data, 10000)) /* time is arbitrary */
{}
}
/*
* Try to post the "msg" to the mailbox. Returns ERR_MEM if this one
* is full, else, ERR_OK if the "msg" is posted.
*/
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *data)
{
LWIP_ASSERT("mbox != NULL", mbox != NULL);
(void)xQueueSend(*mbox, &data, (TickType_t)0);
return ERR_OK;
}
/*
* 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). 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_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
{
void** msgptr = msg;
u32_t startTime;
LWIP_ASSERT("mbox != NULL", mbox != NULL);
startTime = sys_now();
if (timeout != 0U)
{
if (pdTRUE == xQueueReceive(*mbox, msgptr, pdMS_TO_TICKS(timeout)))
{
return get_elapsed_time(startTime);
}
else /* timed out blocking for message */
{
*msgptr = NULL;
return SYS_ARCH_TIMEOUT;
}
}
else /* block forever for a message. */
{
while (pdTRUE != xQueueReceive(*mbox, msgptr, 10000)) /* time is arbitrary */
{}
return get_elapsed_time(startTime);
}
}
/*
* 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.
*/
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
void** msgptr = msg;
LWIP_ASSERT("mbox != NULL", mbox != NULL);
if (pdTRUE == xQueueReceive(*mbox, msgptr, 0))
{
return 0U;
}
else /* no message */
{
*msgptr = NULL;
return SYS_MBOX_EMPTY;
}
}
/*
* Creates and returns a new semaphore. The "count" argument specifies
* the initial state of the semaphore. TBD finish and test
*/
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
{
err_t xResult = ERR_OK;
LWIP_UNUSED_ARG(count);
LWIP_ASSERT("sem != NULL", sem != NULL);
portENTER_CRITICAL();
*sem = xSemaphoreCreateBinary();
xResult = check_synchronization_value(sem);
portEXIT_CRITICAL();
return xResult;
}
/*
* 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 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.
*/
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{
u32_t startTime;
LWIP_ASSERT("sem != NULL", sem != NULL);
startTime = sys_now();
if (timeout != 0U)
{
if (pdTRUE == xSemaphoreTake(*sem, pdMS_TO_TICKS(timeout)))
{
return get_elapsed_time(startTime);
}
else
{
return SYS_ARCH_TIMEOUT;
}
}
else /* must block without a timeout */
{
while (xSemaphoreTake(*sem, 10000) != pdTRUE)
{}
return get_elapsed_time(startTime);
}
}
/*
* Signals a semaphore
*/
void sys_sem_signal(sys_sem_t *sem)
{
LWIP_ASSERT("sem != NULL", sem != NULL);
(void)xSemaphoreGive(*sem);
}
/*
* Deallocates a semaphore
*/
void sys_sem_free(sys_sem_t *sem)
{
LWIP_ASSERT("sem != NULL", sem != NULL);
vSemaphoreDelete(*sem);
}
/*
* 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_t sys_mutex_new(sys_mutex_t *mutex)
{
LWIP_ASSERT("mutex != NULL", mutex != NULL);
portENTER_CRITICAL();
*mutex = xSemaphoreCreateMutex();
portEXIT_CRITICAL();
return check_synchronization_value(mutex);
}
/*
* Blocks the thread until the mutex can be grabbed.
*/
void sys_mutex_lock(sys_mutex_t *mutex)
{
LWIP_ASSERT("mutex != NULL", mutex != NULL);
while (xSemaphoreTake(*mutex, portMAX_DELAY) == pdFALSE)
{}
}
/*
* Releases the mutex previously locked through 'sys_mutex_lock()'
*/
void sys_mutex_unlock(sys_mutex_t *mutex)
{
LWIP_ASSERT("mutex != NULL", mutex != NULL);
(void)xSemaphoreGive(*mutex);
}
/*
* Deallocates a mutex
*/
void sys_mutex_free(sys_mutex_t *mutex)
{
LWIP_ASSERT("mutex != NULL", mutex != NULL);
vQueueDelete(*mutex);
}
/* Wrap a thread method and its argument for being passed to sys_thread_function */
struct threadfunc
{
lwip_thread_fn function;
void *arg;
};
/*
* Utility method that wraps a thread function call and provides a way to add
* initialization - eg per-thread semaphores
*/
static void sys_thread_function(void* arg)
{
struct threadfunc* t = (struct threadfunc*)arg;
#if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
netconn_thread_init();
#endif
(void)t->function(t->arg);
/* Regular FreeRTOS tasks don't return. But in case they do, make them exit cleanly. */
#if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
netconn_thread_cleanup();
#endif
mem_free(t);
sys_thread_delete(NULL);
}
/*
* 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.
*/
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
{
TaskHandle_t CreatedTask = NULL;
uint16_t stackDepth = ((uint16_t)stacksize != 0U) ? (uint16_t)stacksize : (uint16_t)configMINIMAL_STACK_SIZE;
struct threadfunc* tf = (struct threadfunc*)mem_malloc(sizeof (struct threadfunc));
LWIP_ASSERT("tf != NULL", tf != NULL);
tf->function = thread;
tf->arg = arg;
xTaskCreate(sys_thread_function, name, stackDepth, tf, prio, &CreatedTask);
return CreatedTask;
}
/*
* Deletes a thread
*/
void sys_thread_delete(sys_thread_t thread)
{
vTaskDelete(thread);
}
/*
* 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.
*/
sys_prot_t sys_arch_protect(void)
{
portENTER_CRITICAL();
return 0;
}
/*
* 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.
*/
void sys_arch_unprotect(sys_prot_t pval)
{
(void) pval;
portEXIT_CRITICAL();
}
#endif /* defined(USING_OS_FREERTOS) */
/*
* Initialize sys arch layer
*/
void sys_init(void)
{
#if NO_SYS
/* One dependency for FreeRTOS config file */
vTaskDelay(pdMS_TO_TICKS(delay));
#endif
}
/*
* Ticks/jiffies since power up
*/
u32_t sys_jiffies(void)
{
return (uint32_t)((((uint64_t) xTaskGetTickCount()) * 1000u) / configTICK_RATE_HZ);
}
/*
* Returns the current time in milliseconds.
*/
u32_t sys_now(void)
{
return (uint32_t)((((uint64_t) xTaskGetTickCount()) * 1000u) / configTICK_RATE_HZ);
}
2.2 文件修改
perf.h文件保持不動,對cc.h文件修改如下:
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <[email protected]>
*
*/
#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define LWIP_PROVIDE_ERRNO 1
/* Define generic types used in lwIP. */
typedef uint8_t u8_t;
typedef int8_t s8_t;
typedef uint16_t u16_t;
typedef int16_t s16_t;
typedef uint32_t u32_t;
typedef int32_t s32_t;
typedef uintptr_t mem_ptr_t;
typedef int sys_prot_t;
/* Define (sn)printf formatters for these lwIP types. */
#define X8_F "02x"
#define U16_F "hu"
#define S16_F "hd"
#define X16_F "hx"
#define U32_F "lu"
#define S32_F "ld"
#define X32_F "lx"
#define SZT_F "lu"
/* Little endian mode. */
#define BYTE_ORDER LITTLE_ENDIAN
/* define compiler specific symbols */
#if defined (__ICCARM__)
#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#define PACK_STRUCT_USE_INCLUDES
#elif defined (__CC_ARM)
#define PACK_STRUCT_BEGIN __packed
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#elif defined (__GNUC__)
#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#elif defined (__TASKING__)
#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#endif
#define LWIP_ERROR(message, expression, handler) do { if (!(expression)) { \
handler;} } while(0)
#ifndef LWIP_PLATFORM_DIAG
#define LWIP_PLATFORM_DIAG(x) do {} while(0)
#endif
#define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion failed: ");\
printf("Assertion failed: ");}while(0)
/* C runtime functions redefined */
#include <stdlib.h>
#define LWIP_RAND() ((u32_t)rand())
#define PPP_INCLUDE_SETTINGS_HEADER
#endif /* LWIP_ARCH_CC_H */
對lwipopts.h文件修改如下:
/**
* @file
*
* lwIP Options Configuration
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <[email protected]>
*
*/
#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H
/*
-----------------------------------------------
---------- Platform specific locking ----------
-----------------------------------------------
*/
/**
* NO_SYS==1: Provides VERY minimal functionality. Otherwise,
* use lwIP facilities.
*/
#define NO_SYS 0
#define USING_OS_FREERTOS
/**
* SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
* critical regions during buffer allocation, deallocation and memory
* allocation and deallocation.
*/
#define SYS_LIGHTWEIGHT_PROT (NO_SYS == 0)
/*
------------------------------------
---------- Memory options ----------
------------------------------------
*/
/**
* MEM_ALIGNMENT: should be set to the alignment of the CPU
* 4 byte alignment -> #define MEM_ALIGNMENT 4
* 2 byte alignment -> #define MEM_ALIGNMENT 2
*/
#define MEM_ALIGNMENT 4
/**
* MEM_SIZE: the size of the heap memory. If the application will send
* a lot of data that needs to be copied, this should be set high.
*/
#define MEM_SIZE (30 * 1024)
/*
------------------------------------------------
---------- Internal Memory Pool Sizes ----------
------------------------------------------------
*/
/**
* MEMP_NUM_PBUF: the number of memp struct pbufs (used for PBUF_ROM and PBUF_REF).
* If the application sends a lot of data out of ROM (or other static memory),
* this should be set high.
*/
#define MEMP_NUM_PBUF 30
/**
* MEMP_NUM_RAW_PCB: Number of raw connection PCBs
* (requires the LWIP_RAW option)
*/
#define MEMP_NUM_RAW_PCB 4
/**
* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
* per active UDP "connection".
* (requires the LWIP_UDP option)
*/
#define MEMP_NUM_UDP_PCB 4
/**
* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP connections.
* (requires the LWIP_TCP option)
*/
#define MEMP_NUM_TCP_PCB 8
/**
* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP connections.
* (requires the LWIP_TCP option)
*/
#define MEMP_NUM_TCP_PCB_LISTEN 8
/**
* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP segments.
* (requires the LWIP_TCP option)
*/
#define MEMP_NUM_TCP_SEG 16
/**
* MEMP_NUM_ARP_QUEUE: the number of simulateously queued outgoing
* packets (pbufs) that are waiting for an ARP request (to resolve
* their destination address) to finish.
* (requires the ARP_QUEUEING option)
*/
#define MEMP_NUM_ARP_QUEUE 2
/**
* MEMP_NUM_SYS_TIMEOUT: the number of simultaneously active timeouts.
* The default number of timeouts is calculated here for all enabled modules.
* The formula expects settings to be either '0' or '1'.
*
* To this default value, 1 was added for the snmp_increment timer.
*/
#define MEMP_NUM_SYS_TIMEOUT (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + PPP_SUPPORT + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) + 1
/**
* MEMP_NUM_NETBUF: the number of struct netbufs.
* (only needed if you use the sequential API, like api_lib.c)
*/
#define MEMP_NUM_NETBUF 16
/**
* MEMP_NUM_NETCONN: the number of struct netconns.
* (only needed if you use the sequential API, like api_lib.c)
*/
#define MEMP_NUM_NETCONN 32
/**
* MEMP_NUM_TCPIP_MSG_API: the number of struct tcpip_msg, which are used
* for callback/timeout API communication.
* (only needed if you use tcpip.c)
*/
#define MEMP_NUM_TCPIP_MSG_API 8
/**
* MEMP_NUM_TCPIP_MSG_INPKT: the number of struct tcpip_msg, which are used
* for incoming packets.
* (only needed if you use tcpip.c)
*/
#define MEMP_NUM_TCPIP_MSG_INPKT 8
/**
* PBUF_POOL_SIZE: the number of buffers in the pbuf pool.
*/
#define PBUF_POOL_SIZE 32
/**
* MEMP_OVERFLOW_CHECK: Memory pool overflow check switch.
*/
#define MEMP_OVERFLOW_CHECK 0
/*
---------------------------------
---------- ARP options ----------
---------------------------------
*/
/**
* LWIP_ARP==1: Enable ARP functionality.
*/
#define LWIP_ARP 1
#define ARP_TABLE_SIZE 10 /* ARP Table size */
#define ARP_QUEUEING 1 /* ARP Queueing */
/*
--------------------------------
---------- IP options ----------
--------------------------------
*/
/**
* IP_FORWARD==1: Enables the ability to forward IP packets across network
* interfaces. If you are going to run lwIP on a device with only one network
* interface, define this to 0.
*/
#define IP_FORWARD 0
/**
* IP_OPTIONS: Defines the behavior for IP options.
* IP_OPTIONS==0_ALLOWED: All packets with IP options are dropped.
* IP_OPTIONS==1_ALLOWED: IP options are allowed (but not parsed).
*/
#define IP_OPTIONS_ALLOWED 1
/**
* IP_REASSEMBLY==1: Reassemble incoming fragmented IP packets. Note that
* this option does not affect outgoing packet sizes, which can be controlled
* via IP_FRAG.
*/
#define IP_REASSEMBLY 1
#define MEMP_NUM_REASSDATA 10
/**
* IP_FRAG==1: Fragment outgoing IP packets if their size exceeds MTU. Note
* that this option does not affect incoming packet sizes, which can be
* controlled via IP_REASSEMBLY.
*/
#define IP_FRAG 1
/**
* IP_REASS_MAXAGE: Maximum time (in multiples of IP_TMR_INTERVAL - so seconds, normally)
* a fragmented IP packet waits for all fragments to arrive. If not all fragments arrived
* in this time, the whole packet is discarded.
*/
#define IP_REASS_MAXAGE 3
/**
* IP_REASS_MAX_PBUFS: Total maximum amount of pbufs waiting to be reassembled.
* Since the received pbufs are enqueued, be sure to configure
* PBUF_POOL_SIZE > IP_REASS_MAX_PBUFS so that the stack is still able to receive
* packets even if the maximum amount of fragments is enqueued for reassembly!
*/
#define IP_REASS_MAX_PBUFS 10
/**
* IP_FRAG_USES_STATIC_BUF==1: Use a static MTU-sized buffer for IP
* fragmentation. Otherwise pbufs are allocated and reference the original
* packet data to be fragmented.
*/
#define IP_FRAG_USES_STATIC_BUF 0
/**
* IP_DEFAULT_TTL: Default value for Time-To-Live used by transport layers.
*/
#define IP_DEFAULT_TTL 255
/*
----------------------------------
---------- ICMP options ----------
----------------------------------
*/
/**
* LWIP_ICMP==1: Enable ICMP module inside the IP stack.
* Be careful, disable that make your product non-compliant to RFC1122
*/
#define LWIP_ICMP 1
/**
* ICMP_TTL: Default value for Time-To-Live used by ICMP packets.
*/
#define ICMP_TTL (IP_DEFAULT_TTL)
/*
----------------------------------
---------- DHCP options ----------
----------------------------------
*/
/**
* LWIP_DHCP==1: Enable DHCP module.
*/
#define LWIP_DHCP 0
/*
------------------------------------
---------- AUTOIP options ----------
------------------------------------
*/
/**
* LWIP_AUTOIP==1: Enable AUTOIP module.
*/
#define LWIP_AUTOIP 0
/*
----------------------------------
---------- SNMP options ----------
----------------------------------
*/
/**
* LWIP_SNMP==1: Turn on SNMP module. UDP must be available for SNMP
* transport.
*/
#define LWIP_SNMP (LWIP_UDP)
#define MIB2_STATS (LWIP_SNMP)
/*
----------------------------------
---------- IGMP options ----------
----------------------------------
*/
/**
* LWIP_IGMP==1: Turn on IGMP module.
*/
#define LWIP_IGMP 1
/*
----------------------------------
---------- DNS options -----------
----------------------------------
*/
/**
* LWIP_DNS==1: Turn on DNS module. UDP must be available for DNS
* transport.
*/
#define LWIP_DNS (LWIP_UDP)
/*
---------------------------------
---------- UDP options ----------
---------------------------------
*/
/**
* LWIP_UDP==1: Turn on UDP.
*/
#define LWIP_UDP 1
/**
* LWIP_UDPLITE==1: Turn on UDP-Lite. (Requires LWIP_UDP)
*/
#define LWIP_UDPLITE (LWIP_UDP)
/**
* UDP_TTL: Default Time-To-Live value.
*/
#define UDP_TTL (IP_DEFAULT_TTL)
/*
---------------------------------
---------- TCP options ----------
---------------------------------
*/
/**
* LWIP_TCP==1: Turn on TCP.
*/
#define LWIP_TCP 1
#define TCP_TTL 255
/* Controls if TCP should queue segments that arrive out of
order. Define to 0 if your device is low on memory. */
#define TCP_QUEUE_OOSEQ 0
/* TCP Maximum segment size. */
#define TCP_MSS (1500 - 40)
/* TCP sender buffer space (bytes). */
#define TCP_SND_BUF (4 * TCP_MSS)
/* TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at
least as much as (4 * TCP_SND_BUF/TCP_MSS) for things to work. */
#define TCP_SND_QUEUELEN (4 * TCP_SND_BUF / TCP_MSS)
/* TCP receive window. */
#define TCP_WND (4 * TCP_MSS)
/* TCP writable space (bytes). This must be less than or equal
to TCP_SND_BUF. It is the amount of space which must be
available in the tcp snd_buf for select to return writable */
#define TCP_SNDLOWAT (TCP_SND_BUF / 2)
#define TCP_MAXRTX 12 /* Maximum number of retransmissions of data segments. */
#define TCP_SYNMAXRTX 4 /* Maximum number of retransmissions of SYN segments. */
/*
----------------------------------
---------- Pbuf options ----------
----------------------------------
*/
/**
* PBUF_LINK_HLEN: the number of bytes that should be allocated for a
* link level header. The default is 14, the standard value for
* Ethernet.
*/
#define PBUF_LINK_HLEN 16
/*
------------------------------------
---------- LOOPIF options ----------
------------------------------------
*/
/**
* LWIP_HAVE_LOOPIF==1: Support loop interface (127.0.0.1) and loopif.c
*/
#define LWIP_HAVE_LOOPIF 0
/*
---------------------------------
---------- RAW options ----------
---------------------------------
*/
/**
* LWIP_RAW==1: Enable application layer to hook into the IP layer itself.
*/
#define LWIP_RAW 1
/*
------------------------------------
---------- Socket options ----------
------------------------------------
*/
/**
* LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
*/
#define LWIP_SOCKET 1
#define LWIP_SOCKET_SET_ERRNO (LWIP_SOCKET)
/*
------------------------------------
---------- Netconn options ----------
------------------------------------
*/
/**
* LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
*/
#define LWIP_NETCONN 1
#define LWIP_NETCONN_SEM_PER_THREAD (LWIP_NETCONN || LWIP_SOCKET)
/*
----------------------------------------
---------- Statistics options ----------
----------------------------------------
*/
#define TCPIP_MBOX_SIZE 10
#define DEFAULT_UDP_RECVMBOX_SIZE 10
#define DEFAULT_TCP_RECVMBOX_SIZE 10
#define DEFAULT_RAW_RECVMBOX_SIZE 10
#define DEFAULT_ACCEPTMBOX_SIZE 10
#define LWIP_NETIF_TX_SINGLE_PBUF 1
#define LWIP_SUPPORT_CUSTOM_PBUF 1
#define MEMP_USE_CUSTOM_POOLS 0
#define MEM_USE_POOLS 0
/* Self-define data. */
/* LWIP MAC address define. */
#define LWIP_MAC_ADDR 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
/* LWIP net host name define. */
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETIF_HOSTNAME_TEXT "TriCore"
/* LWIP use IPV4 address. */
#define LWIP_IPV4 1
/* Ethernet link speed define. */
#define LWIP_LINK_SPEED_IN_BPS 100000000
/* LWIP gateway, IP address and netmask define. */
#define LWIP_INIT_IPADDR(addr) IP4_ADDR((addr), 192,168,1,20)
#define LWIP_INIT_NETMASK(addr) IP4_ADDR((addr), 255,255,255,0)
#define LWIP_INIT_GW(addr) IP4_ADDR((addr), 192,168,1,1)
/*
---------------------------------------
---------- Debugging options ----------
---------------------------------------
*/
#define TAPIF_DEBUG LWIP_DBG_OFF
#define TUNIF_DEBUG LWIP_DBG_OFF
#define UNIXIF_DEBUG LWIP_DBG_OFF
#define DELIF_DEBUG LWIP_DBG_OFF
#define SIO_FIFO_DEBUG LWIP_DBG_OFF
#define TCPDUMP_DEBUG LWIP_DBG_OFF
#define API_LIB_DEBUG LWIP_DBG_OFF
#define API_MSG_DEBUG LWIP_DBG_OFF
#define TCPIP_DEBUG LWIP_DBG_OFF
#define NETIF_DEBUG LWIP_DBG_OFF
#define SOCKETS_DEBUG LWIP_DBG_OFF
#define DEMO_DEBUG LWIP_DBG_OFF
#define IP_DEBUG LWIP_DBG_OFF
#define IP_REASS_DEBUG LWIP_DBG_OFF
#define RAW_DEBUG LWIP_DBG_OFF
#define ICMP_DEBUG LWIP_DBG_OFF
#define UDP_DEBUG LWIP_DBG_OFF
#define TCP_DEBUG LWIP_DBG_OFF
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
#define TCP_RTO_DEBUG LWIP_DBG_OFF
#define TCP_CWND_DEBUG LWIP_DBG_OFF
#define TCP_WND_DEBUG LWIP_DBG_OFF
#define TCP_FR_DEBUG LWIP_DBG_OFF
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
#define TCP_RST_DEBUG LWIP_DBG_OFF
#define LWIP_DBG_TYPES_ON (LWIP_DBG_ON | LWIP_DBG_TRACE | \
LWIP_DBG_STATE | LWIP_DBG_FRESH \
|LWIP_DBG_HALT)
#endif /* LWIP_LWIPOPTS_H */
2.3 函數修改
自定義LwIP初始化函數,此函數需在FreeRTOS任務調度器運行起來之後調用:
/**
* Abstract:
* LwIP stack initial function.
*
* Parameters:
* None.
*
* Return:
* None.
*/
void LwIP_Init(void)
{
/* TCP/IP service init process. */
tcpip_init(NULL, NULL);
#if LWIP_IPV4
ip4_addr_t ipaddr, netmask, gw;
#if (!LWIP_DHCP) && (!LWIP_AUTOIP)
LWIP_INIT_GW(&gw);
LWIP_INIT_IPADDR(&ipaddr);
LWIP_INIT_NETMASK(&netmask);
#endif /* (!LWIP_DHCP) && (!LWIP_AUTOIP) */
#endif /* LWIP_IPV4 */
/* Config netif layer parameters. */
netif_set_default(netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input));
/* Check to set netif up flag. */
if (netif_is_link_up(&gnetif))
{
/* If netif link is fully configured, this function must be called. */
netif_set_up(&gnetif);
}
else
{
/* If netif link is down, this function must be called. */
netif_set_down(&gnetif);
}
}
low_level_init函數:
static void low_level_init(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state;
uint8_t aMacAddr[NETIF_MAX_HWADDR_LEN] = {LWIP_MAC_ADDR};
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
/* set MAC hardware address */
netif->hwaddr[0] = aMacAddr[0];
netif->hwaddr[1] = aMacAddr[1];
netif->hwaddr[2] = aMacAddr[2];
netif->hwaddr[3] = aMacAddr[3];
netif->hwaddr[4] = aMacAddr[4];
netif->hwaddr[5] = aMacAddr[5];
/* maximum transfer unit */
netif->mtu = 1500;
/* Device ethernet port set. */
netif->num = ETHERNET_PORT0;
/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if LWIP_IPV6 && LWIP_IPV6_MLD
/*
* For hardware/netifs that implement MAC filtering.
* All-nodes link-local is handled by default, so we must let the hardware know
* to allow multicast packets in.
* Should set mld_mac_filter previously. */
if (netif->mld_mac_filter != NULL) {
ip6_addr_t ip6_allnodes_ll;
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
}
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
/* Do whatever else is needed to initialize interface. */
/* Create a semaphore. */
s_xSemaphore = xSemaphoreCreateCounting(40, 0);
/* Create the task that handles the Ethernet module. */
sys_thread_new("LwIPRun", ethernetif_input, netif, 1024, 1);
/* Ethernet module init. */
DevEthInit(netif->num);
}
建議網絡數據接收採用中斷方式,這樣可藉助於FreeRTOS提供的信號機制,實時通知和讀取網絡數據。如下所示:
網絡端口中斷處理函數:
/**
* Abstract:
* Ethernet module interrupt handler function.
*
* Parameters:
* i32Arg: Arguments passing into the function.
*
* Return:
* None.
*/
static void IsrEthHandler(int i32Arg)
{
uint32_t u32Ret = 0;
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
/* Just to avoid compiler warnings about unused parameters. */
(void) i32Arg;
u32Ret = taskENTER_CRITICAL_FROM_ISR();
/* Check for ethernet RX interrupt. */
if (TRUE == IfxEth_isRxInterrupt(&sEth))
{
xSemaphoreGiveFromISR(s_xSemaphore, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
/* Clear Rx interrupt status bit. */
IfxEth_clearRxInterrupt(&sEth);
}
/* Check for ethernet TX interrupt. */
if (TRUE == IfxEth_isTxInterrupt(&sEth))
{
/* Clear Tx interrupt status bit. */
IfxEth_clearTxInterrupt(&sEth);
}
taskEXIT_CRITICAL_FROM_ISR(u32Ret);
}
LwIP鏈路層數據接收函數:
static void ethernetif_input(struct netif *netif)
{
struct eth_hdr *ethhdr = NULL;
struct pbuf *p = NULL;
while (1)
{
//vTaskDelay(pdMS_TO_TICKS(15));
if (pdTRUE == xSemaphoreTake(s_xSemaphore, portMAX_DELAY))
{
/* Move received packet into a new buf. */
taskENTER_CRITICAL();
p = low_level_input(netif);
taskEXIT_CRITICAL();
/* if no packet could be read, silently ignore this */
if (p != NULL)
{
taskENTER_CRITICAL();
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK)
{
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
p = NULL;
}
taskEXIT_CRITICAL();
}
}
}
}
至此,基於FreeRTOS移植LwIP的工作已完成,在項目工程中可以利用socket接口或者netconn接口進行網口編程了。若要進行測試,可添加LwIP源碼包提供的“contrib-2.0.1\apps\”目錄下的例程進行測試,可根據需要選擇要進行測試的app,將選擇的app文件夾添加到工程目錄中,在low_level_init函數中“sys_thread_new(“LwIPRun”, ethernetif_input, netif, 1024, 1);”後調用app提供的初始化函數,便可進行相應測試。