Edit online

测试指南

24 Feb 2025
Read time: 3 minute(s)

准备测试环境

  • 硬件
    • 开发板

    • USB 转串口的线缆

  • 软件
    • PC 端的串口终端软件,用于 PC 和开发板进行串口通信

    • Luban-Lite 自带的 test_hrtimer 命令行工具

软件配置

  1. 在 SDK 根目录下执行 scons --menuconfig,进入 menuconfig 的功能配置界面。

  2. 按如下选择,打开 test_hrtimer 命令行工具:
    Drivers options --->
        Drivers examples --->
            [*] Enable HRTimer driver test command
  3. 保存配置后,退出 menuconfig 并在根目录下执行下列命令进行编译:
    scons

    编译完成后,可以在终端中使用 test_hrtimer 命令行工具。

HRTimer 常见测试

使用 test_hrtimer 命令分别创建和测试下列两类定时器:
注: 关于命令参数详细描述,可查看 test_hrtimer 参数详解
  • 示例 1:启动一个 Oneshot 定时器,例如 hrtime0,超时时间为 2 秒 4 微秒。
    test_hrtimer -c 0 -s 2 -u 4 -d
    输出示例如下,表示创建成功:
    hrtimer0: Create a timer of 2.000004 sec, Oneshot mode
    当计时器超时时,可以看到如下调试日志输出:
    0/0 hrtimer0 timeout callback! Elapsed 2000012 us
  • 示例 2:启动一个 Period 定时器,例如 HRTimer0,超时时间为 3 秒 123 微秒,循环触发 19 次。

    test_hrtimer -c 0 -s 3 -u 123 -d -m period
    输出示例如下,表示创建成功:
    hrtimer0: Create a timer of 3.000123 sec, Period mode
        Will loop 19 times
    当计时器每次超时时,调试日志输出如下所示:
    0/19 hrtimer0 timeout callback! Elapsed 3000129 us
    1/19 hrtimer0 timeout callback! Elapsed 2995773 us
    ...
    19/19 hrtimer0 timeout callback! Elapsed 2995689 us

    本循环计数模式示例仅供演示使用,在持续大约 60 秒后会自动停止定时器。在实际使用过程中,如果配置了循环输出模式,则会不断输出直至配置为其他模式。

注:

示例中的运行 log 出现了一些误差,属于合理现象。这是因为 HRTimer 的精度是微秒级,因此 RTOS 的调度、调试信息输出都会干扰到定时器的精度。

test_hrtimer 参数详解

以下是一个 test_hrtimer 命令示例,配置 HRTimer0 为循环自动计数模式,并设置超时时间为 1 秒 3 微秒:
test_hrtimer -m oneshot -c 0 -s 2 -u 3
test_hrtimer 命令参数解释及示例如下所示:
test_hrtimer parameter
  • -m mode
    创建定时器类型,支持创建两类 Timer:
    • oneshot 类型定时器:单次触发,默认值。
      -m oneshot

      只触发一次超时,随后 Timer 会被注销。适用于需要一次性触发的任务,例如超时报警、单次事件等。

    • period 类型定时器:循环触发
      -m period

      循环触发超时,适用于需要周期性触发的任务,例如心跳包发送、定时数据采集等。

  • -c HRTimer channel number:

    指定使用的 HRTimer 通道编号,可选参数范围是 [0, 2]

    示例:-c 0-c 2

  • -s second

    设置定时器的秒数,必须大于 0。

    示例:-s 1
  • -u microsecon

    设置定时器的微秒数,必须大于 0

    示例:-u 3 表示设置微秒数为 3。
  • -d--debug

    显示超时日志,用于调试目的。

    test_hrtimer 命令默认不打印定时器的超时信息,需要使用 -d 参数打开日志。

  • h: 显示帮助信息。
    test_hrtimer -h
    Usage: test_hrtimer [options]:
         -m, --mode         mode of timer, oneshot/period
         -c, --channel      the number of hrtimer [0, 2]
         -s, --second       the second of timer (must > 0)
         -u, --microsecond  the microsecond of timer (must > 0)
         -d, --debug        show the timeout log
         -h, --usage