cancel
Showing results for 
Search instead for 
Did you mean: 

lwIP on STM32F1: tcp_accept callback function is not called

MTros.1
Associate II

I am going to setup lwIP on STM32f107RCT6 and DP83848. I used CubeMX to generate primary code. After downloading the code into MCU with command ping and arp –a the result is:  

(MCU IP: 192.168.1.57 *** Subnet: 255.255.255.0 *** GW:192.168.1.1 *** MAC: 00-80-e1-00-00-00)

  C:\Users\GmtK>ping 192.168.1.57

   

  Pinging 192.168.1.57 with 32 bytes of data:  

  Reply from 192.168.1.11: Destination host unreachable.  

  Reply from 192.168.1.11: Destination host unreachable.  

  Reply from 192.168.1.11: Destination host unreachable.  

  Reply from 192.168.1.11: Destination host unreachable.  

   

  Ping statistics for 192.168.1.57:

   Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

   

   

   

  C:\Users\GmtK>arp -a

   

  Interface: 192.168.1.11 --- 0x16  

   Internet Address   Physical Address   Type  

   192.168.1.1      74-da-da-64-6a-59   dynamic  

   192.168.1.7      2c-fd-a1-5c-3e-99   dynamic  

   192.168.1.255     ff-ff-ff-ff-ff-ff   static  

   224.0.0.22      01-00-5e-00-00-16   static  

   224.0.0.251      01-00-5e-00-00-fb   static  

   224.0.0.252      01-00-5e-00-00-fc   static  

   239.255.255.250    01-00-5e-7f-ff-fa   static  

   255.255.255.255    ff-ff-ff-ff-ff-ff   static  

However by resetting the MCU and again using `arp –a`:

  C:\Users\GmtK>arp -a

   

  Interface: 192.168.1.11 --- 0x16  

   Internet Address   Physical Address   Type  

   192.168.1.1      74-da-da-64-6a-59   dynamic  

   192.168.1.7      2c-fd-a1-5c-3e-99   dynamic  

   192.168.1.57     00-80-e1-00-00-00   dynamic  //this my MCU  

   192.168.1.255     ff-ff-ff-ff-ff-ff   static  

   224.0.0.22      01-00-5e-00-00-16   static  

   224.0.0.251      01-00-5e-00-00-fb   static  

   224.0.0.252      01-00-5e-00-00-fc   static  

   239.255.255.250    01-00-5e-7f-ff-fa   static  

   255.255.255.255    ff-ff-ff-ff-ff-ff   static  

This cycle is repeated and by resetting the MCU it returns to the list provided by `arp -a` command. I reckon that the `ping` command is somewhat understandable why it not work. Because maybe it is not implemented by lwIP. So I used Hercules `TCP Client` with  

  Madule IP = 192.168.1.57 and Port = 7  

And pressed `Connect` button. The outcome was:  

  Connecting to 192.168.1.57 ...  

  TCP connection timeout

I have employed two LEDs to debug it and since the `RUNNING_LED` is turned on the `tcp_echoserver_init()` is executed. However, `AUX_LED` never has been turned on meaning `tcp_echoserver_accept()` callback function never is called! What is the problem? Why it is not called?

This is my code which adopted from ST TCP ECHO SERVER example:

 
  /**
 
   * @brief Initializes the tcp echo server
 
   * @param None
 
   * @retval None
 
   */
 
  void tcp_echoserver_init(void)
 
  {
 
   /* create new tcp pcb */
 
   tcp_echoserver_pcb = tcp_new();
 
   
 
   if (tcp_echoserver_pcb != NULL)
 
   {
 
    err_t err;
 
    /* bind echo_pcb to port 7 (ECHO protocol) */
 
    err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);
 
     
 
    if (err == ERR_OK)
 
    {
 
   
 
     /* start tcp listening for echo_pcb */
 
     tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
 
      
 
     /* initialize LwIP tcp_accept callback function */
 
     tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
 
  	 HAL_GPIO_WritePin(RUNNING_LED, 0); //RUNNING_LED Will be turned on here
 
    }
 
    else 
 
    {
 
     /* deallocate the pcb */
 
     memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
 
   
 
    }
 
   }
 
  }
 
   
 
  /**
 
   * @brief This function is the implementation of tcp_accept LwIP callback
 
   * @param arg: not used
 
   * @param newpcb: pointer on tcp_pcb struct for the newly created tcp connection
 
   * @param err: not used 
 
   * @retval err_t: error status
 
   */
 
  static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
 
  {
 
   HAL_GPIO_WritePin(AUx_LED, 0); //AUX_LED will be turned on here
 
   err_t ret_err;
 
   struct tcp_echoserver_struct *es;
 
   LWIP_UNUSED_ARG(arg);
 
   LWIP_UNUSED_ARG(err);
 
   
 
   /* set priority for the newly accepted tcp connection newpcb */
 
   tcp_setprio(newpcb, TCP_PRIO_MIN);
 
   
 
   /* allocate structure es to maintain tcp connection informations */
 
   es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct tcp_echoserver_struct));
 
   if (es != NULL)
 
   {
 
    es->state = ES_ACCEPTED;
 
    es->pcb = newpcb;
 
    es->retries = 0;
 
    es->p = NULL;
 
     
 
    /* pass newly allocated es structure as argument to newpcb */
 
    tcp_arg(newpcb, es);
 
     
 
    /* initialize lwip tcp_recv callback function for newpcb */ 
 
    tcp_recv(newpcb, tcp_echoserver_recv);
 
     
 
    /* initialize lwip tcp_err callback function for newpcb */
 
    tcp_err(newpcb, tcp_echoserver_error);
 
     
 
    /* initialize lwip tcp_poll callback function for newpcb */
 
    tcp_poll(newpcb, tcp_echoserver_poll, 0);
 
     
 
    ret_err = ERR_OK;
 
   }
 
   else
 
   {
 
    /* close tcp connection */
 
    tcp_echoserver_connection_close(newpcb, es);
 
    /* return memory error */
 
    ret_err = ERR_MEM;
 
   }
 
   return ret_err;  
 
  }
 
 
 
 
 
 
 

2 REPLIES 2
Piranha
Chief II

ICMP (ping) is implemented in lwIP. If you're using RTOS, then your whole code is broken as it doesn't follow lwIP API rules. The same is true for ST's non-working HAL/Cube bloatware:

https://community.st.com/s/question/0D50X0000BOtfhnSQB/how-to-make-ethernet-and-lwip-working-on-stm32

MTros.1
Associate II

I did not use RTOS. Actually, I employed Raw API. So what is your comment?

More information:

1.      The LINK LED of RJ000002 magjack (Green one) is turned on and solid.

2.      The ACT LED of RJ000002 magjack (Orange one) is blinking continuously.

The precision of my hardware design is also open to question. RMII is selected. The designed schematic has been attached.