MQX memory usage

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX memory usage

3,053 Views
primozrebec
Contributor III

Hi all,

i am pretty new to MQX and i have question about memory usage. I have successfully ported a K60 MQX BSP to MK20DN512VLK10 MCU on our custom board. I ran into a problem when i wanted to reserve a large memory pool (64 kB) in internal RAM. I get an out of memory error in this case so i looked into a memory usage in CodeWarrior debugger:

pastedImage_0.png

Despite i have a very simple application with two tasks using approx. 5 kB of stack there is complete upper part of internal RAM occupied and unavailable (highlighted). Is that something to have to do with the fact, that internal RAM memory on our MCU is split to SRAM_L and SRAM_U? Am i missing something in linker script?

I also tried to run some example applications found in MQX folder but there is no change in usage of upper part of SRAM. I don't believe that MQX kernel and heap should use that much memory in this case...

Any advice would be appreciated.

Best,

Primoz

0 Kudos
3 Replies

868 Views
primozrebec
Contributor III

HI all,

i managed to get this memory free, it seems that there were some remains of RTCS when porting from K60 to K20. Now i have another problem.

Internal RAM (128 kB) on our K20 MCU  is organized in two parts: SRAM_L and SRAM_H. Each part has size 64 kB. There is a boundary address betweeh those two parts at 0x20000000. This memory boundary shouldn't be crossed in single instruction.

I need to allocate 64 kB of RAM in our application. This is exactly half of our RAM or exactly one part if it. Even though i have plenty of RAM free now (more than 117 kB) i cannot allocate 64 kB of RAM from address 0x20000000 on (i get out of memory error). I took a look into linker script and noticed, that there is MQX kernel data and MQX heap allocated at:

MEMORY

{

    vectorrom   (RX): ORIGIN = 0x00000000, LENGTH = 0x00000400

    cfmprotrom  (R): ORIGIN = 0x00000400, LENGTH = 0x00000020

    rom         (RX): ORIGIN = 0x00000420, LENGTH = 0x0007FBE0  /* Code + Const data */

    ram         (RW): ORIGIN = 0x1FFF0000, LENGTH = 0x00020000  /* SRAM - RW data */

    /* kernel space starts after RAM variables (Location of MQX Kernel data + MQX heap) */

    end_of_kd   (RW): ORIGIN = 0x2000FFF0, LENGTH = 0x00000000

   

    /* Boot stack reused by MQX Kernel data */

    bstack      (RW): ORIGIN = 0x2000FA00, LENGTH = 0x00000200  /* Boot stack */

    end_bstack  (RW): ORIGIN = 0x2000FC00, LENGTH = 0x00000000  /* Boot stack end address requires 4B alignment */

}

So moved addresses of MQX kernel data + MQX heap and boot stack to the address space of SRAM_L that i could use SRAM_H for my application.

MEMORY

{

    vectorrom   (RX): ORIGIN = 0x00000000, LENGTH = 0x00000400

    cfmprotrom  (R): ORIGIN = 0x00000400, LENGTH = 0x00000020

    rom         (RX): ORIGIN = 0x00000420, LENGTH = 0x0007FBE0  /* Code + Const data */

    //ram         (RW): ORIGIN = 0x1FFF0000, LENGTH = 0x00020000  /* SRAM - RW data */

    ram         (RW): ORIGIN = 0x1FFF0000, LENGTH = 0x00020000  /* SRAM - RW data */

    /* kernel space starts after RAM variables (Location of MQX Kernel data + MQX heap) */

    //end_of_kd   (RW): ORIGIN = 0x2000FFF0, LENGTH = 0x00000000

    end_of_kd   (RW): ORIGIN = 0x1FFFFFF0, LENGTH = 0x00000000

   

    /* Boot stack reused by MQX Kernel data */

    //bstack      (RW): ORIGIN = 0x2000FA00, LENGTH = 0x00000200  /* Boot stack */

    //end_bstack  (RW): ORIGIN = 0x2000FC00, LENGTH = 0x00000000  /* Boot stack end address requires 4B alignment */

   

    bstack      (RW): ORIGIN = 0x1FFFFA00, LENGTH = 0x00000200  /* Boot stack */

    end_bstack  (RW): ORIGIN = 0x1FFFFC00, LENGTH = 0x00000000  /* Boot stack end address requires 4B alignment */

}

