【LwIP】移植(FreeRTOS)

基于操作系统FreeRTOS的移植又比我想象的复杂一点,虽然前面的文章中移植的LwIP的工程也是基于FreeRTOS系统的,但是将所有网络操作都放在了同一个线程中,相当于模拟了无操作系统的情况,使用的是RAW API进行程序设计的。使用RAW API有一个非常非常局限的地方,就是不能再不同的上下文环境(Context)下同时调用系统API,就像LwIP自己说的:Use lwIP without OS-awareness (no thread, semaphores, mutexes or mboxes). This means threaded APIs cannot be used (socket, netconn, i.e. everything in the 'api' folder), only the callback-style raw API is available (and you have to watch out for yourself that you don't access lwIP functions/structures from more than one context at a time!)。最后一句话说得很清楚,我们不能再多个线程中同时调用LwIP的API函数和结构,因为它们都是线程不安全的(thread unsafe),通俗点举例,我们不能同时在两个不同的线程中创建两个TCP连接,否则容易产生BUG。

这个问题是十分棘手的,我们需要在一个线程中完成所有网络操作,包括网卡数据包的接收、运行TCPIP协议栈、创建TCP/UDP连接、接收发送网络数据以及与其它应用线程进行通讯。LwIP的作者早就意识到这个问题了,他们设计了两种高层API接口:Sequential API、BSD socket API,本文只说明Sequential API的移植。LwIP的作者创建了一个tcpip线程,主要完成前面我说的网络操作,并且提供了非常友好的API接口,使得应用程序的设计相比于RAW API变得十分简洁明了。下面是移植Sequential API版本的LwIP的步骤:

1、想要使用Sequential API,我么需要先在LwIP的配置文件lwipopts.h中将NO_SYS设置成0,LWIP_NETCONN设置成1。然后将LwIP的源码中的api文件夹中的所有文件都添加到工程中去(不管用没用到,没用到的不会被编译)。

2、创建一个线程,主要用于接收网卡数据,注意这个线程需要在网卡硬件初始化完成之后才能执行。

void Task_network( void *pvParameters )
{
	printf("Task network started\r\n");
	TickType_t xLastWakeTime;
	xLastWakeTime = xTaskGetTickCount();
	while(1)
	{  
		while(ETH_CheckFrameReceived())
		{ 
			/* process received ethernet packet */
			ethernetif_input(&netif_st);
		}
		vTaskDelayUntil(&xLastWakeTime,10);	//10ms运行周期
	}
}

3、创建sys_arch.c、sys_arch.h文件,这两个文件是与操作系统相关的,实现了sys.h头文件中定义的一些操作系统相关的接口函数,LwIP 1.4.1版本的sys.h中定义了一些需要用户定义的函数,信号量相关的函数定义:

/* Semaphore functions: */

/** Create a new semaphore
 * @param sem pointer to the semaphore to create
 * @param count initial count of the semaphore
 * @return ERR_OK if successful, another err_t otherwise */
err_t sys_sem_new(sys_sem_t *sem, u8_t count);
/** Signals a semaphore
 * @param sem the semaphore to signal */
void sys_sem_signal(sys_sem_t *sem);
/** Wait for a semaphore for the specified timeout
 * @param sem the semaphore to wait for
 * @param timeout timeout in milliseconds to wait (0 = wait forever)
 * @return time (in milliseconds) waited for the semaphore
 *         or SYS_ARCH_TIMEOUT on timeout */
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
/** Delete a semaphore
 * @param sem semaphore to delete */
void sys_sem_free(sys_sem_t *sem);
/** Wait for a semaphore - forever/no timeout */
#define sys_sem_wait(sem)                  sys_arch_sem_wait(sem, 0)
#ifndef sys_sem_valid
/** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */
int sys_sem_valid(sys_sem_t *sem);
#endif
#ifndef sys_sem_set_invalid
/** Set a semaphore invalid so that sys_sem_valid returns 0 */
void sys_sem_set_invalid(sys_sem_t *sem);
#endif

/* Time functions. */
#ifndef sys_msleep
void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */
#endif

消息邮箱相关函数定义:

/* Mailbox functions. */

/** Create a new mbox of specified size
 * @param mbox pointer to the mbox to create
 * @param size (miminum) number of messages in this mbox
 * @return ERR_OK if successful, another err_t otherwise */
err_t sys_mbox_new(sys_mbox_t *mbox, int size);
/** Post a message to an mbox - may not fail
 * -> blocks if full, only used from tasks not from ISR
 * @param mbox mbox to posts the message
 * @param msg message to post (ATTENTION: can be NULL) */
void sys_mbox_post(sys_mbox_t *mbox, void *msg);
/** Try to post a message to an mbox - may fail if full or ISR
 * @param mbox mbox to posts the message
 * @param msg message to post (ATTENTION: can be NULL) */
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
/** Wait for a new message to arrive in the mbox
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @param timeout maximum time (in milliseconds) to wait for a message
 * @return time (in milliseconds) waited for a message, may be 0 if not waited
           or SYS_ARCH_TIMEOUT on timeout
 *         The returned time has to be accurate to prevent timer jitter! */
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
/* Allow port to override with a macro, e.g. special timout for sys_arch_mbox_fetch() */
#ifndef sys_arch_mbox_tryfetch
/** Wait for a new message to arrive in the mbox
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @param timeout maximum time (in milliseconds) to wait for a message
 * @return 0 (milliseconds) if a message has been received
 *         or SYS_MBOX_EMPTY if the mailbox is empty */
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
#endif
/** For now, we map straight to sys_arch implementation. */
#define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
/** Delete an mbox
 * @param mbox mbox to delete */
void sys_mbox_free(sys_mbox_t *mbox);
#define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
#ifndef sys_mbox_valid
/** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */
int sys_mbox_valid(sys_mbox_t *mbox);
#endif
#ifndef sys_mbox_set_invalid
/** Set an mbox invalid so that sys_mbox_valid returns 0 */
void sys_mbox_set_invalid(sys_mbox_t *mbox);
#endif

线程相关函数定义:

/** The only thread function:
 * Creates a new thread
 * @param name human-readable name for the thread (used for debugging purposes)
 * @param thread thread-function
 * @param arg parameter passed to 'thread'
 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
 * @param prio priority of the new thread (may be ignored by ports) */
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);

