Hi Nordic team,
I'm using nRF52840 with nRF Connect SDK v2.6.0 and using NVS to store RSA key material on flash. The goal is to persistently store a generated RSA key pair and retrieve it across reboots.
Expected Behavior:
-
On first boot after flash erase, the device should:
-
Check for keys in NVS.
-
If not present, generate a new RSA key pair.
-
Store private/public keys and a hash using
nvs_write(). -
Immediately read back for verification.
-
On subsequent boots, it should skip key generation.
-
Actual Behavior:
-
RSA keys are written successfully (confirmed via return values and log).
-
But after reboot,
nvs_read()fails with return code-2(ENODATA). -
This happens intermittently during the first 1–2 boots after flashing, then stabilizes.
-
Once stable, NVS consistently returns the stored values across resets.
Setup:
-
MCU: nRF52840
-
SDK: nRF Connect SDK v2.6.0
-
Storage: NVS (2 sectors, 4 KB each)
Key Definitions:
#define OTP_NVS_ID_PRIVATE_KEY_LEN 0x0300#define OTP_NVS_ID_PRIVATE_KEY 0x0301
#define OTP_NVS_ID_PUBLIC_KEY 0x0302#define OTP_NVS_ID_PUBLIC_KEY_HASH 0x0303#define OTP_NVS_ID_DEVICE_HUID 0x0304
Changing the ID range from 0x0100 to 0x0300 reduced the frequency of this issue but did not eliminate it.
Relevant Flow (simplified):
bt_enable(NULL); // BLE enabled
zb_remote_nvs_init(); // Mounts NVS
if (!is_private_key_present_in_otp()) { RSA2048_Generator(); // Generates & writes to NVS
} else {
printk("RSA key already exists\n");
}
Questions:
-
Why does
nvs_read()intermittently fail with-2after a successful write + reboot? -
Is there a timing/sync issue after
nvs_write()in NCS v2.6.0? -
Is
fs_sync()necessary in this version, and how should it be used iffs.storageisn't available? -
Could BLE or other subsystems interfere with flash if initialized before/after NVS?
-
Are NVS ID ranges like
0x0100or0x0300sensitive or reserved internally?
What I’ve Tried:
-
Changing NVS ID range from
0x0100to0x0300: reduces issue but doesn't solve it. -
Adding
k_sleep(K_MSEC(200))after write.
Any guidance on ensuring reliable write-read behavior on first boot would be very appreciated.
Let me know if I should share minimal reproducible code or boot logs.
Thanks!