Time Management

Basic Concepts

Time management provides all time-related services for applications based on the system clock.

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". The system clock is also called time scale or tick.

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.

The time management module of the OpenHarmony LiteOS-M kernel provides time conversion and statistics functions.

Time Unit

  • Cycle 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.

  • Tick Tick is the basic time unit of the operating system and is determined by the number of ticks per second configured by the user.

Available APIs

The following table describes APIs available for OpenHarmony LiteOS-M time management. For more details about the APIs, see the API reference.

Table 1 APIs of the time management module

API Description
LOS_MS2Tick Converts milliseconds into ticks.
LOS_Tick2MS Converts ticks into milliseconds.
OsCpuTick2MS Converts cycles into milliseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.
OsCpuTick2US Converts cycles into microseconds. Two UINT32 values indicate the high-order and low-order 32 bits of the result value, respectively.

Table 2 APIs for time statistics

API Description
LOS_SysClockGet Obtains the system clock.
LOS_TickCountGet Obtains the number of ticks since the system starts.
LOS_CyclePerTickGet Obtains the number of cycles for each tick.
LOS_CurrNanosec Obtains the current time, in nanoseconds.

Table 3 API for time registration

API Description
LOS_TickTimerRegister Re-registers the timer of the system clock and the corresponding interrupt handler.

Table 4 APIs for delay

API Description
LOS_MDelay Delays a task, in ms.
LOS_UDelay Delays a task, in μs.

How to Develop

The typical development process of time management is as follows:

  1. 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.

  2. Call the clock conversion and statistics APIs.

icon-note.gif 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 preceding configuration is in the target_config.h file of the development board project. The default values of some configuration items are defined in the los_config.h file of the kernel.

Development Example

Example Description

The following example describes basic time management methods, including:

  1. Time conversion: convert milliseconds to ticks or convert ticks to milliseconds.

  2. Time statistics: obtain 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:

The sample code is compiled and verified in ./kernel/liteos_m/testsuites/src/osTest.c. Call ExampleTransformTime and ExampleGetTime in TestTaskEntry.

VOID ExampleTransformTime(VOID)
{
    UINT32 ms;
    UINT32 tick;

    /* Convert 10000 ms to ticks. */
    tick = LOS_MS2Tick(10000);
    printf("tick = %d \n", tick);

    /* Convert 100 ticks to ms. */
    ms = LOS_Tick2MS(100);
    printf("ms = %d \n", ms);
}

Time statistics and delay:

VOID ExampleGetTime(VOID)
{
    UINT32 cyclePerTick;
    UINT64 tickCountBefore;
    UINT64 tickCountAfter;

    cyclePerTick  = LOS_CyclePerTickGet();
    if (0 != cyclePerTick) {
        printf("LOS_CyclePerTickGet = %d \n", cyclePerTick);
    }

    tickCountBefore = LOS_TickCountGet();
    LOS_TaskDelay(200);
    tickCountAfter = LOS_TickCountGet();
    printf("LOS_TickCountGet after delay rising = %d \n", (UINT32)(tickCountAfter - tickCountBefore));
}

Verification

The development is successful if the return result is as follows:

Time conversion:

tick = 1000
ms = 1000

Time statistics and delay:

LOS_CyclePerTickGet = 250000 (The data may vary depending on the actual running environment.)
LOS_TickCountGet after delay rising = 200