1# Time Management<a name="EN-US_TOPIC_0000001078876462"></a> 2 3 4 5## Basic Concepts<a name="section36251149131120"></a> 6 7Time management provides all time-related services for applications based on the system clock. 8 9The system clock is generated by the interrupts triggered by the output pulse of a timer or counter. The system clock is generally defined as an integer or a long integer. The period of an output pulse is a "clock tick". The system clock is also called time scale or tick. 10 11People use second or millisecond as the time unit, while the operating system uses tick. When operations such as suspending a task or delaying a task are performed, the time management module converts time between ticks and seconds or milliseconds. 12 13The time management module of the OpenHarmony LiteOS-M kernel provides time conversion and statistics functions. 14 15## Time Unit<a name="section97172532397"></a> 16 17- Cycle 18 19 Cycle is the minimum time unit in the system. The cycle duration is determined by the system clock frequency, that is, the number of cycles per second. 20 21- Tick 22 23 Tick is the basic time unit of the operating system and is determined by the number of ticks per second configured by the user. 24 25 26## Available APIs<a name="section158501652121514"></a> 27 28The following table describes APIs available for the OpenHarmony LiteOS-M time management. For more details about the APIs, see the API reference. 29 30**Table 1** APIs of the time management module 31 32| Category| API| Description| 33| -------- | -------- | -------- | 34| Time conversion| LOS_MS2Tick | Converts milliseconds into ticks.| 35| | LOS_Tick2MS | Converts ticks into milliseconds.| 36| | OsCpuTick2MS | Converts cycles into milliseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.| 37| | OsCpuTick2US | Converts cycles into microseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.| 38| Time statistics| LOS_SysClockGet | Obtains the system clock.| 39| | LOS_TickCountGet | Obtains the number of ticks since the system starts.| 40| | LOS_CyclePerTickGet | Obtains the number of cycles for each tick.| 41| | LOS_CurrNanosec |Obtains the number of nanoseconds since the system starts.| 42| Delay management| LOS_UDelay |Performs busy waiting in μs, which can be preempted by a task with a higher priority.| 43| | LOS_MDelay |Performs busy waiting in ms, which can be preempted by a task with a higher priority.| 44 45## How to Develop<a name="section783435801510"></a> 46 47The typical development process of time management is as follows: 48 491. 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. 502. Call the clock conversion and statistics APIs. 51 52> **NOTE** 53> 54>- The time management module depends on **OS\_SYS\_CLOCK** and **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND**. 55>- 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. 56>- The configuration options are maintained in the **target\_config.h** file of the development board project. 57 58## Development Example<a name="section460018317164"></a> 59 60### Example Description<a name="section127752801718"></a> 61 62The following example describes basic time management methods, including: 63 64- Time conversion: convert milliseconds to ticks or convert ticks to milliseconds. 65- Time statistics: obtain the number of cycles per tick, number of ticks since system startup, and number of delayed ticks. 66 67### Sample Code<a name="section321653551711"></a> 68 69Prerequisites 70 71- The default value of **LOSCFG\_BASE\_CORE\_TICK\_PER\_SECOND** is **100**. 72- The system clock frequency **OS\_SYS\_CLOCK** is configured. 73 74Time conversion: 75 76``` 77VOID Example_TransformTime(VOID) 78{ 79 UINT32 ms; 80 UINT32 tick; 81 82 tick = LOS_MS2Tick(10000); // Convert 10000 ms into ticks. 83 dprintf("tick = %d \n", tick); 84 ms = LOS_Tick2MS(100); // Convert 100 ticks into ms. 85 dprintf("ms = %d \n", ms); 86} 87``` 88 89Time statistics and delay: 90 91``` 92VOID Example_GetTime(VOID) 93{ 94 UINT32 cyclePerTick; 95 UINT64 tickCount; 96 97 cyclePerTick = LOS_CyclePerTickGet(); 98 if(0 != cyclePerTick) { 99 dprintf("LOS_CyclePerTickGet = %d \n", cyclePerTick); 100 } 101 102 tickCount = LOS_TickCountGet(); 103 if(0 != tickCount) { 104 dprintf("LOS_TickCountGet = %d \n", (UINT32)tickCount); 105 } 106 107 LOS_TaskDelay(200); 108 tickCount = LOS_TickCountGet(); 109 if(0 != tickCount) { 110 dprintf("LOS_TickCountGet after delay = %d \n", (UINT32)tickCount); 111 } 112} 113``` 114 115### Verification<a name="section4366193318167"></a> 116 117The development is successful if the return result is as follows: 118 119Time conversion: 120 121``` 122tick = 1000 123ms = 1000 124``` 125 126Time statistics and delay: 127 128``` 129LOS_CyclePerTickGet = 495000 130LOS_TickCountGet = 1 131LOS_TickCountGet after delay = 201 132``` 133