STACK Overflow problem in QD4

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

STACK Overflow problem in QD4

2,265 Views
Amey
Contributor I
Dear All,

I am using QD4 device for universal IR receiver project. I have allocated 150 bytes of RAM(array of unions..each having size of one int) to save incoming waveform. Initially it was giving 'out of space allocation' error. But i made some changes to project.prm file by changing some allocation for Z_RAM and it didn't give any error. My 'project.prm' file is attached below.
SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */    ROM                      =  READ_ONLY    0xF000 TO 0xFFAD;//4k(65453-61440)    Z_RAM                    =  READ_WRITE   0x0060 TO 0x0080;//32(128-96)    RAM                      =  READ_WRITE   0x081 TO 0x015F;//222(129 to 351)    ROM1                     =  READ_ONLY    0xFFC0 TO 0xFFCF;//ENDPLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */    DEFAULT_RAM                         INTO  RAM;    DEFAULT_ROM, ROM_VAR, STRINGS       INTO  ROM; /* ROM1 In case you want to use ROM1 as well, be sure the option -OnB=b is passed to the compiler. */    _DATA_ZEROPAGE, MY_ZEROPAGE         INTO  Z_RAM;END//STACKSIZE 54STACKSIZE 60

 

Now my problem is, my code only works some time;otherwise it always fails to execute. I think this large allocation of RAM might be resulting in overflow of STACK. I don't have any alternative to avoid this allocation. Can anybody please tell me how much space i have to leave for Z_RAM? Is there any alternative to relocate stack;so that i can continue with current allocation space?  Please Help..

Thanks 
Labels (1)
0 Kudos
Reply
6 Replies

799 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

The QD4 has only 256 bytes of RAM, with 160 bytes in page 0, and 96 bytes in page 1.  This means that you will need to explicitly allocate your static variables to zero page.

Before you define the variables, use the following pragma.

 

#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE

To keep the variables and the stack separated as much as possible, within the PRM file you might replace the STACKSIZE entry, with the entry STACKTOP 0x15F, otherwise the stack will be placed immediately above your static variables.  The idea is for the static variables to commence at address 0x0060, and build upward, and for the stack to grow downward from 0x015F.  This will maximize the amount of room available for the stack.

 

You also seem to have reduced the upper limit of Z_RAM within the PRM file.  I suggest that this is returned to the correct upper limit, and the RAM segment adjusted accordingly.

 

Regards,

Mac

0 Kudos
Reply

799 Views
Amey
Contributor I

hi bigmac. thanks a lot.

 

I tried as per your instructions by relocating stack to upper memory location where STACKTOP is at 0x015F.My new prm file is as below.

 

/* This is a linker parameter file for the mc9s08qd4 */NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */    Z_RAM                    =  READ_WRITE   0x0060 TO 0x00FF;//160 bytes page 0    RAM                      =  READ_WRITE   0x0100 TO 0x013C;// 60 bytes page 1    STACK                    =  READ_WRITE   0x013D TO 0x015F;//22 bytes page 1    ROM                      =  READ_ONLY    0xF000 TO 0xFFA9;    ROM1                     =  READ_ONLY    0xFFC0 TO 0xFFCF; /* INTVECTS                 =  READ_ONLY    0xFFD0 TO 0xFFFF; Reserved for Interrupt Vectors */ENDPLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */    DEFAULT_RAM                         /* non-zero page variables */                                        INTO  RAM;    _PRESTART,                          /* startup code */    STARTUP,                            /* startup data structures */    ROM_VAR,                            /* constant variables */    STRINGS,                            /* string literals */    VIRTUAL_TABLE_SEGMENT,              /* C++ virtual table segment */    DEFAULT_ROM,    COPY                                /* copy down information: how to initialize variables */                                        INTO  ROM; /* ,ROM1: To use "ROM1" as well, pass the option -OnB=b to the compiler */    _DATA_ZEROPAGE,                     /* zero page variables */    MY_ZEROPAGE                         INTO  Z_RAM;END//STACKSIZE 0x30STACKTOP 0x015FVECTOR 0 _Startup /* Reset vector: this is the default entry point for an application. */

 But now it is giving error while linking "L1206 stack overlaps with a segment which appear in the    placement block". kindly help. Please tell me how much space i should leave for STACK.
Thanks

 

0 Kudos
Reply

799 Views
Amey
Contributor I

Hi again

 

After struggling a bit I have managed to link the project by changing some things in the prm file which is listed below

 

