<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>NVS not compatible with MCUBOOT</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93394/nvs-not-compatible-with-mcuboot</link><description>Hello, 
 
 I am working with the nrf52840 chip, and would like to store a serial number on the device that persists across boots and OTA-DFU. I have integrated the nvs sample into my code, and it seems to work, except for when I try to use a separate</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 07 Nov 2022 01:43:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93394/nvs-not-compatible-with-mcuboot" /><item><title>RE: NVS not compatible with MCUBOOT</title><link>https://devzone.nordicsemi.com/thread/394293?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 01:43:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b99a93a-cb36-487e-b57b-b50db0e951df</guid><dc:creator>anisaw</dc:creator><description>&lt;p&gt;Great thanks! This did the trick.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVS not compatible with MCUBOOT</title><link>https://devzone.nordicsemi.com/thread/393279?ContentTypeID=1</link><pubDate>Mon, 31 Oct 2022 14:37:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d5ecdc1-d52a-4644-b8a6-917ce7c08fa4</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello Andrew,&lt;/p&gt;
&lt;p&gt;In general, we recommend using the CUSTOMER registers in&amp;nbsp; &lt;span&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/uicr.html?cp=4_0_0_3_4"&gt;UICR&lt;/a&gt;&lt;/span&gt; for keeping permanent values such serial numbers, Bluetooth addresses, etc, as it is easier (does not require FLASH area to be allocated by the linker). It&amp;#39;s better to use a filesystem in FLASH when you have data that needs to get updated at some point (Note: the ERASEUICR operation is blocked when APPROTECT is enabled - &lt;span&gt;&lt;a title="Access port protection behavior" href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/nvmc.html?cp=4_0_0_3_2_5#concept_protection"&gt;Access port protection behavior&lt;/a&gt;&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;Example of how you can write to a UICR.CUSTOMER[] register with nrfjprog:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$ nrfjprog --memwr 0x10001080 --val 0x12345678 // Write 0x12345678 to UICR.CUSTOMER[0].&lt;/p&gt;
&lt;p&gt;But if you prefer to use NVS, please have a look at the example I made below. It&amp;#39;s based on the peripheral_lbs sample from SDK 2.1.1 and the code snippets you posted. But instead of making a separate storage partition for the user data I opted to share the storage partition between the NVS instance used by BT settings and the one used by the app.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/2260.peripheral_5F00_lbs_5F00_w_5F00_nvs.zip"&gt;devzone.nordicsemi.com/.../2260.peripheral_5F00_lbs_5F00_w_5F00_nvs.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note: NVS requires a minimum of 2 sectors ( 2x 4096 bytes) for each instance, but there are not checks in my implementation to ensure this requirement is met.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/services/storage/nvs/nvs.html#nvs-api"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/services/storage/nvs/nvs.html#nvs-api&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Changes:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="diff"&gt;--- /home/vidarbe/ncs/v2.1.1/nrf/samples/bluetooth/peripheral_lbs/prj.conf	2022-10-24 09:14:32.019870394 +0200
+++ prj.conf	2022-10-31 14:36:06.904168035 +0100
@@ -15,3 +15,13 @@
 CONFIG_DK_LIBRARY=y
 
 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
+
+CONFIG_BOOTLOADER_MCUBOOT=y
+
+CONFIG_NVS=y
+CONFIG_NVS_LOG_LEVEL_DBG=y
+# Double the size of the settings storage partition,
+# and allocate the first half of it to BT_SETTINGS.
+CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x4000
+CONFIG_SETTINGS_NVS_SECTOR_COUNT=2
+
--- /home/vidarbe/ncs/v2.1.1/nrf/samples/bluetooth/peripheral_lbs/src/main.c	2022-10-24 09:14:32.019870394 +0200
+++ src/main.c	2022-10-31 15:12:25.290963916 +0100
@@ -26,6 +26,13 @@
 
 #include &amp;lt;dk_buttons_and_leds.h&amp;gt;
 
