更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_sys.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "los_sys_pri.h"
33#include "los_sched_pri.h"
34
35/*!
36 * @file los_sys.c
37 * @brief 系统时间转化
38 * @details
39 * @link kernel-small-basic-time http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-basic-time.html @endlink
40 * @verbatim
41 基本概念
42 时间管理以系统时钟为基础,给应用程序提供所有和时间有关的服务。
43
44 系统时钟是由定时器/计数器产生的输出脉冲触发中断产生的,一般定义为整数或长整数。
45 输出脉冲的周期叫做一个“时钟滴答”。系统时钟也称为时标或者Tick。
46
47 用户以秒、毫秒为单位计时,而操作系统以Tick为单位计时,当用户需要对系统进行操作时,
48 例如任务挂起、延时等,此时需要时间管理模块对Tick和秒/毫秒进行转换。
49 时间管理模块提供时间转换、统计、延迟功能
50
51 相关概念
52 Cycle
53 系统最小的计时单位。Cycle的时长由系统主时钟频率决定,系统主时钟频率就是每秒钟的Cycle数。
54
55 Tick
56 Tick是操作系统的基本时间单位,由用户配置的每秒Tick数决定。
57
58 使用场景
59 用户需要了解当前系统运行的时间以及Tick与秒、毫秒之间的转换关系等。
60
61 时间管理的典型开发流程
62 根据实际需求,在板级配置适配时确认是否使能LOSCFG_BASE_CORE_TICK_HW_TIME宏选择外部定时器,
63 并配置系统主时钟频率OS_SYS_CLOCK(单位Hz)。OS_SYS_CLOCK的默认值基于硬件平台配置。
64 通过make menuconfig配置LOSCFG_BASE_CORE_TICK_PER_SECOND。
65
66 参考
67 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-basic-time.html
68 @endverbatim
69 * @attention
70 获取系统Tick数需要在系统时钟使能之后。
71 时间管理不是单独的功能模块,依赖于los_config.h中的OS_SYS_CLOCK和LOSCFG_BASE_CORE_TICK_PER_SECOND两个配置选项。
72 系统的Tick数在关中断的情况下不进行计数,故系统Tick数不能作为准确时间计算。
73 */
74
75#define OS_MAX_VALUE 0xFFFFFFFFUL
76
77/**
78 * @brief 获取自系统启动以来的Tick数
79 *
80 * @return LITE_OS_SEC_TEXT_MINOR
81 */
82LITE_OS_SEC_TEXT_MINOR UINT64 LOS_TickCountGet(VOID)
83{
84 return OsGetCurrSchedTimeCycle() / OS_CYCLE_PER_TICK;
85}
86/**
87 * @brief 每个Tick多少Cycle数
88 *
89 * @return LITE_OS_SEC_TEXT_MINOR
90 */
91LITE_OS_SEC_TEXT_MINOR UINT32 LOS_CyclePerTickGet(VOID)
92{
93 return g_sysClock / LOSCFG_BASE_CORE_TICK_PER_SECOND;
94}
95/**
96 * @brief 毫秒转换成Tick
97 *
98 * @param millisec
99 * @return LITE_OS_SEC_TEXT_MINOR
100 */
101LITE_OS_SEC_TEXT_MINOR UINT32 LOS_MS2Tick(UINT32 millisec)
102{
103 if (millisec == OS_MAX_VALUE) {
104 return OS_MAX_VALUE;
105 }
106
107 return ((UINT64)millisec * LOSCFG_BASE_CORE_TICK_PER_SECOND) / OS_SYS_MS_PER_SECOND;
108}
109/**
110 * @brief Tick转化为毫秒
111 *
112 * @param tick
113 * @return LITE_OS_SEC_TEXT_MINOR
114 */
115LITE_OS_SEC_TEXT_MINOR UINT32 LOS_Tick2MS(UINT32 tick)
116{
117 return ((UINT64)tick * OS_SYS_MS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND;
118}
119/**
120 * @brief 纳秒转化成 tick
121 *
122 * @param nanoseconds
123 * @return LITE_OS_SEC_TEXT_MINOR
124 */
125LITE_OS_SEC_TEXT_MINOR UINT32 OsNS2Tick(UINT64 nanoseconds)
126{
127 const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
128
129 UINT64 ticks = (nanoseconds + nsPerTick - 1) / nsPerTick;
130 if (ticks > OS_MAX_VALUE) {
131 ticks = OS_MAX_VALUE;
132 }
133 return (UINT32)ticks;
134}
135
LITE_OS_SEC_DATA_INIT UINT32 g_sysClock
系统时钟,是绝大部分部件工作的时钟源,也是其他所有外设的时钟的来源
Definition: los_tick.c:40
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_Tick2MS(UINT32 tick)
Tick转化为毫秒
Definition: los_sys.c:115
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_MS2Tick(UINT32 millisec)
毫秒转换成Tick
Definition: los_sys.c:101
LITE_OS_SEC_TEXT_MINOR UINT32 OsNS2Tick(UINT64 nanoseconds)
纳秒转化成 tick
Definition: los_sys.c:125
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_TickCountGet(VOID)
获取自系统启动以来的Tick数
Definition: los_sys.c:82
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_CyclePerTickGet(VOID)
每个Tick多少Cycle数
Definition: los_sys.c:91
STATIC INLINE UINT64 OsGetCurrSchedTimeCycle(VOID)
Definition: los_sched_pri.h:75
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned int UINT32
Definition: los_typedef.h:57