<?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>nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/64011/nrf52840-sdk16-s140---how-to-put-in-ram-retention</link><description>Hi everyone, 
 I am working on a project where I have to enter the system into System OFF mode and RAM retention. What I actually want is to retain the value of some variables. 
 How could I do that in terms of software? Are there any specific functions</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 17 Aug 2020 23:53:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/64011/nrf52840-sdk16-s140---how-to-put-in-ram-retention" /><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/264961?ContentTypeID=1</link><pubDate>Mon, 17 Aug 2020 23:53:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa483992-e1b3-4720-9b87-a02e017a00bc</guid><dc:creator>Nikosant03</dc:creator><description>[quote userid="13562" url="~/f/nordic-q-a/64011/nrf52840-sdk16-s140---how-to-put-in-ram-retention/263771"]See&amp;nbsp;&lt;a title="Memory" href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/memory.html?cp=4_0_0_3_1"&gt;Memory&lt;/a&gt;: RAM0-9 and the sections within each RAM block. The sections correspond to the Q-F bit positions. (&lt;span&gt;S[i]RETENTION (i=0..15)). F.ex. if the variable is at address 0x2002 8420 that will be RAM8, section 3.&amp;nbsp;&lt;/span&gt;[/quote]
&lt;p&gt;You mean RAM0-8? I found that my variable is at address&amp;nbsp;0x200041E0, that means RAM2, section 0 isn&amp;#39;t it?&lt;/p&gt;
&lt;p&gt;Is this correct?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define RAM_AHB_slave 2

