Explanation of tcp_echoserver routine of STM32F2xx

Publisher:TechVoyagerLatest update time:2017-09-18 Source: eefocusKeywords:STM32F2xx  tcp_echoserver Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//======================================================================
//TITLE:
// STM32F2xx's tcp_echoserver routine explanation
//AUTHOR:
// norains
//DATE:
// Monday 04-July-2011
//Environment:
// Keil MDK 4.2
// STM32F207 core version
//==============================================================================

     I recently got the core version of STM32F207, which has a network card chip on board, so I naturally want to tinker with it. For a novice who has never been exposed to the Internet, the best way to get started is to speculate on the routines of ST company, so norains is no exception today. Then let's take a look at this official routine!

    First, let's understand the workflow of the C/S network program, as shown in the figure:

 


    This diagram is not drawn by norains, but is captured from a PPT called "TCP/IP Socket Network Programming" that is circulated on the Internet. You must be familiar with this process, because the example process described below is very consistent with the diagram.

    ST's TCP routines are divided into client and server. According to the literal meaning, we can know that the tcp_echoserver routine uses STM32F2xx as a server. The first step of the routine is initialization, which calls the tcp_echoserver_init() function.

    In the tcp_echoserver_init() function, the following things are done:
     1. Create a new TCP protocol control block
     2. Bind the address and port number (port)
     3. Start listening (listen)
     4. Set the accept callback function

    The complete code is as follows:


  1. void tcp_echoserver_init(void)  

  2. {  

  3.   //Create a new TCP control block  

  4.   tcp_echoserver_pcb = tcp_new();  

  5.   

  6.   if (tcp_echoserver_pcb != NULL)  

  7.   {  

  8.     err_t err;  

  9.       

  10.     //Bind to port 7  

  11.     err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);  

  12.       

  13.     if (err == ERR_OK)  

  14.     {  

  15.       //Start monitoring  

  16.       tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);  

  17.         

  18.       //Set tcp_echoserver_accept as the callback function of accept  

  19.       tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);  

  20.     }  

  21.     else   

  22.     {  

  23.       printf("Can not bind pcb\n"); //norains 2011-7-4 comment  

  24.     }  

  25.   }  

  26.   else  

  27.   {  

  28.     printf("Can not create new pcb\n"); //norains 2011-7-4 comment  

  29.   }  

  30. }  

 

    When the client starts to connect, the set tcp_echoserver_accept() callback function will be called. This function mainly creates a new data structure and passes it to the underlying TCP, and finally sets the three callback functions of receive, error and poll.
  
  The code of tcp_echoserver_accept() is as follows:


  1. static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)  

  2. {  

  3.   err_t ret_err;  

  4.   struct tcp_echoserver_struct *es;  

  5.   

  6.   LWIP_UNUSED_ARG(arg);  

  7.   LWIP_UNUSED_ARG(err);  

  8.   

  9.   ///Set priority for new connections  

  10.   tcp_setprio(newpcb, TCP_PRIO_MIN);  

  11.   

  12.   //Allocate a structure space to maintain the TCP connection  

  13.   es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct tcp_echoserver_struct));  

  14.   if (es != NULL)  

  15.   {  

  16.     es->state = ES_ACCEPTED;  

  17.     es->pcb = newpcb;  

  18.     es->p = NULL;  

  19.       

  20.     //Pass the newly allocated structure data to the new pcb  

  21.     tcp_arg(newpcb, es);  

  22.       

  23.     //Set the receive callback function for the new connection   

  24.     tcp_recv(newpcb, tcp_echoserver_recv);  

  25.       

  26.     //Set the error callback function for the new connection  

  27.     tcp_err(newpcb, tcp_echoserver_error);  

  28.       

  29.     //Set the poll callback function for the new connection  

  30.     tcp_poll(newpcb, tcp_echoserver_poll, 1);  

  31.       

  32.     ret_err = ERR_OK;  

  33.   }  

  34.   else  

  35.   {  

  36.     /* return memory error */  

  37.     ret_err = ERR_MEM;  

  38.   }  

  39.   return ret_err;    

  40. }  



  
  Next is the callback function tcp_echoserver_recv(). Since this function is quite long, I will not list all the codes here. For users, it is enough to know what the corresponding judgment conditions mean, such as:


  1. static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)  

  2. {  

  3.   struct tcp_echoserver_struct *es;  

  4.   err_t ret_err;  

  5.   

  6.   LWIP_ASSERT("arg != NULL",arg != NULL);  

  7.     

  8.   es = (struct tcp_echoserver_struct *)arg;  

  9.   

  10.   if (p == NULL)  

  11.   {  

  12.    //If an empty frame is received, release the connection  

  13.    ...  

  14.   }     

  15.   else if(err != ERR_OK)  

  16.   {  

  17.    //A non-empty frame is received, but an error may occur for some reason, resulting in a return value other than ERR_OK, so the buffer is released here  

  18.    ...  

  19.   }  

  20.   else if(es->state == ES_ACCEPTED)  

  21.   {  

  22.    //Connection is successful, here you need to set the sent callback function  

  23.    ...  

  24.   }  

  25.   else if (es->state == ES_RECEIVED)  

  26.   {  

  27.    //Receive data from the client  

  28.    ...  

  29.   }  

  30.   else  

  31.   {  

  32.    //When the connection is closed, data is still received  

  33.    ...  

  34.   

  35.   }  

  36.     

  37.   return ret_err;  

  38. }  



  
  The code part of STM32F207 is temporarily discussed here. The question now is, how to test the correctness of this code? This requires the use of the echotool.exe program provided by ST. The program is located in the PC_Software folder of stm32f2x7_eth_lwip. The program must be opened in the command line, and its approximate parameters are as follows:
  
  
  If our server ip address is 192.168.0.8, then you can enter the following command for testing:
  echotool.exe 192.168.0.8 /p tcp /r 7 /n 15 /t 2 /d Testing LwIP TCP echo server
  
  If the network is connected, the test will be successful and the following screen will be displayed, as shown in the figure:
  


Keywords:STM32F2xx  tcp_echoserver Reference address:Explanation of tcp_echoserver routine of STM32F2xx

Previous article:How to deal with STM32 entering HardFault_Handler
Next article:How to code the STM32 serial port to receive messages more stably

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号