全部文档
OneOS简介 硬件支持 编译构造工具 API参考文档 高级语言 用户编程手册 应用笔记 FAQ

Clocksource设备


API 列表

函数 说明
os_clocksource_gettime() 获取当前时间戳
os_clocksource_ndelay() CPU忙延时

os_clocksource_gettime

该函数用于获取当前时间戳,函数原型如下:

os_uint64_t os_clocksource_gettime(void);
参数 说明
返回 说明
当前时间戳 时间戳单位是:纳秒

os_clocksource_ndelay

该函数用于延时,函数原型如下:

void os_clocksource_ndelay(os_uint64_t nsec);
参数 说明
nsec 延时时间,单位:纳秒
返回 说明

使用示例

clocksource设备使用示例

硬件Clocksource设备的具体使用方式和步骤如下所示:

  1. 内核时钟配置为1000Hz,即:OS_TICK_PER_SECOND=1000。
  2. 读取时间戳,并记录。
  3. 调用 msleep 函数,睡眠一段时间。
  4. 读取时间戳,并记录;比对两次时间戳差值是否为 msleep 睡眠时间。
  5. 调用 os_clocksource_ndelay 延时一段时间。
  6. 读取时间戳,并记录;比对两次时间戳差值是否为 ndelay 延时时间。
#include <board.h>
#include <os_clock.h>
#include <oneos_config.h>
#include <timer/clocksource.h>
#include <shell.h>

void clocksource_test(int argc, char *argv[])
{
    os_uint32_t i;
    os_uint64_t start, now, delay;
    os_tick_t tick_start, tick_now;

    os_task_t *self = os_task_self();
    os_uint8_t task_prio = self->current_priority;
    os_uint8_t high_prio = 1;
    os_task_control(self, OS_TASK_CTRL_CHANGE_PRIORITY, &high_prio);

    for (i = 0; i < 14; i++)
    {
        delay = 1ULL << i;

        /* sync tick */
        os_task_msleep(1);

        tick_start = os_tick_get();
        start = os_clocksource_gettime();
        os_task_msleep(delay);
        tick_now = os_tick_get();
        now = os_clocksource_gettime();

        os_kprintf("msleep start tick: %d\r\n", tick_start);
        os_kprintf("msleep now   tick: %d             delta tick: %d\r\n",
                   tick_now, tick_now - tick_start);
        os_kprintf("msleep start time: %d.%09d\r\n", (int)(start / NSEC_PER_SEC), (int)(start % NSEC_PER_SEC));
        os_kprintf("msleep now   time: %d.%09d    %Ld, delta time: %d us\r\n\r\n",
                   (int)(now / NSEC_PER_SEC), (int)(now % NSEC_PER_SEC), delay, (int)((now - start) / 1000));
    }

    for (i = 0; i < 14; i++)
    {
        delay = (1ULL << i) * 1000 * 1000;
        tick_start = os_tick_get();
        start = os_clocksource_gettime();
        os_clocksource_ndelay(delay);
        tick_now = os_tick_get();
        now = os_clocksource_gettime();

        os_kprintf("udelay start tick: %d\r\n", tick_start);
        os_kprintf("udelay now   tick: %d             delta tick: %d\r\n",
                   tick_now, tick_now - tick_start);
        os_kprintf("udelay start time: %d.%09d\r\n", (int)(start / NSEC_PER_SEC), (int)(start % NSEC_PER_SEC));
        os_kprintf("udelay now   time: %d.%09d    %Ld, delta time: %d us\r\n\r\n",
                   (int)(now / NSEC_PER_SEC), (int)(now % NSEC_PER_SEC), delay, (int)((now - start) / 1000));
    }

    os_task_control(self, OS_TASK_CTRL_CHANGE_PRIORITY, &task_prio);
}

SH_CMD_EXPORT(clocksource_test, clocksource_test, "clocksource_test");

运行结果如下:

sh>clocksource_test
msleep start tick: 8980
msleep now   tick: 8981             delta tick: 1
msleep start time: 8.991786600
msleep now   time: 8.992786512    1, delta time: 999 us

msleep start tick: 9000
msleep now   tick: 9002             delta tick: 2
msleep start time: 9.011786462
msleep now   time: 9.013786525    2, delta time: 2000 us

msleep start tick: 9021
msleep now   tick: 9025             delta tick: 4
msleep start time: 9.032786500
msleep now   time: 9.036786912    4, delta time: 4000 us

