Invoking timers after certain seconds

saadsaeed wrote on Wednesday, September 19, 2018:

Hello,
I want to invoke my timer after exactly 2 sec
Folloing is the configuration in freeRTOSConfig.h

#define configTICK_RATE_HZ                      ((TickType_t)100)

and following is the time in ticks I am passing to timer

const TickType_t xTicksToWait = pdMS_TO_TICKS(2000);

But, what I am observing its invoing very fast. Way before 2 sec.
Help would be appricaited. Thankyou,

rtel wrote on Thursday, September 20, 2018:

Sorry for the apparent delay in replying - for some reason my replies via email are not showing up at the moment. My original replay suggested checking that the tick interrupt is executing at the speed you think it is. That can be done very crudely by writing a task that does nothing but toggle an LED every five seconds, then time (using a stopwatch) ten toggles to check it is approximately 50 seconds:

void vTask( void *pvParameters )
{
    for( ;; )
    {
        vTaskDelay( pdMS_TO_TICKS( 5000UL );
        toggle_led(); /* You will have to write this. */
    }
}

saadsaeed wrote on Thursday, September 20, 2018:

For what I am observing,
If i use

const TickType_t xTicksToWait = pdMS_TO_TICKS(2000);

The timer startsafter every 400ms. To do this I am multiplying it with my desired time with 5 and now it opens after every 2 seconds. Is this has to do something with clock frequency.

rtel wrote on Thursday, September 20, 2018:

Almost certainly the tick frequency is wrong.

saadsaeed wrote on Thursday, September 20, 2018:

From, where do you change this tick frequency.?

saadsaeed wrote on Friday, October 12, 2018:

Hey here is my code

/************************************************************************
* DESCRIPTION: Main entry point of the FreeRTOS shell demo.
************************************************************************/
int ulCallCount=0;
#define mainAUTO_RELOAD_TIMER_PERIOD pdMS_TO_TICKS(2000)
static void prvAutoReloadTimerCallback( TimerHandle_t xTimer )
{
TickType_t xTimeNow;
/* Obtain the current tick count. */
xTimeNow = xTaskGetTickCount();
/* Output a string to show the time at which the callback was executed. */
print( "Auto-reload timer callback executing %d \n", xTimeNow );
ulCallCount++;
}
void fapp_main_freertos( void ) 
{
	if(xTaskCreate(
	            fapp_task,  /* pointer to the task */
	            "fapp_Task", /* task name for kernel awareness debugging */
	            FAPP_CFG_FREERTOS_TASK_STACK_SIZE/sizeof(portSTACK_TYPE), /* task stack size */
	            NULL, /* optional task startup argument */
	           3,  /* initial priority */
	            NULL /* optional task handle to create */
	        ) != pdPASS)
	    {
	        fnet_println("[FREERTOS] Failed to create task fapp_task."); /* Probably out of memory */
	    }


	    else
	    {
	        /* Attempt to create the event group. */
	        fapp_freertos_event_group = xEventGroupCreate();

	        /* Was the event group created successfully? */
	        if( fapp_freertos_event_group == NULL )
	        {
	            /* The event group was not created because there was insufficient heap available. */
	            fnet_println("[FREERTOS] Failed to create event group.");
	        }
	        else
	        {
	            /* Registers the "socket layer activity" event handler.*/
	            fnet_socket_set_callback_on_rx(fapp_socket_rx_callback);


	            fnet_println("[FREERTOS] Starting scheduler.");
	        	TimerHandle_t xAutoReloadTimer;
	        	BaseType_t xTimer1Started;
	        	/* Create the auto-reload timer, storing the handle to the created timer in xAutoReloadTimer. */
	        	xAutoReloadTimer = xTimerCreate(
	        	/* Text name for the software timer - not used by FreeRTOS. */
	        	"AutoReload",
	        	/* The software timer's period in ticks. */
				mainAUTO_RELOAD_TIMER_PERIOD,
	        	/* Setting uxAutoRealod to pdTRUE creates an auto-reload timer. */
	        	pdTRUE,
	        	/* This example does not use the timer id. */
	        	0,
	        	/* The callback function to be used by the software timer being created. */
	        	prvAutoReloadTimerCallback );
	        	if(( xAutoReloadTimer != NULL ))
	        	{
	        	/* Start the software timers, using a block time of 0 (no block time). The scheduler has
	        	not been started yet so any block time specified here would be ignored anyway. */
	        	xTimer1Started = xTimerStart( xAutoReloadTimer, 0 );
	        	if(( xTimer1Started == pdPASS ))
	        	{

	                fnet_println("[FREERTOS] Starting scheduler.");
	                vTaskStartScheduler();
	        	}
	        	}
	            /* The code should never reach here. */

	    }

	    }



            /* The code should never reach here. */
}
#endif /* FAPP_CFG_FREERTOS */

As, you can see. I am creating an autorelaod timer with 2000ms delay. But instead its running after 400 ms . The config tick frequeny is set to 200. increasing tick frequency is not helping me. Please tellme whats wrong. Thanks

saadsaeed wrote on Friday, October 12, 2018:

I have changed the macro to

//PDMS to TICKS MACRO
#ifndef pdMS_TO_TICKS
#define pdMS_TO_TICKS( xTimeInMs ) ( (( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ  * ( TickType_t )5)  / ( TickType_t )1000)) )
#endif

saadsaeed wrote on Friday, October 12, 2018:

After that its working fine

rtel wrote on Friday, October 12, 2018:

Not sure I’m following, but it looks like you fixed the symptom rather
than the cause, so the cause will still exist. As per previous posts,
it looks like time is not passing at the frequency you think it is, and
you have changed how time is converted between ms and ticks to account
for that, rather than fix the time reading itself.

saadsaeed wrote on Friday, October 12, 2018:

Yes, thats true. Can you tell me whats the cause behind it. I cant understand the CAUSE The board is frdm k64

rtel wrote on Friday, October 12, 2018:

You need to be 100% sure of the frequency of the clock that is feeding
the SysTick timer on your MCU, then set the constant configCPU_CLOCK_HZ
to that frequency in FreeRTOSConfig.h - not the frequency that feeds the
timer may be divided down from the frequency at which the rest of the
core executes.

You can then test very crudely by having a single task that does nothing
but toggle an LED every second, and time 60 toggles using a stopwatch.
If the timing is correct, then the stop watch you say one minute at the
end - plus or minus whatever your reaction time is hitting the stopwatch
button. Just something like this is fine if there is only one task:

void vTest( void *pvParameters )
{
/* Using unmodified pdMS_TO_TICKS() macro* /
const TickType_t xPeriod = pdMS_TO_TICKS( 1000 );

     for( ;; )
     {
         vTaskDelay( xPeriod );
         ToggleLED(); /* write this yourself */
     }
}