Problem is that i "see" only 64 kB of RAM (only SRAM_L) if i check memory pool with the debuger tool in CodeWarrior. I get an out of memory error if i try to allocate memory in this case. I also tried do allocate another memory pool with _mem_create_pool command but my application ends with error in "dispatch.s".

Is there a way to modify linker script that everything is being kept in SRAM_L and i have complete SRAM_H avalilable for my application data?

0 Kudos

868 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Primoz,

There are 4 things you must consider:

1) The tasks' stacks are reserved from the heap.

2) Malloc function also reserves memory form heap.

3) The 2 RAM segments are connected to different buses (this is why an object connot overlap the two segments)

4) Linker is not smart enough to separate the objects in both segments, this must be done manually.

Based on points 1 and 2, we can see that it is not possible to reserve 64k of RAM using malloc because the tasks' stacks will occupy some space in the same segment.

What I recommend is next:

1) Edit the linker file to place the content of ram segment at address 0x20000000 (be sure not to averlap end_of_kd, bstack and end_bstack segments),

2) Create a new segment in 0x1FFF000, in the example below I named it my_ram,

3) Update __INTERNAL_SRAM_BASE and __INTERNAL_SRAM_SIZE labels

4) Create a section which content is going to be place in segment my_ram

5) Finally create your own malloc function which will allocate space in the new section.

The attached document will also be very helpful to understand how to manipulate the linker file. Please see chapter 4.4 which explains how to relocate a function in a specific RAM address. You can do the same with variables and arrays. If you need to reference labels declared in in linker file (for example __my_section_start in the code below) you can refer to chapter 4.5 to see how to use them in your c code.

Below you can see an example of what I explained above.

Hope this helps.

Carlos


MEMORY
{
    vectorrom  (RX): ORIGIN = 0x00000000, LENGTH = 0x00000400
    cfmprotrom  (R): ORIGIN = 0x00000400, LENGTH = 0x00000020
    rom        (RX): ORIGIN = 0x00000420, LENGTH = 0x0007FBE0  /* Code + Const data */

  my_ram        (RW): ORIGIN = 0x1FFF0000, LENGTH = 0x00010000  /* SRAM - RW data */

  ram              (RW): ORIGIN = 0x20000000, LENGTH = 0x0000FA00  /* SRAM - RW data */
   
    /* kernel space starts after RAM variables (Location of MQX Kernel data + MQX heap) */
    end_of_kd  (RW): ORIGIN = 0x2000FFF0, LENGTH = 0x00000000
   
    /* Boot stack reused by MQX Kernel data */
    bstack      (RW): ORIGIN = 0x2000FA00, LENGTH = 0x00000200  /* Boot stack */
    end_bstack  (RW): ORIGIN = 0x2000FC00, LENGTH = 0x00000000  /* Boot stack end address requires 4B alignment */
}

SECTIONS

{

    __INTERNAL_SRAM_BASE  = 0x20000000;

    __INTERNAL_SRAM_SIZE  =  0x0000FA00;

...

...

...

.my_section

    {

        __my_section_start = .;

              . = ALIGN (0x4);

    } > my_ram      

...

...

...

867 Views
primozrebec
Contributor III

Hi Carlos,

thank you for you your explanation, it was helpful for me to understand linker script better.

Anyway, i tried to declare an array of size 65536 bytes in my code and place it to RAM section ".my_section"  using linker directive. Section ".my_section" itself has also assigned 65536 bytes of memory. But i get an error when building my project that array cannot be put to ".my_section" because RAM region is oveflowed by 24 bytes. Is that because array declaration itself occupies some space in this RAM section?

Best,

Primoz

0 Kudos