全部文档
概述 硬件支持 快速开发指南 内核 驱动 通用组件 专业组件 常见问题

Clocksource设备用户开发


基础BSP配置--以STM32为例

建立工程文件

如果在提供的 OneOS源码中已经有适合的工程 DEMO 工程,则可以直接使用;如果没有请参照快速上手中的操作指南,新建一个适合的工程。

使用STM32CUBEMX配置硬件

  1. 打开 oneos\projects\xxxxx(project文件夹)\board\CubeMX_Config 下 的 CUBE 工程文件;
  2. 在 CUBE 工程中进行定时器配置,如下图所示,选中 Timer,配置工作模式、时钟源; adc_cube
  3. 点击 GENARATE CODE 生成代码。

使用 Menuconfig 配置工程选项

  1. 在对应的 oneos\projects\xxxxx(project文件夹) 目录下打开 OneOS-Cube 工具,在命令行输入 menuconfig 打开可视化配置界面;

  2. 如下图示,通过空格或向右方向键选择 Drivers->Timer 下的选项clocksource 使能 clocksource 提供高精度时间戳;

(Top) → Drivers→ Timer
                                              OneOS Configuration
[*] Using timer driver
[*]     clocksource
[ ]         clocksource show
[ ]         Timekeeping
[ ]     clockevent
[ ]     hrtimer(soft timer)
        cortex-m hardware timer config  --->
  1. stm32 芯片可以使用 DWT、SysTick 作为时间戳,如果使能了DWT或者SysTick作为时间戳,则会挑一个最优的定时器,作为时间戳;如果不使能该选项,则会挑选上一步 stm32cube 配置的定时器作为时间戳,下图为不使能DWT的配置界面。
(Top) → Drivers→ Timer→ Using timer driver → cortex-m hardware timer config
                  ↑↑↑↑↑↑↑              OneOS Configuration
    cortex-m systick config (systick for kernel tick)  --->
[ ] cortex-m dwt for clocksource

3.Esc键退出menuconfig,注意保存所修改的设置。

使用 Scons 构建工程

在上一步打开的 OneOS-Cube 工具命令行输入 scons --ide=mdk5 构建工程;

工程编译及实现

  1. 打开对应的 oneos\projects\xxxxx(project文件夹) 目录下的 project.uvprojx 工程文件;
  2. 编译并下载工程,运行程序。

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_set_priority(self, 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_set_priority(self, task_prio);
}

SH_CMD_EXPORT(clocksource_test, clocksource_test, "clocksource_test");

运行结果如下:

clocksource_test
sh>clocksource_test
msleep start tick: 40063
msleep now   tick: 40064             delta tick: 1
msleep start time: 400.626546556
msleep now   time: 400.636546631    1, delta time: 10000 us

msleep start tick: 40066
msleep now   tick: 40067             delta tick: 1
msleep start time: 400.656554306
msleep now   time: 400.666546718    2, delta time: 9992 us

msleep start tick: 40069
msleep now   tick: 40070             delta tick: 1
msleep start time: 400.686547018
msleep now   time: 400.696546906    4, delta time: 9999 us

msleep start tick: 40072
msleep now   tick: 40073             delta tick: 1
msleep start time: 400.716546668
msleep now   time: 400.726546743    8, delta time: 10000 us

msleep start tick: 40075
msleep now   tick: 40077             delta tick: 2
msleep start time: 400.746546706
msleep now   time: 400.766546843    16, delta time: 20000 us

msleep start tick: 40079
msleep now   tick: 40083             delta tick: 4
msleep start time: 400.786546756
msleep now   time: 400.826546231    32, delta time: 39999 us

msleep start tick: 40085
msleep now   tick: 40092             delta tick: 7
msleep start time: 400.846546831
msleep now   time: 400.916546343    64, delta time: 69999 us

msleep start tick: 40094
msleep now   tick: 40107             delta tick: 13
msleep start time: 400.936547331
msleep now   time: 401.066547218    128, delta time: 129999 us

msleep start tick: 40109
msleep now   tick: 40135             delta tick: 26
msleep start time: 401.086547131
msleep now   time: 401.346547268    256, delta time: 260000 us

msleep start tick: 40137
msleep now   tick: 40189             delta tick: 52
msleep start time: 401.366547531
msleep now   time: 401.886547556    512, delta time: 520000 us

msleep start tick: 40191
msleep now   tick: 40294             delta tick: 103
msleep start time: 401.906548156
msleep now   time: 402.936548868    1024, delta time: 1030000 us

msleep start tick: 40296
msleep now   tick: 40501             delta tick: 205
msleep start time: 402.956553455
msleep now   time: 405.006551455    2048, delta time: 2049998 us

msleep start tick: 40503
msleep now   tick: 40913             delta tick: 410
msleep start time: 405.026552055
msleep now   time: 409.126556605    4096, delta time: 4100004 us

msleep start tick: 40915
msleep now   tick: 41735             delta tick: 820
msleep start time: 409.146557205
msleep now   time: 417.346566880    8192, delta time: 8200009 us

udelay start tick: 41736
udelay now   tick: 41736             delta tick: 0
udelay start time: 417.363397293
udelay now   time: 417.364401355    1000000, delta time: 1004 us

udelay start tick: 41738
udelay now   tick: 41738             delta tick: 0
udelay start time: 417.381047655
udelay now   time: 417.383051718    2000000, delta time: 2004 us

udelay start tick: 41740
udelay now   tick: 41740             delta tick: 0
udelay start time: 417.399696593
udelay now   time: 417.403700655    4000000, delta time: 4004 us

udelay start tick: 41742
udelay now   tick: 41743             delta tick: 1
udelay start time: 417.420343568
udelay now   time: 417.428348118    8000000, delta time: 8004 us

udelay start tick: 41744
udelay now   tick: 41746             delta tick: 2
udelay start time: 417.444982693
udelay now   time: 417.460987218    16000000, delta time: 16004 us

udelay start tick: 41748
udelay now   tick: 41751             delta tick: 3
udelay start time: 417.477823793
udelay now   time: 417.509828143    32000000, delta time: 32004 us

udelay start tick: 41753
udelay now   tick: 41759             delta tick: 6
udelay start time: 417.526665105
udelay now   time: 417.590669618    64000000, delta time: 64004 us

udelay start tick: 41761
udelay now   tick: 41773             delta tick: 12
udelay start time: 417.607506193
udelay now   time: 417.735510593    128000000, delta time: 128004 us

udelay start tick: 41775
udelay now   tick: 41801             delta tick: 26
udelay start time: 417.752630530
udelay now   time: 418.008635243    256000000, delta time: 256004 us

udelay start tick: 41802
udelay now   tick: 41854             delta tick: 52
udelay start time: 418.025731318
udelay now   time: 418.537735768    512000000, delta time: 512004 us

udelay start tick: 41855
udelay now   tick: 41958             delta tick: 103
udelay start time: 418.554836580
udelay now   time: 419.578841243    1024000000, delta time: 1024004 us

udelay start tick: 41959
udelay now   tick: 42164             delta tick: 205
udelay start time: 419.596228493
udelay now   time: 421.644233018    2048000000, delta time: 2048004 us

udelay start tick: 42166
udelay now   tick: 42576             delta tick: 410
udelay start time: 421.661635493
udelay now   time: 425.757640180    4096000000, delta time: 4096004 us

udelay start tick: 42577
udelay now   tick: 43397             delta tick: 820
udelay start time: 425.775026255
udelay now   time: 433.967030980    8192000000, delta time: 8192004 us

sh>

results matching ""

    No results matching ""