在sys_arch.h文件中定义信号量、消息邮箱以及任务的数据类型:

typedef xSemaphoreHandle sys_sem_t;
typedef xQueueHandle sys_mbox_t;
typedef xTaskHandle sys_thread_t;

在sys_arch.c文件中实现RTOS相关函数(现成的一个sys_arch.c函数进行大量修改得到):

/*
 * 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]>
 *
 */

/* lwIP includes. */
#include "lwip/debug.h"
#include "lwip/def.h"
#include "lwip/sys.h"
#include "lwip/mem.h"
#include "lwip/stats.h"
#include "FreeRTOS.h"
#include "task.h"


xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION;

// struct timeoutlist
// {
// 	struct sys_timeouts timeouts;
// 	xTaskHandle pid;
// };

/* This is the number of threads that can be started with sys_thread_new() */
#define SYS_THREAD_MAX 6

// static struct timeoutlist s_timeoutlist[SYS_THREAD_MAX];
static u16_t s_nextthread = 0;


/*-----------------------------------------------------------------------------------*/
//  Creates an empty mailbox.
// sys_mbox_t sys_mbox_new(int size)
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
{
	*mbox = xQueueCreate( size, sizeof( void * ) );

#if SYS_STATS
      ++lwip_stats.sys.mbox.used;
      if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) {
         lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
	  }
