gptimer.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. 通用定时器
  2. =====================
  3. 简介
  4. -----------------
  5. 通用定时器是 {IDF_TARGET_NAME} 定时器组外设的驱动程序。{IDF_TARGET_NAME} 硬件定时器分辨率高,具有灵活的报警功能。定时器内部计数器达到特定目标数值的行为被称为定时器报警。定时器报警时将调用用户注册的不同定时器回调函数。
  6. 通用定时器通常在以下场景中使用:
  7. - 如同挂钟一般自由运行,随时随地获取高分辨率时间戳;
  8. - 生成周期性警报,定期触发事件;
  9. - 生成一次性警报,在目标时间内响应。
  10. 功能概述
  11. -----------------
  12. 下文介绍了配置和操作定时器的常规步骤:
  13. - :ref:`gptimer-resource-allocation` - 获取定时器句柄应设置的参数,以及如何在通用定时器完成工作时回收资源。
  14. - :ref:`set-and-get-count-value` - 如何强制定时器从起点开始计数,以及如何随时获取计数值。
  15. - :ref:`set-up-alarm-action` - 启动警报事件应设置的参数。
  16. - :ref:`gptimer-register-event-callbacks` - 如何将用户的特定代码挂载到警报事件回调函数。
  17. - :ref:`enable-and-disable-timer` - 如何使能和禁用定时器。
  18. - :ref:`start-and-stop-timer` - 通过不同报警行为启动定时器的典型使用场景。
  19. - :ref:`gptimer-power-management` - 选择不同的时钟源将会如何影响功耗。
  20. - :ref:`gptimer-iram-safe` - 在 cache 禁用的情况下,如何更好地让定时器处理中断事务以及实现 IO 控制功能。
  21. - :ref:`gptimer-thread-safety` - 驱动程序保证哪些 API 线程安全。
  22. - :ref:`gptimer-kconfig-options` - 支持的 Kconfig 选项,这些选项会对驱动程序行为产生不同影响。
  23. .. _gptimer-resource-allocation:
  24. 资源分配
  25. ^^^^^^^^^^^^^^^^^^
  26. 不同的 ESP 芯片可能有不同数量的独立定时器组,每组内也可能有若干个独立定时器。[1]_
  27. 通用定时器实例由 :cpp:type:`gptimer_handle_t` 表示。后台驱动会在资源池中管理所有可用的硬件资源,这样您便无需考虑硬件所属的定时器以及定时器组。
  28. 要安装一个定时器实例,需要提前提供配置结构体 :cpp:type:`gptimer_config_t`:
  29. - :cpp:member:`gptimer_config_t::clk_src` 选择定时器的时钟源。:cpp:type:`gptimer_clock_source_t` 中列出多个可用时钟,仅可选择其中一个时钟。了解不同时钟源对功耗的影响,请查看章节 :ref:`gptimer-power-management`。
  30. - :cpp:member:`gptimer_config_t::direction` 设置定时器的计数方向,:cpp:type:`gptimer_count_direction_t` 中列出多个支持的方向,仅可选择其中一个方向。
  31. - :cpp:member:`gptimer_config_t::resolution_hz` 设置内部计数器的分辨率。计数器每滴答一次相当于 **1 / resolution_hz** 秒。
  32. - 选用 :cpp:member:`gptimer_config_t::intr_shared` 设置是否将定时器中断源标记为共享源。了解共享中断的优缺点,请参考 :doc:`Interrupt Handling <../../api-reference/system/intr_alloc>`。
  33. 完成上述结构配置之后,可以将结构传递给 :cpp:func:`gptimer_new_timer`,用以实例化定时器实例并返回定时器句柄。
  34. 该函数可能由于内存不足、参数无效等错误而失败。具体来说,当没有更多的空闲定时器(即所有硬件资源已用完)时,将返回 :c:macro:`ESP_ERR_NOT_FOUND`。可用定时器总数由 :c:macro:`SOC_TIMER_GROUP_TOTAL_TIMERS` 表示,不同的 ESP 芯片该数值不同。
  35. 如已不再需要之前创建的通用定时器实例,应通过调用 :cpp:func:`gptimer_del_timer` 回收定时器,以便底层硬件定时器用于其他目的。在删除通用定时器句柄之前,请通过 :cpp:func:`gptimer_disable` 禁用定时器,或者通过 :cpp:func:`gptimer_enable` 确认定时器尚未使能。
  36. 创建分辨率为 1 MHz 的通用定时器句柄
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. .. code:: c
  39. gptimer_handle_t gptimer = NULL;
  40. gptimer_config_t timer_config = {
  41. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  42. .direction = GPTIMER_COUNT_UP,
  43. .resolution_hz = 1 * 1000 * 1000, // 1MHz, 1 tick = 1us
  44. };
  45. ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));
  46. .. _set-and-get-count-value:
  47. 设置和获取计数值
  48. ^^^^^^^^^^^^^^^^^^^^^^^^^
  49. 创建通用定时器时,内部计数器将默认重置为零。计数值可以通过 :cpp:func:`gptimer_set_raw_count` 异步更新。最大计数值取决于硬件定时器的位宽,这也会在 SOC 宏 :c:macro:`SOC_TIMER_GROUP_COUNTER_BIT_WIDTH` 中有所反映。当更新活动定时器的原始计数值时,定时器将立即从新值开始计数。
  50. 计数值可以随时通过 :cpp:func:`gptimer_get_raw_count` 获取。
  51. .. _set-up-alarm-action:
  52. 设置警报动作
  53. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  54. 对于大多数通用定时器使用场景而言,应在启动定时器之前设置警报动作,但不包括简单的挂钟场景,该场景仅需自由运行的定时器。设置警报动作,需要根据如何使用警报事件来配置 :cpp:type:`gptimer_alarm_config_t` 的不同参数:
  55. - :cpp:member:`gptimer_alarm_config_t::alarm_count` 设置触发警报事件的目标计数值。设置警报值时还需考虑计数方向。尤其是当 :cpp:member:`gptimer_alarm_config_t::auto_reload_on_alarm` 为 true 时,:cpp:member:`gptimer_alarm_config_t::alarm_count` 和 :cpp:member:`gptimer_alarm_config_t::reload_count` 不能设置为相同的值,因为警报值和重载值相同时没有意义。
  56. - :cpp:member:`gptimer_alarm_config_t::reload_count` 代表警报事件发生时要重载的计数值。此配置仅在 :cpp:member:`gptimer_alarm_config_t::auto_reload_on_alarm` 设置为 true 时生效。
  57. - :cpp:member:`gptimer_alarm_config_t::auto_reload_on_alarm` 标志设置是否使能自动重载功能。如果使能,硬件定时器将在警报事件发生时立即将 :cpp:member:`gptimer_alarm_config_t::reload_count` 的值重载到计数器中。
  58. 要使警报配置生效,需要调用 :cpp:func:`gptimer_set_alarm_action`。特别是当 :cpp:type:`gptimer_alarm_config_t` 设置为 ``NULL`` 时,报警功能将被禁用。
  59. .. 注解::
  60. 如果警报值已设置且定时器超过该值,则会立即触发警报。
  61. .. _gptimer-register-event-callbacks:
  62. 注册事件回调函数
  63. ^^^^^^^^^^^^^^^^^^^^^^^^
  64. 定时器启动后,可动态产生特定事件(如“警报事件”)。如需在事件发生时调用某些函数,请通过 :cpp:func:`gptimer_register_event_callbacks` 将函数挂载到中断服务例程 (ISR)。:cpp:type:`gptimer_event_callbacks_t` 中列出了所有支持的事件回调函数:
  65. - :cpp:member:`gptimer_event_callbacks_t::on_alarm` 设置警报事件的回调函数。由于此函数在 ISR 上下文中调用,必须确保该函数不会试图阻塞(例如,确保仅从函数内调用具有 ``ISR`` 后缀的 FreeRTOS API)。函数原型在 :cpp:type:`gptimer_alarm_cb_t` 中有所声明。
  66. 您也可以通过参数 ``user_data`` 将自己的上下文保存到 :cpp:func:`gptimer_register_event_callbacks` 中。用户数据将直接传递给回调函数。
  67. 此功能将为定时器延迟安装中断服务,但不使能中断服务。所以,请在 :cpp:func:`gptimer_enable` 之前调用这一函数,否则将返回 :c:macro:`ESP_ERR_INVALID_STATE` 错误。了解详细信息,请查看章节 :ref:`enable-and-disable-timer`。
  68. .. _enable-and-disable-timer:
  69. 使能和禁用定时器
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. 在对定时器进行 IO 控制之前,需要先调用 :cpp:func:`gptimer_enable` 使能定时器。此函数功能如下:
  72. * 此函数将把定时器驱动程序的状态从 **init** 切换为 **enable**。
  73. * 如果 :cpp:func:`gptimer_register_event_callbacks` 已经延迟安装中断服务,此函数将使能中断服务。
  74. * 如果选择了特定的时钟源(例如 APB 时钟),此函数将获取适当的电源管理锁。了解更多信息,请查看章节 :ref:`gptimer-power-management`。
  75. 调用 :cpp:func:`gptimer_disable` 会进行相反的操作,即将定时器驱动程序恢复到 **init** 状态,禁用中断服务并释放电源管理锁。
  76. .. _start-and-stop-timer:
  77. 启动和停止定时器
  78. ^^^^^^^^^^^^^^^^
  79. 启动和停止是定时器的基本 IO 操作。调用 :cpp:func:`gptimer_start` 可以使内部计数器开始工作,而 :cpp:func:`gptimer_stop` 可以使计数器停止工作。下文说明了如何在存在或不存在警报事件的情况下启动定时器。
  80. 将定时器作为挂钟启动
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. .. code:: c
  83. ESP_ERROR_CHECK(gptimer_enable(gptimer));
  84. ESP_ERROR_CHECK(gptimer_start(gptimer));
  85. // Retrieve the timestamp at anytime
  86. uint64_t count;
  87. ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer, &count));
  88. 触发周期性事件
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~
  90. .. code:: c
  91. typedef struct {
  92. uint64_t event_count;
  93. } example_queue_element_t;
  94. static bool example_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  95. {
  96. BaseType_t high_task_awoken = pdFALSE;
  97. QueueHandle_t queue = (QueueHandle_t)user_ctx;
  98. // Retrieve the count value from event data
  99. example_queue_element_t ele = {
  100. .event_count = edata->count_value
  101. };
  102. // Optional: send the event data to other task by OS queue
  103. // Don't introduce complex logics in callbacks
  104. // Suggest dealing with event data in the main loop, instead of in this callback
  105. xQueueSendFromISR(queue, &ele, &high_task_awoken);
  106. // return whether we need to yield at the end of ISR
  107. return high_task_awoken == pdTRUE;
  108. }
  109. gptimer_alarm_config_t alarm_config = {
  110. .reload_count = 0, // counter will reload with 0 on alarm event
  111. .alarm_count = 1000000, // period = 1s @resolution 1MHz
  112. .flags.auto_reload_on_alarm = true, // enable auto-reload
  113. };
  114. ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  115. gptimer_event_callbacks_t cbs = {
  116. .on_alarm = example_timer_on_alarm_cb, // register user callback
  117. };
  118. ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, queue));
  119. ESP_ERROR_CHECK(gptimer_enable(gptimer));
  120. ESP_ERROR_CHECK(gptimer_start(gptimer));
  121. 触发一次性事件
  122. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. .. code:: c
  124. typedef struct {
  125. uint64_t event_count;
  126. } example_queue_element_t;
  127. static bool example_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  128. {
  129. BaseType_t high_task_awoken = pdFALSE;
  130. QueueHandle_t queue = (QueueHandle_t)user_ctx;
  131. // Stop timer the sooner the better
  132. gptimer_stop(timer);
  133. // Retrieve the count value from event data
  134. example_queue_element_t ele = {
  135. .event_count = edata->count_value
  136. };
  137. // Optional: send the event data to other task by OS queue
  138. xQueueSendFromISR(queue, &ele, &high_task_awoken);
  139. // return whether we need to yield at the end of ISR
  140. return high_task_awoken == pdTRUE;
  141. }
  142. gptimer_alarm_config_t alarm_config = {
  143. .alarm_count = 1 * 1000 * 1000, // alarm target = 1s @resolution 1MHz
  144. };
  145. ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  146. gptimer_event_callbacks_t cbs = {
  147. .on_alarm = example_timer_on_alarm_cb, // register user callback
  148. };
  149. ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, queue));
  150. ESP_ERROR_CHECK(gptimer_enable(gptimer));
  151. ESP_ERROR_CHECK(gptimer_start(gptimer));
  152. 警报值动态更新
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. 通过更改 :cpp:member:`gptimer_alarm_event_data_t::alarm_value`,可以在 ISR 程序回调中动态更新警报值。警报值将在回调函数返回后更新。
  155. .. code:: c
  156. typedef struct {
  157. uint64_t event_count;
  158. } example_queue_element_t;
  159. static bool example_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  160. {
  161. BaseType_t high_task_awoken = pdFALSE;
  162. QueueHandle_t queue = (QueueHandle_t)user_data;
  163. // Retrieve the count value from event data
  164. example_queue_element_t ele = {
  165. .event_count = edata->count_value
  166. };
  167. // Optional: send the event data to other task by OS queue
  168. xQueueSendFromISR(queue, &ele, &high_task_awoken);
  169. // reconfigure alarm value
  170. gptimer_alarm_config_t alarm_config = {
  171. .alarm_count = edata->alarm_value + 1000000, // alarm in next 1s
  172. };
  173. gptimer_set_alarm_action(timer, &alarm_config);
  174. // return whether we need to yield at the end of ISR
  175. return high_task_awoken == pdTRUE;
  176. }
  177. gptimer_alarm_config_t alarm_config = {
  178. .alarm_count = 1000000, // initial alarm target = 1s @resolution 1MHz
  179. };
  180. ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  181. gptimer_event_callbacks_t cbs = {
  182. .on_alarm = example_timer_on_alarm_cb, // register user callback
  183. };
  184. ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, queue));
  185. ESP_ERROR_CHECK(gptimer_enable(gptimer));
  186. ESP_ERROR_CHECK(gptimer_start(gptimer, &alarm_config));
  187. .. _gptimer-power-management:
  188. 电源管理
  189. ^^^^^^^^^^^^^^^^^
  190. 当使能电源管理时(即 :ref:`CONFIG_PM_ENABLE` 已打开),系统将在进入 Light-sleep 模式之前调整 APB 频率,从而可能会改变通用定时器的计数步骤周期,导致计时不准确。
  191. 然而,驱动程序可以通过获取类型为 :cpp:enumerator:`ESP_PM_APB_FREQ_MAX` 的电源管理锁来阻止系统更改 APB 频率。每当驱动程序创建一个通用定时器实例,且该实例选择 :cpp:enumerator:`GPTIMER_CLK_SRC_APB` 作为其时钟源的时,驱动程序会确保在通过 :cpp:func:`gptimer_enable` 使能定时器时,已经获取了电源管理锁。同样,当为该定时器调用 :cpp:func:`gptimer_disable` 时,驱动程序会释放电源管理锁。
  192. 如果选择 :cpp:enumerator:`GPTIMER_CLK_SRC_XTAL` 等其他时钟源,那么驱动程序不会安装电源管理锁。只要时钟源仍可提供足够的分辨率,XTAL 时钟源就更适合低功耗应用。
  193. .. _gptimer-iram-safe:
  194. IRAM 安全
  195. ^^^^^^^^^^^^^^^^^^
  196. 默认情况下,当 cache 因写入或擦除 flash 等原因而被禁用时,通用定时器的中断服务将会延迟,造成警报中断无法及时执行。在实时应用程序中通常需要避免这一情况发生。
  197. 调用 Kconfig 选项 :ref:`CONFIG_GPTIMER_ISR_IRAM_SAFE` 可实现如下功能:
  198. - 即使禁用 cache 也可使能正在运行的中断
  199. - 将 ISR 使用的所有函数放入 IRAM [2]_
  200. - 将驱动程序对象放入 DRAM(以防意外映射到 PSRAM)
  201. 这将允许中断在 cache 禁用时运行,但会增加 IRAM 使用量。
  202. 调用另一 Kconfig 选项 :ref:`CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` 也可将常用的 IO 控制功能放入 IRAM,以便这些函数在 cache 禁用时也能执行。常用的 IO 控制功能如下:
  203. - :cpp:func:`gptimer_start`
  204. - :cpp:func:`gptimer_stop`
  205. - :cpp:func:`gptimer_get_raw_count`
  206. - :cpp:func:`gptimer_set_raw_count`
  207. - :cpp:func:`gptimer_set_alarm_action`
  208. .. _gptimer-thread-safety:
  209. 线程安全
  210. ^^^^^^^^^^^^^^^^^^
  211. 驱动程序会保证工厂函数 :cpp:func:`gptimer_new_timer` 的线程安全,这意味着您可以从不同的 RTOS 任务中调用这一函数,而无需额外的锁保护。
  212. 由于驱动程序通过使用临界区来防止这些函数在任务和 ISR 中同时被调用,所以以下函数能够在 ISR 上下文中运行。
  213. - :cpp:func:`gptimer_start`
  214. - :cpp:func:`gptimer_stop`
  215. - :cpp:func:`gptimer_get_raw_count`
  216. - :cpp:func:`gptimer_set_raw_count`
  217. - :cpp:func:`gptimer_set_alarm_action`
  218. 将 :cpp:type:`gptimer_handle_t` 作为第一个位置参数的其他函数不被视作线程安全,也就是说应该避免从多个任务中调用这些函数。
  219. .. _gptimer-kconfig-options:
  220. Kconfig 选项
  221. ^^^^^^^^^^^^^^^^^^^^^^
  222. - :ref:`CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` 控制放置通用定时器控制函数(IRAM 或 flash)的位置。了解更多信息,请参考章节 :ref:`gptimer-iram-safe`。
  223. - :ref:`CONFIG_GPTIMER_ISR_IRAM_SAFE` 控制默认 ISR 程序在 cache 禁用时是否可以运行。了解更多信息,请参考章节 :ref:`gptimer-iram-safe`。
  224. - :ref:`CONFIG_GPTIMER_ENABLE_DEBUG_LOG` 用于启用调试日志输出。启用这一选项将增加固件二进制文件大小。
  225. 应用示例
  226. ------------------
  227. * 示例 :example:`peripherals/timer_group/gptimer` 中列出了通用定时器的典型用例。
  228. API 参考
  229. -------------------
  230. .. include-build-file:: inc/gptimer.inc
  231. .. include-build-file:: inc/timer_types.inc
  232. .. [1]
  233. 不同 ESP 芯片系列的通用定时器实例数量可能不同。了解详细信息,请参考《{IDF_TARGET_NAME} 技术参考手册》 > 章节定时器组 (TIMG) [`PDF <{IDF_TARGET_TRM_CN_URL}#timg>`__]。驱动程序不会禁止您申请更多的定时器,但是当所有可用的硬件资源用完时将会返回错误。在分配资源时,请务必检查返回值(例如 :cpp:func:`gptimer_new_timer`)。
  234. .. [2]
  235. :cpp:member:`gptimer_event_callbacks_t::on_alarm` 回调函数和这一函数调用的函数也需放在 IRAM 中,请自行处理。