ret_code_t err_code;
err_code = sd_power_ram_power_set(RAM_AHB_slave, POWER_RAM_POWER_S2RETENTION_On &amp;lt;&amp;lt; POWER_RAM_POWER_S2RETENTION_Pos);
APP_ERROR_CHECK(err_code);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/263861?ContentTypeID=1</link><pubDate>Mon, 10 Aug 2020 13:47:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a60c440a-89b4-4f8a-9c76-4b70138e7bf1</guid><dc:creator>Nikosant03</dc:creator><description>&lt;p&gt;Thank you for your assistance haakonsh!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/263834?ContentTypeID=1</link><pubDate>Mon, 10 Aug 2020 13:02:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3a541211-1a6b-4a89-8ad6-b1596dd689d2</guid><dc:creator>haakonsh</dc:creator><description>[quote user="Nikosant03"]The address of a global variable during program execution and when I rebuild the application (say that I did some code changes) it &lt;strong&gt;remains constant&lt;/strong&gt;?[/quote]
&lt;p&gt;I don&amp;#39;t think that&amp;#39;s a safe assumption. I assume the linker can choose freely wherein the given Section a variable will be placed. Probably it depends on when the variable is processed by the build system, ie if it is the 4th variable to be placed in a section it will probably be placed&amp;nbsp;on top of the previous three, so if you&amp;nbsp;change the number of (or size of) the variables the addresses changes.&amp;nbsp;&lt;/p&gt;
[quote user="Nikosant03"]Should I declare the variable with a special type in order to retain its address?[/quote]
&lt;p&gt;&amp;nbsp;If you declare a user-defined section that only contains your special variable, and the address of that section is constant, then I think it&amp;#39;s safe to assume the address of the variable is constant across builds.&amp;nbsp;&lt;br /&gt;see&amp;nbsp;&lt;a href="https://www.google.com/search?newwindow=1&amp;amp;sxsrf=ALeKk03Rot_MBgmYBmeqylePyUzOOpxPoQ:1597063079918&amp;amp;q=arm+gcc+section+placement&amp;amp;spell=1&amp;amp;sa=X&amp;amp;ved=2ahUKEwjCvr_M05DrAhVHxIsKHaPUDDEQBSgAegQIDBAo&amp;amp;biw=1080&amp;amp;bih=1777"&gt;https://www.google.com/search?newwindow=1&amp;amp;sxsrf=ALeKk03Rot_MBgmYBmeqylePyUzOOpxPoQ:1597063079918&amp;amp;q=arm+gcc+section+placement&amp;amp;spell=1&amp;amp;sa=X&amp;amp;ved=2ahUKEwjCvr_M05DrAhVHxIsKHaPUDDEQBSgAegQIDBAo&amp;amp;biw=1080&amp;amp;bih=1777&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/263803?ContentTypeID=1</link><pubDate>Mon, 10 Aug 2020 11:33:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d63d6afc-4e44-45fd-a7e6-f3b93dce0bbd</guid><dc:creator>Nikosant03</dc:creator><description>[quote userid="13562" url="~/f/nordic-q-a/64011/nrf52840-sdk16-s140---how-to-put-in-ram-retention/263771"]See&amp;nbsp;&lt;a title="Memory" href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/memory.html?cp=4_0_0_3_1"&gt;Memory&lt;/a&gt;: RAM0-9 and the sections within each RAM block. The sections correspond to the Q-F bit positions. (&lt;span&gt;S[i]RETENTION (i=0..15)). F.ex. if the variable is at address 0x2002 8420 that will be RAM8, section 3.&amp;nbsp;&lt;/span&gt;[/quote]
&lt;p&gt;Perfect explanation!! Thanks a lot!! The address of a global variable during program execution and when I rebuild the application (say that I did some code changes) it &lt;strong&gt;remains constant&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;Should I declare the variable with a special type in order to retain its address?&lt;/p&gt;
&lt;p&gt;Nick&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/263771?ContentTypeID=1</link><pubDate>Mon, 10 Aug 2020 09:26:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e454805b-027e-431d-b6e6-659e85e31b9e</guid><dc:creator>haakonsh</dc:creator><description>[quote user="Nikosant03"]Ok, what I have understood so far is that in order to keep retention in RAM you have to write on RAM[n].POWERSET register and if you are using a softdevice you have to use the&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.0.1/group___n_r_f___s_o_c___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_1_2_7_2_23#ga9f1c3f64a4c56f2f6109e214cac02912"&gt;sd_power_ram_power_set()&lt;/a&gt;&amp;nbsp;API. &lt;strong&gt;right?&lt;/strong&gt;[/quote]
&lt;p&gt;&amp;nbsp;Yes, that&amp;#39;s correct.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[quote user="Nikosant03"][/quote]&lt;/p&gt;
&lt;p&gt;By default the POWER field (bits A-P) is set while the RETENTION field (bits Q-f) of&amp;nbsp;&lt;span&gt;RAM[n].POWERSET register&lt;/span&gt; is unset. So in my case I have to configure only the RETENTION field (as I want RAM ON on system ON mode). What I have understand is that the RAM has 9 sections and you can select which of the sections to retain during system OFF..&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can I find the memory section (n=0..8) that my variable is located?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do I know what bit positions (Q-f) I have to set?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;See&amp;nbsp;&lt;a title="Memory" href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/memory.html?cp=4_0_0_3_1"&gt;Memory&lt;/a&gt;: RAM0-9 and the sections within each RAM block. The sections correspond to the Q-F bit positions. (&lt;span&gt;S[i]RETENTION (i=0..15)). F.ex. if the variable is at address 0x2002 8420 that will be RAM8, section 3.&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#Define RAM_SECTION_3 (1 &amp;lt;&amp;lt; 19)
#Define RAM_INSTANCE 8
#Define RAM_ON_SYSTEMON 0xFFFF //All sections ON in SystemON

err_code = sd_power_ram_power_set(RAM_INSTANCE, RAM_ON_SYSTEMON | RAM_SECTION_3);

