cancel
Showing results for 
Search instead for 
Did you mean: 

user heap and stack will not fit in region RAM error.

parth kothiya
Senior

in stm32cube mx settings

min heap size = 0x200

min stack size = 0x400

i am using freertos v2 in my application memory management heap_4.

How to solve this issue?

/*
******************************************************************************
**
** @file        : LinkerScript.ld
**
** @author      : Auto-generated by STM32CubeIDE
**
** @brief       : Linker script for STM32F030C8Tx Device from STM32F0 series
**                      64Kbytes FLASH
**                      8Kbytes RAM
**
**                Set heap size, stack size and stack location according
**                to application requirements.
**
**                Set memory bank area and size if external memory is used
**
**  Target      : STMicroelectronics STM32
**
**  Distribution: The file is distributed as is, without any warranty
**                of any kind.
**
******************************************************************************
** @attention
**
** Copyright (c) 2022 STMicroelectronics.
** All rights reserved.
**
** This software is licensed under terms that can be found in the LICENSE file
** in the root directory of this software component.
** If no LICENSE file comes with this software, it is provided AS-IS.
**
******************************************************************************
*/
 
/* Entry Point */
ENTRY(Reset_Handler)
 
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
 
_Min_Heap_Size = 0x200 ; /* required amount of heap */
_Min_Stack_Size = 0x400 ; /* required amount of stack */
 
/* Memories definition */
MEMORY
{
  RAM    (xrw)    : ORIGIN = 0x20000000,   LENGTH = 8K
  FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 64K
}
 
/* Sections */
SECTIONS
{
  /* The startup code into "FLASH" Rom type memory */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH
 
  /* The program code and other data into "FLASH" Rom type memory */
  .text :
  {
    . = ALIGN(4);
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)
 
    KEEP (*(.init))
    KEEP (*(.fini))
 
    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH
 
  /* Constant data into "FLASH" Rom type memory */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >FLASH
 
  .ARM.extab   : {
    . = ALIGN(4);
    *(.ARM.extab* .gnu.linkonce.armextab.*)
    . = ALIGN(4);
  } >FLASH
 
  .ARM : {
    . = ALIGN(4);
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
    . = ALIGN(4);
  } >FLASH
 
  .preinit_array     :
  {
    . = ALIGN(4);
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array*))
    PROVIDE_HIDDEN (__preinit_array_end = .);
    . = ALIGN(4);
  } >FLASH
 
  .init_array :
  {
    . = ALIGN(4);
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array*))
    PROVIDE_HIDDEN (__init_array_end = .);
    . = ALIGN(4);
  } >FLASH
 
  .fini_array :
  {
    . = ALIGN(4);
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
    . = ALIGN(4);
  } >FLASH
 
  /* Used by the startup to initialize data */
  _sidata = LOADADDR(.data);
 
  /* Initialized data sections into "RAM" Ram type memory */
  .data :
  {
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */
    *(.RamFunc)        /* .RamFunc sections */
    *(.RamFunc*)       /* .RamFunc* sections */
 
    . = ALIGN(4);
    _edata = .;        /* define a global symbol at data end */
 
  } >RAM AT> FLASH
 
  /* Uninitialized data section into "RAM" Ram type memory */
  . = ALIGN(4);
  .bss :
  {
    /* This is used by the startup in order to initialize the .bss section */
    _sbss = .;         /* define a global symbol at bss start */
    __bss_start__ = _sbss;
    *(.bss)
    *(.bss*)
    *(COMMON)
 
    . = ALIGN(4);
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM
 
  /* User_heap_stack section, used to check that there is enough "RAM" Ram  type memory left */
  ._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM
 
  /* Remove information from the compiler libraries */
  /DISCARD/ :
  {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
  }
 
  .ARM.attributes 0 : { *(.ARM.attributes) }
}

7 REPLIES 7
TDK
Guru

You can't fit more data into the RAM than it has space. Reduce your RAM usage. The stack and heap are already pretty small, so you'll likely have to reduce your usage by reducing or eliminating other variables.

If you feel a post has answered your question, please click "Accept as Solution".

>>How to solve this issue?

Use less memory?

or understand how you're using the memory you've got, and whether that's concurrent or overlapping.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

every new project generated using cubeMX and uses FREE RTOS V2 with total 2 task and no application code till its gives me same error older version of cubemx is works fine where is the problem in generated code?

below my whole project file is attached.

KnarfB
Principal III

Cannot reproduce.

Note that _Min_Heap_Size and _Min_Stack_Size are very small by default. These are no real limits, but constants you can set to your needs in .ioc Project Manager Settings. Kinda plausibility check, testing at link time that your minimum requirements are met (rather than producing a hard to detect overflow at runtime).

FreeRTOS task stacks are allocated from heap. So if you create two tasks with 624 [sic!] words of stack each, set Min. Heap Size to say 0x2000 and the "error" will go away, tested with the lastest 6.5. toolset.

The chip you've chosen has quite a small RAM for RTOS apps (or your task settings are overkill). It will overflow soon if you add more global vars etc..

hth

KnarfB

Before trying to learn C language, you should learn using human languages. If you're not capable of splitting thoughts in sentences, you will also not be able of splitting algorithmic tasks into functions.

You are talking about CMSIS-RTOSv2 (no, it's not FreeRTOS v2), but are using CMSIS-RTOSv1.

Newlib will waste almost 2KB or RAM. You have set main heap to 512B and stack at 1024B. Then you have FreeRTOS idle task with 512B stack and FreeRTOS heap at 3KB. Add all of the the other minor structures and do the math!

There is a nice .map file viewer: Amap.

STM32CubeIDE has built-in Build Analyzer for that. Don't see any RAM "wasted" by newlib in a freshly generated project:0693W00000Lyp1TQAR.pnghth

KnarfB

That list seems to be filtered as it doesn't show sections from C runtime. Take a look at it with Amap. Filter the ".data" section and sort it by size (descending).

https://community.platformio.org/t/why-impure-data-appears-on-custom-build-and-eats-1k-memory-need-fix/11353

One can avoid it by linking with -nostartfiles and not using printf() and anything accessing errno. And, when printf() is used with floating point formatting, it allocates ~1KB on heap with malloc() and takes 12KB of flash. Obviously it's not reentrant an has to be specially managed to work with multi-threading. My solution was to get rid of it all and use LwPRINTF or nanoprintf, depending on project type. Additionally both of those are significantly more flexible. For example, printf() output target is a callback function passed in as a parameter.