官方文档翻译-ESP32-High Resolution Timer
高分辨率定時器
概述
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任務中定時器超時的時候調用。
- To create a timer, call esp_timer_create().
- 創建計時器,請調用esp_timer_create()。
- To delete the timer when it is no longer needed, call esp_timer_delete().
- 刪除定時器,請調用esp_timer_delete()。
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參考
頭文件
- esp32/include/esp_timer.h
本文翻譯自:https://esp-idf.readthedocs.io/en/latest/api-reference/system/esp_timer.html
翻譯水平有限,如有錯誤歡迎指正
總結
以上是生活随笔為你收集整理的官方文档翻译-ESP32-High Resolution Timer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看SSID的mac地址
- 下一篇: 官方文档翻译-ESP32-SPI Fla