cancel
Showing results for 
Search instead for 
Did you mean: 

strange problem with lwip udp, only broadcast address works

HYu.5
Associate II

I'm using stm32cubeMX to create the project on a nucleo F767ZI board

I'm using FreeRTOS and LWIP for a simple UDP sender.

I can get IP address form DHCP and ping works, and the packet can be sentout without any problem if the destination is the broadcast address, if I set it to some computer's address, the program will hang, I tried to put a LED GPIO after the send function call, seems it's not return from that function. on line debuging shows it's endup in a hard fault with unaligned memory access.

I also tried specify the IP address instead of using DHCP, still only works in boradcast only. The program will also endup in hard fault with unaligned memory access.

While the dubuging, I found a stange problem, The DHCP works, both ping and DHCP server shows the board has successfuly got the ip address.

If the destination is a broadcast, during the debug, I can find the "struct netif *netif_default;" and "struct netif *netif_list;" in netif.c is pointing to the correct interface with the correct ip address, netmask and gateway.

However, when the UDP send destination is set to a specific ip address, even before calling the udp send, the "struct netif *netif_default;" and "struct netif *netif_list;" in netif.c shows the ipaddress is 0, which lead to unable to find the route. and hanging the system which shouldn't happen. It suppose to return error that unable to find the host instead of hanging.

Here is the code:

void StartDefaultTask(void const * argument)

{

uint8_t pui8Text[19] = "This is a Test 1.\n";

 volatile err_t tErr;

struct netconn* ptUDP;

 struct netbuf* ptNetBuf;

ptUDP = netconn_new(NETCONN_UDP);

 tErr = netconn_bind(ptUDP, IP_ADDR_ANY, 5555);

// IP4_ADDR(&tDest, 192, 168, 001, 160); // not work

 IP4_ADDR(&tDest, 192, 168, 001, 255); // works

// IP4_ADDR(&tDest, 255, 255, 255, 255); // works

// tDest.addr = 0xa001a8c0; // not work

// tDest.addr = 0xffffffff; // works

 ptNetBuf = netbuf_new();

 netbuf_alloc(ptNetBuf, 18);

 netbuf_ref(ptNetBuf, pui8Text, 18);

 vTaskDelay(pdMS_TO_TICKS(1000));

for(;;)

 {

  tErr = netconn_sendto(ptUDP, ptNetBuf, &tDest, 5555);

vTaskDelay(pdMS_TO_TICKS(100));

}

}

1 ACCEPTED SOLUTION

Accepted Solutions
alister
Lead

> the packet can be sentout without any problem if the destination is the broadcast address

Unicast uses ARP. Broadcast doesn't. Guessing ARP is tipping a stack-size.

If LWIP_TCPIP_CORE_LOCKING is non-zero, it'll be StartDefaultTask's stack-size.

Else it'll be the tcpip_thread's stack size, TCPIP_THREAD_STACKSIZE.

Confirm by increasing the requisite stack size by 0x200 words (where word = 32-bits) and re-test.

If it's definitively the cause, then tune the stack size more carefully.

View solution in original post

6 REPLIES 6
alister
Lead

> the packet can be sentout without any problem if the destination is the broadcast address

Unicast uses ARP. Broadcast doesn't. Guessing ARP is tipping a stack-size.

If LWIP_TCPIP_CORE_LOCKING is non-zero, it'll be StartDefaultTask's stack-size.

Else it'll be the tcpip_thread's stack size, TCPIP_THREAD_STACKSIZE.

Confirm by increasing the requisite stack size by 0x200 words (where word = 32-bits) and re-test.

If it's definitively the cause, then tune the stack size more carefully.

HYu.5
Associate II

Thanks you very much!

It's turned out the stack size is the problem.

MMałk.1
Associate

HI,

I have more strange problem.

Like the example above, broadcast works fine, but also sending to PC(windows) works fine. I can't send UDP frame to diferent ip adress.

I use Wireshark software to monitor ethernet and I can see that ARP works-  I see mac address of PC computer in the UDP frame. When I write different ip address from my local network, the frame isn’t sent.

I would like to send data from STM32F207ZG to raspberryPi 4 with influxdb. Raspberry responds on ping and I can send correct UDP frame from PC to Pi and the value is written to database.

I have tried to add manually #define TCPIP_THREAD_STACKSIZE 1024 to lwipopts.h because I couldn’t find it in STM32CubeMX LwIP Configuration GUI. It doesn’t work.

I have read the topic from this post but I couldn’t connect it with my case. I am beginner in STM32.

My function to send UDP is attached:

Where can be a problem?

Thank You in advance for help.

Piranha
Chief II

If you are also using RTOS, then your code breaks lwIP multithread requirements just like all ST's code does.

MMałk.1
Associate

FeeRTOS is disabled.