Q群:
电话:
邮箱:
地址:
时钟节拍基于硬件定时器实现,定时器的周期性中断到来时,系统时间增加1个tick。我们可以通过修改OS_TICK_PER_SECOND来调整tick的时间长度,1个tick的时间长度为1/OS_TICK_PER_SECOND秒。
时钟节拍是操作系统的心跳,任何操作系统都需要时钟节拍,以便处理和时间有关的事情,比如任务延时、任务的时间片轮转调度、定时器等。
接口 | 说明 |
---|---|
os_tick_get | 获取系统时钟节拍 |
os_tick_set | 设置系统时钟节拍 |
os_tick_increase | 系统时钟中断调用,时钟节拍增1 |
os_tick_from_ms | 把时间(以ms为单位)转化为时钟节拍值 |
该函数用于获取操作系统从启动以来到目前为止的系统时钟节拍计数值,函数原型如下:
os_tick_t os_tick_get(void);
参数 | 说明 |
---|---|
无 | 无 |
返回 | 说明 |
os_tick_t | 系统时钟节拍计数值 |
该函数用于设置系统时钟节拍计数值,函数原型如下:
void os_tick_set(os_tick_t tick);
参数 | 说明 |
---|---|
tick | 设置的值 |
返回 | 说明 |
无 | 无 |
该函数将系统时钟节拍数加1,一般在系统时钟中断里面调用,函数原型如下:
void os_tick_increase(void);
该函数把以ms为单位的时间转换为时钟节拍值,函数原型如下:
os_tick_t os_tick_from_ms(os_uint32_t ms);
参数 | 说明 |
---|---|
ms | 毫秒 |
返回 | 说明 |
os_tick_t | 时钟节拍值 |
本例创建了一个任务,每延时100个tick之后,去获取当前tick
#include <oneos_config.h>
#include <os_dbg.h>
#include <os_task.h>
#include <shell.h>
#include <os_clock.h>
#define TEST_TAG "TEST"
#define TASK_STACK_SIZE 1024
#define TASK_PRIORITY 15
#define TASK_TIMESLICE 10
void task_entry(void *para)
{
os_uint32_t count = 0;
os_uint32_t tick;
while (1)
{
count ++;
tick = os_tick_get();
LOG_E(TEST_TAG, "task_entry count:%d tick:%d", count, tick);
os_task_sleep(100);
}
}
void tick_sample(void)
{
os_task_t *task = OS_NULL;
task = os_task_create("task",
task_entry,
OS_NULL,
TASK_STACK_SIZE,
TASK_PRIORITY,
TASK_TIMESLICE);
if (task)
os_task_startup(task);
}
SH_CMD_EXPORT(tick_sample, tick_sample, "test get tick");
运行结果如下,由于串口打印及其它任务运行还需要消耗时间,所以每次看到的tick增长间隔数比延时的100tick要大一些:
sh />tick_sample
E/TEST: task_entry count:1 tick:62110
E/TEST: task_entry count:2 tick:62215
E/TEST: task_entry count:3 tick:62320
E/TEST: task_entry count:4 tick:62425
E/TEST: task_entry count:5 tick:62530
E/TEST: task_entry count:6 tick:62635
E/TEST: task_entry count:7 tick:62740
E/TEST: task_entry count:8 tick:62845
E/TEST: task_entry count:9 tick:62950
E/TEST: task_entry count:10 tick:63055