Ethernet (MAC) address?

07 Aug 2010

How is the ethernet/MAC address assigned? Where is it stored? Where can I retreive it if I'm not using the Ethernet.address() class?

(I'm not using the mbed libraries at all, but would like to use a valid MAC address rather than just making one up)

 

Thanks!

07 Aug 2010

Hi Hugo,

If you are using the mbed libraries, you can call:

 

extern "C" void mbed_mac_address(char *mac);

which returns the 6 character mac address.

 

We haven't officially exposed the underlying mechanism it uses to generate this MAC, but it is currently a semihost request to the interface to get its UID from which it is extracted. Every mbed module has a UID which includes an officially allocated MAC address, so it is guaranteed unique. To get the UID code, you'll need to do something like:

int semihost_uid(char *uid) {
	uint32_t args[2];
	args[0] = (uint32_t)uid;
	args[1] = 33;
	return __semihost(0x101, &args);
}
In that string returned, you'll see starting at index 20 is a MAC address which you can read and encode.

Simon

08 Aug 2010 . Edited: 08 Aug 2010

Thanks heaps Simon! That's exactly the info I needed, works perfectly.

08 Aug 2010

Which library has mbed_mac_address() defined?  I have the std mbed.h and EthernetNetIf.h libs; with the above extern declarartion, when trying to use the function, it remains undefined.

10 Aug 2010

user avatar C Neroth wrote:

Which library has mbed_mac_address() defined?  I have the std mbed.h and EthernetNetIf.h libs; with the above extern declarartion, when trying to use the function, it remains undefined.

It appears to be in a private header file and not exported as part of the public API. For what it's worth, it looks like the compiled code is in capi.ar.

10 Aug 2010

Hi,

Here is an example I just posted showing it working:

Hope that gets you going.

Simon

22 Mar 2011

Hello.

I make prototype in mbed and what will do when I shift to production in this ?

http://mbed.org/projects/cookbook/wiki/Prototype2Product

http://mbed.org/users/chris/notebook/prototype-to-hardware/

The shift method of the hardware is listed in the URL mentioned above, but it is not written about MAC ADDRESS.

I think using a Serial EEPROM with Node Identity to get unique MAC ADDRESS.

http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=25AA02E48-I/SN-ND

But I cannot understand whether I let an address of target board which how about reflect it. If there is a good thought, please teach it.

Thank you.

22 Mar 2011

The Mbed comes with a preprogrammed MAC address as part of it's backend firmware. You can get at it with this function:-

extern "C" void mbed_mac_address(char *s);

int main() {
    char mac[6];
    mbed_mac_address(mac);
    printf("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 
}

The function is "weakly defined" which means you can create your own function called void mbed_mac_address(char *s); and the linker will use your implementation over the standard one defined by the Mbed libraries.

This still leaves you with the problem that once you leave the Mbed to your own production unit, where do you actually get a MAC address from given that you no longer have the Mbed backend to give it to you? That's where you need a device like an 25AA02E48. You then need to write some code that can read the MAC address from that device.

22 Mar 2011

I know mbed_mac_address(char *s).

I want to hear how can I transport the software which I developed in mbed to an original board ?

22 Mar 2011

#ifdef PRODUCTION_RELEASE
extern "C" void mbed_mac_address(char *s) {
    char mac[6];
    // Write your code to get the MAC address from the 25AA02E48 into mac[6]
    memcpy(s, mac, 6);
}
#endif

When testing on the Mbed ensure PRODUCTION_RELEASE is NOT defined so that it uses Mbed's standard library function.

When creating a version for your target pruction board define PRODUCTION_RELEASE to replace Mbed's library function with your own version instead.

If your prototype Mbed has the 25AA02E48 on it then you can just use your version instead even on the Mbed as well as the production target. But without more knowledge of your test setup and production unit I can't provide more info.

22 Mar 2011

Thank you, Andy. It works good !

19 Mar 2012
  1. include "mbed.h"

extern "C" void mbed_mac_address(char *mac);

int main() { char mac[6]; mbed_mac_address(mac); char temp[1]={0},id[10]={0}; for(int i=0; i<6;i++) { sprintf(temp,"%02X ", mac[i]); strcat(id, temp); } printf("%s \n",id); }

17 Nov 2012

Hey guys,

I was just wondering if I can actually change the MAC address on my mBed. I want to do this since my university will assign a static ip only on the MAC address that they provide.

Any help in this matter will be greatly appreciated.

18 Nov 2012

Hi Neel,

The mbed Ethernet library sets the MAC address by calling a weak function extern "C" void mbed_mac_address(char * mac) to copy in a 6 Byte MAC address. This function performs a semihosting request to the mbed interface to get the serial number, which contains a MAC address unique to every mbed device. If you are using the ethernet library on your own board (i.e. not an mbed board), or if you want to set your own MAC address you should implement your own extern "C" void mbed_mac_address(char * mac) function to overwrite the existing one and avoid a call to the interface (which doesnt exist on your own hardware anyhow).

extern "C" void mbed_mac_address(char * mac) {

// define your own MAC Address
  mac[0] = 0x00;  
  mac[1] = 0x01;  
  mac[2] = 0x02;  
  mac[3] = 0x03;  
  mac[4] = 0x04;  
  mac[5] = 0x05;           
  
};

Just make sure the MAC address is unique on your network (or even better: unique in the whole world!)

23 Nov 2012

Thanks Wim!

I could get it to work!! :)

17 Apr 2014

Hi guys, I'm a bit late to this tread. but im having a bit of trouble getting the Ethernet to work on my custom board

Here's what i get from wireshark for the mbed module:

/media/uploads/WilliamCoventry/mbedwork.jpg

Heres what i get from my custom board:

/media/uploads/WilliamCoventry/wiresharkcapture.jpg

is the 25AA02E48 chip necessary?

I'm just using the UDP echo server example

Thanks Will

17 Apr 2014

You dont need the mbed interface chip nor its flash mem as long as there are no call to the interface. The weak function shown above should prevent any such calls related to getting a mac address.

22 Apr 2014

Hi Wim, thanks for your reply

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;           
  
};

