diff --git a/include/sys/time.h b/include/sys/time.h index 2e1821b46d7..688528a63b6 100644 --- a/include/sys/time.h +++ b/include/sys/time.h @@ -44,6 +44,7 @@ #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -138,6 +139,8 @@ * Public Type Definitions ****************************************************************************/ +typedef clock_t hrtime_t; + /* struct timeval represents time as seconds plus microseconds */ struct timeval @@ -383,6 +386,19 @@ int setitimer(int which, FAR const struct itimerval *value, int utimes(FAR const char *path, const struct timeval times[2]); +/**************************************************************************** + * Name: gethrtime + * + * Description: + * Get the current time + * + * Returned Value: + * The current value of the system time in ns + * + ****************************************************************************/ + +hrtime_t gethrtime(void); + #undef EXTERN #if defined(__cplusplus) } diff --git a/libs/libc/time/Make.defs b/libs/libc/time/Make.defs index f18b50ecf54..8fa1099d2a0 100644 --- a/libs/libc/time/Make.defs +++ b/libs/libc/time/Make.defs @@ -40,6 +40,7 @@ CSRCS += lib_strftime.c lib_calendar2utc.c lib_daysbeforemonth.c CSRCS += lib_gettimeofday.c lib_isleapyear.c lib_settimeofday.c lib_time.c CSRCS += lib_timespec_get.c lib_nanosleep.c lib_difftime.c lib_dayofweek.c CSRCS += lib_asctime.c lib_asctimer.c lib_ctime.c lib_ctimer.c +CSRCS += lib_gethrtime.c ifdef CONFIG_LIBC_LOCALTIME CSRCS += lib_localtime.c diff --git a/libs/libc/time/lib_gethrtime.c b/libs/libc/time/lib_gethrtime.c new file mode 100644 index 00000000000..f620ee61c23 --- /dev/null +++ b/libs/libc/time/lib_gethrtime.c @@ -0,0 +1,56 @@ +/**************************************************************************** + * libs/libc/time/lib_gethrtime.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gethrtime + * + * Description: + * Get the current time + * + * Returned Value: + * The current value of the system time in ns + * + ****************************************************************************/ + +hrtime_t gethrtime(void) +{ + struct timespec ts; + +#ifdef CONFIG_CLOCK_MONOTONIC + clock_gettime(CLOCK_MONOTONIC, &ts); +#else + clock_gettime(CLOCK_REALTIME, &ts); +#endif + + return (hrtime_t)1000000000 * ts.tv_sec + ts.tv_nsec; +}