Marios Pelekis

Kinetis memory map addressing and use

Discussion created by Marios Pelekis on Jun 13, 2017
Latest reply on Jun 13, 2017 by Mark Butcher


Currently I am have troubles on understanding and use efficiently the Memory map of the kinetis.

Actually I had already finished the development of one of our products but I had to look back again since after some days of normal working mode the system seems to have strange behavior.


Looking forward to understand and handle the kinetis memory map with the best way I would like to write down some thoughts and considarations.


Useful informations:

MCU : MK22FN512VLH12


SDK : 2.2

Debuger : P&E (Multilink universal)


  1. Curent MCU has 2 pages of SRAM name SRAM_L and SRAM_U with 0xFFFF size each. My program requires use of malloc() functions and so proper Heap size management should be done. After calculating real size for every variable that malloc() function will ocupy in the RAM I got the number 47436 (0xB94C)Bytes. So I change the *.ld file acoording to my requirments and so on the Heap size to the calculated.
    HEAP_SIZE  = DEFINED(__heap_size__)  ? __heap_size__  : 0xB94C;       
    STACK_SIZE = DEFINED(__stack_size__) ? __stack_size__ : 0x0400;
    M_VECTOR_RAM_SIZE = DEFINED(__ram_vector_table__) ? 0x0400 : 0x0;
    But on the debug some of the last malloc() calls seems not to work. So after many tries I figure out the actual Heap size(0xB994) that my application needs is 72(0x48)Bytes more than the calculated .
    Why this could happen? Is there something that I miss?
  2. After that I noticed that both Heap and Stack are placed into the "m_data_2" place of memory and so at the SRAM_U.
    According to the old school way Stack is located at the very end of the Ram.
    Is this true? Can you confirm this for kinetis series also?
  3. With proper Heap size and stack I debuged my code and noticed that the very first malloc(); function executed, set my variable at 0x2000 0008 location and not at 0x2000 0000 as excpected. After several tries nothing was good enough to set my variable at the start of SRAM_U.
    Why this happens? In what way malloc(); use Heap Size?
  4. Considering that the malfunction of my code could may appear from Stack handling I changed the Stack Size to 2048(0x0800). But searching around the internet,community and the manuals I wasn't able to identify if this Stack limit is able to get overflowed ( under application needs) and what is the expected behavior if this happens (Hard Fault Error OR Memory overwrite).

  5. Finally, and assuming the fact that Stack is on the very end of the SRAM_U page. I realzed that under my own modification on the *.ld file I have let a free space of RAM between Heap and Stack sizes for the program to put working variable there. So in order to prevent wrong behavior I tried to occupy this free RAM space by extending the Stack size and then I realize that something goes Wrong. If you try to set
    Heap size = 0xB994
    Stack size = 0x466B    So 0xB994 + 0x466B = 0xFFFF
    In order to get the maximum of the SRAM_U size(0xFFFF), the compiler is complaining for "m_data_2" overflowd.
    What going on wrong here and what am I missing?



Concluding I would like to mention that I do memory allocation on pointers and according uploaded topics on community (Using pointers, malloc in Kinetis Microcontroller ) there big posibility of memory allocated pointers runs out of Heap size and overwrites Stack content. So according to your thoughts what the most posible scenario of this malfunction (Stack Overflow or pointer overun).


Ps. Discusion created after remaining problem on my project that is posted here
(Undefined error after several time of running mode ).


Best Regards,

Pelekis Marios