M4OSA_Clock.c revision aa382f3637a68361989d5b70e3184bddcc472d3d
1/* 2 * Copyright (C) 2004-2011 NXP Software 3 * Copyright (C) 2011 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 19/** 20 ************************************************************************ 21 * @file M4OSA_Clock.c 22 * @brief Clock related functions 23 * @note This file implements functions to manipulate clock 24 ************************************************************************ 25*/ 26 27#include <sys/time.h> 28#include <time.h> 29 30#include "M4OSA_Debug.h" 31#include "M4OSA_Clock.h" 32#include "M4OSA_Memory.h" 33#include "M4OSA_Types.h" 34 35 36 37 38/** 39 ************************************************************************ 40 * @brief This function gets an absolute time to an unknown reference with 41 * a high precision. 42 * @note It means it can only be used to get a relative time by computing 43 * differences between to times. 44 * It is to the caller to allocate time. Time is expressed in 45 * timescale unit. 46 * M4OSA_ROLLOVER_CLOCK in M4OSA_Types.h must be configured with the rollover 47 * offset of this function. 48 * @param time: (IN/OUT) time 49 * @param timescale: (IN) The timescale (time unit per second) 50 * @return M4NO_ERROR: there is no error 51 * @return M4ERR_PARAMETER: at least one parameter is NULL 52 * @return M4WAR_TIMESCALE_TOO_BIG: the precision of the system clock is 53 * not 54 * compliant with the input timescale 55 ************************************************************************ 56*/ 57M4OSA_ERR M4OSA_clockGetTime(M4OSA_Time* pTime, M4OSA_UInt32 timescale) 58{ 59 struct timeval tv; 60 struct timezone tz; 61 M4OSA_UInt32 u32_time = 0; 62 M4OSA_UInt32 u32_time_hi; 63 M4OSA_UInt32 u32_time_lo; 64 M4OSA_UInt32 u32_time_lh; 65 M4OSA_UInt32 factor; 66 67 M4OSA_TRACE1_2("M4OSA_clockGetTime\t\tM4OSA_Time* 0x%x\tM4OSA_UInt32 %d", 68 pTime, timescale); 69 70 M4OSA_DEBUG_IF2(M4OSA_NULL == pTime, M4ERR_PARAMETER, 71 "M4OSA_clockGetTime: pTime is M4OSA_NULL"); 72 M4OSA_DEBUG_IF2(0 == timescale, M4ERR_PARAMETER, 73 "M4OSA_clockGetTime: timescale is 0"); 74 75 factor = 1000000 / timescale; 76 77 if(gettimeofday(&tv, &tz) == 0) 78 { 79 u32_time_lo = (tv.tv_sec & 0xFFFF) * timescale; 80 u32_time_hi = (((tv.tv_sec >> 16) & 0xFFFF) * timescale) + ((u32_time_lo >> 16) & 0xFFFF); 81 u32_time_lo &= 0xFFFF; 82 u32_time_lo += tv.tv_usec / factor; 83 u32_time_hi += ((u32_time_lo >> 16) & 0xFFFF); 84 u32_time_lo &= 0xFFFF; 85 u32_time = ((u32_time_hi & 0x7FFF) << 16) | u32_time_lo; 86 } 87 88 /* M4OSA_Time is signed, so we need to check the max value*/ 89 if (u32_time > M4OSA_INT32_MAX) 90 { 91 u32_time = u32_time - M4OSA_INT32_MAX; 92 } 93 94 *pTime = (M4OSA_Time)u32_time; 95 96 if( timescale > 10000 ) 97 { 98 return M4WAR_TIMESCALE_TOO_BIG; 99 } 100 101 return M4NO_ERROR; 102} 103