cancel
Showing results for 
Search instead for 
Did you mean: 

Using STM32H753 Nucleo-144 Board with lwIP (no OS) and can't get an IP address for the network interface

TRaic.1
Associate

Hi everybody!

I'm using STM32H754 Nucleo-144 Eval Board in the combination with the lwIP (bare-metal) to make it work as a simple TCP echo client.

Also, I'm using STMCubeMX for basic configuration and code generation.

I'm stuck at the very beginning of my solution because it seems like I can't get an IP address from a router (DHCP enabled in lwIP).

Details:

  • JP6 is connected, ETH adapter LEDs (green and yellow) are turned on normally when the cable is connected
  • All the functions called from the MX_LWIP_Init() are executed without errors (including dhcp_start ) - checked by debugging
  • netif_is_up() returns true => network interface is up

  • code snippet from main.c:
/* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USB_OTG_FS_PCD_Init();
  MX_LWIP_Init();
  MX_USART2_UART_Init();
  MX_LWIP_Process();
  /* USER CODE BEGIN 2 */
  
  error = tcp_application_start();
  if(error != ERROR_OK)
  {
	  error_handling(error);
  }
 
  if(dhcp_supplied_address(&gnetif) == 1)
  {
	  HAL_UART_Transmit(&huart2, "IP Set\r\n", strlen("IP Set\r\n"), 100);
  }
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  MX_LWIP_Process();
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
  • dhcp_supplied_address() returns false
  • When using live expressions I can see that the network interface (gnetif) has no local IP (set to 0)

  • code snippet from tcp_application_start():
        error_t error = ERROR_OK;
	err_t status = ERR_OK;
	ip4_addr_t server_ip = {0};
 
	tcp_echo_pcb = tcp_new();
	if(tcp_echo_pcb == NULL)
	{
		error_set_function_file_line(__FUNCTION__, __FILENAME__, __LINE__);
		error = ERROR_MEM_NOT_ALLOCATED;
		goto cleanup;
	}
 
	IP4_ADDR(&server_ip, SERVER_IP_0, SERVER_IP_1, SERVER_IP_2, SERVER_IP_3);
 
	status = tcp_bind(tcp_echo_pcb, IP4_ADDR_ANY, TCP_LOCAL_PORT);
	if(status != ERR_OK)
	{
		error_set_function_file_line(__FUNCTION__, __FILENAME__, __LINE__);
		error = ERROR_TCP_BIND;
		goto cleanup;
	}
 
	status = tcp_connect(tcp_echo_pcb, &server_ip, TCP_SERVER_PORT, tcp_echo_client_connected_callback);
	if(status != ERR_OK)
	{
		global_status = status;
		error_set_function_file_line(__FUNCTION__, __FILENAME__, __LINE__);
		error = ERROR_TCP_CONNECT;
		goto cleanup;
	}
  • tcp_connect() function fails, it returns ERR_RTE, which means routing error
  • I tried both with and without tcp_bind code from line 15 to 21, no difference

  • tcp_connect code part where it fails:
if ((pcb == NULL) || (ipaddr == NULL)) {
    return ERR_VAL;
  }
 
  LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
 
  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
  ip_addr_set(&pcb->remote_ip, ipaddr);
  pcb->remote_port = port;
 
  /* check if we have a route to the remote host */
  if (ip_addr_isany(&pcb->local_ip)) {
    /* no local IP address set, yet. */
    struct netif *netif;
    const ip_addr_t *local_ip;
    ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip);
    if ((netif == NULL) || (local_ip == NULL)) {
      /* Don't even try to send a SYN packet if we have no route
         since that will fail. */
      return ERR_RTE;
    }
    /* Use the address as local address of the pcb. */
    ip_addr_copy(pcb->local_ip, *local_ip);
  }
  • function returns from line 20 because local_ip is NULL

  • I set up the TCP echo server on my PC which is on the same network as the board, server is tested with other devices and is working properly
  • I can't see the board in the router configuration interface
  • Tried with two different routers (and networks)
  • I tried both DHCP and static assignment, same result
  • Clock configuration is default
  • LAN port is working and has internet connection (tested on my PC)

STMCubeMX lwIP configuration screenshots:

  • General Settings

0693W000001rkRrQAI.png

  • Key Options

0693W000001rkRwQAI.png

So, from my point of view, it looks like the board doesn't get an IP address from a router. I simply can't connect to the server because the local IP doesn't exist. I tried with two different routers and the behaviour is the same, so I doubt the problem is in the router.

As you can see I haven't written so much code. The whole set-up and lwIP configuration is generated by the Cube.

Either I am doing something wrong with the lwIP configuration, or there is some bug in the generated code.

I would really appreciate if someone could help me. If you need any additional info, I will provide it ASAP.

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

Before your while(1) { MX_LWIP_Process();} loop starts, not much will happen. This loop drives the communication.

Please find a LwIP TCP examples in the Cube H7 package (or "repository") and try to understand.

Unfortunately these examples have not been written using Cube code generator so they differ.

-- pa

View solution in original post

2 REPLIES 2
Pavel A.
Evangelist III

Before your while(1) { MX_LWIP_Process();} loop starts, not much will happen. This loop drives the communication.

Please find a LwIP TCP examples in the Cube H7 package (or "repository") and try to understand.

Unfortunately these examples have not been written using Cube code generator so they differ.

-- pa

Thank you Pavel for pushing me in the right direction!

I added the following code to the main function before my main loop:

  while(dhcp_supplied_address(&gnetif) != 1)
  {
	  MX_LWIP_Process();
  }

I also followed these instructions: https://community.st.com/s/article/How-to-create-project-for-STM32H7-with-Ethernet-and-LwIP-stack-working

Found in this repo: https://github.com/AnielShri/STM32H745_Ethernet

Now I get IP address and I can connect to the TCP server.

Thanks!