AB&T / SOEM

Dependents:   EasyCAT_LAB_simple EasyCAT_LAB_very_simple EasyCAT_LAB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers osal.cpp Source File

osal.cpp

00001 /*
00002  * Licensed under the GNU General Public License version 2 with exceptions. See
00003  * LICENSE file in the project root for full license information
00004  */
00005 
00006 #include "mbed.h"
00007 
00008 #include <osal.h>
00009 #include <config.h>
00010 
00011 
00012 uint64_t tick = 0;
00013 uint64_t last_tick = 0;
00014 
00015 
00016 #define  timercmp(a, b, CMP)                                \
00017   (((a)->tv_sec == (b)->tv_sec) ?                           \
00018    ((a)->tv_usec CMP (b)->tv_usec) :                        \
00019    ((a)->tv_sec CMP (b)->tv_sec))
00020 
00021 
00022 #define  timeradd(a, b, result)                             \
00023   do{                                                       \
00024     (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;           \
00025     (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;        \
00026     if ((result)->tv_usec >= 1000000)                       \
00027     {                                                       \
00028        ++(result)->tv_sec;                                  \
00029        (result)->tv_usec -= 1000000;                        \
00030     }                                                       \
00031   } while (0)
00032 #define  timersub(a, b, result)                             \
00033   do {                                                      \
00034     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;           \
00035     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;        \
00036     if ((result)->tv_usec < 0) {                            \
00037       --(result)->tv_sec;                                   \
00038       (result)->tv_usec += 1000000;                         \
00039     }                                                       \
00040   } while (0)
00041 
00042 
00043 #define CFG_TICKS_PER_SECOND 1000000            
00044 #define USECS_PER_SEC   1000000
00045 #define USECS_PER_TICK  (USECS_PER_SEC / CFG_TICKS_PER_SECOND)
00046 
00047 
00048 
00049 int gettimeofday_(struct timeval *tp, void *tzp)
00050 {
00051     //tick_t tick = tick_get();     
00052     uint32_t actual_tick = us_ticker_read();
00053     tick += (actual_tick - last_tick);
00054     last_tick = actual_tick;
00055     
00056     //tick_t ticks_left;           
00057     uint64_t ticks_left;
00058   
00059     tp->tv_sec = tick / CFG_TICKS_PER_SECOND;
00060     ticks_left = tick % CFG_TICKS_PER_SECOND;
00061     tp->tv_usec = ticks_left * USECS_PER_TICK;
00062 
00063     return 0;
00064 }
00065 
00066 int osal_usleep (uint32 usec)
00067 {
00068     wait_us(usec); 
00069     return 0;
00070 }
00071 
00072 
00073 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
00074 {
00075     return gettimeofday_(tv, tz);
00076 }
00077 
00078 
00079 ec_timet osal_current_time (void)
00080 {
00081     struct timeval current_time;
00082     ec_timet return_value;
00083 
00084     gettimeofday_ (&current_time, 0);
00085     return_value.sec = current_time.tv_sec;
00086     return_value.usec = current_time.tv_usec;
00087     return return_value;
00088 }
00089 
00090 void osal_timer_start (osal_timert * self, uint32 timeout_usec)
00091 {
00092     struct timeval start_time;
00093     struct timeval timeout;
00094     struct timeval stop_time;
00095 
00096     gettimeofday_ (&start_time, 0);
00097     timeout.tv_sec = timeout_usec / USECS_PER_SEC;
00098     timeout.tv_usec = timeout_usec % USECS_PER_SEC;
00099     timeradd (&start_time, &timeout, &stop_time);
00100 
00101     self->stop_time.sec = stop_time.tv_sec;
00102     self->stop_time.usec = stop_time.tv_usec;
00103 }
00104 
00105 boolean osal_timer_is_expired (osal_timert * self)
00106 {
00107     struct timeval current_time;
00108     struct timeval stop_time;
00109     int is_not_yet_expired;
00110 
00111     gettimeofday_ (&current_time, 0);
00112     stop_time.tv_sec = self->stop_time.sec;
00113     stop_time.tv_usec = self->stop_time.usec;
00114     is_not_yet_expired = timercmp (&current_time, &stop_time, <);
00115 
00116     return is_not_yet_expired == false;
00117 }