# FreeRTOS\_IPInit function Variable MAC Address

**URL:** <https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419>\
**Category:** Libraries\
**Created:** [April 3, 2020, 9:36pm UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419 "2020-04-03T21:36:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iftikhares](https://avatars.discourse-cdn.com/v4/letter/i/dfb087/32.png) [@iftikhares](https://forums.freertos.org/u/iftikhares)\
**Post date:** [April 3, 2020, 9:36pm UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419/1 "2020-04-03T21:36:52Z")

</div>

Hi,

I am working on a project where I need to make the MAC address variable such that my ucontroller is connected to a chip which has 32bit unique ID. I read this 32bit ID into a buffer and then I want to use this 32bit ID for MAC Address.

The problem is FreeRTOS\_IPInit function takes a const MAC address as shown in the following declaration in FreeRTOS\_IP.h file.

BaseType\_t FreeRTOS\_IPInit( const uint8\_t ucIPAddress[ipIP\_ADDRESS\_LENGTH\_BYTES],  
const uint8\_t ucNetMask[ipIP\_ADDRESS\_LENGTH\_BYTES],  
const uint8\_t ucGatewayAddress[ipIP\_ADDRESS\_LENGTH\_BYTES],  
const uint8\_t ucDNSServerAddress[ipIP\_ADDRESS\_LENGTH\_BYTES],  
const uint8\_t ucMACAddress[ipMAC\_ADDRESS\_LENGTH\_BYTES] );

When I modify the above function by removing “const” term. I can pass the 32bit ID and can initialize the IP stack. I can even see my board gets IP address assigned by the DHCP, however when I try to communicate with Development board (TM4C129E). My Putty/YAT terminal says the connection refused. YAT terminal gives me following message;  
\<Warning: An existing connection was forcibly closed by the remote host\>

Please note, if I do not modify the above function and rather use a const MAC address. Everything works perfect and I can talk to my board using Putty/YAT Terminal.

Any hint or any alternative approach that can make me accomplish it?

Regards,  
Abid

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [April 3, 2020, 9:47pm UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419/2 "2020-04-03T21:47:18Z")

</div>

Store the ID in a persistent (static) buffer and you should be fine. The const buffer of the MAC address buffer implies that the buffer must not change after IPInit because I think it’s not copied ‘into’ the stack but the provided data is just used (by reference). You might check the stack sources if this assumption is right.  
I assume the unique chip ID is truly unique. Otherwise … strange things might happen.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [April 4, 2020, 5:34am UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419/3 "2020-04-04T05:34:52Z")

</div>

Hartmut, thanks for getting into this!

> I think it’s not copied ‘into’ the stack but the  
> provided data is just used (by reference)

Yes it is copied into a safe place:

```c
    /* The MAC address is stored in the start of the default packet
    header fragment, which is used when sending UDP packets. */
    memcpy( ipLOCAL_MAC_ADDRESS, ucMACAddress, ipMAC_ADDRESS_LENGTH_BYTES );

```

`ipLOCAL_MAC_ADDRESS` points to a structure owned by the IP-stack. So the bytes are stored, not the address.

Normally, for commercial products, a special MAC chip is mounted onto the board. It gives an administered, globally unique MAC address. The chip normally has a simple 1-wire access.

If you make up your own MAC-address, please look at e.g. this [Wiki page](https://en.wikipedia.org/wiki/MAC_address#/media/File:MAC-48_Address.svg) about MAC addresses.

So unless your unique ID is a real MAC address, please set the lower to bis of the first byte to 0x02.

I do not see why you need to change the const-ness of the `ucMACAddress` parameter?  
And I don’t see neither why it would change anything to its behaviour.

What happens is the following:

```c
int main()
{
    /* No need for const here. The bytes will be copied into another buffer. */
    uint8_t ucMACAddress[6];
    /* Your function reads the unique ID. */
    read_unique_ID( ucMACAddress );
    /* Initialise the IP-stack. ucMACAddress is casted to const uint8_t*
    in the call. */
    FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGateway, ucDNS, ucMACAddress );
    /* And start the scheduler. */
    vTaskStartScheduler();
}

```

After `FreeRTOS_IPInit`: the MAC-address ( stored in `ipLOCAL_MAC_ADDRESS` ) will be written into the MAC registers of the EMAC, so your device gets an address.

As it is now, a FreeRTOS+TCP project takes a MAC address at start-up and never changes this address.

Summary: it makes no difference whether your MAC-address is defined with `#define` ( `configMAC_ADDR0` ), or dynamically obtained before calling `FreeRTOS_IPInit()`.

---

<div class="post-metadata">

**Author:** ![GiselleLFreude](https://avatars.discourse-cdn.com/v4/letter/g/85f322/32.png) [@GiselleLFreude](https://forums.freertos.org/u/GiselleLFreude)\
**Post date:** [December 20, 2022, 11:27pm UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419/4 "2022-12-20T23:27:31Z")

</div>

I’m really stuck on this problem. The things that Hein said make sense… but it can’t be the right answer. I tried updating the default IP address freeRTOS uses to the one I was trying to change it to and then it worked fine. If I change uMACAddress to be non const and then dynamically update it, pings always fail. Its weird because I see the entry for the device in the arp cache but it just wont reply to pings. There’s something going on with that ucMACAddress variable. It cannot be changed it seems. Has anybody successfully updated their mac address dynamically in freertos? Its crazy this question is still unsolved.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 21, 2022, 6:41am UTC](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419/5 "2022-12-21T06:41:24Z")

</div>

You have two posts with an almost identical title. Do forget to read the answers to the new one.

Old: [FreeRTOS\_IPInit function Variable MAC Address](https://forums.freertos.org/t/freertos-ipinit-function-variable-mac-address/9419) (April this year)  
New: [Cannot change MAC address dynamically?](https://forums.freertos.org/t/cannot-change-mac-address-dynamically/16428) (now in December)
