Part Number: AM3359
Other Parts Discussed in Thread: SYSBIOS
Tool/software: TI-RTOS
Hello,
i am currently porting the paho mqtt embedded client to sysbios.
https://github.com/eclipse/paho.mqtt.embedded-c
Code in FreeRTOS:
typedef struct Timer
{
TickType_t xTicksToWait;
TimeOut_t xTimeOut;
} Timer;
void TimerCountdownMS(Timer* timer, unsigned int timeout_ms)
{
timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */
}
void TimerCountdown(Timer* timer, unsigned int timeout)
{
TimerCountdownMS(timer, timeout * 1000);
}
int TimerLeftMS(Timer* timer)
{
xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */
return (timer->xTicksToWait < 0) ?
0 : (timer->xTicksToWait * portTICK_PERIOD_MS);
}
char TimerIsExpired(Timer* timer)
{
return xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait)
== pdTRUE;
}
void TimerInit(Timer* timer)
{
timer->xTicksToWait = 0;
memset(&timer->xTimeOut, '\0', sizeof(timer->xTimeOut));
}
Code in Linux:
typedef struct Timer
{
struct timeval end_time;
} Timer;
void TimerInit(Timer* timer)
{
timer->end_time = (struct timeval){0, 0};
}
char TimerIsExpired(Timer* timer)
{
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->end_time, &now, &res);
return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
}
void TimerCountdownMS(Timer* timer, unsigned int timeout)
{
struct timeval now;
gettimeofday(&now, NULL);
struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
timeradd(&now, &interval, &timer->end_time);
}
void TimerCountdown(Timer* timer, unsigned int timeout)
{
struct timeval now;
gettimeofday(&now, NULL);
struct timeval interval = {timeout, 0};
timeradd(&now, &interval, &timer->end_time);
}
int TimerLeftMS(Timer* timer)
{
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->end_time, &now, &res);
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
}
My Code:
typedef struct Timer
{
timeval end_time;
} Timer;
void TimerInit(Timer* tim)
{
tim->end_time.tv_sec = 0;
tim->end_time.tv_usec = 0;
timespec time_z;
time_z.tv_sec = time(NULL);
clock_settime(CLOCK_REALTIME, &time_z);
clock_start();
System_printf("TimerInit: %u \n", time_z.tv_sec);
}
char TimerIsExpired(Timer* tim)
{
timespec now;
timeval now_val;
timeval res;
clock_gettime(CLOCK_REALTIME, &now);
TIMESPEC_TO_TIMEVAL(&now_val, &now);
timsub(&tim->end_time, &now_val, &res);
System_printf("TimerIsExpired: %u \n", res.tv_sec);
return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
}
void TimerCountdownMS(Timer* tim, unsigned int timeout)
{
timespec now;
timeval now_val;
clock_gettime(CLOCK_REALTIME, &now);
TIMESPEC_TO_TIMEVAL(&now_val, &now);
timeval interval = { timeout / 1000, (timeout % 1000) * 1000 };
timadd(&now_val, &interval, &tim->end_time);
System_printf("TimerCountdownMS: %u \n", &tim->end_time);
}
void TimerCountdown(Timer* tim, unsigned int timeout)
{
timespec now;
timeval now_val;
clock_gettime(CLOCK_REALTIME, &now);
TIMESPEC_TO_TIMEVAL(&now_val, &now);
timeval interval = { timeout, 0 };
timadd(&now_val, &interval, &tim->end_time);
System_printf("TimerCountdown: %u \n", &tim->end_time);
}
int TimerLeftMS(Timer* tim)
{
timespec now;
timeval now_val;
timeval res;
clock_gettime(CLOCK_REALTIME, &now);
TIMESPEC_TO_TIMEVAL(&now_val, &now);
timsub(&tim->end_time, &now_val, &res);
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
System_printf("TimerLeftMS: %u \n", &tim->end_time);
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
}
Is there a simpler equivalent in SYSBIOS?
I can't use gettimeofday because its not in the posix support
I adapted the code with clock_settime because i found this:
http://man7.org/linux/man-pages/man2/gettimeofday.2.html
"POSIX.1-2008 marks gettimeofday() as obsolete,
recommending the use of clock_gettime(2) instead."
But it looks like the clock is not counting but i havent found a start function.
I hope this isn't a complex question and got a simple answer.
Thank,
Marvin