cancel
Showing results for 
Search instead for 
Did you mean: 

Changing IP address / MAC address during runtime

yuri CH
Senior

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!

5 REPLIES 5
Pavel A.
Evangelist III

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
Senior

thanks for the response,

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

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
Chief II
// 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
Senior

Hi, thank you for answering!

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

how might that change your answer?