Hi,
Working with nRF5340 in NCS 1.9.2, I've overridden weak usb_update_sn_string_descriptor function to set iSerialNumber at runtime.
I wanted to read the actual serial number from settings subsystem, backend-ed in internal flash (NVS).
Code looks like this
/** @brief Overriding weak function to set iSerialNumber at runtime.
* @return either original or default serial number string.
*/
static uint8_t sn[sizeof(CONFIG_USB_DEVICE_SN) + 1];
uint8_t *usb_update_sn_string_descriptor(void)
{
int rc;
uint32_t val = COSMED_SN_DEFAULT;
memset(sn, 0, sizeof(sn));
if (IS_ENABLED(CONFIG_SETTINGS)) {
rc = settings_subsys_init();
if (rc) {
LOG_ERR("error initializing settings subsys [%d]", rc);
} else {
rc = load_immediate_value("id/sn", &val, sizeof(val));
if (rc == -ENOENT) {
val = COSMED_SN_DEFAULT;
} else if (rc == 0) {
LOG_DBG("<id/sn> = %u", val);
} else {
LOG_ERR("error loading sn from settings [%d]", rc);
}
}
LOG_WRN("TODO: manage to store sn in settings from user input");
rc = settings_save_one("id/sn", (const void *)&val, sizeof(val));
if (rc) {
LOG_ERR("error saving sn in settings [%d]", rc);
}
}
snprintk(sn, sizeof(sn), "%u", val);
return sn;
}
Unfortunately last call of the chain settings_subsys_init() -> settings_backend_init() -> device_get_binding("NRF_FLASH_DRV_NAME") returns NULL.
Settings subsystem initialization at the beginning of main() doesn't rise this exception.
Is there any explanation or workaround ?