Select one of the symbols to view example projects that use it.
 
Outline
#include <sys/param.h>
#include <string.h>
#include "soc/soc.h"
#include "esp_types.h"
#include "esp_attr.h"
#include "esp_err.h"
#include "esp_task.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_timer_impl.h"
#include "esp_compiler.h"
#include "esp_private/startup_internal.h"
#include "esp_private/esp_timer_private.h"
#include "esp_private/system_internal.h"
#include "sdkconfig.h"
#define WITH_PROFILING
#define INVARIANTS
#include "sys/queue.h"
#define EVENT_ID_DELETE_TIMER
flags_t
esp_timer
TAG
s_timers
s_timer_task
s_timer_lock
esp_timer_create(const esp_timer_create_args_t *, esp_timer_handle_t *)
esp_timer_restart(esp_timer_handle_t, uint64_t)
esp_timer_start_once(esp_timer_handle_t, uint64_t)
esp_timer_start_periodic(esp_timer_handle_t, uint64_t)
esp_timer_stop(esp_timer_handle_t)
esp_timer_delete(esp_timer_handle_t)
timer_insert(esp_timer_handle_t, bool)
timer_remove(esp_timer_handle_t)
timer_armed(esp_timer_handle_t)
timer_list_lock(esp_timer_dispatch_t)
timer_list_unlock(esp_timer_dispatch_t)
timer_process_alarm(esp_timer_dispatch_t)
timer_task(void *)
timer_alarm_handler(void *)
is_initialized()
init_timer_task()
deinit_timer_task()
esp_timer_init()
#define ESP_TIMER_INIT_MASK
#define ESP_TIMER_INIT_MASK
#define ESP_TIMER_INIT_MASK
esp_system_init_fn_esp_timer_init_os
esp_timer_deinit()
print_timer_info(esp_timer_handle_t, char **, size_t *)
esp_timer_dump(FILE *)
esp_timer_get_next_alarm()
esp_timer_get_next_alarm_for_wake_up()
esp_timer_get_period(esp_timer_handle_t, uint64_t *)
esp_timer_get_expiry_time(esp_timer_handle_t, uint64_t *)
esp_timer_is_active(esp_timer_handle_t)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_timer/src/esp_timer.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <sys/param.h> #include <string.h> #include "soc/soc.h" #include "esp_types.h" #include "esp_attr.h" #include "esp_err.h" #include "esp_task.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "esp_timer.h" #include "esp_timer_impl.h" #include "esp_compiler.h" #include "esp_private/startup_internal.h" #include "esp_private/esp_timer_private.h" #include "esp_private/system_internal.h" #include "sdkconfig.h"18 includes #ifdef CONFIG_ESP_TIMER_PROFILING #define WITH_PROFILING 1 #endif #ifndef NDEBUG // Enable built-in checks in queue.h in debug builds #define INVARIANTS/* ... */ #endif #include "sys/queue.h" #define EVENT_ID_DELETE_TIMER 0xF0DE1E1E typedef enum { FL_ISR_DISPATCH_METHOD = (1 << 0), //!< 0=Callback is called from timer task, 1=Callback is called from timer ISR FL_SKIP_UNHANDLED_EVENTS = (1 << 1), //!< 0=NOT skip unhandled events for periodic timers, 1=Skip unhandled events for periodic timers }{ ... } flags_t; struct esp_timer { uint64_t alarm; uint64_t period: 56; flags_t flags: 8; union { esp_timer_cb_t callback; uint32_t event_id; }{ ... }; void* arg; #if WITH_PROFILING const char* name; size_t times_triggered; size_t times_armed; size_t times_skipped; uint64_t total_callback_run_time;/* ... */ #endif // WITH_PROFILING LIST_ENTRY(esp_timer) list_entry; }{ ... }; static inline bool is_initialized(void); static esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm); static esp_err_t timer_remove(esp_timer_handle_t timer); static bool timer_armed(esp_timer_handle_t timer); static void timer_list_lock(esp_timer_dispatch_t timer_type); static void timer_list_unlock(esp_timer_dispatch_t timer_type); #if WITH_PROFILING static void timer_insert_inactive(esp_timer_handle_t timer); static void timer_remove_inactive(esp_timer_handle_t timer);/* ... */ #endif // WITH_PROFILING __attribute__((unused)) static const char* TAG = "esp_timer"; // lists of currently armed timers for two dispatch methods: ISR and TASK static LIST_HEAD(esp_timer_list, esp_timer) s_timers[ESP_TIMER_MAX] = { [0 ...(ESP_TIMER_MAX - 1)] = LIST_HEAD_INITIALIZER(s_timers) }{...}; #if WITH_PROFILING // lists of unarmed timers for two dispatch methods: ISR and TASK, // used only to be able to dump statistics about all the timers static LIST_HEAD(esp_inactive_timer_list, esp_timer) s_inactive_timers[ESP_TIMER_MAX] = { [0 ...(ESP_TIMER_MAX - 1)] = LIST_HEAD_INITIALIZER(s_timers) }{...};/* ... */ #endif // task used to dispatch timer callbacks static TaskHandle_t s_timer_task; // lock protecting s_timers, s_inactive_timers static portMUX_TYPE s_timer_lock[ESP_TIMER_MAX] = { [0 ...(ESP_TIMER_MAX - 1)] = portMUX_INITIALIZER_UNLOCKED }{...}; #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD // For ISR dispatch method, a callback function of the timer may require a context switch static volatile BaseType_t s_isr_dispatch_need_yield = pdFALSE;/* ... */ #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD esp_err_t esp_timer_create(const esp_timer_create_args_t* args, esp_timer_handle_t* out_handle) { if (!is_initialized()) { return ESP_ERR_INVALID_STATE; }{...} if (args == NULL || args->callback == NULL || out_handle == NULL || args->dispatch_method < 0 || args->dispatch_method >= ESP_TIMER_MAX) { return ESP_ERR_INVALID_ARG; }{...} esp_timer_handle_t result = (esp_timer_handle_t) heap_caps_calloc(1, sizeof(*result), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); if (result == NULL) { return ESP_ERR_NO_MEM; }{...} result->callback = args->callback; result->arg = args->arg; result->flags = (args->dispatch_method ? FL_ISR_DISPATCH_METHOD : 0) | (args->skip_unhandled_events ? FL_SKIP_UNHANDLED_EVENTS : 0); #if WITH_PROFILING result->name = args->name; esp_timer_dispatch_t dispatch_method = result->flags & FL_ISR_DISPATCH_METHOD; timer_list_lock(dispatch_method); timer_insert_inactive(result); timer_list_unlock(dispatch_method);/* ... */ #endif *out_handle = result; return ESP_OK; }{ ... } /* * We have placed this function in IRAM to ensure consistency with the esp_timer API. * esp_timer_start_once, esp_timer_start_periodic and esp_timer_stop are in IRAM. * But actually in IDF esp_timer_restart is used only in one place, which requires keeping * in IRAM when PM_SLP_IRAM_OPT = y and ESP_TASK_WDT USE ESP_TIMER = y. *//* ... */ esp_err_t IRAM_ATTR esp_timer_restart(esp_timer_handle_t timer, uint64_t timeout_us) { esp_err_t ret = ESP_OK; if (timer == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!is_initialized() || !timer_armed(timer)) { return ESP_ERR_INVALID_STATE; }{...} esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; timer_list_lock(dispatch_method); const int64_t now = esp_timer_impl_get_time(); const uint64_t period = timer->period; /* We need to remove the timer to the list of timers and reinsert it at * the right position. In fact, the timers are sorted by their alarm value * (earliest first) *//* ... */ ret = timer_remove(timer); if (ret == ESP_OK) { /* Two cases here: * - if the alarm was a periodic one, i.e. `period` is not 0, the given timeout_us becomes the new period * - if the alarm was a one-shot one, i.e. `period` is 0, it remains non-periodic. *//* ... */ if (period != 0) { /* Remove function got rid of the alarm and period fields, restore them */ const uint64_t new_period = MAX(timeout_us, esp_timer_impl_get_min_period_us()); timer->alarm = now + new_period; timer->period = new_period; }{...} else { /* The new one-shot alarm shall be triggered timeout_us after the current time */ timer->alarm = now + timeout_us; timer->period = 0; }{...} ret = timer_insert(timer, false); }{...} timer_list_unlock(dispatch_method); return ret; }{ ... } esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us) { if (timer == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!is_initialized()) { return ESP_ERR_INVALID_STATE; }{...} int64_t alarm = esp_timer_get_time() + timeout_us; esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; esp_err_t err; timer_list_lock(dispatch_method); /* Check if the timer is armed once the list is locked. * Otherwise another task may arm the timer between the checks * and us locking the list, resulting in us inserting the * timer to s_timers a second time. This will create a loop * in s_timers. *//* ... */ if (timer_armed(timer)) { err = ESP_ERR_INVALID_STATE; }{...} else { timer->alarm = alarm; timer->period = 0; #if WITH_PROFILING timer->times_armed++; #endif err = timer_insert(timer, false); }{...} timer_list_unlock(dispatch_method); return err; }{ ... } esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us) { if (timer == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!is_initialized()) { return ESP_ERR_INVALID_STATE; }{...} period_us = MAX(period_us, esp_timer_impl_get_min_period_us()); int64_t alarm = esp_timer_get_time() + period_us; esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; esp_err_t err; timer_list_lock(dispatch_method); /* Check if the timer is armed once the list is locked to avoid a data race */ if (timer_armed(timer)) { err = ESP_ERR_INVALID_STATE; }{...} else { timer->alarm = alarm; timer->period = period_us; #if WITH_PROFILING timer->times_armed++; timer->times_skipped = 0;/* ... */ #endif err = timer_insert(timer, false); }{...} timer_list_unlock(dispatch_method); return err; }{ ... } esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer) { if (timer == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (!is_initialized()) { return ESP_ERR_INVALID_STATE; }{...} esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; esp_err_t err; timer_list_lock(dispatch_method); /* Check if the timer is armed once the list is locked to avoid a data race */ if (!timer_armed(timer)) { err = ESP_ERR_INVALID_STATE; }{...} else { err = timer_remove(timer); }{...} timer_list_unlock(dispatch_method); return err; }{ ... } esp_err_t esp_timer_delete(esp_timer_handle_t timer) { if (timer == NULL) { return ESP_ERR_INVALID_ARG; }{...} int64_t alarm = esp_timer_get_time(); esp_err_t err; timer_list_lock(ESP_TIMER_TASK); /* Check if the timer is armed once the list is locked to avoid a data race */ if (timer_armed(timer)) { err = ESP_ERR_INVALID_STATE; }{...} else { // A case for the timer with ESP_TIMER_ISR: // This ISR timer was removed from the ISR list in esp_timer_stop() or in timer_process_alarm() -> LIST_REMOVE(it, list_entry) // and here this timer will be added to another the TASK list, see below. // We do this because we want to free memory of the timer in a task context instead of an isr context. timer->flags &= ~FL_ISR_DISPATCH_METHOD; timer->event_id = EVENT_ID_DELETE_TIMER; timer->alarm = alarm; timer->period = 0; err = timer_insert(timer, false); }{...} timer_list_unlock(ESP_TIMER_TASK); return err; }{ ... } static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm) { #if WITH_PROFILING timer_remove_inactive(timer); #endif esp_timer_handle_t it, last = NULL; esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; if (LIST_FIRST(&s_timers[dispatch_method]) == NULL) { LIST_INSERT_HEAD(&s_timers[dispatch_method], timer, list_entry); }{...} else { LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) { if (timer->alarm < it->alarm) { LIST_INSERT_BEFORE(it, timer, list_entry); break; }{...} last = it; }{...} if (it == NULL) { assert(last); LIST_INSERT_AFTER(last, timer, list_entry); }{...} }{...} if (without_update_alarm == false && timer == LIST_FIRST(&s_timers[dispatch_method])) { esp_timer_impl_set_alarm_id(timer->alarm, dispatch_method); }{...} return ESP_OK; }{ ... } static IRAM_ATTR esp_err_t timer_remove(esp_timer_handle_t timer) { esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; timer_list_lock(dispatch_method); esp_timer_handle_t first_timer = LIST_FIRST(&s_timers[dispatch_method]); LIST_REMOVE(timer, list_entry); timer->alarm = 0; timer->period = 0; if (timer == first_timer) { // if this timer was the first in the list. uint64_t next_timestamp = UINT64_MAX; first_timer = LIST_FIRST(&s_timers[dispatch_method]); if (first_timer) { // if after removing the timer from the list, this list is not empty. next_timestamp = first_timer->alarm; }{...} esp_timer_impl_set_alarm_id(next_timestamp, dispatch_method); }{...} #if WITH_PROFILING timer_insert_inactive(timer); #endif timer_list_unlock(dispatch_method); return ESP_OK; }{ ... } #if WITH_PROFILING static IRAM_ATTR void timer_insert_inactive(esp_timer_handle_t timer) { /* May be locked or not, depending on where this is called from. * Lock recursively. *//* ... */ esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; esp_timer_handle_t head = LIST_FIRST(&s_inactive_timers[dispatch_method]); if (head == NULL) { LIST_INSERT_HEAD(&s_inactive_timers[dispatch_method], timer, list_entry); }{...} else { /* Insert as head element as this is the fastest thing to do. * Removal is O(1) anyway. *//* ... */ LIST_INSERT_BEFORE(head, timer, list_entry); }{...} }{...} static IRAM_ATTR void timer_remove_inactive(esp_timer_handle_t timer) { LIST_REMOVE(timer, list_entry); }{...} /* ... */ #endif // WITH_PROFILING static IRAM_ATTR bool timer_armed(esp_timer_handle_t timer) { return timer->alarm > 0; }{ ... } static IRAM_ATTR void timer_list_lock(esp_timer_dispatch_t timer_type) { portENTER_CRITICAL_SAFE(&s_timer_lock[timer_type]); }{ ... } static IRAM_ATTR void timer_list_unlock(esp_timer_dispatch_t timer_type) { portEXIT_CRITICAL_SAFE(&s_timer_lock[timer_type]); }{ ... } #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD static IRAM_ATTR bool timer_process_alarm(esp_timer_dispatch_t dispatch_method) #else static bool timer_process_alarm(esp_timer_dispatch_t dispatch_method) #endif { timer_list_lock(dispatch_method); bool processed = false; esp_timer_handle_t it; while (1) { it = LIST_FIRST(&s_timers[dispatch_method]); int64_t now = esp_timer_impl_get_time(); ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE("-Wanalyzer-use-after-free") // False-positive detection. TODO GCC-366 if (it == NULL || it->alarm > now) { break; }{...} ESP_COMPILER_DIAGNOSTIC_POP("-Wanalyzer-use-after-free") processed = true; LIST_REMOVE(it, list_entry); if (it->event_id == EVENT_ID_DELETE_TIMER) { // It is handled only by ESP_TIMER_TASK (see esp_timer_delete()). // All the ESP_TIMER_ISR timers which should be deleted are moved by esp_timer_delete() to the ESP_TIMER_TASK list. // We want to free memory of the timer in a task context instead of an isr context. free(it); it = NULL; }{...} else { if (it->period > 0) { int skipped = (now - it->alarm) / it->period; if ((it->flags & FL_SKIP_UNHANDLED_EVENTS) && (skipped > 1)) { it->alarm = now + it->period; #if WITH_PROFILING it->times_skipped += skipped; #endif }{...} else { it->alarm += it->period; }{...} timer_insert(it, true); }{...} else { it->alarm = 0; #if WITH_PROFILING timer_insert_inactive(it); #endif }{...} #if WITH_PROFILING uint64_t callback_start = now; #endif esp_timer_cb_t callback = it->callback; void* arg = it->arg; timer_list_unlock(dispatch_method); (*callback)(arg); timer_list_lock(dispatch_method); #if WITH_PROFILING it->times_triggered++; it->total_callback_run_time += esp_timer_impl_get_time() - callback_start;/* ... */ #endif }{...} }{...} // while(1) if (it) { if (dispatch_method == ESP_TIMER_TASK || (dispatch_method != ESP_TIMER_TASK && processed == true)) { esp_timer_impl_set_alarm_id(it->alarm, dispatch_method); }{...} }{...} else { if (processed) { esp_timer_impl_set_alarm_id(UINT64_MAX, dispatch_method); }{...} }{...} timer_list_unlock(dispatch_method); return processed; }{...} static void timer_task(void* arg) { while (true) { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // all deferred events are processed at a time timer_process_alarm(ESP_TIMER_TASK); }{...} }{ ... } #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD IRAM_ATTR void esp_timer_isr_dispatch_need_yield(void) { assert(xPortInIsrContext()); s_isr_dispatch_need_yield = pdTRUE; }{...} /* ... */#endif static void IRAM_ATTR timer_alarm_handler(void* arg) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; bool isr_timers_processed = false; #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD esp_timer_impl_try_to_set_next_alarm(); // process timers with ISR dispatch method isr_timers_processed = timer_process_alarm(ESP_TIMER_ISR); xHigherPriorityTaskWoken = s_isr_dispatch_need_yield; s_isr_dispatch_need_yield = pdFALSE;/* ... */ #endif if (isr_timers_processed == false) { vTaskNotifyGiveFromISR(s_timer_task, &xHigherPriorityTaskWoken); }{...} if (xHigherPriorityTaskWoken == pdTRUE) { portYIELD_FROM_ISR(); }{...} }{ ... } static IRAM_ATTR inline bool is_initialized(void) { return s_timer_task != NULL; }{ ... } static esp_err_t init_timer_task(void) { esp_err_t err = ESP_OK; if (is_initialized()) { ESP_EARLY_LOGE(TAG, "Task is already initialized"); err = ESP_ERR_INVALID_STATE; }{...} else { int ret = xTaskCreatePinnedToCore( &timer_task, "esp_timer", ESP_TASK_TIMER_STACK, NULL, ESP_TASK_TIMER_PRIO, &s_timer_task, CONFIG_ESP_TIMER_TASK_AFFINITY); if (ret != pdPASS) { ESP_EARLY_LOGE(TAG, "Not enough memory to create timer task"); err = ESP_ERR_NO_MEM; }{...} }{...} return err; }{ ... } static void deinit_timer_task(void) { if (s_timer_task) { vTaskDelete(s_timer_task); s_timer_task = NULL; }{...} }{ ... } esp_err_t esp_timer_init(void) { esp_err_t err = ESP_OK; #ifndef CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY err = init_timer_task(); #else /* This function will be run on all cores if CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY is enabled, * We do it that way because we need to allocate the timer ISR on MULTIPLE cores. * timer task will be created by CPU0. *//* ... */ if (xPortGetCoreID() == 0) { err = init_timer_task(); }{...} #endif/* ... */ // CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY if (err == ESP_OK) { err = esp_timer_impl_init(&timer_alarm_handler); if (err != ESP_OK) { ESP_EARLY_LOGE(TAG, "ISR init failed"); deinit_timer_task(); }{...} }{...} return err; }{ ... } #if CONFIG_ESP_TIMER_ISR_AFFINITY_CPU0 #define ESP_TIMER_INIT_MASK BIT(0) #elif CONFIG_ESP_TIMER_ISR_AFFINITY_CPU1 #define ESP_TIMER_INIT_MASK BIT(1) #elif CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY #define ESP_TIMER_INIT_MASK ESP_SYSTEM_INIT_ALL_CORES #endif // CONFIG_ESP_TIMER_ISR_AFFINITY_* /* * This function initializes a task and ISR that esp_timer uses. * * We keep the esp_timer initialization function here to allow the linker * to automatically include esp_timer_init_os if other components call esp_timer APIs. * If no other code calls esp_timer APIs, then esp_timer_init_os will be skipped. *//* ... */ ESP_SYSTEM_INIT_FN(esp_timer_init_os, SECONDARY, ESP_TIMER_INIT_MASK, 100) { return esp_timer_init(); }{ ... } esp_err_t esp_timer_deinit(void) { if (!is_initialized()) { return ESP_ERR_INVALID_STATE; }{...} /* Check if there are any active timers */ for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { if (!LIST_EMPTY(&s_timers[dispatch_method])) { return ESP_ERR_INVALID_STATE; }{...} }{...} /* We can only check if there are any timers which are not deleted if * profiling is enabled. *//* ... */ #if WITH_PROFILING for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { if (!LIST_EMPTY(&s_inactive_timers[dispatch_method])) { return ESP_ERR_INVALID_STATE; }{...} }{...} #endif/* ... */ esp_timer_impl_deinit(); deinit_timer_task(); return ESP_OK; }{ ... } static void print_timer_info(esp_timer_handle_t t, char** dst, size_t* dst_size) { #if WITH_PROFILING size_t cb; // name is optional, might be missed. if (t->name) { cb = snprintf(*dst, *dst_size, "%-20.20s ", t->name); }{...} else { cb = snprintf(*dst, *dst_size, "timer@%-10p ", t); }{...} cb += snprintf(*dst + cb, *dst_size + cb, "%-10lld %-12lld %-12d %-12d %-12d %-12lld\n", (uint64_t)t->period, t->alarm, t->times_armed, t->times_triggered, t->times_skipped, t->total_callback_run_time); /* keep this in sync with the format string, used in esp_timer_dump */ #define TIMER_INFO_LINE_LEN 90/* ... */ #else size_t cb = snprintf(*dst, *dst_size, "timer@%-14p %-10lld %-12lld\n", t, (uint64_t)t->period, t->alarm); #define TIMER_INFO_LINE_LEN 46/* ... */ #endif *dst += cb; *dst_size -= cb; }{ ... } esp_err_t esp_timer_dump(FILE* stream) { /* Since timer lock is a critical section, we don't want to print directly * to stdout, since that may cause a deadlock if stdout is interrupt-driven * (via the UART driver). Allocate sufficiently large chunk of memory first, * print to it, then dump this memory to stdout. *//* ... */ esp_timer_handle_t it; /* First count the number of timers */ size_t timer_count = 0; for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { timer_list_lock(dispatch_method); LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) { ++timer_count; }{...} #if WITH_PROFILING LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) { ++timer_count; }{...} #endif/* ... */ timer_list_unlock(dispatch_method); }{...} /* Allocate the memory for this number of timers. Since we have unlocked, * we may find that there are more timers. There's no bulletproof solution * for this (can't allocate from a critical section), but we allocate * slightly more and the output will be truncated if that is not enough. *//* ... */ size_t buf_size = TIMER_INFO_LINE_LEN * (timer_count + 3); char* print_buf = calloc(1, buf_size + 1); if (print_buf == NULL) { return ESP_ERR_NO_MEM; }{...} /* Print to the buffer */ char* pos = print_buf; for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { timer_list_lock(dispatch_method); LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) { print_timer_info(it, &pos, &buf_size); }{...} #if WITH_PROFILING LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) { print_timer_info(it, &pos, &buf_size); }{...} #endif/* ... */ timer_list_unlock(dispatch_method); }{...} if (stream != NULL) { fprintf(stream, "Timer stats:\n"); #if WITH_PROFILING fprintf(stream, "%-20s %-10s %-12s %-12s %-12s %-12s %-12s\n", "Name", "Period", "Alarm", "Times_armed", "Times_trigg", "Times_skip", "Cb_exec_time");/* ... */ #else fprintf(stream, "%-20s %-10s %-12s\n", "Name", "Period", "Alarm"); #endif /* Print the buffer */ fputs(print_buf, stream); }{...} free(print_buf); return ESP_OK; }{ ... } int64_t IRAM_ATTR esp_timer_get_next_alarm(void) { int64_t next_alarm = INT64_MAX; for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { timer_list_lock(dispatch_method); esp_timer_handle_t it = LIST_FIRST(&s_timers[dispatch_method]); if (it) { if (next_alarm > it->alarm) { next_alarm = it->alarm; }{...} }{...} timer_list_unlock(dispatch_method); }{...} return next_alarm; }{ ... } int64_t IRAM_ATTR esp_timer_get_next_alarm_for_wake_up(void) { int64_t next_alarm = INT64_MAX; for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) { timer_list_lock(dispatch_method); esp_timer_handle_t it = NULL; LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) { // timers with the SKIP_UNHANDLED_EVENTS flag do not want to wake up CPU from a sleep mode. if ((it->flags & FL_SKIP_UNHANDLED_EVENTS) == 0) { if (next_alarm > it->alarm) { next_alarm = it->alarm; }{...} break; }{...} }{...} timer_list_unlock(dispatch_method); }{...} return next_alarm; }{ ... } esp_err_t IRAM_ATTR esp_timer_get_period(esp_timer_handle_t timer, uint64_t *period) { if (timer == NULL || period == NULL) { return ESP_ERR_INVALID_ARG; }{...} esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; timer_list_lock(dispatch_method); *period = timer->period; timer_list_unlock(dispatch_method); return ESP_OK; }{ ... } esp_err_t IRAM_ATTR esp_timer_get_expiry_time(esp_timer_handle_t timer, uint64_t *expiry) { if (timer == NULL || expiry == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (timer->period > 0) { /* Return error for periodic timers */ return ESP_ERR_NOT_SUPPORTED; }{...} esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; timer_list_lock(dispatch_method); *expiry = timer->alarm; timer_list_unlock(dispatch_method); return ESP_OK; }{ ... } bool IRAM_ATTR esp_timer_is_active(esp_timer_handle_t timer) { if (timer == NULL) { return false; }{...} return timer_armed(timer); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.