2020-06-29 08:51 AM
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:
/* 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 */
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;
}
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);
}
STMCubeMX lwIP configuration screenshots:
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!
Solved! Go to Solution.
2020-06-29 11:22 AM
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
2020-06-29 11:22 AM
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
2020-07-07 12:14 AM
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!