Understand clock management, just look at this~

This article is shared from the China Mobile OneOS WeChat public account "Clock Management".

Clock management consists of two parts, the first part is the minimum time unit of the operating system (clock tick), and the second part is the timer based on the clock tick, which will be described separately below.

clock beat

A clock tick is a specific periodic interrupt, which can be thought of as a system heartbeat. The time interval of the system heartbeat depends on different applications, generally 1ms to 100ms. This periodic interrupt allows the kernel to delay the task by several clock ticks, as well as provide a basis for waiting time-out when the task waits for an event to occur. The faster the clock tick rate, the more overhead the system has.

When the system heartbeat is generated, os_tick_increase() will be called once in the interrupt handler to notify the operating system that a clock tick has elapsed. In os_tick_increase(), first increment the system time by 1, then check whether the running task time slice is exhausted, if it is exhausted, perform task switching, and finally check whether there is a timer timeout, if there is a timeout, execute the corresponding callback function.

Timer management

Timers are divided into hardware timers and software timers .

The hardware timer is provided by the CPU . The software configures the relevant registers through the driver interface to control the timeout time of the timer, etc. After the timeout, the CPU will generate a clock interrupt to achieve the timing effect. Hardware timers have high precision and can reach the nanosecond level, but the number is limited.

The software timer is provided by the operating system . It provides a type of system interface based on the hardware timer. It can provide an unlimited number of timers. However, compared with the hardware timer, the precision is poor, and the timeout duration must be an integer multiple of the clock beat. .

According to the trigger mode, the software timer is divided into a single trigger mode and a periodic trigger mode; according to the context of the execution of the timeout function, the software timer is further divided into an interrupt mode and a task mode.

The interrupt mode is executed in the interrupt function of the system heartbeat. The execution time of the timeout processing function is required to be as short as possible, and operations such as dynamic memory application and waiting for semaphore cannot be performed, otherwise the response time of other interrupts will be longer or the execution time of other tasks will be taken up. The system timer uses the interrupt mode by default, and can be specified using the parameter OS_TIMER_FLAG_HARD_TIMER when creating a timer.

The task mode is executed in the task context, which is not enabled by default and can be enabled by OS_USING_TIMER_SOFT. In this mode, a timer task is created when the system is initialized, and the timeout function of the timer in this mode is executed in the context of the timer task. When creating a timer, you can use the parameter OS_TIMER_FLAG_SOFT_TIMER to specify.

The clock management interface is designed as follows:

(1) Get system tick

 (2) Create a timer

(3) Destroy the timer

(4) Start the timer

(5) Stop the timer

(6) Control the timer

OneOS is a lightweight operating system launched by China Mobile for the Internet of Things. It features tailorable, cross-platform, low power consumption, and high security. It supports mainstream CPUs such as ARM Cortex-M/R/A, MIPS, and RISC-V. The architecture is compatible with POSIX, CMSIS and other standard interfaces, supports Micropython language development, and provides graphical development tools, which can effectively improve development efficiency and reduce development costs, helping customers develop stable, reliable, safe and easy-to-use IoT applications. Official website address: https://os.iot.10086.cn/
OneOS software address: http://www.oschina.net/p/cmcc-oneos
OneOS project address: https://gitee.com/cmcc-oneos/ OneOS
OneOS technical exchange group: 158631242

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324030212&siteId=291194637