/* This is a linker parameter file for the mc9s08qd4 */NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */    Z_RAM                    =  READ_WRITE   0x0060 TO 0x00FF;//160 bytes page 0    RAM                      =  READ_WRITE   0x0100 TO 0x013C;// 60 bytes page 1    STACK                    =  READ_WRITE   0x013D TO 0x015F;//22 bytes page 1    ROM                      =  READ_ONLY    0xF000 TO 0xFFA9;    ROM1                     =  READ_ONLY    0xFFC0 TO 0xFFCF; /* INTVECTS                 =  READ_ONLY    0xFFD0 TO 0xFFFF; Reserved for Interrupt Vectors */ENDPLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */    DEFAULT_RAM                         /* non-zero page variables */                                        INTO  RAM;    _PRESTART,                          /* startup code */    STARTUP,                            /* startup data structures */    ROM_VAR,                            /* constant variables */    STRINGS,                            /* string literals */    VIRTUAL_TABLE_SEGMENT,              /* C++ virtual table segment */    DEFAULT_ROM,    COPY                                /* copy down information: how to initialize variables */                                        INTO  ROM; /* ,ROM1: To use "ROM1" as well, pass the option -OnB=b to the compiler */    _DATA_ZEROPAGE,                     /* zero page variables */    MY_ZEROPAGE                         INTO  Z_RAM;END//STACKSIZE 0x30STACKTOP 0x015FVECTOR 0 _Startup /* Reset vector: this is the default entry point for an application. */

 

 I am not sure whether the STACK grows into RAM now.

 

Is the allocation okay?...Can you please suggest a better way of doing this if there's one?

 

Please get back

 

Thanks.

 

0 Kudos
Reply

801 Views
bigmac
Specialist III

Hello,

 

If you are programming in C, the size of the stack segment that you have allowed would generally be far too small, and would definitely be problematic if you were specifying a STACKSIZE of this amount. 

 

However, with the use of STACKTOP, and if most of your static variables are allocated within zero page, this may not matter too much.  Any variables within the RAM segment would be allocated starting from the beginning of the segment, giving maximum separation to the top of stack.  In this case, the size of the stack segment will have no bearing on whether or not the variables are over-written.

 

However, specifying a realistic stack segment size will provide warning if too many variables have been allocated to the RAM segment.

 

Regards,

Mac

0 Kudos
Reply

801 Views
Amey
Contributor I

 

Hello Mac,

Thanks for your reply.

 

I tried with the allocations mentioned in prm file which i have posted here. It works fine sometime if i only try to capture the waveform. But in my application after capturing a waveform i write it to the flash memory. This flash writing routine needs about 37 bytes of RAM. It writes data to the flash correctly but after that code misbehaves.

 

To avoid STACK overflow I tried to RESET my processor everytime i capture a new frame. In this case it works fine. But i have to take some decision after receiving a frame. e.g. driving a relay ON/OFF. But after restting; RAM gets cleared and this decision is no longer valid. Even writing a decision(bit-ON/OFF) to flash is not possible because it's frequent operation. So i left this approach.

You said i should declare my variables to page zero. Shall i declare my local variables to page zero as well? Will this help more?

Waiting for your valuable reply mac..!

Thanks

 

 

 

 

0 Kudos
Reply

801 Views
bigmac
Specialist III

Hello Amey,


Amey wrote:

I tried with the allocations mentioned in prm file which i have posted here. It works fine sometime if i only try to capture the waveform. But in my application after capturing a waveform i write it to the flash memory. This flash writing routine needs about 37 bytes of RAM. It writes data to the flash correctly but after that code misbehaves. 



I assume your RAM based function is located on the stack, i.e. doonstack.  If so, the additional stack requirements are probably over-writing your static variables in the RAM segment, and perhaps even into Z_RAM.

 

If you search this forum, you should find an alternative method of writing to flash memory that uses a fixed location within RAM, and requires only about 10 bytes.  Since the code would be loaded to RAM just prior to the erase/write process, it does not matter if this is eventually over-written by the stack.  The location I might choose would be just above the static variables in RAM, or at the bottom of the stack segment (assuming a realistic stack segment size).

 

Always check the map file for the project, to determine where the variables and the stack are actually being placed by the linker.

 


You said i should declare my variables to page zero. Shall i declare my local variables to page zero as well? Will this help more?


By definition, local variables are placed on the stack.  For a variable to be located at a fixed location it would need to be defined as static, but this is generally not a good idea unless the variable value needs to be retained once the function exits.  Within your local variables, do you have any large array variables?  These may greatly expand the stack requirements when the function is executed.

 

Local variables defined within main() will need special consideration.  Since this function does not exit, the stack space occupied by these variables will never be recovered, and will increase the overall stack size requirements.  If these are significant, it may be better to define them as static, to avoid complicating the stack size issue.

 

Regards,

Mac

0 Kudos
Reply