Development Guidelines
The time management module provides APIs to implement conversion between the system running time, ticks, and seconds/milliseconds.
Available APIs
The following table describes APIs available for the OpenHarmony LiteOS-M time management. For more details about the APIs, see the API reference.
Table 1 APIs of the time management module
How to Develop
The typical development process of time management is as follows:
- 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.
- Call the clock conversion and statistics APIs.
NOTE:
- The time management module depends on OS_SYS_CLOCK and LOSCFG_BASE_CORE_TICK_PER_SECOND.
- 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.
- The configuration options are maintained in the target_config.h file of the development board project.
Development Example
Example Description
The following example describes basic time management methods, including:
- Time conversion: converts milliseconds to ticks or converts ticks to milliseconds.
- Time statistics: obtains the number of cycles per tick, number of ticks since system startup, and number of delayed ticks.
Sample Code
Prerequisites
- The default value of LOSCFG_BASE_CORE_TICK_PER_SECOND is 100.
- The system clock frequency OS_SYS_CLOCK is configured.
Time conversion:
VOID Example_TransformTime(VOID)
{
UINT32 ms;
UINT32 tick;
tick = LOS_MS2Tick(10000); // Convert 10000 ms into ticks.
dprintf("tick = %d \n", tick);
ms = LOS_Tick2MS(100); // Convert 100 ticks into ms.
dprintf("ms = %d \n", ms);
}
Time statistics and delay:
VOID Example_GetTime(VOID)
{
UINT32 cyclePerTick;
UINT64 tickCount;
cyclePerTick = LOS_CyclePerTickGet();
if(0 != cyclePerTick) {
dprintf("LOS_CyclePerTickGet = %d \n", cyclePerTick);
}
tickCount = LOS_TickCountGet();
if(0 != tickCount) {
dprintf("LOS_TickCountGet = %d \n", (UINT32)tickCount);
}
LOS_TaskDelay(200);
tickCount = LOS_TickCountGet();
if(0 != tickCount) {
dprintf("LOS_TickCountGet after delay = %d \n", (UINT32)tickCount);
}
}
Verification
The development is successful if the return result is as follows:
Time conversion:
tick = 1000
ms = 1000
Time statistics and delay:
LOS_CyclePerTickGet = 495000
LOS_TickCountGet = 1
LOS_TickCountGet after delay = 201