<?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 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use</link><description>Hello - 
 We are working with the nRF52840 and SDK v17.0.0 and tuning our power consumption during System ON sleep. We understand how to wakeup the device using the RTC and also how to selectively power off RAM banks and sections. We know that powered</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 18 Sep 2020 08:30:06 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use" /><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/270262?ContentTypeID=1</link><pubDate>Fri, 18 Sep 2020 08:30:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5ec7961b-ad01-42db-8c5f-a7168cdfd515</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Yes, you can define your own malloc() and free() implementations. The linker will look into your files first - if they&amp;#39;re defined, it will not search standard libraries for them.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/270192?ContentTypeID=1</link><pubDate>Thu, 17 Sep 2020 23:30:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b9250ce-8e14-4c3a-b66b-344ee273c103</guid><dc:creator>bfriedk1</dc:creator><description>&lt;p&gt;Dmitry -&lt;/p&gt;
&lt;p&gt;Thanks again for answering our questions. Before I close this issue out, I&amp;#39;d like to clarify if it is possible or not to override the malloc() and free() functions provided through the nRF5 SDK (via GCC). If we can do that then it is straightforward for us to track all allocations that use those functions.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Brian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/269219?ContentTypeID=1</link><pubDate>Fri, 11 Sep 2020 16:59:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:feffa697-7e28-4de7-868f-f9b9cdfad1ec</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;GCC malloc uses memory between&amp;nbsp;__HeapBase and&amp;nbsp;__HeapLimit defined in startup file, it will not use any other memory. For third-party malloc implementations, the memory is usually defined as an array inside malloc source, or as direct address and size of memory area.&lt;/p&gt;
[quote userid="84781" url="~/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use/269210"]But wouldn&amp;#39;t we still have the issue where we don&amp;#39;t know where &amp;nbsp;in the main heap SDK malloc() and balloc() calls land and hence we could potentially power off memory in use?[/quote]
&lt;p&gt;You can expose your functions with tracking instead of original API, then SDK will use your layer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/269210?ContentTypeID=1</link><pubDate>Fri, 11 Sep 2020 15:25:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5a63bb51-bf61-4074-98ae-3320e758215f</guid><dc:creator>bfriedk1</dc:creator><description>&lt;p&gt;Dmitry -&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you again for your detailed answers and clarifications. It makes sense that balloc is used for I/O applications that require same sized buffer pools. I see that some of the SDK uses balloc for these types of scenarios.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If we use balloc for application allocations, with linker section boundaries, we know exactly where that memory is being allocated. That is good. I&amp;#39;d like to confirm that&amp;nbsp;the linker attribute section also prevent ordinary calls to malloc() from being satisfied from that section. I&amp;#39;ve copied our&amp;nbsp;main linker script MEMORY definition below. I assume that the balloc section(s) would appear where RAM currently resides, and RAM would be pushed lower? This also means that we&amp;#39;d need to define how much dynamic allocation memory we require at build time I presume:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;MEMORY
{
  FLASH (rx) : ORIGIN = 0x27000, LENGTH = 0xcb000
  PREFERENCE (rx) : ORIGIN = 0xF2000, LENGTH = 0x2000
  RAM (rwx) :  ORIGIN = 0x20008000, LENGTH = 0x20040000-0x20008000
  NOINIT (rwx) :  ORIGIN = 0x2003fb3c, LENGTH = 0x100
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Considering the potentially simpler approach, since our allocations tend to be more arbitrary both in size and use, we can override our malloc() function to track the start address of unused memory. But wouldn&amp;#39;t we still have the issue where we don&amp;#39;t know where &amp;nbsp;in the main heap SDK malloc() and balloc() calls land and hence we could potentially power off memory in use? I suppose patching the nrf_balloc.h file does handle the SDK cases, assuming none of the pre-built libraries call balloc/malloc.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Brian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/269104?ContentTypeID=1</link><pubDate>Fri, 11 Sep 2020 08:49:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22e1a227-138f-4026-b508-0139c482ddf5</guid><dc:creator>Dmitry</dc:creator><description>[quote userid="84781" url="~/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use/269065"]1) I know how to create linker sections, but it isn&amp;#39;t clear to me how to modify the NRF_BALLOC_DBG_DEF macro to allocate &amp;quot;_nrf_balloc_pool_mem&amp;quot; items there. I think that the .p_stack_base needs to point at the RAM section boundary. Any code snippets could surely help if possible. FWIW, we are building with GCC.[/quote]
&lt;p&gt;Just&amp;nbsp; add an attribute to a pool definition:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint32_t CONCAT_2(_name,_nrf_balloc_pool_mem) \

 [NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) * (_pool_size) / sizeof(uint32_t)] __attribute__((section(“myPowerOffPool”)));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Concerning stack &amp;ndash; it&amp;rsquo;s not necessary to place its base at section boundary, because you&amp;rsquo;ll probably enter WFE with not an empty stack. If you wish to save power also for unused stack space, keep in mind that CPU will wake up by interrupt, and an interrupt handler will fill the stack space before you power on RAM. &amp;nbsp;As you&amp;rsquo;re using RTOS with different stacks for each task, I think it&amp;rsquo;s not worth an effort.&lt;/p&gt;
