yyang2000 wrote on Thursday, 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,