I need to be able to set the mac address on an Ethernet interface. OS6.6.0 added the command ‘set_mac_address’ which seemed to be the answer to my prayers, but on testing the command using a FRDM-K66 board, it doesn’t actually work!
The command executes correctly and returns a nsapi_error_t of NSAPI_ERROR_OK which the documentation says means that the command has completed successfully, however it doesn’t set the mac address, in fact it doesn’t actually do anything at all.
From what I understand the Mbed OS generates a random MAC address based on the serial number of the chip, it is not a proper IEEE registered MAC address. This is fine for home hobbyists, but no use for commercial products.
If I’ve got this wrong and it is a proper IEEE MAC then I’ll be delighted to be corrected.
I had similar Problems as @rogermcardell, however I used STM32 boards where this problem is easily solvable.
STM32 way
The MAC address during startup is “generated” in the function mbed_mac_address(…) in platform\source\mbed_interface.c, where it is defined as WEAK. The standard STM32Emac overwrites this with a STM32-specific way of generating MAC addresses. However, there is another weak function definition, mbed_otp_mac_address(…), which can be used to set specific MAC addresses.
Freescale way
From my very brief search in the MBed OS source code, only the STM32 targets have this mechanism. For the FRDM-K66 board there is no such code or mechanism. In the file targets\TARGET_Freescale\TARGET_MCUXpresso_MCUS\TARGET_K66F\TARGET_FRDM\mbed_overrides.c the MAC address is generated and cannot be changed.
My opinion
My feeling is that a similar mechanism to the STM32 way would be very helpful also for other targets, but unfortunately I don’t have access to such devices currently to implement it.
I set a breakpoint and found that the ENET_SetMacAddr function is run twice when ‘net->connect();’ is executed. This function is not called by ‘net->set_mac_address(my_macaddr,6);’ The first time round *macAddr is zero, the second time it has values but these do not seem to have any resemblance to the Mac address that is finally set.
At this point I got stuck, so I tried hard coding my new MacAddress into the ENET_SetMacAddr function, but then I get an error when ‘net->connect()’ is executed.
when the MCU is stopped at the BP, you can look for a window ‘call stack’ (MCUXpresso IDE?). In this window, you can click on the callers and see where the call comes finally from.
The first call with zeros is maybe from the initialization. I don’t know the K66f, maybe setting the MAC is somehow limited? Reading the MAC seems also not to be supported.