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.

CC2652R7: Timer CallBack

Part Number: CC2652R7
Other Parts Discussed in Thread: SYSCONFIG

Hello,

So I've been trying to stop and start a non-blocking oneshot callback timer. I've noticed that whenever I've tried to start the timer back up it will start where it left off instead of starting from the last time it was started. In the code I've provided below shows me trying to start the timer put it to sleep for 500ms stop it, close it, open it and start it back up. However after 5ish seconds after the code is run I get the printf statement in the callback rather than 5 seconds after the last start of the timer. I am I doing something wrong and if not what is the most effective work around?

void timerCallback(Timer_Handle handle, int_fast16_t status)
{
     printf("Timer CB\r\n");
}

void test()

{
Timer_Params _timer_params;
Timer_Params_init(&_timer_params);
_timer_params.periodUnits = Timer_PERIOD_US;
Timer_Handle _timer = NULL;

_timer_params.period = 5000000;
_timer_params.timerCallback = timerCallback;
_timer_params.timerMode = Timer_ONESHOT_CALLBACK;

_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);


Timer_start(_timer);
usleep(500000);
printf("usleep1\r\n");

Timer_stop(_timer);
Timer_close(_timer);
_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);
Timer_start(_timer);
usleep(500000);
printf("usleep2\r\n");

Timer_stop(_timer);
Timer_close(_timer);
_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);
Timer_start(_timer);
usleep(500000);
printf("usleep3\r\n");

Timer_stop(_timer);
Timer_close(_timer);
_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);
Timer_start(_timer);
usleep(500000);
printf("usleep4\r\n");

Timer_stop(_timer);
Timer_close(_timer);
_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);
Timer_start(_timer);
usleep(500000);
printf("usleep5\r\n");

Timer_stop(_timer);
Timer_close(_timer);
_timer = Timer_open(CONFIG_TIMER_3, &_timer_params);
Timer_start(_timer);
printf("timer go\r\n");

sleep(60);

Timer_close(_timer);

}

  • Hello Kenneth Thomas,

    I hope you are well. I took the base timerled example and I was able to use ONESHOT_CALLBACK in a similar fashion, more specifically I used a basic while(1) loop to only call timer_start to keep the timer going (led keeps blinking, like in CONTINOUS_CALLBACK). Why are you trying to use Timer_stop/close when you wanted to "resume" the clock? Could you try removing the timer_stop/close and see how that changes what you are seeing? Additionally, could you provide the SDK you are using, and the example you have based this on. 

    However after 5ish seconds after the code is run I get the printf statement in the callback rather than 5 seconds after the last start of the timer

    So to confirm, what you see currently is a printf statement showing the "Timer is CB" instead of the expected "usleep1" message? And you do not see any other messages (does the program continue after this)?

    Thanks,
    Alex F 

  • No I'm seeing all the usleep messages however the Timer CB occurs after 1 second after the last Timer_start not 5 seconds after it. In essence I'm trying to count a 5 second period after the last subsequent button was pressed. So to achieve it I first stopped and started the timer back up again every time a button was pressed. I noticed that there was a difference in the interval of time after the last button was press when I pressed it 1-2 times versus 4-6 times where it should've been a consistent 5 seconds it seemed that it was counting from the first button press and not the last. Also the SDK is simplelink_cc13xx_cc26xx_sdk_7_10_01_24.

  • Hello Kenneth Thomas,

    I wanted to ask if in your application you need to enter a low power mode, which could be the reason why you want to stop then close the timer, rather than just starting it again. Included is a small snippet of code I put together which reads the state of the BTN before starting the timer, and also toggles the green LED after 5 seconds (if you press btn1 once a second, or hold it); is this somewhat similar to what you are trying to accomplish? In your program how is the BTN handled/related to your timer specifically (include a code snippet)? If you also happen to have a logic analyzer on hand you could add GPIO highs in the callback to read the specific timing interval.    

        while(1){
    
        if(GPIO_read(CONFIG_GPIO_BTN1) == 0)
        {
        Timer_start(timer0);
        }
        else
            counter1++;
        }
        return (NULL);
    }
    
    /*
     * This callback is called every 1,000,000 microseconds, or 1 second. Because
     * the LED is toggled each time this function is called, the LED will blink at
     * a rate of once every 2 seconds.
     */
    void timerCallback(Timer_Handle myHandle, int_fast16_t status)
    {
        GPIO_toggle(CONFIG_GPIO_LED_0);
        globalCount++;
        if (globalCount > 4)
        {
            globalCount = 0;
            GPIO_toggle(CONFIG_GPIO_LED_1);
        }
    }

    Thanks,
    Alex F 

  • Hey Alex,

    So I was able to get the button to work by using two semaphores and a global counter (got the idea from this example). But I still would like to know the functionality of the timer more specifically whether or not the timer resets its count after closing and reopening it. From my testing from the initial post, it seems as though the count is still maintained and it continues where it left off until it reaches the timeout. 

  • Hello Kenneth Thomas,

    When using Timer_close it follows: Timer_close --> GPTimerCC26XX_close --> GPTimerCC26XX_resetHw (which should reset the timer/count).

    Look into the files in the sdk: TimerCC26XX.c and GPTimerCC26XX.c, for more detailed info about these functions (there is also a GPTimer checkbox in .sysconfig). 

    You can see if the timer is updating/resetting via GPT# (in my case GPT0) in registers. You should also try to stick with one timer functions (GPTimer, or Timer). When using the timer functions, even if there is a value leftover from the previous Timer_open, then the new Timer_open will overwrite the count. 

    Thanks,
    Alex F

  • Hey Alex,

    Thanks again for your help.

    Kenneth T