Should this be inside or before the main loop?

I'm guessing before the main function, but im still having no luck :/

22 Apr 2014

Hi William, the extern code should be outside/before the main. However, I am not sure that this is the problem. Can you publish code and are you able to figure out where the mbed code gets stuck using printf for example.

23 Apr 2014

Hi Wim,

#include "mbed.h"
#include "EthernetInterface.h"

DigitalOut led(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

#define ECHO_SERVER_PORT   7

extern "C" void mbed_mac_address(char *mac){

    mac[0] = 0x00;
    mac[1] = 0x02;
    mac[2] = 0xF7;
    mac[3] = 0xF1;
    mac[4] = 0x91;
    mac[5] = 0x9F;
    };    

int main (void) {
    
    
    EthernetInterface eth;
     eth.init("192.168.2.7","255.255.255.0","192.168.2.5");
    eth.connect();
    led=1;
    
    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);
    led2=1;
    
    Endpoint client;
    char buffer[256];
    
    while (true) {
        led3=1;
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        led=0;
        
        server.sendTo(client, buffer, n);
        led2=0;
        
        
        
      
    }
}

I'm using the LED's for debugging. I haven't got a serial to usb converter, so i cant use printf for debugging.

when i plug in my board i can see an arp request from it, showing the ip and mac address, but when i send a udp packet, my board doesn't receive it.

24 Apr 2014

This is my code to GET ethernet address from MBED LPC1768:

#include "mbed.h"

extern "C" void mbed_mac_address(char *s);

// -> http://mbed.org/forum/mbed/topic/972/?page=1#comment-10318

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); 

int main() 
{
    uint64_t uid = 0;
    char mmac[6];
    char mac[6];

    pc.baud(921600);

    pc.printf ("\r\nTrying to read My MAC Address ..\r\n");

    mbed_mac_address(mac);
    uid = mac[0] << 40 | mac[1] << 32 |
          mac[2] << 24 | mac[3] << 16 |
          mac[4] << 8  | mac[5] << 0;

    pc.printf ("Here it is .. %02x %02x %02x %02x %02x %02x !\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);


    while (1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}


and how to SET (the BORROWED) address:

#include "mbed.h"

extern "C" void mbed_mac_address(char *s) 
{
    char mac[6];
    mac[0] = 0x00;
    mac[1] = 0x02;
    mac[2] = 0xf7;
    mac[3] = 0xf0;
    mac[4] = 0x46;
    mac[5] = 0x4b;

    memcpy(s, mac, 6);
}


// the one on my desk ........ 00 02 f7 f0 55 5e
// the one on the fireplace .. 00 02 f7 f0 46 4b

// 00:02:f7:f0:46:4b .. from tweet program ?

// -> http://mbed.org/forum/mbed/topic/972/?page=1#comment-10318

// Going to write the MAC address from one of my MBED's to annother MBED.

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx  pc.baud(921600);

int main() 
{
    uint64_t uid = 0;
    char mmac[6];
    char mac[6];

    pc.baud(921600);

    pc.printf("Trying to overwrite MAC Address ..\r\n");

    mbed_mac_address(mmac);
    uid = mmac[0] << 40 | mmac[1] << 32 |
          mmac[2] << 24 | mmac[3] << 16 |
          mmac[4] << 8  | mmac[5] << 0;

    pc.printf ("Here it is .. %02x %02x %02x %02x %02x %02x .. \r\n", mmac[0], mmac[1], mmac[2], mmac[3], mmac[4], mmac[5]);

    while (1) 
    {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}


Hope this was useful

Ceri

24 Apr 2014

William, you say you dont have a usb-serial connection for debugging, but in another thread you mentioned using flashmagic for programming the board. That same serial port can be used to print messages. Either using the flashmagic terminal window or some other console application. I also noticed in the wireshark data above that the mac address seems to be 00:00 etc. That hints at some failure setting the address.

29 Apr 2014

Hi guys, Thanks a lot for your help.

Turns out it was a hardware problem. I replaced the phy, and hey presto, it worked!

Cheers Will

08 Jul 2015

Seek in SDK mbed source,

function WEAK void mbed_mac_address(char *mac)

defined in mbed_interface.c

value is selected with condition that

1. DEVICE_SEMIHOST supported , adrs is derived from UID

2. DEVICE_SEMIHOST not supported , adrs is fixed value 00:02:F7:F0:00:00

If you want to define in original source, you must edit above lines. (and re-compile) or, replace mbed_mac_address() with your own func. (already descrived by folks)