Change the Device Name via a Name Stored in the UICR register

Hello,

I'm trying to implement a set up of the ble device name from the NVMC UICR register.

But when I try to take it from the register and to set it has a name, it doesn't work very well. I think this is an issue about the utf-8 encoder of the ble device name.

Here's my code: 

1) Write the device name only if it wasn't done before.

static void write_name ()
{    
    uint32_t* deviceName = (uint32_t*)&NRF_UICR->CUSTOMER[0];
    if (*deviceName == 0xFFFFFFFF){
      ret_code_t err_code;
    
      err_code = nrf_sdh_disable_request();
      APP_ERROR_CHECK(err_code);

      NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos;
      while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
      NRF_NVMC->ERASEUICR = NVMC_ERASEUICR_ERASEUICR_Erase;
      NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
      while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

      NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
      while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
      uint8_t* name = "WAW-TEST";
      NRF_UICR->CUSTOMER[0] = 0xFFFFFFFF & *name;
      NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
      while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
      
      NVIC_SystemReset();
    }
}

2) set the ble device name

static void gap_params_init()
{
    ret_code_t              err_code;
    ble_gap_conn_params_t   gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);

    uint32_t* deviceName = (uint32_t*)&NRF_UICR->CUSTOMER[0];
    uint8_t name = 0xFFFFFF00 | *deviceName;
    uint8_t* n = &name;
    NRF_LOG_INFO("%s", (char*)n);

    if (*deviceName == 0xFFFFFFFF){
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                          (const uint8_t *)DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    } else {
      err_code = sd_ble_gap_device_name_set(&sec_mode,
                                          (const uint8_t*)n,
                                          strlen(n));
    }

    APP_ERROR_CHECK(err_code);

    /* YOUR_JOB: Use an appearance value matching the application's use case.
       err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_);
       APP_ERROR_CHECK(err_code); */

    memset(&gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
    gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
    gap_conn_params.slave_latency     = SLAVE_LATENCY;
    gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;

    err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
    APP_ERROR_CHECK(err_code);
}

Currently, when I search for it, I'm seeing this : 

When I should see "WAW-TEST"...

Does someone have a solution to this ?

  • Hello,

    It's only the first character that will get stored UICR when you derefence your uin8_t pointer. You may try something like this instead:

        uint32_t* deviceName = (uint32_t*)&NRF_UICR->CUSTOMER[0];
        if (*deviceName == 0xFFFFFFFF){
          
          uint32_t name[2];
    
          memcpy(name, "WAW-TEST", sizeof(name));
          
          NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos;
          while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
          NRF_NVMC->ERASEUICR = NVMC_ERASEUICR_ERASEUICR_Erase;
          NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
          while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
          
          NRF_UICR->CUSTOMER[0] = name[0];
          while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
    
          NRF_UICR->CUSTOMER[1] = name[1];
          while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
         
          NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
        }

    Also, I notice you are performing a full UICR erase.  It may be worth noting that other 52 variants such as the nRF5240 will block this operation when readback protection is enabled : Access port protection behavior.

    Best regards,

    Vidar

Related