这段时间在STM32F107调试lwIP心得

这段时间在STM32F107调试lwIP心得

开发板:STM3210C-EVALSTM原厂开发板,用起来确实很爽)

 

因为公司有项目,要做一个以太网的通讯模块,所以这段时间就一直在调试lwIP裸机程序。

 

大体上实现了lwIPUDP通讯。后续对UDP传输协议中的数据分析、控制等都会很快就出来了。

 

lwIP中实现UDP协议的客户端,主要过程如下:

unsigned char const UDPArr[6] = {"hello!"};

int main(void)

{

  struct udp_pcb *Udppcb1 ;

  struct ip_addr ipaddr1 ;

  struct pbuf *p ;

  /* Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources */

  System_Setup();

            

  /* Initilaize the LwIP satck */

  LwIP_Init();

 

  //测试UDP客户端发送数据

 

  p = pbuf_alloc( PBUF_RAW , sizeof(UDPArr) , PBUF_RAM ) ;

  p->payload = ( void *)(UDPArr) ;

  IP4_ADDR(&ipaddr1 , 192,168,1,11);

  Udppcb1 = udp_new( ) ;

  udp_bind( Udppcb1 , IP_ADDR_ANY , 161 ) ;

  udp_connect( Udppcb1 , &ipaddr1 , 161 ) ;

  udp_send( Udppcb1 , p ) ;  

 

  /* Infinite loop */   

  while (1)

  {   

/* Periodic tasks */

System_Periodic_Handle(); 

  }

}

 

大体解释一下:

该程序就是将开发板作为一个UDP客户端,不停的向主机发送“hello!”字符,目前已经实现,程序完整调试通过。

UDP处理机制中, System_Periodic_Handle();  是一个周期性的任务,在UDP中其实只是做ARP的老化实现,而不像TCPIP协议中那样需要对连接超时进行处理,这里都不需要。

 

 

下面一个例子是UDP的服务端程序,该程序就是实现将主机发送上来的数据完整的发送给主机。

void UDP_Receive(void *arg, struct udp_pcb *upcb, struct pbuf *p,struct ip_addr *addr, u16_t port) ;

/* Private functions ---------------------------------------------------------*/

/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */

int main(void)

{

  struct udp_pcb *Udppcb1 ;

  //struct ip_addr ipaddr1 ;

  //struct pbuf *p ;

  /* Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources */

  System_Setup();

            

  /* Initilaize the LwIP satck */

  LwIP_Init();

 

  //测试UDP服务端,收到数据就发送给远程主机

  Udppcb1 = udp_new( ) ;

  udp_bind( Udppcb1 , IP_ADDR_ANY , 161 ) ;

  udp_recv( Udppcb1 , UDP_Receive, NULL ) ;

  /* Infinite loop */   

  while (1)

  {   

/* Periodic tasks */

System_Periodic_Handle(); 

  }

}

void UDP_Receive(void *arg, struct udp_pcb *upcb, struct pbuf *p,struct ip_addr *addr, u16_t port)

{

struct ip_addr dAddr = *addr;

if( p != NULL )

{

  udp_sendto( upcb , p , &dAddr , port ) ;

 

  pbuf_free( p ) ;

}

}

 

这里要注意一下,UDP_Receive()这个函数是UPD的回调函数,这个是在使用lwip的时候需要的,意思就是当开发板收到了对应端口的UDP协议后,需要将有用的数据提交给应用层软件处理的过程,在这个函数中的struct pbuf *p参数实际已经是lwipIP头,UDP去掉了的有用数据了。

大体就是这样的一个情况。

转自Tony嵌入式论坛,地址:http://www.cevx.com/bbs/thread-37250-1-1.html

你可能感兴趣的:(这段时间在STM32F107调试lwIP心得)