timer.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Timer
  2. =====
  3. :link_to_translation:`zh_CN:[中文]`
  4. {IDF_TARGET_INT_CLR_REG: default="int_clr", esp32="int_clr_timers"}
  5. Introduction
  6. ------------
  7. The {IDF_TARGET_NAME} chip contains two hardware timer groups. Each group has two general-purpose hardware timers. They are all 64-bit generic timers based on 16-bit prescalers and 64-bit up / down counters which are capable of being auto-reloaded.
  8. Functional Overview
  9. -------------------
  10. The following sections of this document cover the typical steps to configure and operate a timer:
  11. * :ref:`timer-api-timer-initialization` - covers which parameters should be set up to get the timer working, and also what specific functionality is provided depending on the timer configuration.
  12. * :ref:`timer-api-timer-control` - describes how to read a timer's value, pause or start a timer, and change how it operates.
  13. * :ref:`timer-api-alarms` - shows how to set and use alarms.
  14. * :ref:`timer-api-interrupts`- explains how to enable and use interrupts.
  15. .. _timer-api-timer-initialization:
  16. Timer Initialization
  17. ^^^^^^^^^^^^^^^^^^^^
  18. The two {IDF_TARGET_NAME} timer groups, with two timers in each, provide the total of four individual timers for use. An {IDF_TARGET_NAME} timer group should be identified using :cpp:type:`timer_group_t`. An individual timer in a group should be identified with :cpp:type:`timer_idx_t`.
  19. First of all, the timer should be initialized by calling the function :cpp:func:`timer_init` and passing a structure :cpp:type:`timer_config_t` to it to define how the timer should operate. In particular, the following timer parameters can be set:
  20. * **Divider**: Sets how quickly the timer's counter is "ticking". The setting :cpp:member:`divider` is used as a divisor of the incoming 80 MHz APB_CLK clock.
  21. * **Mode**: Sets if the counter should be incrementing or decrementing. It can be defined using :cpp:member:`counter_dir` by selecting one of the values from :cpp:type:`timer_count_dir_t`.
  22. * **Counter Enable**: If the counter is enabled, it will start incrementing / decrementing immediately after calling :cpp:func:`timer_init`. You can change the behavior with :cpp:member:`counter_en` by selecting one of the values from :cpp:type:`timer_start_t`.
  23. * **Alarm Enable**: Can be set using :cpp:member:`alarm_en`.
  24. * **Auto Reload**: Sets if the counter should :cpp:member:`auto_reload` the initial counter value on the timer's alarm or continue incrementing or decrementing.
  25. * **Interrupt Type**: Select which interrupt type should be triggered on the timer's alarm. Set the value defined in :cpp:type:`timer_intr_mode_t`.
  26. To get the current values of the timer's settings, use the function :cpp:func:`timer_get_config`.
  27. .. _timer-api-timer-control:
  28. Timer Control
  29. ^^^^^^^^^^^^^
  30. Once the timer is enabled, its counter starts running. To enable the timer, call the function :cpp:func:`timer_init` with :cpp:member:`counter_en` set to ``true``, or call :cpp:func:`timer_start`. You can specify the timer's initial counter value by calling :cpp:func:`timer_set_counter_value`. To check the timer's current value, call :cpp:func:`timer_get_counter_value` or :cpp:func:`timer_get_counter_time_sec`.
  31. To pause the timer at any time, call :cpp:func:`timer_pause`. To resume it, call :cpp:func:`timer_start`.
  32. To reconfigure the timer, you can call :cpp:func:`timer_init`. This function is described in Section :ref:`timer-api-timer-initialization`.
  33. You can also reconfigure the timer by using dedicated functions to change individual settings:
  34. ============= =================================== ==========================================================================
  35. Setting Dedicated Function Description
  36. ============= =================================== ==========================================================================
  37. Divider :cpp:func:`timer_set_divider` Change the rate of ticking. To avoid unpredictable results, the timer should be paused when changing the divider. If the timer is running, :cpp:func:`timer_set_divider` pauses it, change the setting, and start the timer again.
  38. Mode :cpp:func:`timer_set_counter_mode` Set if the counter should be incrementing or decrementing
  39. Auto Reload :cpp:func:`timer_set_auto_reload` Set if the initial counter value should be reloaded on the timer's alarm
  40. ============= =================================== ==========================================================================
  41. .. _timer-api-alarms:
  42. Alarms
  43. ^^^^^^
  44. To set an alarm, call the function :cpp:func:`timer_set_alarm_value` and then enable the alarm using :cpp:func:`timer_set_alarm`. The alarm can also be enabled during the timer initialization stage, when :cpp:func:`timer_init` is called.
  45. After the alarm is enabled, and the timer reaches the alarm value, the following two actions can occur depending on the configuration:
  46. * An interrupt will be triggered if previously configured. See Section :ref:`timer-api-interrupts` on how to configure interrupts.
  47. * When :cpp:member:`auto_reload` is enabled, the timer's counter will automatically be reloaded to start counting again from a previously configured value. This value should be set in advance with :cpp:func:`timer_set_counter_value`.
  48. .. note::
  49. * If an alarm value is set and the timer has already reached this value, the alarm is triggered immediately.
  50. * Once triggered, the alarm is disabled automatically and needs to be re-enabled to trigger again.
  51. To check the specified alarm value, call :cpp:func:`timer_get_alarm_value`.
  52. .. _timer-api-interrupts:
  53. Interrupts
  54. ^^^^^^^^^^
  55. Registration of the interrupt handler for a specific timer or a timer group can be done by calling :cpp:func:`timer_isr_register`.
  56. To enable interrupts for a timer group, call :cpp:func:`timer_group_intr_enable`, for a specific timer call :cpp:func:`timer_enable_intr`.
  57. To disable interrupts for a timer group, call :cpp:func:`timer_group_intr_disable`, for a specified timer, call :cpp:func:`timer_disable_intr`.
  58. When handling an interrupt within an interrupt serivce routine (ISR), the interrupt status bit needs to be explicitly cleared. To do that, set the ``TIMERGN.{IDF_TARGET_INT_CLR_REG}.tM`` structure, defined in :component_file:`soc/soc/{IDF_TARGET_PATH_NAME}/include/soc/timer_group_struct.h`. In this structure, ``N`` is the timer group number [0, 1], ``M`` is the timer number [0, 1]. For example, to clear an interrupt status bit for the timer 1 in the timer group 0, call the following::
  59. TIMERG0.{IDF_TARGET_INT_CLR_REG}.t1 = 1
  60. For more information on how to use interrupts, please see the application example below.
  61. Application Example
  62. -------------------
  63. The 64-bit hardware timer example: :example:`peripherals/timer_group`.
  64. API Reference
  65. -------------
  66. .. include-build-file:: inc/timer.inc
  67. .. include-build-file:: inc/timer_types.inc