Not able to stop Timer after starting on Ti CC3220SF

I am using TI CC3220SF-LaunchXL board with FreeRTOS 202002, Simplelink SDK v2.10.00.04

I start a 10 second timer (using the below mentioned API’s) and after I am finished with my job (involves servicing GPIO interrupts), I try checking if the timer is active or not, but it returns pdFALSE (10seconds have not passed yet), and if I try stopping the timer here, I observe that the device gets hanged.
If I don’t stop this timer the callback gets called after 10seconds.
I have tried increasing the priority of Timer task (configTIMER_TASK_PRIORITY) to max priority (configMAX_PRIORITIES - 1) but same result.

To create and start timer:

volatile TimerHandle_t SensorTimer = NULL;
static SDF_VOID SensorTimer(SDF_VOID)
{
    /* Create Sensor Timer */
    SensorTimer = xTimerCreate("SensorTimer",
                                  (10000 / portTICK_PERIOD_MS),
                                  pdFALSE,
                                  0,
                                  SensorTimerCallback);
    
    if (SensorTimer == NULL)
    {
        IotLogInfo("adiSensorTimer(): Failed to create timer");
    }
    else
    {
        /* Start timer*/
        if (xTimerStart(SensorTimer, 0) == pdPASS)
        {
            IotLogInfo("Created timer");
        }
    }
}

To check if Timer is Active and stop it: (if checked before timer expiry returns pdFALSE)

	/* If Timer is active Stop */
	if( xTimerIsTimerActive( SensorTimer ) )
	{
		if(pdFAIL == xTimerStop( SensorTimer, 0 )){
			IotLogInfo("Failed to Stop Sensor timer");
		}
		else{
			IotLogInfo("Stopped Sensor timer");
		}
	}

Through debugging I have observed pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE; this gets executed after I am done with my job and after trying to stop the timer. What might be the reason for this?

Hi @ShubhamY

We need to check if any operations in the Timer Callback function might cause some issue. Could you please share the callback function code.

Can you confirm if you are using the latest version of FreeRTOS-Kernel V11.1.0
If you are not, can you check if this PR fix addresses your issue?

The likely reason is that you are done with your job before the timer task gets a chance to run and process the tmrCOMMAND_START command. Do you have any other task running at configMAX_PRIORITIES - 1 priority? If yes, can you try reducing their priority?

Are you saying that if you remove if( xTimerIsTimerActive( SensorTimer ) ) and directly stop the timer, the device hangs? If so, please break the code in the debugger and see what the code is doing?

Hi Rahul,

Here is my callback function:

volatile static int timer_state = 0;
static SDF_VOID SensorTimerCallback(TimerHandle_t pxTimer)
{
    IotLogInfo("SensorTimerCallback(): timer expired");
    timer_state = -1; //Error Interrupt state
}

I am using the FreeRTOS kernel version 10.3.0 and it is not possible for me to upgrade this.

Though I still tried merging the changes done in the commit with my codebase and I am able to stop the timer with xTimerStop. But xTimerIsTimerActive still returns pdFALSE.
(Now I am closing the timer without checking xTimerIsTimerActive and the device is not hanging)

Hi Aggarg,

I dont have any other tasks running at configMAX_PRIORITIES - 1 priority. Is interrupt servicing after timer is started causing the priority issue?

Regarding the hang, I observe it when the device is not in debugging mode. When I put in debug mode it works as expected and I don’t observe the hang. pretty strange behavior.

But as mentioned in my previous reply the issue seems to be mostly fixed by merging changes from this PR.

If the interrupt is fired too quickly or you are doing too much in the ISR, that may be the issue.

This is certainly strange and you should try to debug what is causing this. Use the simplest hello world example and see if that works.

Glad that it works for you!