Skip to main content
yuri CH
Senior
July 26, 2021
Question

Changing IP address / MAC address during runtime

  • July 26, 2021
  • 4 replies
  • 6198 views

Hello!

Please advise on the proper way to change IP and MAC address during runtime.

Are there any standard ways of doing this? Are there any reinitilizations needed? which functions to use?

thanks in advance!

    This topic has been closed for replies.

    4 replies

    Pavel A.
    Super User
    July 26, 2021

    MAC address is provided in parameters for HAL_ETH_Init(). So the simplest way is to do HAL_ETH_DeInit then HAL_ETH_Init with new address.

    yuri CH
    yuri CHAuthor
    Senior
    July 27, 2021

    thanks for the response,

    understood. i guess the same goes for IP address...?

    Pavel A.
    Super User
    July 27, 2021

    The IP address is assigned and used by software layer such as LwIP.

    The MAC (ETH) has some offloads that use the IP address, but this is advanced stuff.

    Especially, the ARP offload (automatic reply to ARP) is broken in STM32H7 and should be disabled (see errata).

    So after change of the MAC address, make sure that higher layer refreshes its ARP cache or whatever.

    -- pa

    Piranha
    Principal III
    August 8, 2021
    // Change IP addresses
    LOCK_TCPIP_CORE();
    if (hDHCP->state == DHCP_STATE_OFF) {
    	netif_set_ipaddr(hNetif, (ip4_addr_t*)puData);
    	netif_set_netmask(hNetif, (ip4_addr_t*)puData);
    	netif_set_gw(hNetif, (ip4_addr_t*)puData);
    	dns_setserver(0, (ip_addr_t*)puData);
    	dns_setserver(1, (ip_addr_t*)puData);
    }
    UNLOCK_TCPIP_CORE();
     
    // Change MAC address
    LOCK_TCPIP_CORE();
    bool fLink = netif_is_link_up(hNetif);
    netif_set_link_down(hNetif);
     
    memcpy(hNetif->hwaddr, puData, hNetif->hwaddr_len);
    DRV_ETH_MACAddrSet(hNetif->hwaddr); // Set MAC address on ETH peripheral
     
    if (fLink) {
    	netif_set_link_up(hNetif);
    }
    UNLOCK_TCPIP_CORE();

    The rest of the ARP and DHCP things will be done by the lwIP stack automatically.

    yuri CH
    yuri CHAuthor
    Senior
    August 9, 2021

    Hi, thank you for answering!

    i am working with static IP and the DHCP is disabled.

    how might that change your answer?