<?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>nrf9160 Flash access</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/61931/nrf9160-flash-access</link><description>Hi. I need to use some of the flash on the nRF9160 for non-volatile storage. But I want to do it in a very specific way to save on RAM usage. 
 
 Essentially I want to be able to define a character array like; 
 
 const char configInformation[32768];</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 03 Jun 2020 14:48:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/61931/nrf9160-flash-access" /><item><title>RE: nrf9160 Flash access</title><link>https://devzone.nordicsemi.com/thread/253075?ContentTypeID=1</link><pubDate>Wed, 03 Jun 2020 14:48:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1545c7b6-30f4-4c76-964e-f0ab0735a7a5</guid><dc:creator>Randall</dc:creator><description>&lt;p&gt;I guess I should have replied when I figured it out for myself.&amp;nbsp; With no replies I didn&amp;#39;t know if people weren&amp;#39;t interested or it was so simple people thought I was an idiot for asking.&amp;nbsp; Because solution was pretty simple.&amp;nbsp; Maybe people could critique my solution.&lt;/p&gt;
&lt;p&gt;Anyway, standard nrf9160 definition includes a scratchpad and storage flash partition.&amp;nbsp; Although one could also define their own, I&amp;#39;m going to example using the scratchpad partition.&lt;br /&gt;&lt;br /&gt;I create a pointer to constant data;&lt;/p&gt;
&lt;p&gt;const char * scratchData;&lt;/p&gt;
&lt;p&gt;As well as some defines for access to the scratch area;&lt;/p&gt;
&lt;p&gt;#define FLASH_OFFSET&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DT_FLASH_AREA_IMAGE_SCRATCH_OFFSET&lt;br /&gt;#define FLASH_PAGE_SIZE&amp;nbsp; &amp;nbsp; &amp;nbsp; DT_FLASH_ERASE_BLOCK_SIZE&lt;br /&gt;#define FLASH_SIZE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DT_FLASH_AREA_IMAGE_SCRATCH_SIZE&lt;/p&gt;
&lt;p&gt;When I initialize, I assign this to the scratchpad partition; as well as getting a device object for the flash;&lt;/p&gt;
&lt;p&gt;flashDev = device_get_binding( DT_FLASH_DEV_NAME );&lt;/p&gt;
&lt;p&gt;scratchData = (char *)FLASH_OFFSET;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When I erase/write to the section, I use the flash functions&lt;/p&gt;
&lt;p&gt;// Erase&lt;/p&gt;
&lt;p&gt;flash_write_protection_set( flashDev, false );&lt;br /&gt;flash_erase( flashDev, FLASH_OFFSET, FLASH_PAGE_SIZE );&lt;br /&gt;flash_write_protection_set( flashDev, true );&lt;/p&gt;
&lt;p&gt;// Write Block&lt;/p&gt;
&lt;p&gt;if (blockOffset + blockSize) &amp;gt; FLASH_SIZE )&lt;br /&gt; {&lt;br /&gt;&amp;nbsp; LOG_ERR( &amp;quot;Write Exceeds Scratchpad Size&amp;quot; );&lt;br /&gt;&amp;nbsp; return(-1);&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;blockOffset += FLASH_OFFSET;&lt;br /&gt;flash_write_protection_set( flashDev, false );&lt;br /&gt;flash_write( flashDev, blockOffset, blockData, blockSize );&lt;br /&gt;flash_write_protection_set( flashDev, true );&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Then I can access the block using the scratchData pointer.&amp;nbsp; I use the following configuration variables in prj.conf;&lt;/p&gt;
&lt;p&gt;CONFIG_FLASH=y&lt;br /&gt;CONFIG_FLASH_PAGE_LAYOUT=y&lt;br /&gt;CONFIG_MPU_ALLOW_FLASH_WRITE=y&lt;br /&gt;CONFIG_SOC_FLASH_NRF=y&lt;br /&gt;CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The emulate byte access is so that I can write generic data.&amp;nbsp; I&amp;#39;m not worried about the extra overhead.&amp;nbsp; If you want to handle 4-byte alignment yourself, you don&amp;#39;t have to include this.&lt;/p&gt;
&lt;p&gt;I deleted a lot of the error handling from my code to make the concept easier to see.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 Flash access</title><link>https://devzone.nordicsemi.com/thread/253068?ContentTypeID=1</link><pubDate>Wed, 03 Jun 2020 14:36:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcf11162-ab77-41ff-b2d3-be75a2b3cd8f</guid><dc:creator>Heidi</dc:creator><description>&lt;p&gt;Hi! I&amp;#39;m sorry for not replying sooner. I just wanted to let you know I&amp;#39;m looking into this and I&amp;#39;ll update the case when I have some information for you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Heidi&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>