• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3  *
4  * UniProton is licensed under Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *          http://license.coscl.org.cn/MulanPSL2
8  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11  * See the Mulan PSL v2 for more details.
12  * Create: 2009-12-22
13  * Description: System base function implementation
14  */
15 #include "prt_attr_external.h"
16 #include "prt_sys_external.h"
17 
18 /* 用户注册的获取系统时间钩子 */
19 OS_SEC_BSS SysTimeFunc g_sysTimeHook;
20 
OsSetSysTimeHook(SysTimeFunc hook)21 OS_SEC_L2_TEXT U32 OsSetSysTimeHook(SysTimeFunc hook)
22 {
23     /* 钩子不支持重复注册 */
24     if (g_sysTimeHook != NULL) {
25         return OS_ERRNO_SYS_TIME_HOOK_REGISTER_REPEATED;
26     }
27     g_sysTimeHook = hook;
28     return OS_OK;
29 }
30 
31 /*
32  * 描述:当前系统时间
33  */
OsCurCycleGet64(void)34 OS_SEC_L2_TEXT U64 OsCurCycleGet64(void)
35 {
36     if (g_sysTimeHook != NULL) {
37         return g_sysTimeHook();
38     } else {
39         return 0;
40     }
41 }
42 
43 /*
44  * 描述:获取当前的tick计数
45  */
PRT_TickGetCount(void)46 OS_SEC_L2_TEXT U64 PRT_TickGetCount(void)
47 {
48     return g_uniTicks;
49 }
50 
51 /*
52  * 描述:根据输入的周期数进行延时
53  */
OsTimerDelayCount(U64 cycles)54 OS_SEC_ALW_INLINE INLINE void OsTimerDelayCount(U64 cycles)
55 {
56     U64 cur;
57     U64 end;
58 
59     cur = OsCurCycleGet64();
60     end = cur + cycles;
61 
62     if (cur < end) {
63         while (cur < end) {
64             cur = OsCurCycleGet64();
65         }
66     } else {
67         /* 考虑Cycle计数反转的问题,虽然64位的计数需要584年才能反转,但是考虑软件的严密性,需要做反转保护 */
68         /* 当前值大于结束值的场景 */
69         while (cur > end) {
70             cur = OsCurCycleGet64();
71         }
72         /* 正过来之后追赶的场景 */
73         while (cur < end) {
74             cur = OsCurCycleGet64();
75         }
76     }
77 }
78 
79 /*
80  * 描述:时间延迟毫秒
81  */
PRT_ClkDelayMs(U32 delay)82 OS_SEC_L2_TEXT void PRT_ClkDelayMs(U32 delay)
83 {
84     U64 cycles;
85 
86     /* 将毫秒转化为Cycle计数,例如1m为1M个cycle */
87     cycles = OS_MS2CYCLE(delay, g_systemClock);
88 
89     OsTimerDelayCount(cycles);
90 }
91 
92 /*
93  * 描述:时间延迟微秒
94  */
PRT_ClkDelayUs(U32 delay)95 OS_SEC_L4_TEXT void PRT_ClkDelayUs(U32 delay)
96 {
97     U64 cycles;
98 
99     cycles = OS_US2CYCLE(delay, g_systemClock);
100 
101     OsTimerDelayCount(cycles);
102 }
103