嵌入式及其多媒体技术的专栏

终于开始这部分的工作,计划了很久,但一直都没有实施。以前一直只使用TCP/IP但对其处理的流程确一知半解。计划拿出几天的时间好好的学习下,理解其运行的基本原理。

一个嵌入式的网络架构一般都由3部分构成:1、应用层。2、协议层。3、网卡层驱动。为了较好的理解lwip的架构。我们从应用层开始一层一层的剥开整个过程,了解整个系统时怎样串联起来的,

首先是应用层,这里会初始化网络参数。参见下面用户初始化网络的代码:

tcpip_init(tcpip_init_done_signal,(void *) &tcpipdone);

IP4_ADDR(&gw, 10, 1, 10, 1);

IP4_ADDR(&ipaddr,10, 1, 10, 234);

IP4_ADDR(&netmask,255, 255, 255, 0);

/* Add netif interfacefor lpc17xx_8x */

if(!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL,lpc_enetif_init,

tcpip_input)) {

LWIP_ASSERT("Netinterface failed to initialize\r\n", 0);

}

netif_set_default(&lpc_netif);

netif_set_up(&lpc_netif);

这是初始化网络代码的一部分。其中最重要的是tcpip_init()和net_add()函数。

串联其中的就是lpc_netif变量,它是一个struct netif结构体,一般一个网卡对应一个struct netif结构,并用一个list将所有网卡链接起来构成一个链表。首先先看看这个接口的定义:

struct netif {

struct netif *next;

ip_addr_t ip_addr;/**网卡的配置参数**/

ip_addr_t netmask;

ip_addr_t gw;

/** This function iscalled by the network device driver

* to pass a packet up the TCP/IP stack. */

netif_input_fn input;

/** This functionis called by the IP module when it wants

* to send a packet on the interface. Thisfunction typically

* first resolves the hardware address, thensends the packet. */

netif_output_fn output;

/** This function is called by the ARP modulewhen it wants

* to send a packet on the interface. Thisfunction outputs

* the pbuf as-is on the link medium. */

netif_linkoutput_fn linkoutput;

};

在tcpip_init()中会有一个重要的调用既创建了tcpip_thread()的线程。 我们来看看这个函数到底做了些什么呢?

void tcpip_init(tcpip_init_done_fninitfunc, void *arg)

{

lwip_init();

if(sys_mbox_new(&mbox,TCPIP_MBOX_SIZE) != ERR_OK) {

LWIP_ASSERT("failedto create tcpip_thread mbox", 0);

}

#if LWIP_TCPIP_CORE_LOCKING

if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {

LWIP_ASSERT("failed to createlock_tcpip_core", 0);

}

#endif /* LWIP_TCPIP_CORE_LOCKING */

sys_thread_new(TCPIP_THREAD_NAME ,tcpip_thread,NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);

}

我从中可以看到调用了3个函数:

1、lwip_init();初始化了lwip需要要用到的一些参数。

2、sys_mutex_new()创建了一个新的互斥锁。

3 sys_thread_new()新建了一个线程处理tcpip协议栈。在这个线程中你会看到线程会阻塞在读mbox的函数上,直到网卡接收到数据才会继续执行下去。详细的代码请参看tcpip.c文件。

下面我们在来看看另一函数net_add(),我们先看它的第六个参数:lpc_enetif_init(),这个参数是一个函数指针,它是lpc mac层的驱动程序中的一个函数。我们在来看看这个函数中到底干了些什么。

{

sys_thread_new("receive_thread",vPacketReceiveTask, netif->state,

DEFAULT_THREAD_STACKSIZE,tskRECPKT_PRIORITY);

//底层的接收线程

sys_thread_new("txclean_thread",vTransmitCleanupTask, netif->state,

不论你在什么时候开始,重要的是开始之后就不要停止

嵌入式及其多媒体技术的专栏

相关文章:

你感兴趣的文章:

标签云: