00001 /** @file 00002 * @brief Utility functions for time manipulation */ 00003 #ifndef __TIME_FUNCS_H_ 00004 #define __TIME_FUNCS_H_ 00005 00006 /* Author: Ratish J. Punnoose, 2006 00007 * This file is part of TiROS, the Tickless Real-Time Operating System. 00008 * Copyright(c) 2006, 2007: Ratish J. Punnoose. 00009 * Copyright(c) 2006 Sandia Corporation. Under the terms of Contract 00010 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains 00011 * certain rights in this software. 00012 * 00013 * TiROS is free software; you can redistribute it and/or modify it under 00014 * the terms of the GNU General Public License as published by the Free 00015 * Software Foundation; either version 2 or (at your option) any later version. 00016 * 00017 * TiROS is distributed in the hope that it will be useful, but WITHOUT ANY 00018 * WARRANTY; without even the implied warranty of MERCHANTABILITY or 00019 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00020 * for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License along 00023 * with TiROS; if not, write to the Free Software Foundation, Inc., 00024 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00025 * 00026 * As a special exception, if other files instantiate templates or use macros 00027 * or inline functions from this file, or you compile this file and link it 00028 * with other works to produce a work based on this file, this file does not 00029 * by itself cause the resulting work to be covered by the GNU General Public 00030 * License. However the source code for this file must still be made available 00031 * in accordance with section (3) of the GNU General Public License. 00032 * 00033 * This exception does not invalidate any other reasons why a work based on 00034 * this file might be covered by the GNU General Public License. 00035 * 00036 */ 00037 00038 00039 00040 00041 #ifndef subtime_t 00042 #error "Don't include tr_time.h directly. Include tiros.h or tr_port.h" 00043 #endif 00044 00045 /** @defgroup os_time_utils Utility functions for time operations 00046 * @{ */ 00047 00048 /** Time structure 00049 * 00050 * Different hardware architectures may represent time in formats that 00051 * are most efficient and suitable for that hardware. We impose no 00052 * requirements except for two functions that can convert this hardware 00053 * dependent time structure into units of seconds and vice-versa 00054 * The trtime_t structure consists of two elements: 00055 * <OL> 00056 * <LI> A subsecond unit (could be milli-seconds, nano-seconds,etc.) </LI> 00057 * <LI> A supersecond unit ( could be a unit of 1 second, 10 secs, 1 00058 * minute etc. </LI> 00059 * </OL> 00060 * This choice of time is kept deliberately vague, since the 00061 * application is an embedded system, where these functions have to be 00062 * fast, take advantage of the hardware, and cannot waste cycles 00063 * converting time units from the hardware representation to a 00064 * standard form. 00065 */ 00066 typedef struct trtime { 00067 uint32_t units; /**< Time in secs preferably (but 00068 * no requirement) */ 00069 subtime_t subunits; /**< Sub-seconds: hardware dependent */ 00070 } trtime_t; 00071 00072 00073 #ifndef LT_INLINE 00074 #define LT_INLINE 00075 #endif 00076 00077 /** Convert time represented in trtime to seconds 00078 * @param [in] lt Pointer to trtime structure. 00079 * @return Equivalent time in seconds */ 00080 LT_INLINE uint32_t trtime_to_secs(const trtime_t * const lt); 00081 00082 /** Convert time represented in seconds to trtime 00083 * @param seconds Time in seconds to be converted. 00084 * @param [out] lt Pointer to trtime structure. */ 00085 LT_INLINE void secs_to_trtime(uint32_t seconds, trtime_t *lt); 00086 00087 00088 00089 /** Time subtraction 00090 * 00091 * Note: Time1 >= Time2, check using time_compare if needed. 00092 * @param x Time1 00093 * @param y Time2 00094 * @param [out] res Time1 - Time 2 */ 00095 LT_INLINE void time_sub(const trtime_t * x, 00096 const trtime_t * y, trtime_t *res); 00097 00098 /** Time addition 00099 * @param x Time1 00100 * @param y Time2 00101 * @param [out] res Time1 + Time 2 */ 00102 LT_INLINE void time_add(const trtime_t * x, const trtime_t * y, trtime_t *res); 00103 00104 /** Time comparison 00105 * @param t1 Time1 00106 * @param t2 Time2 00107 * @Return (-1,0,1) depending on whether t1 < t2, t1==t2, t1 > t2 */ 00108 LT_INLINE int time_compare(const trtime_t * t1, const trtime_t * t2); 00109 00110 00111 /** Time less than 00112 * @param t1 Time1 00113 * @param t2 Time2 00114 * @Return (1,0) depending on whether t1 < t2. This can easily be 00115 * implemented using time_compare but is provided as an optimization. */ 00116 LT_INLINE int time_lessthan(const trtime_t * t1, const trtime_t * t2); 00117 00118 00119 /** @} End utility time functions module */ 00120 00121 00122 #endif