Is there any way to set the heap location to a specific area in RAM?

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there any way to set the heap location to a specific area in RAM?

532 Views
ocmonte
Contributor III

Hello Group,

 

In KDS and PE, there is a way to set the heap size. Is there a way to set the heap location? I would like to set it to 0x60000000 (.m_data_60000000).

 

Thanks!

 

Monte---

Labels (1)
Tags (2)
7 Replies

173 Views
ocmonte
Contributor III

Hello Group!

After having a nice craft drink I figured it out. It's kind of a chicken and egg thing :smileysilly:

The key items to focus here is that we were trying to use **external** ram for the RTOS heap. Thus **external** ram is a 512K chunk of memory that needs to be configured by the CPU before it can be used.

in startup.c __thumb_startup(void) has the basic initialization to copy our data from ROM to ram. It also has some simple hardware and stack configuration. Looks kind of like this:

void __thumb_startup(void) {

    // Pseudo code:
    Stack setup

    Setup registers

    Setup *some* hardware

    Call user initialization (__init_user())

    Call the C start function (_start())

}

What I had to do is to initialize some of the CPU mux registers in the startup.c file to get the **external** ram functioning. I cheated by copying some of the PE code to the startup.c  __init_user() function.  The code looked safe enough to copy there for simple register initialization - stuff like MUX's, SIM, and MPU registers. Disadvantage here is we need to be sensitive to PE changes in the future.

BlackNight​ - You have any comments?

Thanks group for all your help!

Monte---

0 Kudos

173 Views
BlackNight
NXP Employee
NXP Employee

The init_user() or SystemInit() places are the right place where to do this.

There might be just one more thing, if malloc() always returns NULL.

This could be because malloc checks the locatio of the heap to the stack pointer.

If this is a problem, you might need to add your custom _sbrk() function.

I have documented this in two articles (not related directly to your question here, but somewhat related):

FreeRTOS, malloc() and SP check with GNU Tools | MCU on Eclipse

Code Coverage for Embedded Target with Eclipse, gcc and gcov | MCU on Eclipse

I hope this helps,

Erich

0 Kudos

173 Views
ocmonte
Contributor III

Thanks Erich!

I did have a malloc() == NULL problem; however, I elected to go down the FreeRTOS and use their memory manager which brings me to another question.

I have 512K of external memory (RAM) located at 0x6000000 and have instructed PE FRTOS to use that as its heap by defining a memory segment .m_data_60000000 in the linker file. This seems to work!

Capture.PNG

However when I try to increase the "Total Heap Size", the __copy_rom_sections_to_ram(void) function crashes. If I set the compiler optimization to Optimize for size (-Os) I can increase this number but never get to 512K. If the optimization is None (-O0), then I can only define about 128K to about 196K. I would like to make use of the full 512K.

Question: What is exactly happening here? Below is my PrintSize.

Cheers,

Monte---

'Invoking: Cross ARM GNU Print Size'

arm-none-eabi-size --format=sysv --totals -x "CloverleafOG.elf"

CloverleafOG.elf  :

section                 size         addr

.interrupts            0x1e0          0x0

.cfmprotect             0x10        0x400

.text                0x2c19c        0x410

.ARM.extab            0x1908      0x2c5ac

.ARM                   0xca8      0x2deb4

.init_array             0x54      0x2eb5c

.fini_array              0x4      0x2ebb0

.data                  0x1b0   0x1fff0000

.m_data_60000000     0x50000   0x60000000

.bss                  0x3e04   0x1fff0200

.romp                   0x30   0x1fff4004

._user_heap_stack      0x800   0x1fff4034

.ARM.attributes         0x2a          0x0

...

Total               0x4c5dcb

0 Kudos

173 Views
BlackNight
NXP Employee
NXP Employee

I don't think the crash is related to FreeRTOS. Maybe that startup code does not handle properly large chunks of data.

Do you know where or why it crashes?

Erich

0 Kudos

173 Views
ocmonte
Contributor III

Hi Erich - (sorry for the delay - jumped to another project for a while)

No, I don't think it is FreeRTOS as well. I did not to indicate that. It's crashing in the startup in the middle of the for statement in  __copy_rom_sections_to_ram(). When we enter the function, __S_romp is checked for zero then it proceeds to the for loop. It crashes (Cpu_Cpu_ivINT_Hard_Fault) where I indicated below (image too). Some notes: when I decrease the "Total heap size" to some point, it will start working. However, when you start to add more code that has initializers like strings, it will eventually crash. Decrease it a bit, and all is good until again more initializers.

Thanks for your help!

Monte---

void __copy_rom_sections_to_ram(void)
{
    int    index;
    if (__S_romp == 0L) return;

    /*
     * Go through the entire table, copying sections from ROM to RAM.
     */
    for (index = 0;
         __S_romp[index].Source != 0 ||                <----- Here it crashes :smileydevil:
         __S_romp[index].Target != 0 ||
         __S_romp[index].Size != 0;
         ++index)
    {
        __copy_rom_section( __S_romp[index].Target,
        __S_romp[index].Source,
        __S_romp[index].Size );
    }
}

Capture.PNG

0 Kudos

173 Views
BlackNight
NXP Employee
NXP Employee

Yes, you can do this in the linker file. Define your own MEMORY section, then place the heap into that section (see how this is done in your current linker file).

As you use Processor Expert, you will need to disable linker file generation (otherwise your change might be overwritten).

pastedImage_1.png

See Disable my Code Generation | MCU on Eclipse

I hope this helps,

Erich

173 Views
Carlos_Mendoza
NXP Employee
NXP Employee

Hi Monte,

In addition to Erich's response, I think that the following document might be helpful for you, it shows how to create a new memory section and place specific data in it:

Relocating Code and Data Using the KDS GCC Linker File for Kinetis

Hope it helps!

Best Regards,

Carlos Mendoza

Technical Support Engineer