This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

task context switching

Hi,

i'm new to TI RTOS and i was wondering:

i want to have 2 communication tasks doing the same actions but on a different hardware interface. So i wrote one C function and i have 2 data structs that represent the different interfaces.

ex:

typedef struct {blabla}INTERFACE;

INTERFACE interface1, interface2;

void handle_interface(INTERFACE*);

void Task1()
{
   while(1) handle_interface(&interface1);
}

void Task2()
{
   while(1) handle_interface(&interface2);
}

question1:

Lets say task 2 has higher priority then task 1. I suppose TI RTOS knows that if task1 gets preempted by task2 that even if it will run the same code (handle_interface) that it will have to use interface2 struct instead of interface1 ? Or do i have to code it different and give the struct as a parameter to the task ?

void Task1(&interface)
{
handle_interface(interface);
}
void Task2(&interface)
{
handle_interface(interface);
}

question2: can i use local variables in the handle_interface() function ? lets day i have a local variable index running in task1 with a value of 3, task 2 takes over and resets the index to 0 and on exit index = 10. Will it be reset to 3 when task1 resumes or do i have to embed it in the INTERFACE struct to get saved/restored...

Thanks for the help

Kind regards 

Jan

  • Jan Goris said:
    Lets say task 2 has higher priority then task 1. I suppose TI RTOS knows that if task1 gets preempted by task2 that even if it will run the same code (handle_interface) that it will have to use interface2 struct instead of interface1 ?

    Each task has its own, separate stack.  So, when task1 runs, task1's stack will be used for storing call context and local variables of handle_interface.  In this case, handle_interface will have "interface1".  Similar for task2.

    Jan Goris said:

    Or do i have to code it different and give the struct as a parameter to the task ?

    void Task1(&interface)
    {
    handle_interface(interface);
    }
    void Task2(&interface)
    {
    handle_interface(interface);
    }

    This method would also work.  You can pass the interface you want in the Task_Params structure used when creating task1 and task2.

    Jan Goris said:
    question2: can i use local variables in the handle_interface() function ? lets day i have a local variable index running in task1 with a value of 3, task 2 takes over and resets the index to 0 and on exit index = 10. Will it be reset to 3 when task1 resumes or do i have to embed it in the INTERFACE struct to get saved/restored...

    Yes.  As I mentioned, each task has its own stack and your local index variable will be stored on the calling task's stack.

    The only situation that I can think of in which this wouldn't work is for local static variables within handle_interface().

    Steve

  • Thanks Steve,

    it was just to make sure i didn't do anything stupid.

    Regards

    Jan