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_sleep() non-deterministic delay behaviour

Other Parts Discussed in Thread: AM3359

Hi folks,

In my code I can Task_sleep() to block for 450 microseconds, but my Task delay appears to hover between ~398 microseconds and ~501 microseconds. If I change the delay to, say, ~X microseconds, the delay hovers in about the same range. I guess Task_sleep() blocks/works relative to the time at which it is called.

I also tried using a for loop, as well Clock_getTicks() to keep track, but these two methods jitter more than with Task_sleep(). Unless I'm using Clock_getTicks() wrong?

Is there a way to make my delay more accurate and decrease this jitter? Any other delay functions that work?

Thanks

  • What Clock.tickPeriod have you configured for your application?

    A Task_sleep(1) timeout can be from 0 to Clock.tickPeriod microseconds, depending on when Task_sleep(1) is called relative to the arrival of the next Clock tick interrupt.

    For a more controlled timeout, you could create a Timer in one-shot mode and for a specific period.
    The Timer would then begin when you called Timer_start() and the interrupt routine would be called when the timer period expired.
    The Timer ISR could post a semaphore that the task blocked on after calling Timer_start().

    Alan

  • Alan,
    Is there an example that can help me on how to set this up in the AM3359 processor?
    Thanks
  • Try creating a one shot Timer instance with the timeout you want.

    Have the Timer function post a semaphore that the task pends on immediately after starting the one-shot timer.

    This should give more accurate results:

    #include <ti/sysbios/hal/Timer.h>
    
    Timer_Handle tHandle;
    Semaphore_Handle sem;
    
    main()
    {
        Timer_Params timerParams;
    
        /* create a one-shot timer with 450us timeout */
        Timer_Params_init(&timerParams);
        timerParams.runMode = Timer_RunMode_ONESHOT;
        timerParams.periodType = Timer_PeriodType_MICROSECS;
        timerParams.startMode = Timer_StartMode_USER;
        timerParams.period = 450;
        tHandle = Timer_create(-1, myTimerFunc, &timerParams, NULL);
    
        /* create a counting semaphore with initial count of zero */
        sem = Semaphore_create(0, NULL, NULL);
    }
    
    myTimerFunc(UArg arg)
    {
        Semaphore_post(sem);
    }
    
    task(UArh arg0, UArg arg1)
    {
        Timer_start(tHandle);
    
        Semaphore_pend(sem, BIOS_WAIT_FOREVER);
    
        /* should arrive here fairly close to 450us after Timer_start() call */
    }
    

    Alan

  • This looks great. I was just wondering if my other Tasks currently running can still affect myTimerFunc(). I am going to test it and let you know how it goes.
  • HI,

    I am using cc2650 controller, and i want to generate 200 microsecond delay in clock interrupt, i used CPU_delay() it worked fine but the Bluetooth functions are getting disconnected, and further code is not working,

    can any one suggest any alternative method to generate delay.

    Thanks,

    Madhusudan

  • Hi Madhusudan, 
    We generally discourage posting a new question to an old closed thread because the person who answered before may no longer be available, and also it will allow whomever is currently assigned to monitor the forum to respond to you more quickly. For these reasons, I suggest you start a new thread with your question and reference this thread.

    Thank you