Page 1 of 1

local auto variables in tasks

PostPosted: Thu Dec 15, 2005 3:41 am
by tyski
This pertains to the C18 compiler, although I'm sure it could affect other platforms as well.

The Salvo manual says that local auto variables are no problem. I think this may cause problems, although I cannot say for sure.

In the following discussion, the stack pointer referes to the software stack, essentially the value of FSR1.

Let's use the following sequence of events:
1. Stack pointer is set to 0.
2. TaskA is created (TaskA declares one byte worth of local auto variables).
3. OSSched is called as part of an infinite loop in main.
4. OSSched enters a critical section and pushes the interrupt status onto the stack. Stack pointer is now 1.
5. OSSched dispatches TaskA.
6. TaskA increments the stack pointer by 1 (stack pointer is now 2) for its single byte local auto variable.
7. TaskA context switches.
8. At the end of OSSched, the critical section is exited by popping the interrupt status off the stack. Stack pointer is now 1 (should be 0).

The problem here is that when TaskA context switches, it never releases the local auto that it allocated on the stack. So the byte that OSSched is popping is not the byte that it actually pushed onto the stack.

With this thought in mind, the stack pointer will never get back to where it began as long as new tasks are started that declare local auto variables.

The only reason my application has worked thus far is because once all local autos are pushed onto the stack, OSSched just happens to pop a byte off the stack that enables interrupts. With my move to Salvo Pro, something changed slightly and my application reaches a point where interrupts are permanently disabled because the stack frame at the end of OSSched just happens to sit on a byte that doesn't reenable interrupts.

Like I said, I'm not entirely sure if this is a problem as its hard to keep track of everything while stepping through the code. However, the problem does seem to have merit in my mind.



Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX

Re: local auto variables in tasks

PostPosted: Thu Dec 15, 2005 3:48 am
by tyski
Well, I just found the service bulletin that confirms the problem I was having. Silly me for not reading them prior to posting. Oh well...

Re: local auto variables in tasks

PostPosted: Thu Dec 15, 2005 3:50 am
by tyski
Allow me to post a link to the service bulletin in question.