|
| 1 | +########## |
| 2 | +Timer |
| 3 | +########## |
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +The ESP32 SoCs contains from 2 to 4 hardware timers. |
| 9 | +They are all 64-bit (54-bit for ESP32-C3) generic timers based on 16-bit pre-scalers and 64-bit (54-bit for ESP32-C3) |
| 10 | +up / down counters which are capable of being auto-reloaded. |
| 11 | + |
| 12 | +========= ================ |
| 13 | +ESP32 SoC Number of timers |
| 14 | +========= ================ |
| 15 | +ESP32 4 |
| 16 | +ESP32-S2 4 |
| 17 | +ESP32-C3 2 |
| 18 | +ESP32-S3 4 |
| 19 | +========= ================ |
| 20 | + |
| 21 | +Arduino-ESP32 Timer API |
| 22 | +---------------------------- |
| 23 | + |
| 24 | +timerBegin |
| 25 | +********** |
| 26 | + |
| 27 | +This function is used to configure the timer. After successful setup the timer will automatically start. |
| 28 | + |
| 29 | +.. code-block:: arduino |
| 30 | +
|
| 31 | + hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp); |
| 32 | +
|
| 33 | +* ``num`` select timer number. |
| 34 | +* ``divider`` select timer divider. |
| 35 | +* ``resolution`` select timer resolution. |
| 36 | + |
| 37 | + * range is 1-14 bits (1-20 bits for ESP32). |
| 38 | + |
| 39 | +This function will return ``timer`` structure if configuration is successful. |
| 40 | +If ``NULL`` is returned, error occurs and the timer was not configured. |
| 41 | + |
| 42 | +timerEnd |
| 43 | +******** |
| 44 | + |
| 45 | +This function is used to end timer. |
| 46 | + |
| 47 | +.. code-block:: arduino |
| 48 | +
|
| 49 | + void timerEnd(hw_timer_t *timer); |
| 50 | +
|
| 51 | +* ``timer`` timer struct. |
| 52 | + |
| 53 | +timerSetConfig |
| 54 | +************** |
| 55 | + |
| 56 | +This function is used to configure initialized timer (timerBegin() called). |
| 57 | + |
| 58 | +.. code-block:: arduino |
| 59 | +
|
| 60 | + uint32_t timerGetConfig(hw_timer_t *timer); |
| 61 | +
|
| 62 | +* ``timer`` timer struct. |
| 63 | + |
| 64 | +This function will return ``configuration`` as uint32_t number. |
| 65 | +This can be translated by inserting it to struct ``timer_cfg_t.val``. |
| 66 | + |
| 67 | +timerAttachInterrupt |
| 68 | +******************** |
| 69 | + |
| 70 | +This function is used to attach interrupt to timer. |
| 71 | + |
| 72 | +.. code-block:: arduino |
| 73 | +
|
| 74 | + void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge); |
| 75 | +
|
| 76 | +* ``timer`` timer struct. |
| 77 | +* ``fn`` funtion to be called when interrupt is triggered. |
| 78 | +* ``edge`` select edge to trigger interrupt (only LEVEL trigger is currently supported). |
| 79 | + |
| 80 | +timerDetachInterrupt |
| 81 | +******************** |
| 82 | + |
| 83 | +This function is used to detach interrupt from timer. |
| 84 | + |
| 85 | +.. code-block:: arduino |
| 86 | +
|
| 87 | + void timerDetachInterrupt(hw_timer_t *timer); |
| 88 | +
|
| 89 | +* ``timer`` timer struct. |
| 90 | + |
| 91 | +timerStart |
| 92 | +********** |
| 93 | + |
| 94 | +This function is used to start counter of the timer. |
| 95 | + |
| 96 | +.. code-block:: arduino |
| 97 | +
|
| 98 | + void timerStart(hw_timer_t *timer); |
| 99 | +
|
| 100 | +* ``timer`` timer struct. |
| 101 | + |
| 102 | +timerStop |
| 103 | +********* |
| 104 | + |
| 105 | +This function is used to stop counter of the timer. |
| 106 | + |
| 107 | +.. code-block:: arduino |
| 108 | +
|
| 109 | + void timerStop(hw_timer_t *timer); |
| 110 | +
|
| 111 | +* ``timer`` timer struct. |
| 112 | + |
| 113 | +timerRestart |
| 114 | +************ |
| 115 | + |
| 116 | +This function is used to restart counter of the timer. |
| 117 | + |
| 118 | +.. code-block:: arduino |
| 119 | +
|
| 120 | + void timerRestart(hw_timer_t *timer); |
| 121 | +
|
| 122 | +* ``timer`` timer struct. |
| 123 | + |
| 124 | +timerWrite |
| 125 | +********** |
| 126 | + |
| 127 | +This function is used to set counter value of the timer. |
| 128 | + |
| 129 | +.. code-block:: arduino |
| 130 | +
|
| 131 | + void timerWrite(hw_timer_t *timer, uint64_t val); |
| 132 | +
|
| 133 | +* ``timer`` timer struct. |
| 134 | +* ``val`` counter value to be set. |
| 135 | + |
| 136 | +timerSetDivider |
| 137 | +*************** |
| 138 | + |
| 139 | +This function is used to set the divider of the timer. |
| 140 | + |
| 141 | +.. code-block:: arduino |
| 142 | +
|
| 143 | + void timerSetDivider(hw_timer_t *timer, uint16_t divider); |
| 144 | +
|
| 145 | +* ``timer`` timer struct. |
| 146 | +* ``divider`` divider to be set. |
| 147 | + |
| 148 | +timerSetCountUp |
| 149 | +*************** |
| 150 | + |
| 151 | +This function is used to configure counting direction of the timer. |
| 152 | + |
| 153 | +.. code-block:: arduino |
| 154 | +
|
| 155 | + void timerSetCountUp(hw_timer_t *timer, bool countUp); |
| 156 | +
|
| 157 | +* ``timer`` timer struct. |
| 158 | +* ``countUp`` select counting direction (``true`` = increment). |
| 159 | + |
| 160 | +timerSetAutoReload |
| 161 | +****************** |
| 162 | + |
| 163 | +This function is used to set counter value of the timer. |
| 164 | + |
| 165 | +.. code-block:: arduino |
| 166 | +
|
| 167 | + void timerSetAutoReload(hw_timer_t *timer, bool autoreload); |
| 168 | +
|
| 169 | +* ``timer`` timer struct. |
| 170 | +* ``autoreload`` select autoreload (``true`` = enabled). |
| 171 | + |
| 172 | +timerStarted |
| 173 | +************ |
| 174 | + |
| 175 | +This function is used to get if the timer is running. |
| 176 | + |
| 177 | +.. code-block:: arduino |
| 178 | +
|
| 179 | + bool timerStarted(hw_timer_t *timer); |
| 180 | +
|
| 181 | +* ``timer`` timer struct. |
| 182 | + |
| 183 | +This function will return ``true`` if the timer is running. If ``false`` is returned, timer is stopped. |
| 184 | + |
| 185 | +timerRead |
| 186 | +********* |
| 187 | + |
| 188 | +This function is used to read counter value of the timer. |
| 189 | + |
| 190 | +.. code-block:: arduino |
| 191 | +
|
| 192 | + uint64_t timerRead(hw_timer_t *timer); |
| 193 | +
|
| 194 | +* ``timer`` timer struct. |
| 195 | + |
| 196 | +This function will return ``counter value`` of the timer. |
| 197 | + |
| 198 | +timerReadMicros |
| 199 | +*************** |
| 200 | + |
| 201 | +This function is used to read counter value in microseconds of the timer. |
| 202 | + |
| 203 | +.. code-block:: arduino |
| 204 | +
|
| 205 | + uint64_t timerReadMicros(hw_timer_t *timer); |
| 206 | +
|
| 207 | +* ``timer`` timer struct. |
| 208 | + |
| 209 | +This function will return ``counter value`` of the timer in microseconds. |
| 210 | + |
| 211 | +timerReadMilis |
| 212 | +************** |
| 213 | + |
| 214 | +This function is used to read counter value in miliseconds of the timer. |
| 215 | + |
| 216 | +.. code-block:: arduino |
| 217 | +
|
| 218 | + uint64_t timerReadMilis(hw_timer_t *timer); |
| 219 | +
|
| 220 | +* ``timer`` timer struct. |
| 221 | + |
| 222 | +This function will return ``counter value`` of the timer in miliseconds. |
| 223 | + |
| 224 | +timerReadSeconds |
| 225 | +**************** |
| 226 | + |
| 227 | +This function is used to read counter value in seconds of the timer. |
| 228 | + |
| 229 | +.. code-block:: arduino |
| 230 | +
|
| 231 | + double timerReadSeconds(hw_timer_t *timer); |
| 232 | +
|
| 233 | +* ``timer`` timer struct. |
| 234 | + |
| 235 | +This function will return ``counter value`` of the timer in seconds. |
| 236 | + |
| 237 | +timerGetDivider |
| 238 | +*************** |
| 239 | + |
| 240 | +This function is used to get divider of the timer. |
| 241 | + |
| 242 | +.. code-block:: arduino |
| 243 | +
|
| 244 | + uint16_t timerGetDivider(hw_timer_t *timer); |
| 245 | +
|
| 246 | +* ``timer`` timer struct. |
| 247 | + |
| 248 | +This function will return ``divider`` of the timer. |
| 249 | + |
| 250 | +timerGetCountUp |
| 251 | +*************** |
| 252 | + |
| 253 | +This function is used get counting direction of the timer. |
| 254 | + |
| 255 | +.. code-block:: arduino |
| 256 | +
|
| 257 | + bool timerGetCountUp(hw_timer_t *timer); |
| 258 | +
|
| 259 | +* ``timer`` timer struct. |
| 260 | + |
| 261 | +This function will return ``true`` if the timer counting direction is UP (incrementing). |
| 262 | +If ``false`` returned, the timer counting direction is DOWN (decrementing). |
| 263 | + |
| 264 | +timerGetAutoReload |
| 265 | +****************** |
| 266 | + |
| 267 | +This function is used to get configuration of auto reload of the timer. |
| 268 | + |
| 269 | +.. code-block:: arduino |
| 270 | +
|
| 271 | + bool timerGetAutoReload(hw_timer_t *timer); |
| 272 | +
|
| 273 | +* ``timer`` timer struct. |
| 274 | + |
| 275 | +This function will return ``true`` if the timer auto reload is enabled. |
| 276 | +If ``false`` returned, the timer auto reload is disabled. |
| 277 | + |
| 278 | +timerAlarmEnable |
| 279 | +**************** |
| 280 | + |
| 281 | +This function is used to enable generation of timer alarm events. |
| 282 | + |
| 283 | +.. code-block:: arduino |
| 284 | +
|
| 285 | + void timerAlarmEnable(hw_timer_t *timer); |
| 286 | +
|
| 287 | +* ``timer`` timer struct. |
| 288 | + |
| 289 | +timerAlarmDisable |
| 290 | +***************** |
| 291 | + |
| 292 | +This function is used to disable generation of timer alarm events. |
| 293 | + |
| 294 | +.. code-block:: arduino |
| 295 | +
|
| 296 | + void timerAlarmDisable(hw_timer_t *timer); |
| 297 | +
|
| 298 | +* ``timer`` timer struct. |
| 299 | + |
| 300 | +timerAlarmWrite |
| 301 | +*************** |
| 302 | + |
| 303 | +This function is used to configure alarm value and autoreload of the timer. |
| 304 | + |
| 305 | +.. code-block:: arduino |
| 306 | +
|
| 307 | + void timerAlarmWrite(hw_timer_t *timer, uint64_t alarm_value, bool autoreload); |
| 308 | +
|
| 309 | +* ``timer`` timer struct. |
| 310 | +* ``alarm_value`` alarm value to generate event. |
| 311 | +* ``autoreload`` enabled/disabled autorealod. |
| 312 | + |
| 313 | +timerAlarmEnabled |
| 314 | +***************** |
| 315 | + |
| 316 | +This function is used to get status of timer alarm. |
| 317 | + |
| 318 | +.. code-block:: arduino |
| 319 | +
|
| 320 | + bool timerAlarmEnabled(hw_timer_t *timer); |
| 321 | +
|
| 322 | +* ``timer`` timer struct. |
| 323 | + |
| 324 | +This function will return ``true`` if the timer alarm is enabled. |
| 325 | +If ``false`` returned, the timer alarm is disabled. |
| 326 | + |
| 327 | +timerAlarmRead |
| 328 | +************** |
| 329 | + |
| 330 | +This function is used to read alarm value of the timer. |
| 331 | + |
| 332 | +.. code-block:: arduino |
| 333 | +
|
| 334 | + uint64_t timerAlarmRead(hw_timer_t *timer); |
| 335 | +
|
| 336 | +* ``timer`` timer struct. |
| 337 | + |
| 338 | +timerAlarmReadMicros |
| 339 | +******************** |
| 340 | + |
| 341 | +This function is used to read alarm value of the timer in microseconds. |
| 342 | + |
| 343 | +.. code-block:: arduino |
| 344 | +
|
| 345 | + uint64_t timerAlarmReadMicros(hw_timer_t *timer); |
| 346 | +
|
| 347 | +* ``timer`` timer struct. |
| 348 | + |
| 349 | +This function will return ``alarm value`` of the timer in microseconds. |
| 350 | + |
| 351 | +timerAlarmReadSeconds |
| 352 | +********************* |
| 353 | + |
| 354 | +This function is used to read alarm value of the timer in seconds. |
| 355 | + |
| 356 | +.. code-block:: arduino |
| 357 | +
|
| 358 | + double timerAlarmReadSeconds(hw_timer_t *timer); |
| 359 | +
|
| 360 | +* ``timer`` timer struct. |
| 361 | + |
| 362 | +This function will return ``alarm value`` of the timer in seconds. |
| 363 | + |
| 364 | +Example Applications |
| 365 | +******************** |
| 366 | + |
| 367 | +There are 2 examples uses of Timer: |
| 368 | + |
| 369 | +Repeat timer example: |
| 370 | + |
| 371 | +.. literalinclude:: ../../../libraries/ESP32/examples/Timer/RepeatTimer/RepeatTimer.ino |
| 372 | + :language: arduino |
| 373 | + |
| 374 | +Watchdog timer example: |
| 375 | + |
| 376 | +.. literalinclude:: ../../../libraries/ESP32/examples/Timer/WatchdogTimer/WatchdogTimer.ino |
| 377 | + :language: arduino |
0 commit comments