The content has been cited from https://community.nxp.com/thread/429919
Below is the summary on how to write and read MAC address for KW40/41 MCUs cited from NXP community https://community.nxp.com/thread/429919
On the BLE Host Stack, the device MAC addresses are defined at compile time. For each device, the MAC address can be modified in the app_preinclude.h file.
If you want to read a MAC address from the BLE Client you must call the function Gap_ReadPublicDeviceAddress (in our demos it is called in the function BleApp_Config in app.c). After this, the gPublicAddressRead_c event will occur every time a new MAC address is discovered. You must obtain these MAC addresses in the BleApp_GenericCallback (in app.c) by reading the pGenericEvent pointer as shown in the image below.
Every time a new MAC address is found nearby, this callback function will be called.
To program different MAC address for different KW40/41
The way you program different MAC addresses in production is by using a programmer with serialization capabilities (i.e. P&E Cyclone Max). In our stack, the MAC Address is stored in a global variable under the name gBDAddress_c. First you need to open the .map file that is generated in you code, look for this variable and get where it is located in flash.
Once you have the variable address, you can now use the same binary to program all the MCUs and use the serialization tool to overwrite only the memory location that contains the MAC address. Instructions for doing this will depend on the flashing tool you will use in production.
we need to overwrite the memory location again after we update another Apps on our unit.
For example, if we have heart_rate_sensor_app on KW41Z first then we program different MAC addresses on each unit. After that we update hci_app on it.
The memory location of the MAC address holding arraywill change with every application or even in the same application if the application code changes. To overcome this you need to make sure that this variable will have always the same location in memory. This has a different procedure depending on the IDE you are using. In IAR for example, adding a pragma before the variable in question does the trick.
#pragma location=address
char variable[size];
The content has been cited from https://community.nxp.com/thread/429919