Pumpkin, Inc.

Pumpkin User Forums

local variables and OS calls

If you can't make Salvo do what you want it to do, post it here.

local variables and OS calls

Postby DHenry » Wed Oct 31, 2001 1:48 am

I don't think it is simply a "call thru function pointers" problem. It's really more of a PIC and PICC issue since the PIC does not have an accessible stack. PICC allocates auto variables to fixed locations (as opposed to on a stack), analyzes the user's call tree and determines where it can overlay (reuse) data locations allocated to these autos. This is (usually) a good thing, but with Salvo managing actual function threading execution, there's no way for the compiler to know that a shared data location shouldn't really be shared. And there's not really any (practical) way for Salvo to know which data locations are shared either. Some other compilers (e.g., Keil C51 for 8051's) do this same kind of overlaying, but give you some control over the overlay process if you want to manage it yourself.

I agree that it seems like a serious limitation, but I just don't see how Pumpkin would handle it any other way given the PIC, PICC, Salvo combination.

DHenry
 
Posts: 18
Joined: Sat Aug 04, 2001 11:00 pm
Location: Boulder, CO, U.S.A.

Re: local variables and OS calls

Postby James » Wed Oct 31, 2001 3:05 am

So does it violate ISO C?
James
 
Posts: 12
Joined: Thu Oct 04, 2001 11:00 pm
Location: San Diego, CA, USA

Re: local variables and OS calls

Postby aek » Wed Oct 31, 2001 3:14 am

quote:
That seems like a serious limitation. Is this an ISO-C violation or does the C standard allow calls thru function pointers to corrupt auto variables?

Neither -- that's not what's happening. There's no ISO C violation. DHenry's explanation is a good one.

It's always safe to use local auto variables in a Salvo task as long as they're not expected / required to persist in value after a context switch. For example:

code:
void Task( void )
{
unsigned char i;

for (;;) {
for (i=0;i<5;i++) {
do_something();
}
OS_Delay(delay, label);
i=3;
do {
do_something_else();
} while (--i);
OS_Yield(label);
}
}

is perfectly alright and won't cause any problems. But

code:
void Task( void )
{
unsigned char i;

for(;;) {
i=4;
do {
do_this();
OS_Yield(label);
} while (--i);
}
}


will most definitely cause problems because the RAM allocated to i may be re-used by another function or task while Task() is blocked by another higher-priority task. That's all due to PICC's overlay scheme -- local auto variables are shared among functions, and a Salvo task is a (special type of) function.

If you're finding that you're using up too much RAM due to multiple local auto variables, here's an approach you may be able to use. (This is useful because PICC places all local variables and function parameters in RAM Bank 0. Local static variables, however, can be placed in any RAM bank.) Anyway, if the two local auto variables are never used at the same time, you can combine them into a union, like this:

code:
void Task( void )
{
union {
unsigned char i;
int myInt;
} u;

for (;;) {
for (u.i=0;u.i<5;u.i++) {
do_something();
}
OS_Delay(delay, label);
u.myInt = 2320;
do {
do_something_else(u.myInt);
} while (--u.myInt);
OS_Yield(label);
}
}


------------------

[This message has been edited by aek (edited October 31, 2001).]

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Re: local variables and OS calls

Postby James » Wed Oct 31, 2001 3:26 am

I have no complaint. It was a discovery I made the hard way. Salvo is beautiful. The limitation is reasonable. Obviously this is more a PICC/PIC issue than Salvo. I mainly wanted some confirmation (I now have it) and was wondering if this is a violation of ISO C. (Since HiTech states "ANSI C Compiler" although I see that in their manual they do not cite which ANSI standard).
James
 
Posts: 12
Joined: Thu Oct 04, 2001 11:00 pm
Location: San Diego, CA, USA

Re: local variables and OS calls

Postby James » Wed Oct 31, 2001 10:31 am

I find that if I call an OS service, such as OS_Delay, from a Task, local auto variables are corrupted. This is using PICC 7.87 and Salvo full 2.2. Is this normal?
James
 
Posts: 12
Joined: Thu Oct 04, 2001 11:00 pm
Location: San Diego, CA, USA

Re: local variables and OS calls

Postby DHenry » Wed Oct 31, 2001 11:31 am

Yes it is normal and has an FAQ topic in the manual. See "Why must tasks use static variables?" on page 213 of the Salvo User Manual. There, it also describes when it *is* safe to use auto variables. I have use the static/auto mix reliably in my projects.

[This message has been edited by DHenry (edited October 31, 2001).]

[This message has been edited by DHenry (edited October 31, 2001).]

DHenry
 
Posts: 18
Joined: Sat Aug 04, 2001 11:00 pm
Location: Boulder, CO, U.S.A.

Re: local variables and OS calls

Postby James » Wed Oct 31, 2001 12:07 pm

That seems like a serious limitation. Is this an ISO-C violation or does the C standard allow calls thru function pointers to corrupt auto variables?
James
 
Posts: 12
Joined: Thu Oct 04, 2001 11:00 pm
Location: San Diego, CA, USA


Return to Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron