Problem with sending UDP packets with lwIP NETCONN API

I am trying to generate an application that sends UDP packets to a given IP address. I tried many different approaches, but the program doesn’t work. I tried debugging it - all functions work correctly up to netconn_send() - it doesn’t return 0. Furthermore the for loop, that is supposed to send the message 5 times in 0.5 s intervals, freezes after the first call to the netconn_send() - the debugger shows that second loop is never executed… I really don’t know why this happens, and would be very gratefull for any hints. Thanks in advance!

void vInitTask(void* pvParamateres){
    struct netconn *conn;
    struct netbuf *buf;
    ip_addr_t dst_addr;
    char msg[] = "MPC5748G";
    int i;
    err_t connection_err, send_err;    
     u16_t dst_port = 7000;

     /* init the stack */
     tcpip_init(NULL, NULL);    
    /* create a new connection */
    conn = netconn_new(NETCONN_UDP);

    /* set up the IP address of the remote host */
    dst_addr.addr = inet_addr("");

    /* connect the connection to the remote host */
    connection_err = netconn_connect(conn, &dst_addr, dst_port);

    if (connection_err== ERR_OK) { PINS_DRV_TogglePins(PTB, 0x1000); } // blink LED PB12

    /* create a new netbuf */
    buf = netbuf_new();
    /* point the buffer payload to the text */
    netbuf_ref(buf, msg, sizeof(msg));
   /* send the messagge five times */
		send_err = netconn_send(conn, buf);   // THIS FAILS!!
		if (send_err == 0) { PINS_DRV_TogglePins(PTA, 0x2) ;} // blink the LED on PA1
		vTaskDelay( 500 / portTICK_PERIOD_MS );



I’m using MPC5748G a microcontroller form the NXP. I have added the lwIP stack with the GUI in the s32ds (NXP’s eclipse).

Sorry - I don’t know much about lwIP. I’m using the FreeRTOS network stack.
I afraid you have to debug into it to see what’s going wrong …
But a small issue is or will be caused by sizeof(msg) == 4 used with netbuf_ref() because msg is just a const char* pointer.
Good luck !

thank you for your reply.
Unfortunatelly I can’t debug it properly because somehow the send_err variable is always ‘optimzed out’ in the debugger. I don’t know why…

I tired your code on a different hardware and it works for me (I commented out tcpip_init because it was called during wifi connect for me and changed IP and port). I sent the message to a server and verified that it was received on the server.

Would you please try to step through the code of netconn_send? You will reach the function tcpip_send_msg_wait_sem in the file tcpip.c. Depending on the value of the macro LWIP_TCPIP_CORE_LOCKING, it will either directly call lwip_netconn_do_send or post a message so that tcpip_thread calls lwip_netconn_do_send. Either ways you will get a call in
lwip_netconn_do_send. So please put a breakpoint in lwip_netconn_do_send and try to step from there.

Regarding the optimized out variable, can you try to disable compiler optimizations?


Thanks a lot for the answer. I tried debugging the function. Evertyhing up to the ip4_route() function seems fine. This function however returns NULL, although if I inspect the netif struct that is being returned, it has some values - I’am attaching a picture of the variables connected to a struct that should be NULL…
Anyway, this is the piece of code in ip4_route() code that returns NULL:

 if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||
      ip4_addr_isany_val(*netif_ip4_addr(netif_default))) {
     /* No matching netif found and default netif is not usable.
       If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */
    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
      ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
    return NULL; }

I figured out that the interface is not set up properly. So I tried setting it up with the following code and it doesn’t work. I will try debugging it prprely tomorrow, but so far I already have two questions about the netif_add() function.
The last two parameters, meaning the init parameter and inpout paramater of the netif_add() function are problematic for me: how should I set them up for a lwIP on a multithreaded chip? the wiki page is not so clear for me. And if I finally decide to use the egtcpip_ethinput() method, where can I find it?
This is the code to initialise network interface. I am running it just before netconn_new() function call:

 /* set up the netif */
 struct netif* mynetif;
 ip4_addr_t addr;
 ip4_addr_t netmask;
 ip4_addr_t gw;
  uint32_t start;
   tcpip_init(NULL, NULL);
  IP4_ADDR(&addr, 192, 168, 0, 200);
  IP4_ADDR(&gw, 192, 168, 0, 1);
  IP4_ADDR(&netmask, 255, 255, 255, 0);
  /* add the network interface */
   netif_add(&mynetif, &addr, &netmask, &gw, NULL, enet_ethernetif_init,  tcpip_input);
   /* register the default network interface */

I narrowed down the problem to the ENET_DRV_Init() function, that is being called when the new network interface is being created.
I know that I should debug the program further, but it is really tidious especially that I don’t know what am I looking for. All the paramaters of this function call seem correct, however the ENET_DRV_Init() just freezes. Therefore I have a question - did anybody experience problems with this function call in lwip stack?
I am using MPC5748G from NXP, and I am attaching the parameters for the function call in pictures, precisely the enetState, enetConfig, enet BufferConfigs and netif struct.
Function called:

     ENET_DRV_Init(netif->num, &enetState, &enetConfig, enetBufferConfigs, netif->hwaddr);

netif->num, &enetState, &enetConfig, enetBufferConfigs, netif->hwaddr);

Thanks in advance for your answer!!!

The function ENET_DRV_INIT seems from your Ethernet driver (not the common LWIP source) which initializes Ethernet interface. The hardware I use is different and does not have that function.

Since the code is freezing while trying to initialize the Ethernet interface, can you check that you have connectivity and you are using the correct cable? Another step might be to use Wireshark to see if any traffic is coming out.


Thanks for the answer.
Actually as I am debugging it, it is not connected to the ethernet - my router is in a different place… I am just checking the debugger if the code runs correctly (I also set up few LEDs for the debugging purpose).
generallu speaking I am planning to be using UDP, so it should not matter - UDP is connectionless. And, apart from that, I wish to design a system that works and intiliases without a present network connection.

Well, that would depend on how your Ethernet driver is implemented. Would you please connect it first to narrow down the problem?


I guess I found out where the problem is… The pins of the ETH socket have not been intialiased/connected… I never configured them - I focused on setting the lwIP stack.

Even if this doesn’t solve the problem, it definitelly needs to be adressed. It is getting late, I will try to debug it tomorrow, and of course I will post any new information if I have any.

I give up.
The problem was indeed to initalisation of the board and enabling correct pins in appropriate configuration. I failed to do that. The stack never worked.

To solve the issue I downloaded the lwIP example provided by the NXP with the stack already enabled. I deleted all the tasks, and started my project with hardware initialiased properly.

With the configuration provided by NXP, the tcpip_init(NULL, NULL) and the enetif_init() functions work properly, as expected. The stack now works, and I can use the UDP functionality now.

1 Like

Thanks Peter for taking the effort to write this down. Often users read posts because they have similar problems. Thanks!