IAR C, Safe settings for CSTACK - RSTACK - HEAP

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using IAR C...

1. What are the best and safer settings for CSTACK, RSTACK?
Lets consider that an application can use about 500bytes from variables, and the stack for return address may not exceed the 30 levels:
With what value I have to set the CSTACK, and RSTACK to feel safe that nothning bad will happen in my variables.

2. In AVRGCC we can see the size of the produced hex code, and also the used memory, as compile the program. How can see the same things in IAC C?

3. What about HEAP, if am not using malloc,free, calloc, and realloc what the value that I have to set the HEAP?

Learn as long as you live

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
1. What are the best and safer settings for CSTACK, RSTACK?

Whatever is right for your application. With knowing almost nothing about your application, it is pretty much impossible to say.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kzdev wrote:
Using IAR C...

1. What are the best and safer settings for CSTACK, RSTACK?
Lets consider that an application can use about 500bytes from variables, and the stack for return address may not exceed the 30 levels:
With what value I have to set the CSTACK, and RSTACK to feel safe that nothning bad will happen in my variables.


Depends on your application, there is no universal right answer.

kzdev wrote:

2. In AVRGCC we can see the size of the produced hex code, and also the used memory, as compile the program. How can see the same things in IAC C?

You generate a map file

kzdev wrote:

3. What about HEAP, if am not using malloc,free, calloc, and realloc what the value that I have to set the HEAP?

You don't need to specify a heap size [ie set it to 0], unless you use it. Note that some library functions do use some heap. I usually set mine equal to the free space between the stacks, and the statically allocated memory.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

RSTACK if am well undestanding, is for return address stack so if my program is not exceed the 30 nested calls RSTACK = 35 must be safe. Right?

But I am not sure for the CSTACK. What is its purpose? In what cases the compiler is using it? It has any relation with the total variables size?

In what case I have danger my variables to be overwritten?

Learn as long as you live

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
if my program is not exceed the 30 nested calls RSTACK = 35 must be safe. Right?

That depends on if the stack size is in bytes or words. Return addresses take up 2 bytes each (or 3 on larger chips).
Quote:
But I am not sure for the CSTACK. What is its purpose?

To hold automatic variables (local variables and parameters).

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so if I exceed the CSTACK from my application, IAR C compiler will have any warning?

Learn as long as you live

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IAR Reference guide says:

Quote:
Stack size considerations
The compiler uses the internal data stack, CSTACK, for a variety of user program
operations, and the required stack size depends heavily on the details of these
operations. If the given stack size is too large, RAM will be wasted. If the given stack
size is too small, there are two things that can happen, depending on where in memory
you have located your stack. Both alternatives are likely to result in application failure.
Either variable storage will be overwritten, leading to undefined behavior, or the stack
will fall outside of the memory area, leading to an abnormal termination of your
application. Because the second alternative is easier to detect, you should consider
placing your stack so that it grows towards the end of the memory.

But the question is how can the stack size being estimated in a very complicated program with multiple function calls when there is no CSTACK warning in IAR?

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In debug mode, IAR intializes your entire stack with a fixed value (0xCD) I believe. If you run your program and break execution. You will be able to tell the maximum height of your stack that way. In some cases it will flag an error in debug if your stack usage crosses some threshold.

Definitely not a perfect solution, but as good as your going to get with the toolchain.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Other than debugging by tracing the code, is there any option in IAR for detecting CSTACK overflow?

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately not. AFAIK IAR doesn't do any kind of static stack analysis, so it can only be checked at run-time. You can obviously make your own routine to check the stack level and transmit it over some other interface (UART for instance) if jtag is not available.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

glitch wrote:

kzdev wrote:

2. In AVRGCC we can see the size of the produced hex code, and also the used memory, as compile the program. How can see the same things in IAR C?

You generate a map file

Yes, that's it! I have tested *.map in IAR and CSTACK usage can be verified by this file.

Example:

#include"ioavr.h"
void f(unsigned char a[])
{
 unsigned char b[256],i=0;
 do{b[i]=a[i];}while(++i);
}
void main()
{  
 unsigned char a[256],i=0; 
 do{a[i]=0x55;}while(++i); 
 f(a);
 while(1);
} 

Generated by .map file:

  ->Sub-tree of type: Function tree
                             RSTACK   CSTACK
    | Stack used (prev) :  00000000 00000000
  02    f(unsigned char *)
        | Stack used (prev) :  00000000 00000000
        | + function block  :  00000002 00000100
  01  main
      | Stack used (prev) :  00000002 00000100
      | + function block  :  00000002 00000101
  <-Sub-tree of type: Function tree
    | Stack used          :  00000004 00000201


  ->Sub-tree of type: Function tree
                             RSTACK   CSTACK
    | Stack used (prev) :  00000004 00000201
  01  exit
      | Stack used (prev) :  00000004 00000201
      | + function block  :  00000002 00000000
  <-Sub-tree of type: Function tree
    | Stack used          :  00000006 00000201


  ->Sub-tree of type: Function tree
                             RSTACK   CSTACK
    | Stack used (prev) :  00000006 00000201
  01  __low_level_init
      | Stack used (prev) :  00000006 00000201
      | + function block  :  00000002 00000000
  <-Sub-tree of type: Function tree
    | Stack used          :  00000008 00000201

As can see, max CSTACK is 0x201.

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

and allow some spare stack space for the worst-case nesting of function calls PLUS interrupts.