<?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>Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/63837/used-25-of-memory-but-the-program-restarts-due-to-insufficient-memory</link><description>At the very beginning of the program in the main function, my stack pointer is 0x20026390, although I expected it to be in the 0x20040000 area. 
 In this case, static memory is used 26% - 35072 bytes. Those. my static memory should be in the memory area</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 21 Jul 2020 07:14:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/63837/used-25-of-memory-but-the-program-restarts-due-to-insufficient-memory" /><item><title>RE: Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/thread/260914?ContentTypeID=1</link><pubDate>Tue, 21 Jul 2020 07:14:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:170def41-3548-4b17-b658-aaa06bd6133e</guid><dc:creator>Yury Morgunov</dc:creator><description>&lt;p&gt;In the end, I experimentally found out that the stack pointer is not set to the end of flash memory, but to the end of the main thread, because of this I got confused.&lt;/p&gt;
&lt;p&gt;In addition, the CONFIG_MAIN_STACK_SIZE parameter does not set the main thread&amp;#39;s stack size directly, it only increases the memory size by CONFIG_MAIN_STACK_SIZE. This also caused problems in my understanding.&lt;/p&gt;
&lt;p&gt;As a result, it turns out that the size of the stack memory available for the main thread is 0x8000 + CONFIG_MAIN_STACK_SIZE&lt;br /&gt;Where 0x8000 came from, I still do not understand.&lt;/p&gt;
&lt;p&gt;Therefore, if there is not enough memory, you need to increase CONFIG_MAIN_STACK_SIZE or CONFIG_HEAP_MEM_POOL_SIZE if there is not enough heap.&lt;/p&gt;
&lt;p&gt;Correct me if I am wrong somewhere&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/thread/260496?ContentTypeID=1</link><pubDate>Fri, 17 Jul 2020 08:31:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d65694cb-94a4-4d69-a0c9-d90880b9c3d6</guid><dc:creator>Yury Morgunov</dc:creator><description>&lt;p&gt;Using the example&lt;strong&gt; \ ncs \ nrf \ samples \ nrf9160 \ at_client&lt;/strong&gt;.&lt;br /&gt; I wrote a code that will break if there is enough memory for buffers.&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;drivers/uart.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

void test_function();

static uint8_t trash[8000];

/**@brief Recoverable BSD library error. */
void bsd_recoverable_error_handler(uint32_t err)
{
	printk(&amp;quot;bsdlib recoverable error: %u\n&amp;quot;, err);
}

void test_function()
{
	uint8_t tras2h[40000];
	for (int i = 0; i &amp;lt; sizeof(tras2h); ++i) {
		tras2h[i] = i;
		if (i == sizeof(tras2h) - 1)
		{
			__BKPT(0);  // After stopping at this point, you can see that the data in trash [8000] has corrupted
		}
	}
}

void main(void)
{
	printk(&amp;quot;The AT host sample started %d\n&amp;quot;, *trash);
	test_function();
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It turns out that there is no error anywhere, and the problem is that the stack pointer does not point to the end of memory. Unfortunately, I haven&amp;#39;t found any settings to fix this.&lt;/p&gt;
&lt;p&gt;Then, as a solution, you will have to use more static variables in the code instead of variables in the stack&lt;/p&gt;
&lt;div class="TnITTtw-translate-selection-button" style="left:260.5px;top:343px;"&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/thread/260483?ContentTypeID=1</link><pubDate>Fri, 17 Jul 2020 07:26:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:35618588-42da-4b5c-9080-e9b403b84d8d</guid><dc:creator>Yury Morgunov</dc:creator><description>&lt;p&gt;It looks like somewhere there really is an offset for the stack pointer.&lt;/p&gt;
&lt;p&gt;As I found out empirically, the stack pointer is offset by 32 KB relative to static memory. Therefore, the following memory allocation is obtained:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;SP = 0x20020000 + static memory + 0x8000&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Why is that?&lt;/p&gt;
&lt;p&gt;P.S.&amp;nbsp;But unfortunately this does not work in my project&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/thread/260286?ContentTypeID=1</link><pubDate>Thu, 16 Jul 2020 08:06:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d7bb15ac-0eba-4dba-a705-6619a8afd986</guid><dc:creator>Yury Morgunov</dc:creator><description>&lt;p&gt;Run example &lt;strong&gt;\ncs\nrf\samples\nrf9160\at_client&lt;/strong&gt;&lt;br /&gt;Looked at the stack pointer in it is 0x20027a98 &amp;lt;z_main_stack + 3056&amp;gt;&lt;br /&gt;Those. In fact, only 32KB is used in the work?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Used 25% of memory, but the program restarts due to insufficient memory</title><link>https://devzone.nordicsemi.com/thread/260275?ContentTypeID=1</link><pubDate>Thu, 16 Jul 2020 06:52:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ebdff8a-fb24-4f7d-a807-bf1760314eaa</guid><dc:creator>Yury Morgunov</dc:creator><description>&lt;p&gt;Now I looked at the file zephyr.dts This is how the memory is ultimately allocated&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;chosen {
	zephyr,flash-controller = &amp;amp;flash_controller;
	zephyr,entropy = &amp;amp;cryptocell_sw;
	zephyr,console = &amp;amp;uart0;
	zephyr,shell-uart = &amp;amp;uart0;
	zephyr,uart-mcumgr = &amp;amp;uart0;
	zephyr,flash = &amp;amp;flash0;
	zephyr,sram = &amp;amp;sram0_ns;
	zephyr,code-partition = &amp;amp;slot0_ns_partition;
	zephyr;
};

...

sram0: memory@20000000 {
	compatible = &amp;quot;mmio-sram&amp;quot;;
	reg = &amp;lt; 0x20000000 0x40000 &amp;gt;;
};

...

reserved-memory {
	#address-cells = &amp;lt; 0x1 &amp;gt;;
	#size-cells = &amp;lt; 0x1 &amp;gt;;
	ranges;
	sram0_s: image_s@20000000 {
		reg = &amp;lt; 0x20000000 0x10000 &amp;gt;;
	};
	sram0_bsd: image_bsd@20010000 {
		reg = &amp;lt; 0x20010000 0x10000 &amp;gt;;
	};
	sram0_ns: image_ns@20020000 {
		reg = &amp;lt; 0x20020000 0x20000 &amp;gt;;
	};
};&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>