• 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: 硬件tick模块的C文件。
14  */
15 #include "prt_hw_tick_internal.h"
16 
17 /*
18  * 描述:CycleUpdate内部函数,该接口必须关中断情况下调用
19  */
OsCycleUpdate(void)20 OS_SEC_L2_TEXT void OsCycleUpdate(void)
21 {
22     U32 hwCycleFirst;  /* PosA */
23     U32 hwCycleCtrl;   /* PosB */
24     U32 hwCycleSecond; /* PosC */
25     /*
26      * 此处关中断后SysTick 中断上报会发生在以上四个时机
27      * 1), Before  PosA then CONTROL_COUNTFLAG will be set and hwCycleFirst >= hwCycleSecond
28      * 2), Between PosA and PosB then CONTROL_COUNTFLAG will be set and hwCycleFirst < hwCycleSecond
29      * 3), Between PosB and PosC then CONTROL_COUNTFLAG will be clear and hwCycleFirst < hwCycleSecond
30      * 4), After PosC we'll see it next time
31      */
32     hwCycleFirst = *(volatile U32 *)OS_SYSTICK_CURRENT_REG;
33     hwCycleCtrl = *(volatile U32 *)OS_SYSTICK_CONTROL_REG;
34     hwCycleSecond = *(volatile U32 *)OS_SYSTICK_CURRENT_REG;
35 
36     /* 如果1、装载值前一次比后一次小 或 2、OS_SYSTICK_CONTROL_COUNTFLAG 已经SET, */
37     /* 则 说明tick反转,需要手动补偿一个周期 */
38     if (((hwCycleCtrl & OS_SYSTICK_CONTROL_COUNTFLAG_MSK) != 0) || (hwCycleFirst < hwCycleSecond)) {
39         g_cycleByTickNow += OsGetCyclePerTick();
40         /* 再一次读时钟定时器,读清COUNTFLAG位 */
41         /* COUNTFLAG位(bit16):Returns 1 If Timer Counted To 0 Since Last Time This Was Read */
42         (void)*(volatile U32 *)OS_SYSTICK_CONTROL_REG;
43     }
44     g_cycleNow = (g_cycleByTickNow + (OsGetCyclePerTick() - hwCycleSecond));
45 }
46 
PRT_ClkGetCycleCount64(void)47 OS_SEC_L2_TEXT U64 PRT_ClkGetCycleCount64(void)
48 {
49     U64 cycle;
50     uintptr_t intSave;
51     intSave = PRT_HwiLock();
52     OsCycleUpdate();
53     cycle = g_cycleNow;
54     PRT_HwiRestore(intSave);
55     return cycle;
56 }
57