osal.c
Go to the documentation of this file.
00001 /******************************************************************************
00002  *                *          ***                    ***
00003  *              ***          ***                    ***
00004  * ***  ****  **********     ***        *****       ***  ****          *****
00005  * *********  **********     ***      *********     ************     *********
00006  * ****         ***          ***              ***   ***       ****   ***
00007  * ***          ***  ******  ***      ***********   ***        ****   *****
00008  * ***          ***  ******  ***    *************   ***        ****      *****
00009  * ***          ****         ****   ***       ***   ***       ****          ***
00010  * ***           *******      ***** **************  *************    *********
00011  * ***             *****        ***   *******   **  **  ******         *****
00012  *                           t h e  r e a l t i m e  t a r g e t  e x p e r t s
00013  *
00014  * http://www.rt-labs.com
00015  * Copyright (C) 2009. rt-labs AB, Sweden. All rights reserved.
00016  *------------------------------------------------------------------------------
00017  * $Id: osal.c 416 2013-01-08 21:54:25Z smf.arthur $
00018  *------------------------------------------------------------------------------
00019  */
00020 
00021 #include <time.h>
00022 #include <sys/time.h>
00023 #include <unistd.h>
00024 #include <ethercat_soem/osal.h>
00025 
00026 #define USECS_PER_SEC     1000000
00027 
00028 int osal_usleep (uint32 usec)
00029 {
00030    struct timespec ts;
00031    ts.tv_sec = usec / USECS_PER_SEC;
00032    ts.tv_nsec = (usec % USECS_PER_SEC) * 1000;
00033    /* usleep is depricated, use nanosleep instead */
00034    return nanosleep(&ts, NULL);
00035 }
00036 
00037 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
00038 {
00039    struct timespec ts;
00040    int return_value;
00041    
00042    /* Use clock_gettime to prevent possible live-lock.
00043     * Gettimeofday uses CLOCK_REALTIME that can get NTP timeadjust.
00044     * If this function preempts timeadjust and it uses vpage it live-locks.
00045     * Also when using XENOMAI, only clock_gettime is RT safe */
00046    return_value = clock_gettime (CLOCK_MONOTONIC, &ts), 0;
00047    tv->tv_sec = ts.tv_sec;
00048    tv->tv_usec = ts.tv_nsec / 1000;
00049    return return_value;
00050 }
00051 
00052 ec_timet osal_current_time (void)
00053 {
00054    struct timeval current_time;
00055    ec_timet return_value;
00056 
00057    osal_gettimeofday (&current_time, 0);
00058    return_value.sec = current_time.tv_sec;
00059    return_value.usec = current_time.tv_usec;
00060    return return_value;
00061 }
00062 
00063 void osal_timer_start (osal_timert * self, uint32 timeout_usec)
00064 {
00065    struct timeval start_time;
00066    struct timeval timeout;
00067    struct timeval stop_time;
00068 
00069    osal_gettimeofday (&start_time, 0);
00070    timeout.tv_sec = timeout_usec / USECS_PER_SEC;
00071    timeout.tv_usec = timeout_usec % USECS_PER_SEC;
00072    timeradd (&start_time, &timeout, &stop_time);
00073 
00074    self->stop_time.sec = stop_time.tv_sec;
00075    self->stop_time.usec = stop_time.tv_usec;
00076 }
00077 
00078 boolean osal_timer_is_expired (const osal_timert * self)
00079 {
00080    struct timeval current_time;
00081    struct timeval stop_time;
00082    int is_not_yet_expired;
00083 
00084    osal_gettimeofday (&current_time, 0);
00085    stop_time.tv_sec = self->stop_time.sec;
00086    stop_time.tv_usec = self->stop_time.usec;
00087    is_not_yet_expired = timercmp (&current_time, &stop_time, <);
00088 
00089    return is_not_yet_expired == FALSE;
00090 }


ethercat_soem
Author(s): Arthur Ketels, M.J.G. van de Molengraft
autogenerated on Wed Aug 26 2015 11:32:40