msleep start tick: 9044
msleep now   tick: 9052             delta tick: 8
msleep start time: 9.055786450
msleep now   time: 9.063790525    8, delta time: 8004 us

msleep start tick: 9071
msleep now   tick: 9087             delta tick: 16
msleep start time: 9.082786462
msleep now   time: 9.098788987    16, delta time: 16002 us

msleep start tick: 9107
msleep now   tick: 9139             delta tick: 32
msleep start time: 9.118786462
msleep now   time: 9.150804762    32, delta time: 32018 us

msleep start tick: 9159
msleep now   tick: 9223             delta tick: 64
msleep start time: 9.170800937
msleep now   time: 9.234788025    64, delta time: 63987 us

msleep start tick: 9243
msleep now   tick: 9371             delta tick: 128
msleep start time: 9.254805312
msleep now   time: 9.382786625    128, delta time: 127981 us

msleep start tick: 9391
msleep now   tick: 9647             delta tick: 256
msleep start time: 9.402786450
msleep now   time: 9.658786525    256, delta time: 256000 us

msleep start tick: 9667
msleep now   tick: 10179             delta tick: 512
msleep start time: 9.678786500
msleep now   time: 10.190800000    512, delta time: 512013 us

msleep start tick: 10199
msleep now   tick: 11223             delta tick: 1024
msleep start time: 10.210800937
msleep now   time: 11.234786850    1024, delta time: 1023985 us

msleep start tick: 11244
msleep now   tick: 13292             delta tick: 2048
msleep start time: 11.255786475
msleep now   time: 13.303788987    2048, delta time: 2048002 us

msleep start tick: 13313
msleep now   tick: 17409             delta tick: 4096
msleep start time: 13.324786812
msleep now   time: 17.420800675    4096, delta time: 4096013 us

msleep start tick: 17430
msleep now   tick: 25622             delta tick: 8192
msleep start time: 17.441787912
msleep now   time: 25.633787962    8192, delta time: 8192000 us

udelay start tick: 25642
udelay now   tick: 25643             delta tick: 1
udelay start time: 25.654089850
udelay now   time: 25.655095900    1000000, delta time: 1006 us

udelay start tick: 25663
udelay now   tick: 25665             delta tick: 2
udelay start time: 25.675075937
udelay now   time: 25.677081925    2000000, delta time: 2005 us

udelay start tick: 25685
udelay now   tick: 25689             delta tick: 4
udelay start time: 25.697062200
udelay now   time: 25.701068150    4000000, delta time: 4005 us

udelay start tick: 25709
udelay now   tick: 25717             delta tick: 8
udelay start time: 25.721048187
udelay now   time: 25.729054000    8000000, delta time: 8005 us

udelay start tick: 25737
udelay now   tick: 25753             delta tick: 16
udelay start time: 25.749031962
udelay now   time: 25.765037375    16000000, delta time: 16005 us

udelay start tick: 25773
udelay now   tick: 25805             delta tick: 32
udelay start time: 25.785337737
udelay now   time: 25.817343812    32000000, delta time: 32006 us

udelay start tick: 25825
udelay now   tick: 25889             delta tick: 64
udelay start time: 25.837643925
udelay now   time: 25.901650000    64000000, delta time: 64006 us

udelay start tick: 25910
udelay now   tick: 26038             delta tick: 128
udelay start time: 25.922011487
udelay now   time: 26.050017075    128000000, delta time: 128005 us

udelay start tick: 26058
udelay now   tick: 26314             delta tick: 256
udelay start time: 26.070644750
udelay now   time: 26.326650625    256000000, delta time: 256005 us

udelay start tick: 26335
udelay now   tick: 26847             delta tick: 512
udelay start time: 26.347324212
udelay now   time: 26.859330200    512000000, delta time: 512005 us

udelay start tick: 26868
udelay now   tick: 27892             delta tick: 1024
udelay start time: 26.880010000
udelay now   time: 27.904015412    1024000000, delta time: 1024005 us

udelay start tick: 27913
udelay now   tick: 29961             delta tick: 2048
udelay start time: 27.925018562
udelay now   time: 29.973024175    2048000000, delta time: 2048005 us

udelay start tick: 29982
udelay now   tick: 34078             delta tick: 4096
udelay start time: 29.994023925
udelay now   time: 34.090029962    4096000000, delta time: 4096006 us

udelay start tick: 34099
udelay now   tick: 42291             delta tick: 8192
udelay start time: 34.111036362
udelay now   time: 42.303042012    8192000000, delta time: 8192005 us

sh>

results matching ""

    No results matching ""

    返回顶部