[quote userid="84781" url="~/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use/269065"]2) Should we be concerned about any parts of the SDK calling malloc() directly and hence bypassing this mechanism? I noticed for example that some of the crypto functions can be configured to use one of several allocators.[/quote]
&lt;p&gt;This method is good for application code that allocates large blocks of memory (for example, I/O buffers). The use of sub-allocators within these blocks is also possible in a scenario like &amp;ldquo;allocate memory &amp;ndash; work with it &amp;ndash;release it completely&amp;rdquo;. For ordinary malloc&amp;rsquo;s called from SDK, leave them in main heap in retained memory &amp;ndash; there is too much work to save 100-200 nA.&lt;/p&gt;
[quote userid="84781" url="~/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use/269065"]3) You had mentioned the need for a small intermediate layer to map allocated blocks. I assume this refers to the sub-allocations we will need to manage? For example, multiple calls to&amp;nbsp;malloc(10) call can be satisfied by a single block[/quote]
&lt;p&gt;What I mean &amp;ndash; you need a map of allocated blocks to quickly find out what memory can be powered off. Sure, you can take this info from balloc internals, but this is not a good way.&lt;/p&gt;
[quote userid="84781" url="~/f/nordic-q-a/65807/nrf52840-ram-power-off---how-to-know-which-banks-sections-are-in-use/269065"]4) Considering other potentially simpler options, I am wondering if the built-in malloc() implementation allocates always by starting on one side of RAM. If that is the case, and if we know a particular app won&amp;#39;t allocate more than X bytes, we could potentially power off the RAM sections on the other side. Not perfect though, depending on how the built-in malloc() coalesces blocks that are freed[/quote]
&lt;p&gt;Most allocators start from the low side of memory. The problem is that memory will always be fragmented &amp;ndash; an allocated block cannot be moved because an application may have pointers to the memory inside a block that allocator is not aware of. Of course, you can take some simple open-source allocator and modify it to&amp;nbsp;keep&amp;nbsp;the start address of unused memory - a better than nothing solution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/269065?ContentTypeID=1</link><pubDate>Fri, 11 Sep 2020 01:26:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cb34976d-6940-4ca9-bcd8-8d50e8b16de5</guid><dc:creator>bfriedk1</dc:creator><description>&lt;p&gt;Dmitry -&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you much for the quick follow-up. I&amp;#39;ve read through the nrf_balloc source and header files and I understand how such an allocator can help to ensure that allocations are kept in known RAM areas. I do have a few follow-up questions for you before diving in here:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;1) I know how to create linker sections, but it isn&amp;#39;t clear to me how to modify the NRF_BALLOC_DBG_DEF macro to allocate &amp;quot;_nrf_balloc_pool_mem&amp;quot; items there. I think that the .p_stack_base needs to point at the RAM section boundary. Any code snippets could surely help if possible. FWIW, we are building with GCC.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2) Should we be concerned about any parts of the SDK calling malloc() directly and hence bypassing this mechanism? I noticed for example that some of the crypto functions can be configured to use one of several allocators.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3) You had mentioned the need for a small intermediate layer to map allocated blocks. I assume this refers to the sub-allocations we will need to manage? For example, multiple calls to&amp;nbsp;malloc(10) call can be satisfied by a single block.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;4) Considering other potentially simpler options, I am wondering if the built-in malloc() implementation allocates always by starting on one side of RAM. If that is the case, and if we know a particular app won&amp;#39;t allocate more than X bytes, we could potentially power off the RAM sections on the other side. Not perfect though, depending on how the built-in malloc() coalesces blocks that are freed.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Brian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 RAM power off - How to know which banks/sections are in use</title><link>https://devzone.nordicsemi.com/thread/269061?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2020 22:03:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0fc36e3e-7a10-4e87-ac39-3ec8da2f521f</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Hi Brian,&lt;/p&gt;
&lt;p&gt;To control RAM power with section granularity, you need an allocator that is able to work with section-sized RAM blocks. Take a look at balloc library in SDK - it does (almost) exactly what you&amp;#39;re looking for.&lt;/p&gt;
&lt;p&gt;Create a linker section that starts at RAM section boundary, then change NRF_BALLOC_DBG_DEF macro in balloc.h to allocate&amp;nbsp;_nrf_balloc_pool_mem items in that section. You also will need a small intermediate layer that will create a map of allocated blocks. Note that in debug mode each block will have a debug header, thus it will not be section-sized,&amp;nbsp;so you need to disable RAM power control in debug mode.&lt;/p&gt;
&lt;p&gt;SoftDevice uses a static linear memory region from the start of RAM. When SoftDevice is disabled, you can safely turn its memory off, except of first section (0x20000000).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>