malloc using

Forum related to ERIKA Enterprise and RT-Druid version 3

Moderator: paolo.gai

Post Reply
Alexanderv66
Newbie
Posts: 17
Joined: Sun Jun 30, 2019 6:13 pm

malloc using

Post by Alexanderv66 » Mon Jul 15, 2019 7:56 am

How to use dynamic memory allocation correctly with Erika 3 on TriCore Free Toolchain?

Im working with TriCore TС299 microcontroller and using ERIKA 3. When i calling standard C function malloc, I have return result null. As if it is impossible to allocate memory. Then I opened the linker script "ee_tc_gcc_flash.ld" and changed the line

Code: Select all

/* Heap size */
//__HEAP_SIZE = DEFINED (__HEAP_SIZE) ? __HEAP_SIZE : 0;
__HEAP_SIZE = DEFINED (__HEAP_SIZE) ? __HEAP_SIZE : 2K;
After this, the result of the memory allocation points on one of the core functions of Erika, which is also not a valid memory allocation.
Выделение памяти2.png
Выделение памяти2.png (26.07 KiB) Viewed 1476 times
Then I found on this forum that in order to use the heap I must indicate in the OIL file the LDFLAGS linker option.
The The GNU linker documentation define this:
--heap reserve
--heap reserve,commit
Specify the number of bytes of memory to reserve (and optionally commit) to
be used as heap for this program. The default is 1Mb reserved, 4K committed.
[This option is specific to the i386 PE targeted port of the linker]
I tried the options:

Code: Select all

...
OS EE {
		...
		LDFLAGS = "--heap 2048";
		...		
and

Code: Select all

...
OS EE {
		...
		LDFLAGS = "--heap = 2048";
		...		
and

Code: Select all

...
OS EE {
		...
		LDFLAGS = "--heap reserve 2048";
		...		
this get result in build console:

Code: Select all

tricore-gcc.exe: error: =: No such file or directory
tricore-gcc.exe: error: 2048: No such file or directory
tricore-gcc.exe: error: unrecognized command line option '--heap'
make[1]: *** [/cygdrive/d/Prj/Photon/DEMOMU~1/erika/mk/ee_arch_rules.mk:105: erika3app.elf] Error 1

Alexanderv66
Newbie
Posts: 17
Joined: Sun Jun 30, 2019 6:13 pm

Re: malloc using

Post by Alexanderv66 » Mon Jul 15, 2019 9:39 am

It looks like my debugging tools incorrectly display variables. I opened the memory view and saw that the malloc() returned a pointer to the newly allocated memory space, and not to the internal variable "osEE_tc_stm_freq_kHz". But in memory this two area located tightly. So, now it's worked normally.
On sreenshot blue highlighted is osEE_tc_stm_freq_kHz variable. And yellow highlited is location, returned by malloc(..)
Выделение памяти3.PNG
Выделение памяти3.PNG (8.26 KiB) Viewed 1469 times

e.guidieri
Full Member
Posts: 166
Joined: Tue May 10, 2011 2:05 pm

Re: malloc using

Post by e.guidieri » Mon Jul 15, 2019 11:25 am

Hi,

I think that using dynamic memory in a hardware AMP architecture like TriCore is looking for horrible and unnecessary troubles.

In any case, it seems that the problem is that .bss segment ends aligned to 8 and the Hightec sbrk function do not allocate any guard area (as usually is done).

Try to move the start of the HEAP by force.

In the linker script

Code: Select all

  CORE_SEC(.heap) (NOLOAD) : ALIGN(8) FLAGS(aw)
  {
    . = 8; /* <-- This should do the trick */
    PROVIDE(__HEAP = .);
    . += __HEAP_SIZE;
    PROVIDE(__HEAP_END = .);
  } > GLOBAL_RAM

Post Reply