We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hi all, I have written a complex program (CAN higher layer protocol driver ),which has many "for" loops and lots of calls.But after ceratin number of calls the stack reaches 37 :1F and then on the program hangs ..I tried defining as much of global variables ,passing less parameters , overlaying etc .. but when theres a "for" loop of just @4 or 5 iterations , the stack problem comes .. what are the possible options to avoid this from happening ? regards sameer
A for loop shouldn't affect the stack - unless there's something wrong within the body of the loop which is messing your stack up! C51 doesn't use the stack for parameters, so it's probably just down to the depth of nesting of your function calls. Is the Linker telling you anything? Are you using any assembler which might have unbalanced PUSH/POP? Have you checked for interrupt problems? Can you reproduce the problem on the simulator - might be easier to debug there.
HI Thanks! There is nothing in the "for" loop thats messing up the stack also there were no linker warnings.,.. but why should the problem should disappear once the for loop commented out ? few points :1>here was another for loop inside a for loop .. total iterations were 108 .. another question is for initiailization of some XRAM in infineon 505 , i am calling certain driver routines , which access only the XDATA .. through ptrs which i pass them through reference. now when i call this routine more than 3 times consecutively , the stack over flows !! (this is in the simulation itself ),but for less than 3 times , theres no problem in the stack .. and the complete program works fine .. this has put a very irritating limiation on the no of message objects i can use for CAN communication. regards sameer
"access only the XDATA .. through ptrs" Are you sure that they're accessing XDATA? - use the debugger to verify that the pointers really are pointing to XDATA. With C51, your pointers need to specify not only the correct address, but also the correct memory space (CODE, DATA, IDATA, PDATA, XDATA). Therefore, if you have a bad pointer it could be pointing to the wrong address, or the wrong memory space, or both!!
Using the simulator, you can check that the stack pointer has the same value before and after each call. If it is not, then the call is at fault and needs to be sorted out.
Hi Andrew 1 Thanks a lot .. after carefully looking at the for loop i found out .... it was modifying the stack !! .....since one of the array of pointers was not initialized properly !! and when i was storing something ..to those locations .. i was actually modifying my internal ram ie the stack !!!! well this as solved all my problems now !! well thanks again for giving me the correct direction !! cheers sameer
Ah, the old Uninitialised Pointer fault. This causes plenty of grief with standard 'C', but C51 adds the complication of the different memory spaces: The 1st byte of a C51 Generic Pointer specifies the memory space. For an uninitialised pointer, this is likely to be zero - which points into IDATA/DATA/BDATA, so you can soon be pointing into the stack! BTW: Unless you really need a Generic Pointers, a Memory-Specific Pointer would be more efficient. See the C51 manual