6 years, 10 months ago.

Self defined MAC address

Hello,

I am building a project using a LPC1768 micro on a custom board (not an MBED module). I therefore need to manually assign a MAC address tothe Ethernet interface.

While I can use: EthernetInterface.get_mac_address() to get the MAC address on a standard module, I can't see anyway to set it. Can someone tell me how to do this please?

Thanks

Roger

1 Answer

6 years, 10 months ago.

Hello Roger,

There is a weak function mbed_mac_address defined in the mbed library - see Ethernet (MAC) address?.
The function is declared in the platform/mbed_interface.h as follows:

/** This returns a unique 6-byte MAC address, based on the interface UID
 * If the interface is not present, it returns a default fixed MAC address (00:02:F7:F0:00:00)
 *
 * This is a weak function that can be overwritten if you want to provide your own mechanism to
 * provide a MAC address.
 *
 *  @param mac A 6-byte array to write the MAC address
 */
void mbed_mac_address(char *mac);

So you can define a custom MAC address by defining your own mbed_mac_address function in main.cpp before the main() function, for example as below:

/**
 * @brief   Defines a custom MAC address
 * @note    Have to be unique within the connected network!
 *          Modify the mac array items as needed.
 * @param   mac A 6-byte array defining the MAC address
 * @retval
 */
extern "C" void mbed_mac_address(char* mac) {
    mac[0] = 0x00;
    mac[1] = 0x01;
    mac[2] = 0x02;
    mac[3] = 0x03;
    mac[4] = 0x04;
    mac[5] = 0x05;
};

Hi Zoltan,

Thanks for this, I've added the function to my program and the net result is that it turns the mac address into 00.00.00.00.00.00. (which I'm getting using the ARP command). I'm using Mbed-OS if that makes any difference?

Regards

Roger

posted by Roger McArdell 15 Jan 2018

Hello Roger,

I used this demo application for testing. It worked fine without and also with custom MAC address on an official mbed LPC1768 as well as on a home made LPC1768 board. I used a LAN8720 module as Ethernet interface for the custom LPC1768 board.

With best regards,

Zoltan

posted by Zoltan Hudak 16 Jan 2018

Hi Zoltan,

I've got it working! I added 'memcpy(s, mac, 6);' to the function and then it all sprang into life. I've tested it on my custom board as well as the Mbed board and it's working on both. So the complete working function is:

extern "C" void mbed_mac_address(char *s) { char mac[6]; mac[0] = 0x00; mac[1] = 0x02; mac[2] = 0xF7; mac[3] = 0xF2; mac[4] = 0xD2; mac[5] = 0xFE;

memcpy(s, mac, 6); };

I can only conclude that this is a oddity of the MBED-OS version. I've found that there is very little correlation between the earlier Ethernet implementations and mbed-OS. Documentation is practically non-existent and useful examples rarer than rocking horse dung.

Sorry, not ranting at you. Thanks for all your help, it is greatly appreciated.

Regards

Roger

posted by Roger McArdell 16 Jan 2018

This method seems to only work on LPC1768. When I tried to compile on ARCH_MAX, NUCLEO-F429ZI, FRDM_K64F, I get

Symbol mbed_mac_address multiply defined.......

Any remedy for this?

posted by Zhiyong Li 22 Nov 2019