+#include &amp;lt;zephyr/drivers/flash.h&amp;gt;
+#include &amp;lt;zephyr/storage/flash_map.h&amp;gt;
+#include &amp;lt;zephyr/fs/nvs.h&amp;gt;
+
+#define STORAGE_NODE_LABEL 		storage
+#define RBT_CNT_ID 				3
+
 #define DEVICE_NAME             CONFIG_BT_DEVICE_NAME
 #define DEVICE_NAME_LEN         (sizeof(DEVICE_NAME) - 1)
 
@@ -38,6 +45,8 @@
 
 #define USER_BUTTON             DK_BTN1_MSK
 
+static struct nvs_fs fs;
+
 static bool app_button_state;
 
 static const struct bt_data ad[] = {
@@ -181,6 +190,67 @@
 	return err;
 }
 
+static void nvs_test(void)
+{
+	int rc = 0;
+	struct flash_pages_info info;
+	uint32_t reboot_counter = 0U;
+	/* define the nvs file system by settings with:
+	 *	sector_size equal to the pagesize,
+	 *	starting at FLASH_AREA_OFFSET(STORAGE_NODE_LABEL)
+	 */
+	fs.flash_device = FLASH_AREA_DEVICE(STORAGE_NODE_LABEL);
+	if (!device_is_ready(fs.flash_device)) {
+		printk(&amp;quot;Flash device %s is not ready\n&amp;quot;, fs.flash_device-&amp;gt;name);
+		return;
+	}
+
+	fs.offset = FLASH_AREA_OFFSET(STORAGE_NODE_LABEL);
+	printk(&amp;quot;Storage partition start address 0x%x\n&amp;quot;, fs.offset);
+
+	rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &amp;amp;info);
+	if (rc) {
+		printk(&amp;quot;Unable to get page info\n&amp;quot;);
+		return;
+	}
+
+	fs.offset += (info.size * CONFIG_SETTINGS_NVS_SECTOR_COUNT);
+    printk(&amp;quot;Offset user data 0x%x\n&amp;quot;, fs.offset);
+
+	fs.sector_size = info.size;
+	printk(&amp;quot;Sector size 0x%x\n&amp;quot;, fs.sector_size);
+	fs.sector_count = (CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE / info.size) -
+					CONFIG_SETTINGS_NVS_SECTOR_COUNT;
+	printk(&amp;quot;Sector count 0x%x\n&amp;quot;, fs.sector_count);
+
+	rc = nvs_mount(&amp;amp;fs);
+	if (rc) {
+		printk(&amp;quot;Flash Init failed\n&amp;quot;);
+		return;
+	}
+
+	/* RBT_CNT_ID is used to store the reboot counter, lets see
+	 * if we can read it from flash
+	 */
+	rc = nvs_read(&amp;amp;fs, RBT_CNT_ID, &amp;amp;reboot_counter, sizeof(reboot_counter));
+	if (rc &amp;gt; 0) { /* item was found, show it */
+		printk(&amp;quot;Id: %d, Reboot_counter: %d\n&amp;quot;,
+			RBT_CNT_ID, reboot_counter);
+	} else   {/* item was not found, add it */
+		printk(&amp;quot;No Reboot counter found, adding it at id %d\n&amp;quot;,
+		       RBT_CNT_ID);
+		(void)nvs_write(&amp;amp;fs, RBT_CNT_ID, &amp;amp;reboot_counter,
+			  sizeof(reboot_counter));
+	}
+
+	reboot_counter++;
+	(void)nvs_write(
+		&amp;amp;fs, RBT_CNT_ID, &amp;amp;reboot_counter,
+		sizeof(reboot_counter));
+
+
+}
+
 void main(void)
 {
 	int blink_status = 0;
@@ -241,6 +311,8 @@
 
 	printk(&amp;quot;Advertising successfully started\n&amp;quot;);
 
+	nvs_test();
+
 	for (;;) {
 		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
 		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>