官方文档翻译-ESP32-High Resolution Timer

9 篇文章 1 订阅
8 篇文章 1 订阅

高分辨率定时器

概述

Although FreeRTOS provides software timers, these timers have a few limitations:
虽然FreeRTOS提供软件定时器,但这些定时器有一些限制:

  • Maximum resolution is equal to RTOS tick period
  • 最大分辨率等于RTOS滴答周期
  • Timer callbacks are dispatched from a low-priority task
  • 定时器回调的任务优先级低

Hardware timers are free from both of the limitations, but often they are less convenient to use. For example, application components may need timer events to fire at certain times in the future, but the hardware timer only contains one “compare” value used for interrupt generation. This means that some facility needs to be built on top of the hardware timer to manage the list of pending events can dispatch the callbacks for these events as corresponding hardware interrupts happen.
硬件定时器不受这两个限制,但通常使用起来不方便。例如,应用程序组件可能需要定时器事件才能在将来的特定时间触发,但硬件定时器只包含一个“比较”值用于产生中断。这意味着需要在硬件定时器的基础上构建一些设施来管理待处理事件列表,以便在相应的硬件中断发生时调度这些事件的回调。

esp_timer set of APIs provide such facility. Internally, esp_timer uses a 32-bit hardware timer (FRC1, “legacy” timer). esp_timer provides one-shot and periodic timers, microsecond time resolution, and 64-bit range.
esp_timerAPI提供这些。其内部,esp_timer使用32位硬件定时器(FRC1,“传统”定时器)。esp_timer提供一次性和周期性定时器,微秒级的分辨率和64位范围。

Timer callbacks are dispatched from a high-priority esp_timer task. Because all the callbacks are dispatched from the same task, it is recommended to only do the minimal possible amount of work from the callback itself, posting an event to a lower priority task using a queue instead.
定时器回调由高优先级的esp_timer任务分派。由于所有回调都是从同一个任务调度的,因此建议仅从回调做最少量的工作,//FIXME:而不是使用队列将事件发布到较低优先级的任务。

使用esp_timerAPI

Single timer is represented by esp_timer_handle_t type. Timer has a callback function associated with it. This callback function is called from the esp_timer task each time the timer elapses.
单个计时器由esp_timer_handle_t类型表示。Timer有一个与之相关的回调函数。这个回调函数在esp_timer任务中定时器超时的时候调用。

The timer can be started in one-shot mode or in periodic mode.
定时器可以在单次模式或周期模式下启动。

  • To start the timer in one-shot mode, call esp_timer_start_once(), passing the time interval after which the callback should be called. When the callback gets called, the timer is considered to be stopped.
  • 要以单发模式启动计时器,请调用esp_timer_start_once(),并设定触发时间。当回调被调用时,定时器被认为是停止的。
  • To start the timer in periodic mode, call esp_timer_start_periodic(), passing the period with which the callback should be called. The timer keeps running until esp_timer_stop() is called.
  • 要以周期模式启动定时器,请调用esp_timer_start_periodic(),并设定触发周期。调用esp_timer_stop()停止定时器。

Note that the timer must not be running when esp_timer_start_once() or esp_timer_start_periodic()is called. To restart a running timer, call esp_timer_stop() first, then call one of the start functions.
请注意,定时器运行前需调用esp_timer_start_once()或者esp_timer_start_periodic()。要重启正在运行的定时器,先调用esp_timer_stop(),然后调用其中一个启动函数。

获取当前时间

esp_timer also provides a convenience function to obtain the time passed since start-up, with microsecond precision: esp_timer_get_time(). This function returns the number of microseconds since esp_timer was initialized, which usually happens shortly before app_main function is called.
esp_timer还提供了一个方便的功能来获得自启动以来的时间,精确到微秒:esp_timer_get_time()。该函数返回从esp_timer初始化完成到函数调用经过的微秒数,esp_timer初始化通常在app_main之前一些完成。

Unlike gettimeofday function, values returned by esp_timer_get_time():
与gettimeofday函数不同,esp_timer_get_time()函数的返回值:

  • Start from zero after the chip wakes up from deep sleep
  • 芯片从深度睡眠中醒来后,从零开始
  • Do not have timezone or DST adjustments applied
  • 没有应用时区或DST调整

API参考

头文件

本文翻译自:https://esp-idf.readthedocs.io/en/latest/api-reference/system/esp_timer.html

翻译水平有限,如有错误欢迎指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值