2
votes

Ok, I've been trying to solve this issue by my own for long time now, and I simply give up. I've searched through all the web until I couldn't take it anymore and I need help!

I read the whole "C18 Compiler User's Guide" and it says C18 supports the use of a stack with size greater than 256 bytes. But I did all they asked, changed linker script and used the multi-bank stack model compiler option(-ls), and I still have problems accessing variables after the first bank of the stack is filled.

I debugged a simple code which simple calls a function recursively. This function have 5 float parameters and 5 float local variables, using around 42 bytes in the stack in each call (5*4 for parameters + 5*4 for local variables + 2 bytes for saving old SFR2 value). After the 6th call, the stack values are overwritten.

While searching the web, I've found lots of people giving specific instructions on how do I create and use big arrays (larger than 256 bytes). One of the steps recommends the use of pointers to access the elements of the array. I feel like my problem is related to this "use pointers to access variables in different banks", but I don't see how I can change my local variables all to pointers in a function call. It just makes no sense.

To be sincere, I'm afraid I'm just doing something really stupid (as I couldn't find no one discussing this problem). All topics related to increasing stack size I've found just suggested using static variables. I solved my problem this way, but I still wanna know how C18 supports larger stack size.

Below follows the test code I used trying to understand this problem. While debugging, I noticed that the FSR1 register is correctly updated to point to the next address in the next bank where the next function parameters and variables should be put. But when the local variables are written, they go to the top of the stack (only the lower byte of the address is considered to move the data to the stack, even when the address indicated to the variable by MPLABX IDE, in debug mode, is correct). Because of this, I'm pretty sure the problem is that the compiler's generated code is not using 16 bit address to use the stack. Still, I do not know how to solve it.

Any discussion on this topic will be greatly appreciated :) Thanks!

float testFunction1(float p1, float p2, float p3, float p4, float p5)
{
float v1, v2, v3, v4, v5;

v1 = p1 + p2;
v2 = p2 + p3;
v3 = p3 + p4;
v4 = p4 + p5;
v5 = p5 + p1;

v1 = testFunction1(v1, v2, v3, v4, v5);
}

void main()
{
float v1, v2, v3, v4, v5;

v1 = 0x11ABCDEF;
v2 = 0xAABBCCDD;
v3 = 0xABCABCAB;
v4 = 0xFFEEDDCC;
v5 = 0x11223344;

v1 = testFunction1(v1, v2, v3, v4, v5);
}
1
Can you show the linker script?mjh2007
Oh! I forgot this question as I gave up trying! I don't have it here now, but I will try to share it tomorrow! Thanks!john1034

1 Answers

0
votes

To enable multi-bank stack code model, use -Lmodel switch in command line of C18 compiler.