lwIP на STM32F1: функция обратного вызова tcp_accept не вызывается - PullRequest
0 голосов
/ 18 июня 2020

Я собираюсь настроить lwIP на STM32f107RCT6 и DP83848. Я использовал CubeMX для генерации первичного кода. После загрузки кода в MCU с помощью команд ping и arp –a результат будет:
(IP-адрес MCU: 192.168.1.57 * Su bnet: 255.255.255.0 * GW: 192.168.1.1 *** MA C: 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  

Однако, сбросив 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  

Этот цикл повторяется, и после сброса MCU он возвращается к списку, предоставленному командой arp -a. Я считаю, что команда ping несколько понятно, почему она не работает. Потому что, возможно, это не реализовано lwIP. Поэтому я использовал Hercules TCP Client с

Madule IP = 192.168.1.57 and Port = 7  

и нажал кнопку Connect. Результат был:

Connecting to 192.168.1.57 ...  
TCP connection timeout

Я использовал два светодиода для его отладки, и, поскольку RUNNING_LED включен, выполняется tcp_echoserver_init(). Однако AUX_LED никогда не был включен, то есть функция обратного вызова tcp_echoserver_accept() никогда не вызывается! В чем проблема? Почему он не вызывается?

Это мой код, заимствованный из примера 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;  
}
...