Pumpkin, Inc.

Pumpkin User Forums

Variables in Salvo

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

Variables in Salvo

Postby luben » Tue Dec 19, 2000 10:10 am

Hello,

As all tasks are like different programs, does that mean that the names of the declared variables should be uniqual. I mean, if in TAsk1 I declare static byte i; and in Task2 static byte i; again - this will appear like mistake? From what I learned from the Salvo's manual it should be an error - all variables should be different and static, right?

Regards
Luben

luben
 
Posts: 324
Joined: Sun Nov 19, 2000 12:00 am
Location: Sofia, Bulgaria

Re: Variables in Salvo

Postby Salvo Tech Support » Tue Dec 19, 2000 10:58 am

Hi Luben.

That's a C question -- not a Salvo question :-)

Any variable that's "inside" a task (which is also a function) cannot be seen by any other function. The variables are "local in scope." Therefore you can do:

code:
void TaskA (void)
{
char i;
static int counter;


for (;;){
for ( i=0 ; i<10 ; i++ ) {
counter++;
}
OS_Delay(TaskA1);
...
}
}


void TaskB (void)
{
char i;
static int counter;


for (;;){
for ( i=10 ; i>0 ; i-- ) {
counter *= 2;
}
OS_Yield(TaskB1);
...
}
}


without any problems, i.e. there are two distinct, different counters. It may be difficult to debug this (e.g. in MPLAB), however, as these two pairs of variables have the same names ("i", "counter").

If you declare the variables outside of the tasks, e.g. in main() or even outside of main, then they have a greater scope, and yes, they will need unique names. They will be implicitly static in this case.

So, you really don't need to worry about names for task variables.

Here's an example where a task variable does not need to be static:

code:
void TaskSend3Splats( void )
{
char loopCounter = 3;


for (;;) {
while (loopCounter--)
SendChar('!');

OS_Delay(400, TaskSend3Splats1);
}

loopCounter = 3;
}
}


Below is an example where a static varibale is required:

code:
void TaskAlsoSend3Splats( void )
{
static char loopCounter = 20;


for (;;) {
while (loopCounter--)
OS_Delay(20, TaskAlsoSend3Splats1);

SendChar('!');
SendChar('!');
SendChar('!');
}

loopCounter = 20;
}
}


Both tasks send 3 '!' characters every 400 system ticks. The difference between them is that the second task has context switches "inside the while() loop." Therefore the loop counter must be declared static in order for 20 loop iterations to happen. If it were an auto variable, the number of loop iterations would be unpredictable because loopCounter might change from one context switch to the next, because the compiler may use that memory location for another (auto) variable.

By the way, a situation where you might want to use the second task and not the first is the case where you have compiled your Salvo application for 8-bit delays. Therefore the maximum value you can specify for a delay is 255 system ticks. You could write:

code:
void TaskC (void)
{
for (;;) {
...
OS_Delay (255, _label1);
OS_Delay (145, _label2);
...
}
}

but that might cost more ROM than a simple for loop, because the context switchers are (n-line) macros. Compiling for 16-bit delays would take an additional byte of RAM per task, and if you only delayed for > 255 system ticks in a single place in your code, that might not be a good tradeoff.

[This message has been edited by Salvo Tech Support (edited December 19, 2000).]

--------
Salvo Technical Support
Please request all tech support through the Forums.
Salvo Tech Support
 
Posts: 173
Joined: Sun Nov 19, 2000 12:00 am

Re: Variables in Salvo

Postby luben » Wed Dec 20, 2000 1:36 am

Hello,

Because Salvo structure is not exactly like C procedure, there are some changes. I mean, this question is not a C, but both C and Salvo. The idea, that going to scheduler you return to zero level of interrupts makes the main program and all tasks to look something like one big program. I mean - without Salvo and with Salvo there some change in the behaviour. Therefore - you should keep in mind both Salvo and C rules.

Regards
Luben

luben
 
Posts: 324
Joined: Sun Nov 19, 2000 12:00 am
Location: Sofia, Bulgaria

Re: Variables in Salvo

Postby Salvo Tech Support » Wed Dec 20, 2000 11:21 am

Hi Luben.

Perhaps another way to look at it is that Salvo tasks are somewhat like reentrant functions themselves -- you can find yourself "back inside" the task at some later time. If you expect a variable to persist from one context switch to another, it needs to be declared as static.

Regards,

--------
Salvo Technical Support
Please request all tech support through the Forums.
Salvo Tech Support
 
Posts: 173
Joined: Sun Nov 19, 2000 12:00 am


Return to Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron