Need a working example that uses CONFIG_BT_SETTINGS and stores a BLE random static address to memory

I have searched everywhere and can't find a working example that shows how to create a new BLE ID and save it to the setting s partition. 

Please show me the configurations necessary to create a new BLE address and show that it was saved upon a reset. Ideally with the nrf52840dk.

Below is a simplification of my code so far.

// prj.conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_DEVICE_NAME="Test"

# These setting came from some samples I found in Zephyr SDK and online forums and not sure if all are necessary
CONFIG_FLASH=y
CONFIG_SETTINGS=y
CONFIG_FLASH_MAP=y
CONFIG_SETTINGS_RUNTIME=y
CONFIG_BT_SETTINGS=y
CONFIG_SETTINGS_NONE=y
CONFIG_NVS=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_BT_GATT_CACHING=n

// main.c
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/addr.h>
#include <zephyr/settings/settings.h>

int main(void) {
    int err;
	bt_addr_le_t addr;
	size_t num_ids = 0;
	char addr_str[BT_ADDR_LE_STR_LEN];

	bt_id_get(NULL, &num_ids);
	bt_id_get(&addr, NULL);
	bt_addr_le_to_str(&addr, addr_str, sizeof(addr_str));
	printk("Num IDs: %d, Addr: %s\n", num_ids, addr_str);

	bt_addr_le_from_str("FF:EE:DD:CC:BB:AA", "random", &addr);
	
	bt_enable(NULL);
	settings_load();
	
	err = bt_id_create(&addr, NULL);
	if(err) printk("ID Create Error %d\n", err);
	
	bt_id_get(NULL, &num_ids);
	bt_id_get(&addr, NULL);
	bt_addr_le_to_str(&addr, addr_str, sizeof(addr_str));
	printk("Num IDs: %d, Addr: %s\n", num_ids, addr_str);
	
	// Continue with setup
}
I am using Zephyr v4.0.0 and the nRF52840DK.
The error I get right now is with bt_id_create(...) which returns a -12 meaning ENOMEM (no memory).
The documentation states that the backend automatically searches for a partition with the label "storage", which the nRF52840 already has, so there is no need for an overlay.
So far when I reset the board the first ID is the random and num_ids is 0, and after bt_enable, I get a recurring address (probably the default ID, and num_ids = 1. 
If I move bt_id_create before bt_enable and take out CONFIG_BT_SETTINGS, the log info from bt_enable and the printed address afterwards bot show the custom ID, but it does not persist across resets.
Related