<?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>NRF9160dk storing to and reading from flash</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/52095/nrf9160dk-storing-to-and-reading-from-flash</link><description>What is the best/easiest way to store a struct or string to flash persistently? I see other boards using FDS and other tools but those don&amp;#39;t appear to be available on this board. Are there any examples that write and read to and from flash? I see DT_FLASH_AREA_6_DEV_LABEL</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 19 Nov 2020 07:47:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/52095/nrf9160dk-storing-to-and-reading-from-flash" /><item><title>RE: NRF9160dk storing to and reading from flash</title><link>https://devzone.nordicsemi.com/thread/280763?ContentTypeID=1</link><pubDate>Thu, 19 Nov 2020 07:47:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76217555-3fd9-466a-97d9-32333ea817f0</guid><dc:creator>Andrey Dodonov</dc:creator><description>&lt;p&gt;Nice answer!&lt;br /&gt;&lt;br /&gt;Just please take into consideration that Zephyr is constantly under development, so API and libary locations might change, it is best to have a look at samples.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Currently good examples are under&lt;br /&gt;/zephyr/samples/subsys/nvs&amp;nbsp;&amp;nbsp;&lt;br /&gt;and&lt;br /&gt;/nrf/lib/bin/lwm2m_carrier/os&lt;br /&gt;&lt;br /&gt;For example, now I had to use different include paths for flash and nvs:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&amp;lt;drivers/flash.h&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&amp;lt;fs/nvs.h&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Also note that second parameter for nvs_write/nvs_read is currently not a memory offset, it is an ID.&lt;br /&gt;For details see&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/latest/reference/storage/nvs/nvs.html?highlight=nvs_write#c.nvs_write"&gt;https://docs.zephyrproject.org/latest/reference/storage/nvs/nvs.html?highlight=nvs_write#c.nvs_write&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Last, but not the least, depending where you are writing (e.g. I needed to write to inernal memory of NRF9160, I have custom layout),&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;CONFIG_MPU_ALLOW_FLASH_WRITE=y&lt;br /&gt;&lt;br /&gt;Might be needed in your prj.conf&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160dk storing to and reading from flash</title><link>https://devzone.nordicsemi.com/thread/212330?ContentTypeID=1</link><pubDate>Fri, 27 Sep 2019 14:30:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:964ab1bd-df19-474e-981a-37b7dd439b7f</guid><dc:creator>Jace</dc:creator><description>&lt;p&gt;Wanted to point out something I noticed after posting this:&lt;/p&gt;
&lt;p&gt;I name the return variable of the nvs read call &amp;quot;err&amp;quot; here, short for error. This could cause confusion because nvs_read returns the number of bytes read successfully and not an error code&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160dk storing to and reading from flash</title><link>https://devzone.nordicsemi.com/thread/209497?ContentTypeID=1</link><pubDate>Thu, 12 Sep 2019 20:58:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3329aefb-78bf-4294-89a7-530880a08c32</guid><dc:creator>Jace</dc:creator><description>&lt;p&gt;I was able to figure it out.&amp;nbsp; Zephyr has a library called NVS.&lt;/p&gt;
&lt;p&gt;In my prj.conf I added:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_NVS=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and then my code looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;nvs/nvs.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;flash.h&amp;gt;


{

static struct nvs_fs fs;

struct flash_pages_info info;
fs.offset = DT_FLASH_AREA_STORAGE_OFFSET;
err = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME),
    fs.offset, &amp;amp;info);
if (err) {
    printk(&amp;quot;Unable to get page info&amp;quot;);
}
fs.sector_size = info.size;
fs.sector_count = 3U;
err = nvs_init(&amp;amp;fs, DT_FLASH_DEV_NAME);
if (err) {
    printk(&amp;quot;Flash Init failed\n&amp;quot;);
}

char wbuf[16];
char rbuf[16];
strcpy(wbuf, &amp;quot;Hello&amp;quot;);
nvs_write(&amp;amp;fs, DT_FLASH_AREA_STORAGE_OFFSET, &amp;amp;wbuf, strlen(wbuf)+1);
err = nvs_read(&amp;amp;fs, DT_FLASH_AREA_STORAGE_OFFSET, &amp;amp;rbuf, sizeof(rbuf));
printk(&amp;quot;%s&amp;quot;, rbuf);

}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>