You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/api-reference/peripherals/gptimer.rst
+33-18Lines changed: 33 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
===============================
1
2
General Purpose Timer (GPTimer)
2
3
===============================
3
4
@@ -11,7 +12,7 @@ This document introduces the features of the General Purpose Timer (GPTimer) dri
11
12
:depth: 2
12
13
13
14
Overview
14
-
--------
15
+
========
15
16
16
17
GPTimer is a dedicated driver for the {IDF_TARGET_NAME} [`Timer Group peripheral <{IDF_TARGET_TRM_EN_URL}#timg>`__]. This timer can select different clock sources and prescalers to meet the requirements of nanosecond-level resolution. Additionally, it has flexible timeout alarm functions and allows automatic updating of the count value at the alarm moment, achieving very precise timing cycles.
17
18
@@ -24,7 +25,7 @@ Based on the **high resolution, high count range, and high response** capabiliti
24
25
- etc.
25
26
26
27
Quick Start
27
-
-----------
28
+
===========
28
29
29
30
This section provides a concise overview of how to use the GPTimer driver. Through practical examples, it demonstrates how to initialize and start a timer, configure alarm events, and register callback functions. The typical usage flow is as follows:
30
31
@@ -54,7 +55,7 @@ This section provides a concise overview of how to use the GPTimer driver. Throu
54
55
}
55
56
56
57
Creating and Starting a Timer
57
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58
+
-----------------------------
58
59
59
60
First, we need to create a timer instance. The following code shows how to create a timer with a resolution of 1 MHz:
60
61
@@ -104,7 +105,7 @@ The :cpp:func:`gptimer_start` and :cpp:func:`gptimer_stop` functions follow the
104
105
However, note that when the timer is in the **intermediate state** of starting (the start has begun but not yet completed), if another thread calls the :cpp:func:`gptimer_start` or :cpp:func:`gptimer_stop` function, it will return the :c:macro:`ESP_ERR_INVALID_STATE` error to avoid triggering uncertain behavior.
105
106
106
107
Setting and Getting the Count Value
107
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
108
+
-----------------------------------
108
109
109
110
When a timer is newly created, its internal counter value defaults to zero. You can set other count values using the :cpp:func:`gptimer_set_raw_count` function. The maximum count value depends on the bit width of the hardware timer (usually no less than ``54 bits``).
110
111
@@ -126,7 +127,7 @@ The :cpp:func:`gptimer_get_raw_count` function is used to get the current count
126
127
double time = (double)count / resolution_hz;
127
128
128
129
Triggering Periodic Alarm Events
129
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
130
+
--------------------------------
130
131
131
132
In addition to the timestamp function, the general-purpose timer also supports alarm functions. The following code shows how to set a periodic alarm that triggers once per second:
132
133
@@ -194,7 +195,7 @@ The supported event callback functions for GPTimer are as follows:
194
195
Be sure to register the callback function before calling :cpp:func:`gptimer_enable`, otherwise the timer event will not correctly trigger the interrupt service.
195
196
196
197
Triggering One-Shot Alarm Events
197
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
198
+
--------------------------------
198
199
199
200
Some application scenarios only require triggering a one-shot alarm interrupt. The following code shows how to set a one-shot alarm that triggers after 1 second:
200
201
@@ -242,17 +243,17 @@ Some application scenarios only require triggering a one-shot alarm interrupt. T
242
243
Unlike periodic alarms, the above code disables the auto-reload function when configuring the alarm behavior. This means that after the alarm event occurs, the timer will not automatically reload to the preset count value but will continue counting until it overflows. If you want the timer to stop immediately after the alarm, you can call :cpp:func:`gptimer_stop` in the callback function.
243
244
244
245
Resource Recycling
245
-
^^^^^^^^^^^^^^^^^^
246
+
------------------
246
247
247
248
When the timer is no longer needed, you should call the :cpp:func:`gptimer_delete_timer` function to release software and hardware resources. Before deleting, ensure that the timer is already stopped.
248
249
249
250
Advanced Features
250
-
-----------------
251
+
=================
251
252
252
253
After understanding the basic usage, we can further explore more features of the GPTimer driver.
253
254
254
255
Dynamic Alarm Value Update
255
-
^^^^^^^^^^^^^^^^^^^^^^^^^^
256
+
--------------------------
256
257
257
258
The GPTimer driver supports dynamically updating the alarm value in the interrupt callback function by calling the :cpp:func:`gptimer_set_alarm_action` function, thereby implementing a monotonic software timer list. The following code shows how to reset the next alarm trigger time when the alarm event occurs:
258
259
@@ -300,15 +301,15 @@ The GPTimer driver supports dynamically updating the alarm value in the interrup
300
301
.. _gptimer-etm-event-and-task:
301
302
302
303
GPTimer's ETM Events and Tasks
303
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304
+
------------------------------
304
305
305
306
GPTimer can generate various events that can be connected to the :doc:`ETM </api-reference/peripherals/etm>` module. The event types are listed in :cpp:type:`gptimer_etm_event_type_t`. Users can create an ``ETM event`` handle by calling :cpp:func:`gptimer_new_etm_event`.
306
307
GPTimer also supports some tasks that can be triggered by other events and executed automatically. The task types are listed in :cpp:type:`gptimer_etm_task_type_t`. Users can create an ``ETM task`` handle by calling :cpp:func:`gptimer_new_etm_task`.
307
308
308
309
For how to connect the timer events and tasks to the ETM channel, please refer to the :doc:`ETM </api-reference/peripherals/etm>` documentation.
309
310
310
311
Power Management
311
-
^^^^^^^^^^^^^^^^
312
+
----------------
312
313
313
314
When power management :ref:`CONFIG_PM_ENABLE` is enabled, the system may adjust or disable the clock source before entering sleep mode, causing the GPTimer to lose accuracy.
314
315
@@ -319,7 +320,7 @@ To prevent this, the GPTimer driver creates a power management lock internally.
319
320
Besides disabling the clock source, the system can also power down the GPTimer before entering sleep mode to further reduce power consumption. To achieve this, set :cpp:member:`gptimer_config_t::allow_pd` to ``true``. Before the system enters sleep mode, the GPTimer register context will be backed up to memory and restored after the system wakes up. Note that enabling this option reduces power consumption but increases memory usage. Therefore, you need to balance power consumption and memory usage when using this feature.
320
321
321
322
Thread Safety
322
-
^^^^^^^^^^^^^
323
+
-------------
323
324
324
325
The driver uses critical sections to ensure atomic operations on registers. Key members in the driver handle are also protected by critical sections. The driver's internal state machine uses atomic instructions to ensure thread safety, with state checks preventing certain invalid concurrent operations (e.g., conflicts between `start` and `stop`). Therefore, GPTimer driver APIs can be used in a multi-threaded environment without extra locking.
325
326
@@ -335,7 +336,7 @@ The following functions can also be used in an interrupt context:
335
336
- :cpp:func:`gptimer_set_alarm_action`
336
337
337
338
Cache Safety
338
-
^^^^^^^^^^^^
339
+
------------
339
340
340
341
When the file system performs Flash read/write operations, the system temporarily disables the Cache function to avoid errors when loading instructions and data from Flash. This causes the GPTimer interrupt handler to be unresponsive during this period, preventing the user callback function from executing in time. If you want the interrupt handler to run normally when the Cache is disabled, you can enable the :ref:`CONFIG_GPTIMER_ISR_CACHE_SAFE` option.
341
342
@@ -344,7 +345,7 @@ When the file system performs Flash read/write operations, the system temporaril
344
345
Note that when this option is enabled, all interrupt callback functions and their context data **must be placed in internal storage**. This is because the system cannot load data and instructions from Flash when the Cache is disabled.
345
346
346
347
Performance
347
-
^^^^^^^^^^^
348
+
-----------
348
349
349
350
To improve the real-time responsiveness of interrupt handling, the GPTimer driver provides the :ref:`CONFIG_GPTIMER_ISR_HANDLER_IN_IRAM` option. Once enabled, the interrupt handler is placed in internal RAM, reducing delays caused by potential cache misses when loading instructions from Flash.
350
351
@@ -355,12 +356,12 @@ To improve the real-time responsiveness of interrupt handling, the GPTimer drive
355
356
As mentioned above, the GPTimer driver allows some functions to be called in an interrupt context. By enabling the :ref:`CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` option, these functions can also be placed in IRAM, which helps avoid performance loss caused by cache misses and allows them to be used when the Cache is disabled.
356
357
357
358
Other Kconfig Options
358
-
^^^^^^^^^^^^^^^^^^^^^
359
+
---------------------
359
360
360
361
- The :ref:`CONFIG_GPTIMER_ENABLE_DEBUG_LOG` option forces the GPTimer driver to enable all debug logs, regardless of the global log level settings. Enabling this option helps developers obtain more detailed log information during debugging, making it easier to locate and solve problems.
361
362
362
363
Resource Consumption
363
-
^^^^^^^^^^^^^^^^^^^^
364
+
--------------------
364
365
365
366
Use the :doc:`/api-guides/tools/idf-size` tool to check the code and data consumption of the GPTimer driver. The following are the test conditions (using ESP32-C2 as an example):
366
367
@@ -386,7 +387,7 @@ Use the :doc:`/api-guides/tools/idf-size` tool to check the code and data consum
386
387
Additionally, each GPTimer handle dynamically allocates about ``100`` bytes of memory from the heap. If the :cpp:member:`gptimer_config_t::flags::allow_pd` option is enabled, each timer will also consume approximately ``30`` extra bytes of memory during sleep to store the register context.
387
388
388
389
Application Examples
389
-
--------------------
390
+
====================
390
391
391
392
.. list::
392
393
@@ -395,12 +396,26 @@ Application Examples
395
396
:SOC_TIMER_SUPPORT_ETM: - :example:`peripherals/timer_group/gptimer_capture_hc_sr04` demonstrates how to use the general-purpose timer and Event Task Matrix (ETM) to accurately capture timestamps of ultrasonic sensor events and convert them into distance information.
0 commit comments