|
|
View previous topic :: View next topic |
Author |
Message |
sharethl
Joined: 05 Apr 2010 Posts: 4
|
PIC24 Stack questions |
Posted: Thu Aug 09, 2012 10:10 am |
|
|
PIC24 has 31 stacks, but CCS compiler could set #build{stack =512}
what is this 512 means?
Is it means in the 31 layers of functions calls, it could hold 512 bytes local variables totally?
Here is the example, Am I right?
eg: ignore the returns, here is a 2 layers function, total have 3 local variables, so #build{stack =3}, and PIC24's 31 stacks could support 2 layers.
Code: |
fun1(){
int a,b=0;
fun2();
}
fun2(){
int b=0;
}
|
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Aug 09, 2012 1:56 pm |
|
|
I don't know where you get '31' from?.
Study the data sheet. The PIC24's, have a _16_ register hardware stack, but then have the extra instructions to extend this into the RAM:
"In addition to its use as a working register, the W15 register
in PIC24F devices is also used as a Software
Stack Pointer. The pointer always points to the first
available free word and grows from lower to higher
addresses. It predecrements for stack pops and
post-increments for stack pushes, as shown in
Figure 4-4. Note that for a PC push during any CALL
instruction, the MSB of the PC is zero-extended before
the push, ensuring that the MSB is always clear.
The Stack Pointer Limit Value (SPLIM) register, associated
with the Stack Pointer, sets an upper address
boundary for the stack. SPLIM is uninitialized at Reset.
As is the case for the Stack Pointer, SPLIM<0> is
forced to ‘0’ because all stack operations must be
word-aligned. Whenever an EA is generated using
W15 as a source or destination pointer, the resulting
address is compared with the value in SPLIM. If the
contents of the Stack Pointer (W15) and the SPLIM
register are equal, and a push operation is performed,
a stack error trap will not occur. The stack error trap will
occur on a subsequent push operation. Thus, for
example, if it is desirable to cause a stack error trap
when the stack grows beyond address 2000h in RAM,
initialize the SPLIM with the value, 1FFEh.
Similarly, a Stack Pointer underflow (stack error) trap is
generated when the Stack Pointer address is found to
be less than 0800h. This prevents the stack from
interfering with the Special Function Register (SFR)
space.
A write to the SPLIM register should not be immediately
followed by an indirect read operation using W15."
Unlike the smaller PIC models, this is a register stack, rather than just a call/return stack. Hence much more use is made of it (why the larger size is particularly needed).
Best Wishes |
|
|
sharethl
Joined: 05 Apr 2010 Posts: 4
|
|
Posted: Fri Aug 10, 2012 11:15 am |
|
|
Sorry, not 31, but it should be in range 2-31, right?
One function consumes a stack.
Ttelmah wrote: | I don't know where you get '31' from?.
Study the data sheet. The PIC24's, have a _16_ register hardware stack, but then have the extra instructions to extend this into the RAM:
"In addition to its use as a working register, the W15 register
in PIC24F devices is also used as a Software
Stack Pointer. The pointer always points to the first
available free word and grows from lower to higher
addresses. It predecrements for stack pops and
post-increments for stack pushes, as shown in
Figure 4-4. Note that for a PC push during any CALL
instruction, the MSB of the PC is zero-extended before
the push, ensuring that the MSB is always clear.
The Stack Pointer Limit Value (SPLIM) register, associated
with the Stack Pointer, sets an upper address
boundary for the stack. SPLIM is uninitialized at Reset.
As is the case for the Stack Pointer, SPLIM<0> is
forced to ‘0’ because all stack operations must be
word-aligned. Whenever an EA is generated using
W15 as a source or destination pointer, the resulting
address is compared with the value in SPLIM. If the
contents of the Stack Pointer (W15) and the SPLIM
register are equal, and a push operation is performed,
a stack error trap will not occur. The stack error trap will
occur on a subsequent push operation. Thus, for
example, if it is desirable to cause a stack error trap
when the stack grows beyond address 2000h in RAM,
initialize the SPLIM with the value, 1FFEh.
Similarly, a Stack Pointer underflow (stack error) trap is
generated when the Stack Pointer address is found to
be less than 0800h. This prevents the stack from
interfering with the Special Function Register (SFR)
space.
A write to the SPLIM register should not be immediately
followed by an indirect read operation using W15."
Unlike the smaller PIC models, this is a register stack, rather than just a call/return stack. Hence much more use is made of it (why the larger size is particularly needed).
Best Wishes |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Aug 10, 2012 3:07 pm |
|
|
A basic 'call', occupies one stack level, but as I said, it is a _register_ stack as well, So the contents of registers used inside the functions are also pushed onto this, so a single function call can often use a dozen stack levels...
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Aug 11, 2012 3:30 am |
|
|
The number 31 thing is still a misunderstanding. There's no particularly limitation in PIC24 call depth,
except the limitation data memory available for stack usage.
As said, stack is used for various temporary data storage purposes, a larger amount is e.g. used for
printf() with float variables. The standard PCD interrupt handler uses e.g. 36 bytes of stack.
The present CCS default settings auf 512 byte should be usually sufficient, a stack size of 256
as mentioned in the manual has been frequently reported to cause stack overflows. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|