wdefu 2007-3-10 09:35
lwIP(TCP-IP)协议栈移植(不包括网卡驱动)
[size=3][color=#000000][font=宋体]一、[/font][font=Times New Roman]lwIP [/font][font=宋体]概述[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]lwIP[/font][font=宋体]是瑞士计算机科学院([/font][font=Times New Roman]Swedish Institute of Computer Science[/font][font=宋体])的[/font][font=Times New Roman]Adam Dunkels[/font][font=宋体]等开发的一套用于嵌入式系统的开放源代码[/font][font=Times New Roman]TCP/IP[/font][font=宋体]协议栈。[/font][font=Times New Roman]Lwip[/font][font=宋体]既可以移植到操作系统上[/font][font=Times New Roman],[/font][font=宋体]又可以在无操作系统的情况下独立运行。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]LwIP[/font][font=宋体]的特性如下:[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](1) [/font][font=宋体]支持多网络接口下的[/font][font=Times New Roman]IP[/font][font=宋体]转发[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](2) [/font][font=宋体]支持[/font][font=Times New Roman]ICMP[/font][font=宋体]协议[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](3) [/font][font=宋体]包括实验性扩展的的[/font][font=Times New Roman]UDP[/font][font=宋体](用户数据报协议)[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](4) [/font][font=宋体]包括阻塞控制,[/font][font=Times New Roman]RTT[/font][font=宋体]估算和快速恢复和快速转发的[/font][font=Times New Roman]TCP[/font][font=宋体](传输控制协议)[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](5) [/font][font=宋体]提供专门的内部回调接口([/font][font=Times New Roman]Raw API[/font][font=宋体])用于提高应用程序性能[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](6) [/font][font=宋体]可选择的[/font][font=Times New Roman]Berkeley[/font][font=宋体]接口[/font][font=Times New Roman]API[/font][font=宋体](多线程情况下)[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](7) [/font][font=宋体]在最新的版本中支持[/font][font=Times New Roman]ppp[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](8) [/font][font=宋体]新版本中增加了的[/font][font=Times New Roman]IP fragment[/font][font=宋体]的支持[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman](9) [/font][font=宋体]支持[/font][font=Times New Roman]DHCP[/font][font=宋体]协议[/font][font=Times New Roman],[/font][font=宋体]动态分配[/font][font=Times New Roman]ip[/font][font=宋体]地址。[/font][/color][/size]
[size=3][color=#000000][font=宋体]以上内容摘自网络[/font][font=Times New Roman] [/font][font=宋体]关于[/font][font=Times New Roman]ucos [/font][font=宋体]上的[/font][font=Times New Roman]lwip[/font][font=宋体]移植[/font][/color][/size]
[font=宋体][size=3][color=#000000]二、移植介绍[/color][/size][/font]
[size=3][color=#000000][font=宋体]整个移植过程主要参考网络上关于移植到[/font][font=Times New Roman]ucos [/font][font=宋体]的说明和源码。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]1[/font][font=宋体].[/font][font=Times New Roman] [/font][font=宋体]目录及文件介绍[/font][/color][/size]
[size=3][color=#000000][font=宋体]原版的[/font][font=Times New Roman]lwIP1.1.0[/font][font=宋体]包含两个目录[/font][font=Times New Roman]src [/font][font=宋体]和[/font][font=Times New Roman] doc[/font][/color][/size]
[font=宋体][size=3][color=#000000]移植后增加如下文件和目录[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000][Arch][/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]Lib_arch.c[/font][font=宋体]本系统没用,系统中没有实现的[/font][font=Times New Roman]C[/font][font=宋体]库函数可以写到这里[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]Sys_arch.c [/font][font=宋体]移植的主要工作在这里,关于信号量、消息队列、任务创建[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman][RX4000] [/font][font=宋体]项目目录[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000][Include][/color][/size][/font]
[font=Times New Roman][size=3][color=#000000][Arch][/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]cc.h [/font][font=宋体]类型定义[/font][font=Times New Roman] [/font][font=宋体]大小端设置[/font][font=Times New Roman] PACK[/font][font=宋体]定义等[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]init.h[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]lib.h [/font][font=宋体]跟[/font][font=Times New Roman]Lib_arch.c[/font][font=宋体]对应[/font][font=Times New Roman] [/font][font=宋体]函数声明[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]perf.h [/font][font=宋体]没用[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]sys_arch.h [/font][font=宋体]跟[/font][font=Times New Roman]Sys_arch.c[/font][font=宋体]对应的一些类型定义和宏定义[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000][Netif][/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]Dm9000a.h[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]Ne2kif.h[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000][Netif][/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]Dm_netif.c [/font][font=宋体]网卡驱动与系统关联的抽象层[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]Dm9000a.c[/font][font=宋体]网卡的硬件操作函数[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]Ne2kif.c [/font][font=宋体]没用[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000][Init][/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]Lwip.c [/font][font=宋体]协议栈初始化和[/font][font=Times New Roman]DHCP[/font][font=宋体]初始化[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]Lwipopts.h [/font][font=宋体]协议栈相关参数设置[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000][Dns][/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]Dns.c [/font][font=宋体]增加域名解析函数[/font][font=Times New Roman] gethostbyname [/font][font=宋体](非可重入函数)[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]Dns.h
2[/font][font=宋体].[/font][font=Times New Roman] [/font][font=宋体]移植相关函数介绍[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]1) sys_init[/color][/size][/font]
[font=宋体][size=3][color=#000000]这个很简单,就是一些全局量的初始化[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]2) sys_thread_new sys_arch_timeouts[/color][/size][/font]
[font=宋体][size=3][color=#000000]相关的三个全局变量如下[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]struct sys_timeouts lwip_timeouts[LWIP_TASK_MAX];[/color][/size][/font]
[size=3][color=#000000][font=宋体]为每一个由[/font][font=Times New Roman]sys_thread_new[/font][font=宋体]创建的任务分配一个存放信号量超时信息的列表[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]struct sys_timeouts null_timeouts;[/color][/size][/font]
[size=3][color=#000000][font=宋体]为一个超过任务上限数的任务和不是由[/font][font=Times New Roman]sys_thread_new[/font][font=宋体]创建的任务取超时列表时返回使用。[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]MMAC_RTOS_TASK_ID LWIP_TASKS[LWIP_TASK_MAX];[/color][/size][/font]
[size=3][color=#000000][font=宋体]任务[/font][font=Times New Roman]id[/font][font=宋体]存放顺序与[/font][font=Times New Roman]lwip_timeouts[/font][font=宋体]相对应[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]sys_thread_new[/font][font=宋体]用来创建一个新的任务,保存任务[/font][font=Times New Roman]ID[/font][font=宋体]。[/font][/color][/size][size=3][color=#000000][font=Times New Roman]sys_arch_timeouts
[/font][font=宋体] 就是通过取得任务[/font][font=Times New Roman]ID[/font][font=宋体]返回任务对应的[/font][font=Times New Roman]timeouts[/font][font=宋体]结构,从而可以添加、删除和判断超时的功能[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]3) sys_sem_new sys_sem_free sys_sem_signal sys_arch_sem_wait[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]sys_sem_new[/font][font=宋体]创建一个信号灯并初始化灯的数量返回[/font][font=Times New Roman]sys_sem_t [/font][font=宋体]类型的变量,定义是这样的[/font][font=Times New Roman]typedef MMAC_RTOS_SEMAPHORE *sys_sem_t; [/font][font=宋体]由于返回失败要返回[/font][font=Times New Roman]NULL[/font][font=宋体]值所以就定义了系统信号量的指针为抽象信号量类型。因此在[/font][font=Times New Roman]sys_sem_new[/font][font=宋体]和[/font][font=Times New Roman] sys_sem_free [/font][font=宋体]分别要进行内存申请和释放的工作。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]sys_sem_signal[/font][font=宋体]释放一个灯,[/font][font=Times New Roman]sys_arch_sem_wait [/font][font=宋体]等待信号,其中参数[/font][font=Times New Roman]timeout[/font][font=宋体]是以[/font][font=Times New Roman]ms[/font][font=宋体]为单位的,若[/font][font=Times New Roman]wei[/font][font=宋体]零则表示永远等待一直到信号的来临。[/font][/color][/size]
[size=3][color=#000000][font=宋体]在这个信号系统中本人还存在一个疑问,具体在[/font][font=Times New Roman]5”[/font][font=宋体]存在的问题[/font][font=Times New Roman]”[/font][font=宋体]中进行说明[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]4) sys_mbox_new sys_mbox_free sys_mbox_post sys_arch_mbox_fetch[/color][/size][/font]
[size=3][color=#000000][font=宋体]同上原因在类型的定义成指针的。那[/font][font=Times New Roman]sys_mbox_new [/font][font=宋体]和[/font][font=Times New Roman]sys_mbox_free[/font][font=宋体]同样要进行内存的申请和释放。在系统中消息队列发送和接收的都是指向数据的指针,因为在发送前所有的数据都已经存放在一个全局的用来管理内存的变量中。所以发送的内容就是四个字节。发送是还要判断发送[/font][font=Times New Roman]msg[/font][font=宋体]是否为[/font][font=Times New Roman]NULL[/font][font=宋体]。因为发送的是[/font][font=Times New Roman]msg[/font][font=宋体]的指针,而不是内容还要取一下地址,[/font][font=Times New Roman]NULL[/font][font=宋体]明显不能取址,所以有一个专门的[/font][font=Times New Roman]static int *msg_null=NULL ([/font][font=宋体]这里的[/font][font=Times New Roman]=NULL [/font][font=宋体]并不重要可以使任何值[/font][font=Times New Roman] * [/font][font=宋体]也可以不要,因为要的是变量的地址在内存中的唯一性[/font][font=Times New Roman])[/font][font=宋体]用来发送[/font][font=Times New Roman]“NULL”[/font][font=宋体]信息,使[/font][font=Times New Roman]msg = &msg_null[/font][font=宋体]再发送。接收到后也要进行[/font][font=Times New Roman] *msg ==&msg_null[/font][font=宋体]的判断。接收时也要进行[/font][font=Times New Roman]msg NULL[/font][font=宋体]的判断,若[/font][font=Times New Roman]msg[/font][font=宋体]为[/font][font=Times New Roman]NULL[/font][font=宋体]就需要零时申请一个空间进行接收。还要注意发送和接收时[/font][font=Times New Roman]msg[/font][font=宋体]的类型,发送是[/font][font=Times New Roman]void* [/font][font=宋体]的[/font][font=Times New Roman] [/font][font=宋体],接收是[/font][font=Times New Roman]void **[/font][font=宋体],要做好相应的处理。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]3[/font][font=宋体].[/font][font=Times New Roman] [/font][font=宋体]移植中相关配置的介绍[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]1) SYS_LIGHTWEIGHT_PROT[/color][/size][/font]
[size=3][color=#000000][font=宋体]我的理解应该是是否使用系统临界区变量,由于本系统没有单独的临界区变量,所以就设置成[/font][font=Times New Roman] 0 [/font][font=宋体],那就用信号灯来完成该任务。且[/font][font=Times New Roman]sys_arch.h[/font][font=宋体]中的最后三个宏也要定义成空。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]2) [/font][font=宋体]累加和[/font][/color][/size]
[font=宋体][size=3][color=#000000]关闭所有的累加检查,因为硬件已有该功能了[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_GEN_IP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_GEN_UDP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_GEN_TCP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_CHECK_IP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_CHECK_UDP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define CHECKSUM_CHECK_TCP 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]3) LWIP_HAVE_LOOPIF[/color][/size][/font]
[size=3][color=#000000][font=宋体]是否开启回环,在还没有网卡驱动的时候,可以设置为[/font][font=Times New Roman] 1 [/font][font=宋体]添加[/font][font=Times New Roman]loop[/font][font=宋体]设备进行调试运行。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]4) [/font][font=宋体]内存分配设定[/font][/color][/size]
[size=3][color=#000000][font=宋体]在协议栈中很多内存都是事先申请的,有协议栈自己进行管理。据我了解有三大块内存[/font][font=Times New Roman]PBUF MEMP MEM[/font][font=宋体]。在[/font][font=Times New Roman]lwipopts.h[/font][font=宋体]中的[/font][font=Times New Roman]Memory options[/font][font=宋体]中定义了各块内存的种类及各种类的数量。这部分的设置要仔细斟酌。具体就不再详述了。[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]5) TCP_SND_BUF[/color][/size][/font]
[size=3][color=#000000][font=宋体]该设置对网络传输的速度由很大的影响。[/font][font=Times New Roman]Ucos+lwIP[/font][font=宋体]的源码中的默认设置是[/font][font=Times New Roman]256[/font][font=宋体],用[/font][font=Times New Roman]socket[/font][font=宋体]进行速度测试时却只有区区的[/font][font=Times New Roman]1KB/S[/font][font=宋体]左右的速度。最后改成[/font][font=Times New Roman]8192[/font][font=宋体]后速度达到[/font][font=Times New Roman] 600KB/S[/font][font=宋体]。[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]6) LWIP_DHCP [/color][/size][/font]
[size=3][color=#000000][font=宋体]本系统需要[/font][font=Times New Roman]DHCP[/font][font=宋体]支持因此需要设置为[/font][font=Times New Roman] 1[/font][font=宋体]。在他下面有一个[/font][font=Times New Roman]DHCP_DOES_ARP_CHECK[/font][font=宋体]的宏设置为[/font][font=Times New Roman] 0[/font][font=宋体]。[/font][font=Times New Roman] [/font][font=宋体]开启后出现错误。原因不明。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]4[/font][font=宋体].[/font][font=Times New Roman] [/font][font=宋体]移植中碰到的问题总结[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]1[/font][font=宋体])[/font][font=Times New Roman] [/font][font=宋体]同时支持[/font][font=Times New Roman]UDP[/font][font=宋体]及[/font][font=Times New Roman]TCP[/font][font=宋体]及[/font][font=Times New Roman]DHCP[/font][font=宋体]的支持[/font][/color][/size]
[font=宋体][size=3][color=#000000]不再详述看出始化代码[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]void Task_lwip_init(void * pParam)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]{[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]struct ip_addr ipaddr, netmask, gw;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sys_sem_t sem;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]err_t result ;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]int icount = 0;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]int idhcpre=0;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#if LWIP_STATS[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]stats_init();[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#endif[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman] // initial lwIP stack[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]sys_init();[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]mem_init();[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]memp_init();[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]pbuf_init();[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_init(); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000] [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]printf("LWIP:TCP/IP initializing...\n"); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sem = sys_sem_new(0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]tcpip_init(tcpip_init_done_ok, &sem);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sys_sem_wait(sem);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sys_sem_free(sem);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]printf("LWIP:TCP/IP initialized.\n");[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]/*[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]//add loop interface //set local loop-interface 127.0.0.1[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&gw, 127,0,0,1);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&ipaddr, 127,0,0,1);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&netmask, 255,0,0,0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_add(&loop_if, &ipaddr, &netmask, &gw, NULL, loopif_init, tcpip_input);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_default(&loop_if);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_up(&loop_if); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]//*/[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#if 0[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&gw, 192,168,0,2);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&ipaddr, 192,168,0,186);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&netmask, 255,255,255,0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_add(&dm9if_if, &ipaddr, &netmask, &gw, NULL, dm9_netif_init, tcpip_input);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_default(&dm9if_if);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_up(&dm9if_if); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#else[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&gw, 0,0,0,0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&ipaddr, 0,0,0,0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&netmask, 0,0,0,0);[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]netif_add(&dm9if_if, &ipaddr, &netmask, &gw, NULL, dm9_netif_init, udp_input);//[/font][font=宋体]添加[/font][font=Times New Roman]udp[/font][font=宋体]支持[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]printf("LWIP:waiting for neif init \n");[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]MMAC_RTOS_Sleep( 3500); [/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]for(idhcpre = 0; idhcpre<4; idhcpre++ )//dhcp[/font][font=宋体]最多重试[/font][font=Times New Roman]4[/font][font=宋体]遍[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]{[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]printf("LWIP:start dhcp request \n");[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]result = dhcp_start(&dm9if_if);//[/font][font=宋体]广播[/font][font=Times New Roman]dhcp[/font][font=宋体]请求[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]IP4_ADDR(&ipaddr, 0,0,0,0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]for(icount = 0; (icount < 10) && (ipaddr.addr == 0); icount ++ )[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]{[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]ipaddr.addr = dm9if_if.ip_addr.addr;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]MMAC_RTOS_Sleep( 1000); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]} [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]// if failed ipaddr = 0.0.0.0 ;timeout = 10 * 1000 ms[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]//[/font][font=宋体]等待[/font][font=Times New Roman]dhcp[/font][font=宋体]是否接受到[/font][font=Times New Roman]IP[/font][font=宋体]了[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]// add dns server ip[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]dns_add(0,&dm9if_if.dhcp->offered_dns_addr[0]);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]dns_add(1,&dm9if_if.dhcp->offered_dns_addr[1]);[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]//[/font][font=宋体]不需要[/font][font=Times New Roman]dns[/font][font=宋体]的去掉上面两句[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]dhcp_stop(&dm9if_if); //[/font][font=宋体]一次[/font][font=Times New Roman]dhcp[/font][font=宋体]结束[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]if (ipaddr.addr != 0)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]break;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]}[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]gw.addr = dm9if_if.gw.addr;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]ipaddr.addr = dm9if_if.ip_addr.addr;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netmask.addr = dm9if_if.netmask.addr;[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]//netif_remove(&dm9if_if);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_add(&dm9if_if_tcp, &ipaddr, &netmask, &gw, NULL, dm9_netif_init, tcpip_input);[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]//[/font][font=宋体]添加[/font][font=Times New Roman]tcp[/font][font=宋体]支持[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]netif_set_up(&dm9if_if_tcp); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_up(&dm9if_if); [/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]netif_set_default(&dm9if_if_tcp);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#endif[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sprintf(STRIPADDR,"%d.%d.%d.%d",ip4_addr1(&ipaddr), ip4_addr2(&ipaddr),ip4_addr3(&ipaddr),ip4_addr4(&ipaddr));[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]printf("LWIP:IPADDR = %s\n",STRIPADDR);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]if (ipaddr.addr != 0)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]{[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]//------------------------------------------------------------[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]// http thread, a web page can be browsed[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]// sys_thread_new(httpd_init, (void*)"httpd",TCPIP_THREAD_PRIO);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]//------------------------------------------------------------[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sys_thread_new(test_net, NULL, TCPIP_THREAD_PRIO);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]}[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]/* Block for ever. */[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sem = sys_sem_new(0);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]sys_sem_wait(sem);[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]}[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]2[/font][font=宋体])对齐问题[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]PBUF_LINK_HLEN 16[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]static u8_t ip_reassbitmap[MEM_ALIGN_SIZE(IP_REASS_BUFSIZE / (8 * 8))];[/color][/size][/font]
[size=3][color=#000000][font=宋体]在调试的时候经常碰到内存访问错误的异常,最后查得原因是内存的起始地址不再[/font][font=Times New Roman]4[/font][font=宋体]的倍数上,导致不能访问。因为内存申请时有字节数来的,有时要强制转换为某种结构。为了保证地址不错,[/font][font=Times New Roman]PBUF_LINK_HLEN [/font][font=宋体]定义为[/font][font=Times New Roman]16[/font][font=宋体],[/font][font=Times New Roman]ip_reassbitmap[/font][font=宋体]的大小也变成[/font][font=Times New Roman]4[/font][font=宋体]的倍数。因为它的大小不是[/font][font=Times New Roman]4[/font][font=宋体]的倍数,就导致附近的内存分配起始不是[/font][font=Times New Roman]4[/font][font=宋体]的倍数。这个解决办法由点不好,但是没有办法,我用[/font][font=Times New Roman] align [/font][font=宋体]等声明没有作用。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]3[/font][font=宋体])大包[/font][font=Times New Roman]ping[/font][font=宋体]问题[/font][/color][/size]
[size=3][color=#000000][font=宋体]原因是以太网络中,最大允许的包大小为[/font][font=Times New Roman]1514[/font][font=宋体]字节,若用[/font][font=Times New Roman]pc[/font][font=宋体]机[/font][font=Times New Roman]ping –l 2000 ip[/font][font=宋体]地址[/font][font=Times New Roman] [/font][font=宋体]测试,[/font][font=Times New Roman]pc[/font][font=宋体]会把[/font][font=Times New Roman]ip[/font][font=宋体]包分解成多个发送,[/font][font=Times New Roman]lwIP[/font][font=宋体]接受后会把他数据合成方在[/font][font=Times New Roman]pbuf[/font][font=宋体]中,并直接发送出去,可惜程序中不会把包分解发送。导致发送网络不允许的包。这样不但[/font][font=Times New Roman]pc[/font][font=宋体]接受不到包,而且[/font][font=Times New Roman]lwIP[/font][font=宋体]也出现问题。[/font][/color][/size]
[size=3][color=#000000][font=宋体]解决方法,在发送的地方,若包大于[/font][font=Times New Roman]1514[/font][font=宋体]就不给发送。虽然解决不了大包[/font][font=Times New Roman]ping[/font][font=宋体]不通问题,但至少[/font][font=Times New Roman]lwIP[/font][font=宋体]不会死。[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]5[/font][font=宋体].[/font][font=Times New Roman] [/font][font=宋体]存在的问题[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]1[/font][font=宋体])没有对应得任务释放函数,除非以后的任务都是一直存在不需释放的[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]2[/font][font=宋体])[/font][font=Times New Roman]DNS gethostbyname [/font][font=宋体]的不可重入问题[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]3[/font][font=宋体])[/font][font=Times New Roman]DHCP_DOES_ARP_CHECK [/font][font=宋体]设置为[/font][font=Times New Roman]1 [/font][font=宋体]死机[/font][/color][/size]
[size=3][color=#000000][font=Times New Roman]4[/font][font=宋体])信号量中[/font][font=Times New Roman]timeout [/font][font=宋体]管理的疑问[/font][/color][/size]
[size=3][color=#000000][font=宋体]操作系统中本身带有的函数就已经有[/font][font=Times New Roman]timeout[/font][font=宋体]参数了,用它多此一举。但想想是不是为了没有[/font][font=Times New Roman]timeout[/font][font=宋体]的操作系统准备的呢。但它在运行过程中又没有使用到,也没有找到什么代码来确定某个[/font][font=Times New Roman]sem[/font][font=宋体]已经超时,而仅仅使用了我使用嵌入式操作系统的[/font][font=Times New Roman]timeout[/font][font=宋体]。[/font][/color][/size]
[font=宋体][size=3][color=#000000]三、上层开发接口[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]1[/font][font=宋体].[/font][font=Times New Roman]Socket [/font][font=宋体]接口[/font][font=Times New Roman] sockets.h[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]#define accept(a,b,c) lwip_accept(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define bind(a,b,c) lwip_bind(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define shutdown(a,b) lwip_shutdown(a,b)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define close(s) lwip_close(s)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define connect(a,b,c) lwip_connect(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define getsockname(a,b,c) lwip_getsockname(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define getpeername(a,b,c) lwip_getpeername(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define listen(a,b) lwip_listen(a,b)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define recv(a,b,c,d) lwip_recv(a,b,c,d)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define read(a,b,c) lwip_read(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define send(a,b,c,d) lwip_send(a,b,c,d)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define sendto(a,b,c,d,e,f) lwip_sendto(a,b,c,d,e,f)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define socket(a,b,c) lwip_socket(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define write(a,b,c) lwip_write(a,b,c)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define select(a,b,c,d,e) lwip_select(a,b,c,d,e)[/color][/size][/font]
[font=Times New Roman][size=3][color=#000000]#define ioctlsocket(a,b,c) lwip_ioctl(a,b,c)[/color][/size][/font]
[size=3][color=#000000][font=Times New Roman]2[/font][font=宋体].[/font][font=Times New Roman]Dns [/font][font=宋体]客户端[/font][font=Times New Roman] dns.h[/font][/color][/size]
[font=Times New Roman][size=3][color=#000000]struct hostent *gethostbyname(const char *name);[/color][/size][/font]
[[i] 本帖最后由 wdefu 于 2007-3-10 09:38 编辑 [/i]]