• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Development Guidelines<a name="EN-US_TOPIC_0000001124066545"></a>
2
3-   [Available APIs](#section158501652121514)
4-   [How to Develop](#section783435801510)
5-   [Development Example](#section460018317164)
6    -   [Example Description](#section127752801718)
7    -   [Sample Code](#section321653551711)
8    -   [Verification](#section4366193318167)
9
10
11The time management module provides APIs to implement conversion between the system running time, ticks, and seconds/milliseconds.
12
13## Available APIs<a name="section158501652121514"></a>
14
15The following table describes APIs available for the OpenHarmony LiteOS-M time management. For more details about the APIs, see the API reference.
16
17**Table  1**  APIs of the time management module
18
19<a name="table14277123518139"></a>
20<table><thead align="left"><tr id="row152771935131315"><th class="cellrowborder" valign="top" width="17.77177717771777%" id="mcps1.2.4.1.1"><p id="p1127733591316"><a name="p1127733591316"></a><a name="p1127733591316"></a>Category</p>
21</th>
22<th class="cellrowborder" valign="top" width="23.782378237823785%" id="mcps1.2.4.1.2"><p id="p22771357138"><a name="p22771357138"></a><a name="p22771357138"></a>API</p>
23</th>
24<th class="cellrowborder" valign="top" width="58.44584458445845%" id="mcps1.2.4.1.3"><p id="p327714358130"><a name="p327714358130"></a><a name="p327714358130"></a>Description</p>
25</th>
26</tr>
27</thead>
28<tbody><tr id="row1627793517136"><td class="cellrowborder" rowspan="4" valign="top" width="17.77177717771777%" headers="mcps1.2.4.1.1 "><p id="p10525141151410"><a name="p10525141151410"></a><a name="p10525141151410"></a>Time conversion</p>
29</td>
30<td class="cellrowborder" valign="top" width="23.782378237823785%" headers="mcps1.2.4.1.2 "><p id="p1027783551315"><a name="p1027783551315"></a><a name="p1027783551315"></a>LOS_MS2Tick</p>
31</td>
32<td class="cellrowborder" valign="top" width="58.44584458445845%" headers="mcps1.2.4.1.3 "><p id="p1717215119159"><a name="p1717215119159"></a><a name="p1717215119159"></a>Converts milliseconds into ticks.</p>
33</td>
34</tr>
35<tr id="row20278035131316"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p148921018154"><a name="p148921018154"></a><a name="p148921018154"></a>LOS_Tick2MS</p>
36</td>
37<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p61721311101517"><a name="p61721311101517"></a><a name="p61721311101517"></a>Converts ticks into milliseconds.</p>
38</td>
39</tr>
40<tr id="row1736713145208"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p13367171492014"><a name="p13367171492014"></a><a name="p13367171492014"></a>OsCpuTick2MS</p>
41</td>
42<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p14367121422016"><a name="p14367121422016"></a><a name="p14367121422016"></a>Converts cycles into milliseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.</p>
43</td>
44</tr>
45<tr id="row19475718122016"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p14475121882012"><a name="p14475121882012"></a><a name="p14475121882012"></a>OsCpuTick2US</p>
46</td>
47<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p26621730152310"><a name="p26621730152310"></a><a name="p26621730152310"></a>Converts cycles into microseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.</p>
48</td>
49</tr>
50<tr id="row327873511316"><td class="cellrowborder" rowspan="3" valign="top" width="17.77177717771777%" headers="mcps1.2.4.1.1 "><p id="p1743134312156"><a name="p1743134312156"></a><a name="p1743134312156"></a>Time statistics</p>
51</td>
52<td class="cellrowborder" valign="top" width="23.782378237823785%" headers="mcps1.2.4.1.2 "><p id="p18278435131316"><a name="p18278435131316"></a><a name="p18278435131316"></a>LOS_SysClockGet</p>
53</td>
54<td class="cellrowborder" valign="top" width="58.44584458445845%" headers="mcps1.2.4.1.3 "><p id="p1827813571317"><a name="p1827813571317"></a><a name="p1827813571317"></a>Obtains the system clock.</p>
55</td>
56</tr>
57<tr id="row1127823541313"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1927810354132"><a name="p1927810354132"></a><a name="p1927810354132"></a>LOS_TickCountGet</p>
58</td>
59<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p17278163512131"><a name="p17278163512131"></a><a name="p17278163512131"></a>Obtains the number of ticks since the system starts.</p>
60</td>
61</tr>
62<tr id="row1027814354131"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1727843510137"><a name="p1727843510137"></a><a name="p1727843510137"></a>LOS_CyclePerTickGet</p>
63</td>
64<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p827819358134"><a name="p827819358134"></a><a name="p827819358134"></a>Obtains the number of cycles for each tick.</p>
65</td>
66</tr>
67</tbody>
68</table>
69
70## How to Develop<a name="section783435801510"></a>
71
72The typical development process of time management is as follows:
73
741.  Complete board configuration and adaptation as required, and configure the system clock frequency \(**OS\_SYS\_CLOCK**  in Hz and  **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND**\). The default value of  **OS\_SYS\_CLOCK**  varies with the hardware platform.
752.  Call the clock conversion and statistics APIs.
76
77>![](../public_sys-resources/icon-note.gif) **NOTE:**
78>-   The time management module depends on  **OS\_SYS\_CLOCK**  and  **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND**.
79>-   The number of system ticks is not counted when the interrupt feature is disabled. Therefore, the number of ticks cannot be used as the accurate time.
80>-   The configuration options are maintained in the  **target\_config.h**  file of the development board project.
81
82## Development Example<a name="section460018317164"></a>
83
84### Example Description<a name="section127752801718"></a>
85
86The following example describes basic time management methods, including:
87
881.  Time conversion: converts milliseconds to ticks or converts ticks to milliseconds.
892.  Time statistics: obtains the number of cycles per tick, number of ticks since system startup, and number of delayed ticks.
90
91### Sample Code<a name="section321653551711"></a>
92
93Prerequisites
94
95-   The default value of  **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND**  is  **100**.
96-   The system clock frequency  **OS\_SYS\_CLOCK**  is configured.
97
98Time conversion:
99
100```
101VOID Example_TransformTime(VOID)
102{
103    UINT32 ms;
104    UINT32 tick;
105
106    tick = LOS_MS2Tick(10000);    // Convert 10000 ms into ticks.
107    dprintf("tick = %d \n", tick);
108    ms = LOS_Tick2MS(100);        // Convert 100 ticks into ms.
109    dprintf("ms = %d \n", ms);
110}
111```
112
113Time statistics and delay:
114
115```
116VOID Example_GetTime(VOID)
117{
118    UINT32 cyclePerTick;
119    UINT64 tickCount;
120
121    cyclePerTick  = LOS_CyclePerTickGet();
122    if(0 != cyclePerTick) {
123        dprintf("LOS_CyclePerTickGet = %d \n", cyclePerTick);
124    }
125
126    tickCount = LOS_TickCountGet();
127    if(0 != tickCount) {
128        dprintf("LOS_TickCountGet = %d \n", (UINT32)tickCount);
129    }
130
131    LOS_TaskDelay(200);
132    tickCount = LOS_TickCountGet();
133    if(0 != tickCount) {
134        dprintf("LOS_TickCountGet after delay = %d \n", (UINT32)tickCount);
135    }
136}
137```
138
139### Verification<a name="section4366193318167"></a>
140
141The development is successful if the return result is as follows:
142
143Time conversion:
144
145```
146tick = 1000
147ms = 1000
148```
149
150Time statistics and delay:
151
152```
153LOS_CyclePerTickGet = 495000
154LOS_TickCountGet = 1
155LOS_TickCountGet after delay = 201
156```
157
158