This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Variable allocation limit

Other Parts Discussed in Thread: HALCOGEN

Hi,

I'm working with a TMS570HDK. I hope the question will not seems too trivial. This two screenshot explain the question. The project is an empty one build with HalCoGen (no drivers enabled, no code added except what's visible in the screenshot):

A uint32[4608] array defined inside the main lead to memory errors.

In the second screenshot the TX_DATA is defined at global scope and now run flawless.What's wrong in allocating a uint32[4608] array inside the main? There's some stack size limit?I've found nothing about in the TRM or in the datasheet. Where this kind of limits are documented?

Thank you

  • Matteo,

    There is a way via the ARM Linker->Basic options (Properties of your project) to define the Heap Size (used for dynamic memory allocation) 

    By default it is set to 0x800 (2048)

    Try to change the value and let me know.

  • Matteo,

    The stack size is only limited by the amount of RAM available. The stack size for each CPU mode is configured during initialization. If you are using the initialization code generated by HALCoGen, then this stack size is hard-coded inside the sys_core.asm file, pasted below:

    ;-------------------------------------------------------------------------------
    ; Initialize Stack Pointers

    .def _coreInitStackPointer_
    .asmfunc

    _coreInitStackPointer_

    cps #17
    ldr sp, fiqSp
    cps #18
    ldr sp, irqSp
    cps #19
    ldr sp, svcSp
    cps #23
    ldr sp, abortSp
    cps #27
    ldr sp, undefSp
    cps #31
    ldr sp, userSp
    bx lr

    userSp .word 0x08000000+0x00001000
    svcSp .word 0x08000000+0x00001000+0x00000100
    fiqSp .word 0x08000000+0x00001000+0x00000100+0x00000100
    irqSp .word 0x08000000+0x00001000+0x00000100+0x00000100+0x00000100
    abortSp .word 0x08000000+0x00001000+0x00000100+0x00000100+0x00000100+0x00000100
    undefSp .word 0x08000000+0x00001000+0x00000100+0x00000100+0x00000100+0x00000100+0x00000100

    .endasmfunc

    As you can see, there are only 256 bytes allocated for SVC stack (CPU mode after reset). You can increase the stack size configured in this file to match your application needs.

    Alternatively (better option), you can follow the below procedure to define the stack size using the linker command file. For this, you will have to assign the .stack section to the STACK region as follows:

    .stack :
    {
    . += 0x200; _StackSVC_pv = .;
    . += 0x100; _StackABT_pv = .;
    . += 0x100; _StackUDEF_pv = .;
    . += 0x80; _StackFIQ_pv = .;
    . += 0x80; _StackIRQ_pv = .;
    . += 0x200; _StackUSER_pv = .;
    } > STACKS

    Then in the sys_core.asm file, add the following at the beginning:

    .global _StackUSER_pv
    .global _StackSVC_pv
    .global _StackFIQ_pv
    .global _StackIRQ_pv
    .global _StackABT_pv
    .global _StackUDEF_pv

    userSp .word _StackUSER_pv
    svcSp .word _StackSVC_pv
    fiqSp .word _StackFIQ_pv
    irqSp .word _StackIRQ_pv
    abortSp .word _StackABT_pv
    undefSp .word _StackUDEF_pv

    Then remove the hard-coded values assigned inside the sys_core.asm file for the stack addresses.

    Let me know how it goes.

    Regards, Sunil

  • So, by default stack is limited to 256 bytes and heap is limited to 2KB?

    The total RAM (excluding the eprom for the code) is completely used by stack+heap? So with default value the available 256Kb cannot be used entirely?

    Why can I allocate a global var of 16Kb (I'm doing this) id heap is limited to 2Kb? The global vars does not stay in the heap?

    Perhaps some kind of vars can resides in the code eprom?

    Be patient with my limited knowledge of this mechanism (the level of expert assigned to me by the forum is quite generous compared to my real knowledge ;-) ). Can you briefly reassume what's limited by the stack size and by the heap size?

    Thank you.

  • Matteo,

    A heap is used to support dynamic memory allocation done by the application software. For example, you can "reserve" memory required for your array using the malloc() or calloc() functions. In this case the memory is not automatically managed for you. That is, the application is then also responsible for releasing this memory when the function exits.

    The way you have written your code (very typical), the compiler chooses to store all local variables onto the stack. Please assign an appropriate stack size for the CPU mode that you are in.

    Regards, Sunil

  • I still don't understand how different syntax affects store location.How is used RAM that's nor heap neither stack? Why default limit doesn't use all available RAM?

    Can someone reassume it? A brief list of the various possiblity would be enough.

    Or at least can you address me to a conprihensive guide of this argument?

  • Matteo, maybe this link can help you...

    Look at the answer w exemple at thr end of the link

    http://stackoverflow.com/questions/959746/when-is-memory-allocated-during-compilation

  • Matteo,

    The compiler generates some sections for you depending on whether a variable is initialized or not.

    • Global and static non-constant initialized variables are stored in a section called .data
    • Global and static uninitialized variables are stored in a section called .bss
    • The stack for the function calls are stored in a section called .stack
    • Memory for malloc functions is defined by a section called .sysmem (this is the heap)
    When you link your program, you must specify where to allocate the sections in memory. In general, initialized sections (normally executable code, constants, table of constructors, and explicitly initialized global and static variables) are linked into the flash or RAM; uninitialized sections are linked into RAM.
    Please refer the "ARM Optimizing C/C++ Compiler User's Guide" (SPNU151) and the "ARM Assembly Language Tools User's Guide" SPNU118 for more details.
    Regards, Sunil
  • To illustrate Sunil post, I've created a basic demo code that uses all kind of section.

    The default linker command file has been modified to better understand this example.
    By default the linker assigns .bss, .data and .sysmem sections to RAM.

    I've on purpose defined 3 different memory region named RAM_bss, RAM_data and RAM_sysmem.
    By looking to the map file, it is easier to see where the different kind of data are stored.

    MEMORY
    {
        VECTORS    (X)  : origin=0x00000000 length=0x00000020
        FLASH0     (RX) : origin=0x00000020 length=0x0017FFE0
        FLASH1     (RX) : origin=0x00180000 length=0x00180000
        STACKS     (RW) : origin=0x08000000 length=0x00005500
        RAM_bss    (RW) : origin=0x08005500 length=0x00001000
        RAM_data   (RW) : origin=0x08006500 length=0x00001000
        RAM_sysmem (RW) : origin=0x08007500 length=0x00037aff

    /* USER CODE BEGIN (2) */
    /* USER CODE END */
    }

    /* USER CODE BEGIN (3) */
    /* USER CODE END */


    /*----------------------------------------------------------------------------*/
    /* Section Configuration                                                      */

    SECTIONS
    {
        .intvecs : {} > VECTORS
        .text    : {} > FLASH0 | FLASH1
        .const   : {} > FLASH0 | FLASH1
        .cinit   : {} > FLASH0 | FLASH1
        .pinit   : {} > FLASH0 | FLASH1
        .bss     : {} > RAM_bss
        .data    : {} > RAM_data
        .sysmem  : {} > RAM_sysmem
        
    Please have a look and let us know if this clarify your question.

    2870.Stack_usage.zip

  • Thank you all for the amount of informations. I've a lot to deepen now.

    Jean-Marc, I cannot import your example. CCS says "compile v5.1" is required but I've intalled the 5.0.6 and looking for updates the last version shown is 5.0.7. Are you testing some beta?

  • Matteo,

    Yes I've installed for test reason an Alpha version. By default, when a project is created in CCS, the latest compiler is picked up.

    I've re-created the project with a released version of code gen tools. You should now be able to import it.

    Note: In this code you will see some commented out printf.
    Printf also uses the .sysmem section.
    Malloc uses the .sysmem as well. You need to define the .sysmem bigger than what you really need. Malloc stores some information in the .sysmem for each successful allocation.

    0167.Stack_usage.zip

  • Now it's ok. Thank you.