HOW TO SET HEAP SIZE FOR MALLOC WITH S08 MCUs CODEWARRIOR 11.1

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

HOW TO SET HEAP SIZE FOR MALLOC WITH S08 MCUs CODEWARRIOR 11.1

1,399 Views
BooleanBob
Contributor IV

 

I have a working C project for MC9S08PT32 and need to make some additions using malloc() for a small amount of memory, say 30 bytes.

Compiling, I get an "OUT OF ALLOCATION SPACE IN SEGMENT RAM 0x010B".

Found this post here  and changed the following original definition, in libdefs.h:

----------------------------------------------------

#define LIBDEF_HEAPSIZE 2000

/*!< Adjust to your requirements, nofBytes = LIBDEF_HEAPSIZE, must be multipe of 4! */

----------------------------------------------------

libdefs.h is located here in my disk:

C:\Freescale\CW MCU v11.1\MCU\lib\hc08c\include\

#define LIBDEF_HEAPSIZE 160

Anyway, I still get exactly the same error message. It seems that the "heap" is just an array taking a portion of the RAM segment as defined in my .PRM linker file for this MCU:

SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */
Z_RAM  = READ_WRITE 0x0040 TO 0x00FF;
RAM    = READ_WRITE 0x0100 TO 0x05FF;
MY_STK = NO_INIT    0x0600 TO 0x103F;
ROM    = READ_ONLY  0x8000 TO 0xFF6F;
ROM1   = READ_ONLY  0xFF80 TO 0xFFAF;
EEPROM = READ_ONLY  0x3100 TO 0x31FF;
/* INTVECTS = READ_ONLY 0xFFB0 TO 0xFFFF; Reserved for Interrupt Vectors */
END

In the solution for this mentioned post includes recompiling the libraries, and I really don't know how to do that.

My question is if this is really necessary or is there a simpler way to make this definition work, the same way a .PRM linker file is configured. I verified that when recompiling the project, the new definition is considered valid, simply removed the # in the #define and checked that this produces a syntax error. I thought that this was all that was needed.

Could somebody help me with a detailed explanation on how to make this work in order to use malloc() in my project?

Thank you

 

 

 

 

 

0 Kudos
4 Replies

1,382 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

HI

Before to use the heap the user must define the heap size.

By default the size is 2000.

If you don't change the heap size, the linker will generate an error because the RAM is not enough important for the chip.

Please send me your project. I need to know which lib you are using.

Thanks.

Jun Zhang

 

0 Kudos

1,373 Views
BooleanBob
Contributor IV

 

Dear Jun Zhang, thank you very much for your reply.

Kindly, could you please reread my post so I don't have to explain again  this issue?

I attached two .ZIP compressed files.

The first one contains a minimum CW 11.1 project, using the same MCU and the same linker file settings.

The second one contains the PT32.PRM linker file itself, configured for use with many working projects.

If you please, notice that the "OUT OF MEMORY" error falls in the RAM segment assigned by the linker PRM file. Please check it.

As I said in my post, the library file where the default size is originally set to 2000 bytes, was tested to be == 160 (as it must be 4x multiple), and the error persists at the same memory location.

Awaiting for your kind reply

 

0 Kudos

1,359 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

HI,

In your project 2000 size heap is at 0x105-0x8D4

ZhangJennie_0-1682151718737.png

But in your PT32.prm, heap is splited by MY_STK.

ZhangJennie_2-1682152077929.png

Changing the segment definition as below can resolve the problem.

ZhangJennie_1-1682151788078.png

I built it on my side, there is no error.

Jun Zhang

 

0 Kudos

1,337 Views
BooleanBob
Contributor IV

 

Dear Jun Zhang, thanks for your reply.

YES, The solution that you are kindly offering works but it takes a lot of RAM memory, which is basically one of the problems we are trying to solve. MC9S08PT32 has a valuable 4K RAM space and was selected for our project.

Please, be so kind to consider the following:

At first sight, there is not enough information in the Smartlinker manual about HEAP_SEGMENT and adjusting the heap size. However, I found this Use malloc in CodeWarrior for HC08.

So, although it seems to work for previous CodeWarrior version,  I did this, in the PRM linker file I added the MY_HEAP segment and made the corresponding PLACEMENT as follows:

---------------------------------

SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */
Z_RAM = READ_WRITE 0x0040 TO 0x00FF;
RAM = READ_WRITE 0x0100 TO 0x02FF; /* 511 BYTES RAM */
MY_HEAP = READ_WRITE 0x0300 TO 0x0AFF; /* 2000 BYTES FOR HEAP COMPILES OK */
// MY_HEAP = READ_WRITE 0x0300 TO 0x03F0; /* 100 BYTES FOR HEAP --ERROR--- */
MY_STK = NO_INIT 0x0400 TO 0x103F; /* MORE THAN 3100 BYTES FOR STACK */
ROM = READ_ONLY 0x8000 TO 0xFF6F;
ROM1 = READ_ONLY 0xFF80 TO 0xFFAF;
EEPROM = READ_ONLY 0x3100 TO 0x31FF;
/* INTVECTS = READ_ONLY 0xFFB0 TO 0xFFFF; Reserved for Interrupt Vectors */
END

PLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */
DEFAULT_RAM, /* non-zero page variables */
INTO RAM;

...

_DATA_ZEROPAGE, /* zero page variables */
MY_ZEROPAGE INTO Z_RAM;
HEAP_SEGMENT INTO MY_HEAP;
SSTACK INTO MY_STK;
END

---------------------------------

Now, in the libdefs.h located in c:\Freescale\CW MCU v11.1\MCU\lib\hc08c\include\

//#define LIBDEF_HEAPSIZE 2000
#define LIBDEF_HEAPSIZE 240

Compiling the project with the PRM file PT32.PRM containing:

MY_HEAP = READ_WRITE 0x0300 TO 0x0AFF; /* 2000 BYTES FOR HEAP COMPILES OK */

This works fine, taking 2000 bytes for the heap, when just 30 or 40 bytes are needed, spoiling half of the total RAM memory space of the chip !

But, if you compile the project with the PRM set as follows:

MY_HEAP = READ_WRITE 0x0300 TO 0x03F0; /* ABOUT 100 BYTES FOR HEAP ---POPS ERROR--- */

The same old error pops out:

ERROR L1102: Out of allocation space in segment MY_HEAP at address 0x300

In the load map file .MAP, this is shown:

-------------------

- LABELS:__SEG_END_SSTACK 0'Error 0 0 0

MODULE: -- heap.o (ansiis.lib) --
- PROCEDURES:
_heapcrash_ 81AB 16 22 0 .text
- VARIABLES:
_heap_ 0'Error 7D0 2000 0 HEAP_SEGMENT   <---- NOTICE SAME 200 BYTES TAKEN !

-------------------

This means that in the libdefs.h the following definition is compiled but doesn't work for some reason.

//#define LIBDEF_HEAPSIZE 2000
#define LIBDEF_HEAPSIZE 240

This should be the reasonable way to dimension the necessary bytes needed for the heap.

Could you please help me find the correct way to set all the needed parameters in order to keep as much RAM as possible and also use the needed heap. I am sure that this is possible with such refined CodeWarrior 11.1 tools.

I attached the corrected PT32.PRM file, the libdefs.h file and again the complete project for your evaluation.

Thank you in advance

 

 

 

 

 

 

0 Kudos