# Working of SoftwareTImers

**URL:** <https://forums.freertos.org/t/working-of-softwaretimers/9248>\
**Category:** Kernel\
**Created:** [March 16, 2020, 7:49am UTC](https://forums.freertos.org/t/working-of-softwaretimers/9248 "2020-03-16T07:49:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mansoor](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mansoor](https://forums.freertos.org/u/mansoor)\
**Post date:** [March 16, 2020, 7:49am UTC](https://forums.freertos.org/t/working-of-softwaretimers/9248/1 "2020-03-16T07:49:04Z")

</div>

Hi,

I was testing SoftwareTimer in metware SYNOPSYS ARC IDE. I am following the example program given in the FreeRTOS Manuel. which like this…

CASE 1:

void vTimerCallback( TimerHandle\_t xTimer )  
{  
printf(“callback function\n”):  
…….  
…….  
}  
void main( void )  
{  
TimerHandle\_t xTimers = NULL;

```
    xTimers = xTimerCreate( "Timer",10,pdTRUE, ( void * ) 0,vTimerCallback );
    vTaskStartScheduler();

```

}

for this I am able to see the print “callback function” in the Resrult only once.

CASE 2 : if I I created a timer in the Task, like as shown in the following…

void vTimerCallback( TimerHandle\_t xTimer )  
{  
printf(“callback function”):  
…….  
…….  
}  
void main( void )  
{  
TaskHandle\_t xTask1Handle1 = NULL;

```
   xTaskCreate( prvTask1, "Tsk1Work", TASK1_STACK_SIZE, (void*)pcTextForTask1, 12, &xTask1Handle1 );

    vTaskStartScheduler();

```

}

void prvTask1( void \*pvParameters )  
{  
printf("task is created\n);

```
    TimerHandle_t xTimers = NULL;

    xTimers = xTimerCreate( "Timer",10,pdTRUE, ( void * ) 0,vTimerCallback );

```

}

in this example I am able to see the “callback function” prints infinite time until I stop the timer.

Why this type of behaviour?, is it we have to create a timer in the Task to work as Auto Reloud Timers. or if I create that in the main function it is behaving as one shot Timer.

above case2 example is printing infinite times, so as shown in example code of FreeRTOS Manuel I gave the condition like count is 30 then timer should stop. after writing this condition the case 2 also printing only once.

please tell the reason behind this or else any mistakes I am doing to test the Timers…

in the freeRTOSConfig.h I have defined Like this

#define configTICK\_RATE\_HZ ( ( TickType\_t ) 1000 )  
#define configUSE\_TIMERS 1  
#define INCLUDE\_xTimerPendFunctionCall 1  
#define configTIMER\_TASK\_STACK\_DEPTH 104  
#define configTIMER\_QUEUE\_LENGTH 10  
#define configTIMER\_TASK\_PRIORITY 10

Thanks and Regards…  
Mansoor Basha

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [March 16, 2020, 10:47am UTC](https://forums.freertos.org/t/working-of-softwaretimers/9248/2 "2020-03-16T10:47:52Z")

</div>

Be careful about defining variables in the main function with FreeRTOS. Some of the ports will reuse the main stack for the ISR stack, and variables in it are overwritten by the ISRs. This is possibly your issue here.

Also, using printf in the timer callback is not advised, as timer callback functions are run in the context of the timer task, and are not supposed to block, which printf might do, or because printf can use a lot of stack, you might be overflowing the timer task stack.
