简体   繁体   English

STM32F1 上的 lwIP:tcp_accept 回调 function 未被调用

[英]lwIP on STM32F1: tcp_accept callback function is not called

I am going to setup lwIP on STM32f107RCT6 and DP83848.我将在 STM32f107RCT6 和 DP83848 上设置 lwIP。 I used CubeMX to generate primary code.我使用 CubeMX 生成主要代码。 After downloading the code into MCU with command ping and arp –a the result is:使用命令pingarp –a将代码下载到 MCU 后,结果为:
(MCU IP: 192.168.1.57 * Subnet: 255.255.255.0 * GW:192.168.1.1 *** MAC: 00-80-e1-00-00-00) (MCU IP:192.168.1.57 * 子网: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 :但是,通过重置 MCU 并再次使用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.重复此循环并通过重置 MCU 返回到由arp -a命令提供的列表。 I reckon that the ping command is somewhat understandable why it not work.我认为ping命令为什么不起作用是可以理解的。 Because maybe it is not implemented by lwIP.因为也许它不是由 lwIP 实现的。 So I used Hercules TCP Client with所以我使用 Hercules TCP Client

Madule IP = 192.168.1.57 and Port = 7  

And pressed Connect button.并按下Connect按钮。 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.我使用了两个 LED 对其进行调试,并且由于RUNNING_LED已打开,因此tcp_echoserver_init()被执行。 However, AUX_LED never has been turned on meaning tcp_echoserver_accept() callback function never is called?但是, AUX_LED从未打开,这意味着tcp_echoserver_accept()回调 function 从未被调用? What is the problem?问题是什么? Why it is not called?为什么不叫它?

This is my code which adopted from ST TCP ECHO SERVER example:这是我从 ST TCP ECHO SERVER 示例中采用的代码:

/**
  * @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;  
}

As I mentioned in my comment, Raw API implementation works in polling mode so you have to ensure continuous software polling whether a new packet is received or not.正如我在评论中提到的,Raw API 实现在轮询模式下工作,因此您必须确保连续的软件轮询是否收到新数据包。 As far as I can remember I added these two lines to my code at body of infinite loop:据我所知,我在无限循环体的代码中添加了这两行:

  while (1)
  {
    ethernetif_input(&gnetif);
    sys_check_timeouts();
  }

for more details please read through the informative user manual provided by ST, UM1713 .有关详细信息,请通读 ST, UM1713提供的内容丰富的用户手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM