Other Parts Discussed in Thread: SYSCONFIG,
Tool/software:
Dear team
I'm working with the CC2340R5 using SDK 8.40 and IDE 12.8.
I'm trying to change the device’s MAC address during a firmware update.
I tried using SysConfig by setting the address type to "RPA with Public Identity", but the MAC address changes on every power cycle, which is not the desired behavior.
I also attempted to change the MAC address programmatically using the code below. It works only the first time — the MAC address does not update with subsequent firmware updates.
Could you please advise on how to ensure the MAC address is updated reliably during each firmware update?
Thank you.
static uint32_t seedCounter = 0;
uint8_t getFakeRandom()
{
seedCounter++; // This increments every time the function is called
return (uint8_t)((Random_getNumber() + seedCounter * 31) & 0xFF); // 31 is just a salt multiplier
}
void generateRandomMAC(uint8_t *macAddr)
{
macAddr[0] = getFakeRandom() & 0x3F; // Lower 6 bits only
macAddr[1] = getFakeRandom();
macAddr[2] = getFakeRandom();
}
// Initialize or load the custom BD address
void setCustomBDAddr(void)
{
uint8_t customBDAddr[6] = { 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A };
// Open NVS
NVS_Params nvsParams;
NVS_Params_init(&nvsParams);
nvsHandle = NVS_open(CONFIG_NVS_0, &nvsParams);
if (nvsHandle == NULL) {
// Handle error
return;
}
NVS_getAttrs(nvsHandle, ®ionAttrs);
// Read existing MAC from flash
uint8_t readMac[MAC_ADDR_SIZE];
int status = NVS_read(nvsHandle, 0, readMac, MAC_ADDR_SIZE);
// If no MAC saved, generate and write it
if (status != NVS_STATUS_SUCCESS || readMac[0] == 0xFF) {
generateRandomMAC(customBDAddr);
NVS_erase(nvsHandle, 0, regionAttrs.sectorSize); // Clean sector
NVS_write(nvsHandle, 0, customBDAddr, MAC_ADDR_SIZE, NVS_WRITE_POST_VERIFY);
}
// Set BD_ADDR with HCI command
HCI_EXT_SetBDADDRCmd(readMac);
// Optional debug
NVS_close(nvsHandle);
}