Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

The Tick_Task

Posted by Bill Yang on April 23, 2009
Hi,

I am implementing 802.1d Spanning Tree algorithm on LM3S6965 Cortex-M3 micro. There is a section pseudo code from the algorithm to implement a timer as below. I wonder if the tick() routine can be replaced by a FreeRTOS Task.

typedef struct {
bool active;
time value;
} Timer;
Timer hello_timer;

tick()
{
if(hello_timer_expired())
{
hello_timer_reset();
}
}
/* where */
hello_timer_expired()
{
if(hello_timer_active && (++hello_timer.value >= 100))
{
hello_timer.active = false;
return true;
}
return false;
}
hello_timer_reset()
{
/* TO DO some jobs here */

start_hello_timer();
}
start_hello_timer()
{
hello_timer.value = 0;
hello_timer.active = true;
}

/* the below Tick_Task() is similar to vuIP_Task() from uIP.c, I want it to replace the tick() function, if is possible?*/
struct timer {
clock_time_t start;
clock_time_t interval;
};
timer hello_timer;
void timer_set(struct timer *t, clock_time_t interval)
{
t->interval = interval;
t->start = clock_time();
}
clock_time_t clock_time( void )
{
return xTaskGetTickCount();
}
timer_reset(struct timer *t)
{
t->start += t->interval;
}
timer_expired(struct timer *t)
{
return (clock_time_t)(clock_time() - t->start) >= (clock_time_t)t->interval;
}
Tick_Task()
{
vSemaphoreCreateBinary( xHelloSemaphore );
timer_set(&hello_timer, configTICK_RATE_HZ / 2); /* configTICK_RATE_HZ = 200, so the timer is set to count of 100 ticks*/

for(;;)
{
if(timer_expired(&hello_timer)
{
timer_reset(&hello_timer)
/* TO DO some jobs here */
}
else
{
xSemaphoreTake( xHelloSemaphore, configTICK_RATE_HZ / 2 );
/*Note: used xHelloSemaphore to block here,
it will wake up every 100 tick-count. */
}
}
}
/*Question: since the hello_timer is also set to expire in count of 100 ticks, does any confliction or inaccurate tick count happen with the xHelloSemaphore?*/

I wonder if some one has any suggestion to do better job than above code.

Regards,

RE: The Tick_Task

Posted by Dave on April 24, 2009
I cannot really read the code because of the crappy formatting in this forum, but take a look at the uIP examples. These block on a semaphore with a timeout. If an Ethernet event occurs it unblocks the task using the semaphore. Timers expiring are implemented simply by allowing the block time to expire and so causing the task to unblock.

An alternative would be to use a tick hook to implement a time, with the tick hook giving the semaphore to unblock the task each time the timer expired. The same semaphore can be used to handle both Ethernet and timer events.

RE: The Tick_Task

Posted by Bill Yang on April 24, 2009
Thanks Dave,

I got your idea that suggested me to use the void vApplicationTickHook( void ) with the semaphore to handle and unblock a task.

Question: Does the ApplicationTickHook() use the same count of tick as it is defined by configTICK_RATE_HZ?

My understood that means the tick hook interrupts every 1/200 second because the configTICK_RATE_HZ is 200. Am I correct?

Regards,
Bill

RE: The Tick_Task

Posted by Dave on April 24, 2009
configTICK_RATE_HZ sets the frequency of the tick so yes, if configTICK_RATE_HZ is 200 then the tick will occur 200 times a second.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS