cancel
Showing results for 
Search instead for 
Did you mean: 

How configure LwIP to receive UDP broadcast using STM32CubeMx ?

Peko
Associate III

Hello,

I need to receive UDP Broadcast, but The "callback" function is never called.

"No broadcast" UDP communication works fine.

Is IwIP at all able to receive UDP broadcast?

I use LwIP 2.0.3, STM32F407, PHY DP83848

/*----- Default Value for IP_SOF_BROADCAST: 0 ---*/
#define IP_SOF_BROADCAST 1
/*----- Default Value for IP_SOF_BROADCAST_RECV: 0 ---*/
#define IP_SOF_BROADCAST_RECV 1
   
void udp_server_init(void)
{
     struct udp_pcb *upcb;
     err_t err;
       
     upcb = udp_new();
     ip_set_option(upcb, SOF_BROADCAST);
    
     if (upcb)
     {
         err = udp_bind(upcb, IP_ADDR_BROADCAST, 8000);
        
         if(err == ERR_OK)
         {
            udp_recv(upcb, udp_receive_callback, NULL);
         }
     }
}
 
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
   HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_2);
    	
   pbuf_free(p);		// Free the p buffer
    
   //udp_transmit(upcb, addr, udp2_tx_data, udp2_tx_len);
}  

Thank you

Peter

4 REPLIES 4
Piranha
Chief II

DHCP client in dhcp.c uses IP4_ADDR_ANY. Maybe try binding to that for the sake of experiment.

Check BFD bit in ETH->MACFFR and maybe try RA bit.

Also make sure that NETIF_FLAG_BROADCAST flag is set for netif->flags.

Peko
Associate III

Hi Piranha,

NETIF_FLAG_BROADCAST is enabled in: static void low_level_init(struct netif *netif)

static void low_level_init(struct netif *netif)
{ 
 ....
   /* Accept broadcast address and ARP traffic */
  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  #if LWIP_ARP
    netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  #else 
    netif->flags |= NETIF_FLAG_BROADCAST;
  #endif /* LWIP_ARP */
  ...
  }

I tried too: err = udp_bind(upcb, IP_ADDR_BROADCAST, 8000);

I use MX_LWIP_Init() which it generates by STM32CubeMx.

It still doesn't work.

Peter

Piranha
Chief II

https://www.nongnu.org/lwip/2_0_x/group__udp__raw.html#gac7fbda8b12b9b9360e92b51e805e799e

"ipaddr - local IP address to bind with. Use IP4_ADDR_ANY to bind to all local interfaces."

It's the local address of network interface to which binding must be done. It can't be IP_ADDR_BROADCAST as broadcast address is not a local address.

Piranha
Chief II

Some additional information. IP_SOF_BROADCAST and IP_SOF_BROADCAST_RECV enables broadcast filtering per connection. By default those are off and that means that all connections receive broadcasts irrespective of SOF_BROADCAST. When per connection filtering is enabled, no connection receives broadcasts unless SOF_BROADCAST option is set for particular connection.