1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <hi_types_base.h>
19 #include <los_sys.h>
20 #include <hi_stdlib.h>
21 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
22 #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0))
23 #include <time.h>
24 #endif
25 #endif
26 #include "hi_config.h"
27
28 #define SEC_TO_US 1000000
29 #define US_TO_NSEC 1000
30
hi_get_tick(hi_void)31 hi_u32 hi_get_tick(hi_void)
32 {
33 return (hi_u32)(LOS_TickCountGet() & 0xffffffff);
34 }
35
hi_get_tick64(hi_void)36 hi_u64 hi_get_tick64(hi_void)
37 {
38 return LOS_TickCountGet();
39 }
40
hi_get_milli_seconds(hi_void)41 hi_u32 hi_get_milli_seconds(hi_void)
42 {
43 return ((hi_u32)(LOS_TickCountGet() & 0xffffffff) * HI_MILLISECOND_PER_TICK);
44 }
45
46 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
47 #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0))
hi_get_seconds(hi_void)48 hi_u32 hi_get_seconds(hi_void)
49 {
50 struct timespec tp;
51
52 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
53 return (hi_u32)(tp.tv_sec);
54 } else {
55 return (hi_u32)(HI_ERR_FAILURE);
56 }
57 }
58
hi_get_us(hi_void)59 hi_u64 hi_get_us(hi_void)
60 {
61 struct timespec tp;
62
63 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
64 return (hi_u64)(hi_u32)tp.tv_sec * SEC_TO_US + (hi_u32)tp.tv_nsec / US_TO_NSEC;
65 } else {
66 return (hi_u64)(HI_ERR_FAILURE);
67 }
68 }
69
hi_get_real_time(hi_void)70 hi_u32 hi_get_real_time(hi_void)
71 {
72 struct timespec tp;
73
74 if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
75 return (hi_u32)(tp.tv_sec);
76 } else {
77 return (hi_u32)(HI_ERR_FAILURE);
78 }
79 }
80
hi_set_real_time(hi_u32 sec)81 hi_u32 hi_set_real_time(hi_u32 sec)
82 {
83 struct timespec tp;
84
85 memset_s((hi_void *)&tp, sizeof(struct timespec), 0x0, sizeof(struct timespec));
86 tp.tv_sec = (int)sec;
87 tp.tv_nsec = 0;
88
89 if (clock_settime(CLOCK_REALTIME, &tp) == 0) {
90 return (hi_u32)(HI_ERR_SUCCESS);
91 } else {
92 return (hi_u32)(HI_ERR_FAILURE);
93 }
94 }
95 #endif
96 #endif
97