Keil Logo

MDK MIDDLEWARE: How to set MAC address at startup


Information in this knowledgebase article applies to:

  • Keil MDK 5
  • Middleware 7.0.0, and newer 

QUESTION

Usually, the MAC address of my target board can be configured and hardcoded in Net_Config_ETH_0

To implement our application into mass products, we have to set different unique MAC address for each board at the startup of the board.

How can I set a MAC address at startup of the board, instead of the one hardcoded in Net_Config_ETH_0?

RESOLUTION

Since Middleware 7.0.0 the function netIF_SetOption() is implemented that can setup MAC address.

In order to avoid the customized MAC address being overwritten by netInitialize() with the reconfigured MAC address in Net_Config_ETH_0.h, the function netIF_SetOption() should be called in the user callback function netETH_Notify(), when the Ethernet link is up. e.g.

#define MAC_DEFAULT "1E-30-6C-A2-45-5B"
char mac_addr[18];

void change_mac_addr(void){
  uint8_t buf[6];

  strncpy(mac_addr, MAC_DEFAULT, sizeof(mac_addr));

  if (netMAC_aton(mac_addr, buf) == true) {
    netIF_SetOption(NET_IF_CLASS_ETH | 0, netIF_OptionMAC_Address, buf, 6);
  }
}

void netETH_Notify (uint32_t if_num, netETH_Event event, uint32_t val) {

  switch (event) {
    case netETH_LinkDown:
      printf ("Link is down\n");
      break;
    case netETH_LinkUp:
      printf ("Link is up\n");

      change_mac_addr();

      break;
    case netETH_Wakeup:
      printf ("Wakeup frame received\n");
      break;
    case netETH_TimerAlarm:
      printf ("Timer alarm\n");
      break;
  }
}

Note, if the multicast bit is set in the new MAC address, netIF_SetOption will fail with netInvalidParameter.

MORE INFORMATION

  • Refer to netIF_SetOption in the documentation of Network Common Interface API.

Last Reviewed: Friday, October 30, 2020


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.