#endif /* SYS_STATS */

	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)
{
	if( uxQueueMessagesWaiting( *mbox ) )
	{
		/* Line for breakpoint.  Should never break here! */
		portNOP();
#if SYS_STATS
	    lwip_stats.sys.mbox.err++;
#endif /* SYS_STATS */
			
		// TODO notify the user of failure.
	}

	vQueueDelete( *mbox );

#if SYS_STATS
     --lwip_stats.sys.mbox.used;
#endif /* SYS_STATS */
}

int sys_mbox_valid(sys_mbox_t *mbox)
{
	if(*mbox)
		return 1;
	else
		return 0;
}
void sys_mbox_set_invalid(sys_mbox_t *mbox)
{
// 	vQueueDelete(*mbox);
// 	*mbox = NULL;
}
int sys_sem_valid(sys_sem_t *sem)
{
	if(*sem)
		return 1;
	else
		return 0;
}
void sys_sem_set_invalid(sys_sem_t *sem)
{
// 	vSemaphoreDelete(*sem);
// 	*sem = NULL;
}



/*-----------------------------------------------------------------------------------*/
//   Posts the "msg" to the mailbox.
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
{
	while ( xQueueSendToBack(*mbox, &msg, portMAX_DELAY ) != pdTRUE ){}
}


/*-----------------------------------------------------------------------------------*/
//   Try to post the "msg" to the mailbox.
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
{
err_t result;

   if ( xQueueSend( *mbox, &msg, 0 ) == pdPASS )
   {
      result = ERR_OK;
   }
   else {
      // could not post, queue must be full
      result = ERR_MEM;
			
#if SYS_STATS
      lwip_stats.sys.mbox.err++;
#endif /* SYS_STATS */
			
   }

   return result;
}

/*-----------------------------------------------------------------------------------*/
/*
  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 *dummyptr;
portTickType StartTime, EndTime, Elapsed;

	StartTime = xTaskGetTickCount();

	if ( msg == NULL )
	{
		msg = &dummyptr;
	}
		
	if ( timeout != 0 )
	{
		if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) )
		{
			EndTime = xTaskGetTickCount();
			Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
			
			return ( Elapsed );
		}
		else // timed out blocking for message
		{
			*msg = NULL;
			
			return SYS_ARCH_TIMEOUT;
		}
	}
	else // block forever for a message.
	{
		while( pdTRUE != xQueueReceive( *mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary
		EndTime = xTaskGetTickCount();
		Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
		
		return ( Elapsed ); // return time blocked TODO test	
	}
}

/*-----------------------------------------------------------------------------------*/
/*
  Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll
  return with SYS_MBOX_EMPTY.  On success, 0 is returned.
*/
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
void *dummyptr;

	if ( msg == NULL )
	{
		msg = &dummyptr;
	}

   if ( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) )
   {
      return ERR_OK;
   }
   else
   {
      return SYS_MBOX_EMPTY;
   }
}

/*-----------------------------------------------------------------------------------*/
//  Creates and returns a new semaphore. The "count" argument specifies
//  the initial state of the semaphore.
err_t sys_sem_new(sys_sem_t *xSemaphore, u8_t count)
{
	*xSemaphore = xSemaphoreCreateBinary();
	
	if( *xSemaphore == NULL )
	{
		
#if SYS_STATS
      ++lwip_stats.sys.sem.err;
#endif /* SYS_STATS */
			
		return ERR_BUF;	// TODO need assert
	}
	
	if(count == 0)	// Means it can't be taken
	{
		xSemaphoreTake(*xSemaphore,0);
	}
	else
	{
		xSemaphoreGive( *xSemaphore );
	}

#if SYS_STATS
	++lwip_stats.sys.sem.used;
 	if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {
		lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
	}
#endif /* SYS_STATS */
		
	return ERR_OK;
}

