I've created this function
static void set_bd_addr(void)
{
ret_code_t ret_code;
ble_gap_addr_t gap_addr;
gap_addr.addr_id_peer = 0;
gap_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
// Get BD address from UICR memory
uint8_t* ptr = (uint8_t*)&NRF_UICR->CUSTOMER[0];
// Check if not written
uint8_t empty_addr[BLE_GAP_ADDR_LEN];
memset(empty_addr, 0xff, BLE_GAP_ADDR_LEN);
if(!memcmp(ptr, empty_addr, BLE_GAP_ADDR_LEN))
{
NRF_LOG_WARNING("No BD address in flash, using chip default");
ret_code = sd_ble_gap_addr_get(&gap_addr);
APP_ERROR_CHECK(ret_code);
}
else
{
memcpy(&gap_addr.addr, ptr, BLE_GAP_ADDR_LEN);
}
NRF_LOG_INFO("Setting bd address to: %02X:%02X:%02X:%02X:%02X:%02X",
gap_addr.addr[5],
gap_addr.addr[4],
gap_addr.addr[3],
gap_addr.addr[2],
gap_addr.addr[1],
gap_addr.addr[0]);
ret_code = sd_ble_gap_addr_set(&gap_addr);
APP_ERROR_CHECK(ret_code);
}
which I call in my advertising_init in the main application and it works fine for setting the BD address written in UICR. But when I try to perform a DFU it doesn't work and I suspect that it's because of the bootloader still using the chip default BD address. I tried adding the same function in the bootloader just before nrf_bootloader_init like so:
NRF_LOG_INFO("Inside main");
set_bd_addr();
ret_val = nrf_bootloader_init(dfu_observer);
But then the bootloader doesn't start at all. What is the correct way to do it?