xTimerStart hangs the application

freertos99 wrote on Tuesday, January 22, 2019:

Hi Experts,
I have an application run on freertosv10.1 which creates five timers using xTimerCreate() with different timeout periods. After expiry of each timer the callbacks print a message and again start the timer. Four timers are working perfectly but 5th timer calls the callback once after expiry and hangs after the start of the timer. I’ve run out of ideas on how to debug this issue. Can anyone please help ?

#define PERIOD_MS ( 1000UL / portTICK_RATE_MS)

static TimerHandle_t xApp[10] = {NULL};
void timer_start(int p)
{
    switch(p)
    {
    case 1:
    xTimerStart(xApp[1], 0);
    break;
    case 2:
    xTimerStart(xApp[2], 0);
    break;
    case 3:
    xTimerStart(xApp[3], 0);
    break;
    case 4:
    xTimerStart(xApp[4], 0);
    break;
    case 5:
    xTimerStart(xApp[5], 0);
    break;
    default:
    break;
    }
}
static void TimerCallback( TimerHandle_t xTimer )
{
	int i=0;
	i = ( int ) pvTimerGetTimerID( xTimer );
	printf("Inside timer callback\r\n");
    timer_start(i);
}

void init_timer(void)
{
int i;
//create timers
for(i=1;i<6;i++)
{
xApp[i] = xTimerCreate("AppTimer", (PERIOD_MS+ (100*i)), pdFALSE, (void*) i, TimerCallback);
}
//start timers
for(i=1;i<6;i++)
timer_start(i);

}

FreeRTOSConfig.h
#define configUSE_TIMERS                                                1
#define configTIMER_TASK_PRIORITY                               ( 3 )
#define configTIMER_QUEUE_LENGTH                                10
#define configTIMER_TASK_STACK_DEPTH                    ( configMINIMAL_STACK_SIZE  )

richard_damon wrote on Tuesday, January 22, 2019:

One possible issue is that timer callbacks aren’t supposed to block, and depending on how printf is implemented, it might not be suitable for use in a timer callback.

If you use a debugger to stop the system, (and have configASSERT defined) does it show you caught on an assert, and if so which one?

freertos99 wrote on Wednesday, January 23, 2019:

Thanks for pointer.
You are right… the problem was due to printf’s in callback routine. Now I can see the timer getting started. Maybe we can document that timer callbacks should not be blocked (as a NOTE) or maybe this is basic thing which I’m not aware of :frowning:
Thank you.

xz8987f wrote on Wednesday, January 23, 2019:

See https://www.freertos.org/RTOS-software-timer.html:
"Important information on writing timer callback functions
Timer callback functions execute in the context of the timer service task. It is therefore essential that timer callback functions never attempt to block. For example, a timer callback function must not call vTaskDelay(), vTaskDelayUntil(), or specify a non zero block time when accessing a queue or a semaphore. "

freertos99 wrote on Thursday, January 24, 2019:

thank you