/*-----------------------------------------------------------------------------------*/
/*
  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)
{
portTickType StartTime, EndTime, Elapsed;

	StartTime = xTaskGetTickCount();

	if(	timeout != 0)
	{
		if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE )
		{
			EndTime = xTaskGetTickCount();
			Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
			
			return (Elapsed); // return time blocked TODO test	
		}
		else
		{
			return SYS_ARCH_TIMEOUT;
		}
	}
	else // must block without a timeout
	{
// 		while( xSemaphoreTake( *sem, portMAX_DELAY ) != pdTRUE ){}
		xSemaphoreTake( *sem, portMAX_DELAY );
		EndTime = xTaskGetTickCount();
		Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;

		return ( Elapsed ); // return time blocked	
		
	}
}

/*-----------------------------------------------------------------------------------*/
// Signals a semaphore
void sys_sem_signal(sys_sem_t *sem)
{
	xSemaphoreGive( *sem );
}

/*-----------------------------------------------------------------------------------*/
// Deallocates a semaphore
void sys_sem_free(sys_sem_t *sem)
{
#if SYS_STATS
      --lwip_stats.sys.sem.used;
#endif /* SYS_STATS */
			
	vQueueDelete( *sem );
}

/*-----------------------------------------------------------------------------------*/
// Initialize sys arch
void sys_init(void)
{
	int i;

	// Initialize the the per-thread sys_timeouts structures
	// make sure there are no valid pids in the list
	for(i = 0; i < SYS_THREAD_MAX; i++)
	{
// 		s_timeoutlist[i].pid = 0;
// 		s_timeoutlist[i].timeouts.next = NULL;
	}

	// keep track of how many threads have been created
	s_nextthread = 0;
}

/*-----------------------------------------------------------------------------------*/
/*
  Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
  each thread has a list of timeouts which is represented as a linked
  list of sys_timeout structures. The sys_timeouts structure holds a
  pointer to a linked list of timeouts. This function is called by
  the lwIP timeout scheduler and must not return a NULL value.

  In a single threaded sys_arch implementation, this function will
  simply return a pointer to a global sys_timeouts variable stored in
  the sys_arch module.
*/
// struct sys_timeouts *sys_arch_timeouts(void)
// {
// int i;
// xTaskHandle pid;
// struct timeoutlist *tl;

// 	pid =  xTaskGetCurrentTaskHandle();
//               

// 	for(i = 0; i < s_nextthread; i++)
// 	{
// 		tl = &(s_timeoutlist[i]);
// 		if(tl->pid == pid)
// 		{
// 			return &(tl->timeouts);
// 		}
// 	}

// 	// Error
// 	return NULL;
// }

/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
// TODO
/*-----------------------------------------------------------------------------------*/
/*
  Starts a new thread 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 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)
{
xTaskHandle CreatedTask;
int result;

   if ( s_nextthread < SYS_THREAD_MAX )
   {
      result = xTaskCreate( thread, ( portCHAR * ) name, stacksize, arg, prio, &CreatedTask );

	   // For each task created, store the task handle (pid) in the timers array.
	   // This scheme doesn't allow for threads to be deleted
// 	   s_timeoutlist[s_nextthread++].pid = CreatedTask;

	   if(result == pdPASS)
	   {
		   return CreatedTask;
	   }
	   else
	   {
		   return NULL;
	   }
   }
   else
   {
      return NULL;
   }
}

/*
  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)
{
	vPortEnterCritical();
	return 1;
}

/*
  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;
	vPortExitCritical();
}

/*
 * Prints an assertion messages and aborts execution.
 */
void sys_assert( const char *msg )
{	
	( void ) msg;
	/*FSL:only needed for debugging
	printf(msg);
	printf("\n\r");
	*/
    vPortEnterCritical(  );
    for(;;)
    ;
}

