Although FreeRTOS provides software timers, these timers have a few limitations:
虽然FreeRTOS提供软件定时器,但这些定时器有一些限制:
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_timer
API提供这些。其内部,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_timer
APISingle 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
任务中定时器超时的时候调用。
esp_timer_create()
.esp_timer_create()
。esp_timer_delete()
.esp_timer_delete()
。The timer can be started in one-shot mode or in periodic mode.
定时器可以在单次模式或周期模式下启动。
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()
,并设定触发时间。当回调被调用时,定时器被认为是停止的。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()
函数的返回值:
本文翻译自:https://esp-idf.readthedocs.io/en/latest/api-reference/system/esp_timer.html
翻译水平有限,如有错误欢迎指正