This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Used 25% of memory, but the program restarts due to insufficient memory

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 0x20020000 - 0x20028900, it turns out that I have already crossed the memory boundary by the stack pointer

The question arises, what could this be related to? I did not change the memory configuration in any way, i.e. sram0_ns is used with a size of 128 KB

Kind regards,

Yuri

  • Now I looked at the file zephyr.dts This is how the memory is ultimately allocated

    chosen {
    	zephyr,flash-controller = &flash_controller;
    	zephyr,entropy = &cryptocell_sw;
    	zephyr,console = &uart0;
    	zephyr,shell-uart = &uart0;
    	zephyr,uart-mcumgr = &uart0;
    	zephyr,flash = &flash0;
    	zephyr,sram = &sram0_ns;
    	zephyr,code-partition = &slot0_ns_partition;
    	zephyr;
    };
    
    ...
    
    sram0: memory@20000000 {
    	compatible = "mmio-sram";
    	reg = < 0x20000000 0x40000 >;
    };
    
    ...
    
    reserved-memory {
    	#address-cells = < 0x1 >;
    	#size-cells = < 0x1 >;
    	ranges;
    	sram0_s: image_s@20000000 {
    		reg = < 0x20000000 0x10000 >;
    	};
    	sram0_bsd: image_bsd@20010000 {
    		reg = < 0x20010000 0x10000 >;
    	};
    	sram0_ns: image_ns@20020000 {
    		reg = < 0x20020000 0x20000 >;
    	};
    };

  • Run example \ncs\nrf\samples\nrf9160\at_client
    Looked at the stack pointer in it is 0x20027a98 <z_main_stack + 3056>
    Those. In fact, only 32KB is used in the work?

  • It looks like somewhere there really is an offset for the stack pointer.

    As I found out empirically, the stack pointer is offset by 32 KB relative to static memory. Therefore, the following memory allocation is obtained:

    SP = 0x20020000 + static memory + 0x8000

    Why is that?

    P.S. But unfortunately this does not work in my project

  • Using the example \ ncs \ nrf \ samples \ nrf9160 \ at_client.
    I wrote a code that will break if there is enough memory for buffers.

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    
    void test_function();
    
    static uint8_t trash[8000];
    
    /**@brief Recoverable BSD library error. */
    void bsd_recoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib recoverable error: %u\n", err);
    }
    
    void test_function()
    {
    	uint8_t tras2h[40000];
    	for (int i = 0; i < 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("The AT host sample started %d\n", *trash);
    	test_function();
    }
    

    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't found any settings to fix this.

    Then, as a solution, you will have to use more static variables in the code instead of variables in the stack

  • 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.

    In addition, the CONFIG_MAIN_STACK_SIZE parameter does not set the main thread's stack size directly, it only increases the memory size by CONFIG_MAIN_STACK_SIZE. This also caused problems in my understanding.

    As a result, it turns out that the size of the stack memory available for the main thread is 0x8000 + CONFIG_MAIN_STACK_SIZE
    Where 0x8000 came from, I still do not understand.

    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.

    Correct me if I am wrong somewhere

Related