//There&amp;#39;s also bitmasks and bitfield definitions in nrf_power.h:
err_code = sd_power_ram_power_set(RAM_INSTANCE, NRF_POWER-&amp;gt;RAM[RAM_INSTANCE].POWER | NRF_POWER_RAMPOWER_S3RETENTION_MASK);
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Nikosant03"]Taking for example the &amp;quot;state&amp;quot; variable, &lt;strong&gt;which of the following expression is correct?&lt;/strong&gt;[/quote]
&lt;p&gt;From&amp;nbsp;&lt;a href="https://developer.arm.com/documentation/dui0472/m/compiler-specific-features/--attribute----zero-init---variable-attribute"&gt;https://developer.arm.com/documentation/dui0472/m/compiler-specific-features/--attribute----zero-init---variable-attribute&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;uint8_t state __attribute__((zero_init)); will place the variable state in the .bss section.&lt;br /&gt;&lt;br /&gt;uint8_t state __attribute__((section(&amp;quot;zero_init&amp;quot;), zero_init)); will place the variable state in the user-defined .zero_init section&lt;/p&gt;
&lt;p&gt;If you declare a user-defined section then you will most likely need to add it to the flash placement file in order to let the linker know that such a section exists.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/263730?ContentTypeID=1</link><pubDate>Sun, 09 Aug 2020 16:43:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c32a804d-4718-4dc1-876a-ae971fc79ce2</guid><dc:creator>Nikosant03</dc:creator><description>&lt;p&gt;Hi haakonsh,&lt;/p&gt;
&lt;p&gt;Ok, what I have understood so far is that in order to keep retention in RAM you have to write on RAM[n].POWERSET register and if you are using a softdevice you have to use the&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.0.1/group___n_r_f___s_o_c___f_u_n_c_t_i_o_n_s.html?cp=4_7_4_1_2_7_2_23#ga9f1c3f64a4c56f2f6109e214cac02912"&gt;sd_power_ram_power_set()&lt;/a&gt;&amp;nbsp;API. &lt;strong&gt;right?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By default the POWER field (bits A-P) is set while the RETENTION field (bits Q-f) of&amp;nbsp;&lt;span&gt;RAM[n].POWERSET register&lt;/span&gt; is unset. So in my case I have to configure only the RETENTION field (as I want RAM ON on system ON mode). What I have understand is that the RAM has 9 sections and you can select which of the sections to retain during system OFF..&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can I find the memory section (n=0..8) that my variable is located?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do I know what bit positions (Q-f) I have to set?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1596991231151v3.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Moreover, RAM retention is not enough when you enter the system OFF mode, because the system restarts after waking up and&amp;nbsp;the variables are initialized to zero. So you have to add the variables into the&amp;nbsp;non_init section in order to avoid initialization during restart. This is done by using the zero_init attribute. However&amp;nbsp; I am a little confused regarding the syntax.. Taking for example the &amp;quot;state&amp;quot; variable, &lt;strong&gt;which of the following expression is correct?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t state __attribute__((section(&amp;quot;.zero_init&amp;quot;),zero_init));
uint8_t state __attribute__((section(&amp;quot;.zero_init&amp;quot;)));
uint8_t state __attribute__((zero_init));&lt;/pre&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Also, should I include anything into the flash_placement.xml file?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In flash_placement.xml there is the .non_init section. &lt;strong&gt;This is the same as the .zero_init?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Maybe, I could use the expression&amp;nbsp;uint8_t state __attribute__((section(&amp;quot;.&lt;span&gt;non_init&lt;/span&gt;&amp;quot;),zero_init))?&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1596989567937v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Sorry for the big post.. I an new to this and I try to learn..&lt;/p&gt;
&lt;p&gt;Thanks in advance&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Nick&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/261317?ContentTypeID=1</link><pubDate>Thu, 23 Jul 2020 08:18:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc6366a1-2d04-4645-b601-79696ec245c1</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Then you&amp;#39;re using GCC.&amp;nbsp;&lt;br /&gt;Try this one&amp;nbsp;&lt;a href="https://developer.arm.com/documentation/dui0472/m/compiler-specific-features/--attribute----zero-init---variable-attribute"&gt;https://developer.arm.com/documentation/dui0472/m/compiler-specific-features/--attribute----zero-init---variable-attribute&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I also suggest you google &amp;#39;arm gcc uninitialized variable&amp;#39;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/261270?ContentTypeID=1</link><pubDate>Wed, 22 Jul 2020 20:45:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c87842b9-d1e2-42b6-9966-253cd914b62e</guid><dc:creator>Nikosant03</dc:creator><description>&lt;p&gt;Hi and thanks for your respone,&lt;/p&gt;
&lt;p&gt;I am using SES&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 SDK16 S140 - How to put in RAM retention</title><link>https://devzone.nordicsemi.com/thread/261237?ContentTypeID=1</link><pubDate>Wed, 22 Jul 2020 15:08:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00f83b52-a96d-461a-abd4-424845cca128</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;API:&amp;nbsp;&lt;a title="POWER HAL" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__nrf__power__hal.html?cp=7_5_0_6_9_0_22_2"&gt;POWER HAL&lt;/a&gt;,&amp;nbsp;&lt;a title="nrf_power_rampower_mask_on" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__nrf__power__hal.html?cp=7_5_0_6_9_0_22_2_48#ga8ef9745ad414a8b9c8dd1f21de885b07"&gt;nrf_power_rampower_mask_on&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]Are there any specific functions I have to use? I also read that I have to make&lt;span&gt;&amp;nbsp;a no-init/zero_init section in order to ensure that the firmware does not clear the values at boot up but I don&amp;#39;t even know what is this and how to implement it.&lt;/span&gt;[/quote]
&lt;p&gt;&amp;nbsp;It depends on the compiler, which do you use?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>