esp_timer.c 23 KB

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