上面的移植工作有许多需要注意的地方,例如sys_sem_new函数中需要判断如果count的值非0,需要执行一次xSemaphoreGive操作,因为默认创建的信号量的值为0,否则LwIP将会卡死在获取信号量操作上面,还有大部分函数的传入参数都是指针类型的,在函数体内的操作需要注意。然后函数sys_mbox_valid、sys_mbox_set_invalid、sys_sem_valid、sys_sem_set_invalid的具体含义并没有搞懂具体意义,所以干脆没有实现,可能是需要创建一个新的信号量结构体其中包含一个原来的FreeRTOS的信号量和一个标志位(用于表示该信号量是否Valid)。

编译正常,会发现系统无法正常运行,会卡死在某些地方,进过都次一步步的debug发现都是在调用FreeRTOS的API的时候出现参数错误等问题,例如创建任务时传入的stacksize为0,这就会导致系统卡死,而传入的stacksize是由TCPIP_THREAD_STACKSIZE决定的,所以需要重定义TCPIP_THREAD_STACKSIZE的值。下面是一些需要重定义的宏定义:

#define TCPIP_THREAD_STACKSIZE          512
#define TCPIP_THREAD_PRIO               1
#define TCPIP_MBOX_SIZE                 1024
#define DEFAULT_RAW_RECVMBOX_SIZE       1024
#define DEFAULT_UDP_RECVMBOX_SIZE       1024
#define DEFAULT_TCP_RECVMBOX_SIZE       1024

为什么重定义上面这几个宏呢?因为运行时需要这几个宏,如果保持opt.h头文件中的默认值(都是0)的话,会导致错误。到这里基本上算是移植完成了,具体还有什么漏掉了,导致有什么BUG,后面的测试会继续完善。下面进行测试:

void cb_tcpip_init_done(void *arg)
{
	printf("%s\r\n",__FUNCTION__);
	*(int*)arg = 0;
}

static void Task2( void *pvParameters )
{
	printf("%s\r\n",__FUNCTION__);

	ETH_BSP_Config();
	int flag = 1;
	tcpip_init(cb_tcpip_init_done,&flag);
	while(flag)
		vTaskDelay(10);
	LwIP_Init();
	
    xTaskCreate( Task_network, "Task network", 512, NULL, 2, NULL );
	
	struct netconn *p = netconn_new(NETCONN_TCP);
	printf("%.8X\r\n",p);
	struct ip_addr ipaddr;
	IP4_ADDR(&ipaddr,192,168,2,66);           //local IP地址
	netconn_bind(p,&ipaddr,5678);
	
	IP4_ADDR(&ipaddr,192,168,2,1);           //服务器IP地址
	netconn_connect(p,&ipaddr,8888);
	printf("connected\r\n");
	netconn_write(p,"asddsa",6,NETCONN_COPY);
	
	while(1)
	{
// 		printf("%s\r\n",__FUNCTION__);
		vTaskDelay(1000);
	}
}

测试结果没问题:

思考了一个问题,上面的移植工作中,有至少两个任务在运行,一个是负责网卡数据接收的Task_network,一个是负责Sequential API的tcpip_thread,这会不会导致线程间的不安全调用发生呢?仔细看看ethernetif_input函数,里面有两个地方会有线程不安全操作的嫌疑,一个是在low_level_input函数中分配内存块的操作,一个是调用netif->input函数,这里netif->input在初始化的时候被指向了ethernet_input函数,而ethernet_input函数在之前没有操作系统时候使用的,那么在使用操作系统之后,是不是还能使用这个函数呢?得不到一个直接的答案,参考网络上大神“老衲五木”的文章中的一张图片:

图中说明了,网卡在收到数据的时候调用ethernetif_input函数,而ethernetif_input函数调用的是tcpip_input函数,而不是我前面设置的ethernet_input函数,于是我将我的测试程序中的网卡初始化的时候将网卡的input函数指向到tcpip_input函数,测试效果和之前一样,那么这个input函数的为什么设置成ethernet_input依然能够在Sequential API模式下运行呢?

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章