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.

RTOS/SW-EK-TM4C129EXL: Scheduling method

Part Number: SW-EK-TM4C129EXL
Other Parts Discussed in Thread: EK-TM4C129EXL

Tool/software: TI-RTOS

I am working with EK-TM4C129EXL kit. I need to schedule two tasks. Let it be Task1 and Task2. Timer should trigger interrupt for every 0.5 ms and it should execute Task1. Timer should trigger interrupt for every 0.5 ms and it should execute Task2.  

i.e. 0.5ms - Task1

      0.5ms - Task2

      0.5ms - Task1

      0.5ms - Task2

How to design Timer so that these task runs correctly.. 

  • Create a single Timer with period of 500us.

    Create 2 Semaphores: sem1, sem2.

    Task1 is in a loop pending on sem1

    tsk1Func(UArg arg0, UArg arg1)
    {
        while (1) {
        Semaphore_pend(sem1, BIOS_WAIT_FOREVER);
    
         ... /* task1 work here */
     
        }
    }
    
    

    Task2 is in a loop pending on sem2

    tsk2Func(UArg arg0, UArg arg1)
    {
        while (1) {
        Semaphore_pend(sem2, BIOS_WAIT_FOREVER);
    
        ... /* task2 work here */
    
       }
    }
    
    

    In the Timer function, post either sem1 or sem2 depending on a static counter.

    timerFunc(UArg arg)
    {
        static int count = 0;
    
        if (count++ & 1) {
            Semaphore_post(sem2);
        }
        else {
            Semaphore_post(sem1);
        }
    }

  • Hi Alan ,
    Do I have to statically configure the timerFunc as a periodic timer ???.. When will the else part of the code gets executed???... How does the above code execute??
  • You can create the timer statically or at run time.

    Perhaps the "count++" on line 5 is obscure. 'count' is a static variable which is initialized to zero once at application startup.

    Each time the timerFunc runs, 'count' is incremented. When 'count' is even, 'sem1' will be posted. When 'count' is odd, 'sem2' will be posted.

    Alan
  • Hi Alan,

                static int count = 0;   Declaring count inside the timerFunc would make the count to zero every time when the timer interrupt is triggered. Hence the if statement will run everytime... Therefore do I have to declare Count Globally like...

    volatile int count = 0;

  • Alan,

            I have added two sample tasks (task 1: Blink User LED0, task 2: Blink User LED1). Then I had configured static timer and two semaphores (Binary counting type). Still I couldn't able to get the expected result. Those two LED doesn't blink in the interval of 0.5ms

    i.e. LED 0  -- ON  0.5ms

          LED 1 --  OFF 0.5ms

          LED 0  --  ON 0.5ms

          LED 1 --  OFF 0.5ms

          LED 0 --  ON 0.5ms

          LED 1  -- OFF 0.5ms

    The above highlighted operation doesn't run instead both the LED's are ON without blinking...       

    The code is as below....

    /* XDCtools Header files */
    #include <xdc/std.h>
    
    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Semaphore.h>
    
    /* TI-RTOS Header files */
    
    #include <ti/drivers/GPIO.h>
     volatile int count = 0;
     extern Semaphore_Handle sem1;
     extern Semaphore_Handle sem2;
    
    /* Board Header file */
    #include "Board.h"
    
    
    void timerFunc()
    {
        //static int count = 0;
        if (count++ & 1) {
            Semaphore_post(sem2);  /* This part of the code works, and the semaphore gets posted */
        }
        else {
            Semaphore_post(sem1);  /* This part of the code works, and the semaphore gets posted */
        }
    }
    
    /*Task1 is in a loop pending on sem1*/
    
    tsk1Func(UArg arg0, UArg arg1)
    {
        while (1) {
        Semaphore_pend(sem1, BIOS_WAIT_FOREVER);  /* Unable to set breakpoint here, program flow doesn't seem to reach here */
        GPIO_toggle(Board_LED0);
        /* task1 work here */
    
        }
    }
    /*Task2 is in a loop pending on sem2*/
    
    tsk2Func(UArg arg0, UArg arg1)
    {
        while (1) {
        Semaphore_pend(sem2, BIOS_WAIT_FOREVER);  /* Unable to set breakpoint here, program flow doesn't seem to reach here */
        GPIO_toggle(Board_LED1);
        /* task2 work here */
    
       }
    }
    /*
     *  ======== main ========
     */
    int main(void)
    {
        //Task_Params taskParams;
        /* Call board init functions */
        Board_initGeneral();
        Board_initGPIO();
    	
        /* Turn on user LED  */
        GPIO_write(Board_LED0, Board_LED_ON);
        GPIO_write(Board_LED1, Board_LED_ON);
        /* Start BIOS */
        BIOS_start();
        return (0);
    }
    

    The .Cfg file is as follows.

    /* ================ Application Specific Instances ================ */
    var semaphore0Params = new Semaphore.Params();
    semaphore0Params.instance.name = "sem1";
    semaphore0Params.mode = Semaphore.Mode_BINARY_PRIORITY;
    Program.global.sem1 = Semaphore.create(null, semaphore0Params);
    var semaphore1Params = new Semaphore.Params();
    semaphore1Params.instance.name = "sem2";
    semaphore1Params.mode = Semaphore.Mode_BINARY_PRIORITY;
    Program.global.sem2 = Semaphore.create(null, semaphore1Params);
    var timer0Params = new Timer.Params();
    timer0Params.instance.name = "timer0";
    timer0Params.period = 5000000;
    timer0Params.startMode = xdc.module("ti.sysbios.interfaces.ITimer").StartMode_AUTO;
    timer0Params.runMode = xdc.module("ti.sysbios.interfaces.ITimer").RunMode_DYNAMIC;
    Program.global.timer0 = Timer.create(2, "&timerFunc", timer0Params);

  • Regarding static variable initialization, see the wikipedia discussion:

        https://en.wikipedia.org/wiki/Static_variable

    Static variables are initialized only once.

  • I think you have one too many zeros in the timer0Params.period setting. It looks like 5 million microseconds which would be 5 seconds rather than .5 seconds.
  • I had edited that 5 million microseconds to 500000 microseconds (i.e. 5 seconds because in 0.5ms can't able to observe any changes)
    Still tsk2Func and tsk1Func is not running??? I don't know why
  • I assumed that the .cfg file snippet you provided didn't include the statements required to create the two tasks your application uses. I should have paid more attention to your comment about not being able to place breakpoints on the task functions.

    I suspect that the two tasks have not been created. You need to have something like this in your .cfg file:

    var Task = xdc.useModule('ti.sysbios.knl.Task');
    
    var taskParams = new Task.Params();
    
    Task.create('&tsk1Func', taskParams);
    Task.create('&tsk2Func', taskParams);

    Alan