1# Time Management 2 3 4## Basic Concepts 5 6Time management is performed based on the system clock. It provides time-related services for applications. The 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". 7 8The system clock is also called time scale or tick. The duration of a tick can be configured statically. People 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. 9 10The mapping between ticks and seconds can be configured. 11 12- Cycle 13 14 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. 15 16- Tick 17 18 Tick is the basic time unit of the operating system and is determined by the number of ticks per second configured by the user. 19 20The OpenHarmony time management module provides time conversion, statistics, and delay functions. 21 22 23## Development Guidelines 24 25Before you start, learn about the system time and the APIs for time management. 26 27 28### Available APIs 29 30The following table describes APIs for OpenHarmony LiteOS-A time management. For more details about the APIs, see the API reference. 31 32**Table 1** APIs for time management 33 34| Category| API Description | 35| -------- | ------------------------------------------------------------ | 36| Time conversion| **LOS_MS2Tick**: converts milliseconds to ticks.<br>**LOS_Tick2MS**: converts ticks to milliseconds. | 37| Time statistics| **LOS_TickCountGet**: obtains the number of current ticks.<br>**LOS_CyclePerTickGet**: obtains the number of cycles of each tick.| 38 39 40### How to Develop 41 421. Call APIs to convert time. 43 442. Call APIs to perform time statistics. 45 46> **NOTE** 47> 48> - The system tick count can be obtained only after the system clock is enabled. 49> 50> - The time management module depends on **OS_SYS_CLOCK** and **LOSCFG_BASE_CORE_TICK_PER_SECOND** in **los_config.h**. 51> 52> - 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. 53 54 55### Development Example 56 57**Prerequisites** 58 59The following parameters are configured: 60 61- **LOSCFG_BASE_CORE_TICK_PER_SECOND**: number of ticks/second. The value range is (0, 1000). 62 63- **OS_SYS_CLOCK**: system clock, in Hz. 64 65**Sample Code** 66 67Time conversion: 68 69``` 70VOID Example_TransformTime(VOID) 71{ 72 UINT32 uwMs; 73 UINT32 uwTick; 74 uwTick = LOS_MS2Tick(10000); // Convert 10000 ms to ticks. 75 PRINTK("uwTick = %d \n",uwTick); 76 uwMs= LOS_Tick2MS(100); // Convert 100 ticks to ms. 77 PRINTK("uwMs = %d \n",uwMs); 78} 79``` 80 81Time statistics and delay: 82 83 84``` 85VOID Example_GetTime(VOID) 86{ 87 UINT32 uwcyclePerTick; 88 UINT64 uwTickCount; 89 90 uwcyclePerTick = LOS_CyclePerTickGet(); // Obtain the number of cycles per tick. 91 if(0 != uwcyclePerTick) 92 { 93 PRINTK("LOS_CyclePerTickGet = %d \n", uwcyclePerTick); 94 } 95 96 uwTickCount = LOS_TickCountGet(); // Obtain the number of ticks. 97 if(0 != uwTickCount) 98 { 99 PRINTK("LOS_TickCountGet = %d \n", (UINT32)uwTickCount); 100 } 101 LOS_TaskDelay(200);// Delay 200 ticks. 102 uwTickCount = LOS_TickCountGet(); 103 if(0 != uwTickCount) 104 { 105 PRINTK("LOS_TickCountGet after delay = %d \n", (UINT32)uwTickCount); 106 } 107} 108``` 109 110**Verification** 111 112The result is as follows: 113 114Time conversion: 115 116 117``` 118uwTick = 10000 119uwMs = 100 120``` 121 122Time statistics and delay: 123 124 125``` 126LOS_CyclePerTickGet = 49500 127LOS_TickCountGet = 347931 128LOS_TickCountGet after delay = 348134 129``` 130