test_esp_timer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <sys/param.h>
  11. #include "esp_timer.h"
  12. #include "esp_timer_impl.h"
  13. #include "unity.h"
  14. #include "soc/timer_group_reg.h"
  15. #include "esp_heap_caps.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/semphr.h"
  19. #include "test_utils.h"
  20. #include "esp_freertos_hooks.h"
  21. #include "esp_rom_sys.h"
  22. #define SEC (1000000)
  23. #ifdef CONFIG_ESP_TIMER_PROFILING
  24. #define WITH_PROFILING 1
  25. #endif
  26. static void dummy_cb(void* arg)
  27. {
  28. }
  29. TEST_CASE("esp_timer orders timers correctly", "[esp_timer]")
  30. {
  31. uint64_t timeouts[] = { 10000, 1000, 10000, 5000, 20000, 1000 };
  32. size_t indices[] = { 3, 0, 4, 2, 5, 1 };
  33. const size_t num_timers = sizeof(timeouts)/sizeof(timeouts[0]);
  34. esp_timer_handle_t handles[num_timers];
  35. char* names[num_timers];
  36. for (size_t i = 0; i < num_timers; ++i) {
  37. asprintf(&names[i], "timer%d", i);
  38. esp_timer_create_args_t args = {
  39. .callback = &dummy_cb,
  40. .name = names[i]
  41. };
  42. TEST_ESP_OK(esp_timer_create(&args, &handles[i]));
  43. TEST_ESP_OK(esp_timer_start_once(handles[i], timeouts[i] * 100));
  44. }
  45. char* stream_str[1024];
  46. FILE* stream = fmemopen(stream_str, sizeof(stream_str), "r+");
  47. TEST_ESP_OK(esp_timer_dump(stream));
  48. for (size_t i = 0; i < num_timers; ++i) {
  49. TEST_ESP_OK(esp_timer_stop(handles[i]));
  50. TEST_ESP_OK(esp_timer_delete(handles[i]));
  51. free(names[i]);
  52. }
  53. fflush(stream);
  54. fseek(stream, 0, SEEK_SET);
  55. /* Discard header lines */
  56. char line[128];
  57. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  58. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  59. for (size_t i = 0; i < num_timers; ++i) {
  60. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  61. #if WITH_PROFILING
  62. int timer_id;
  63. sscanf(line, "timer%d", &timer_id);
  64. TEST_ASSERT_EQUAL(indices[timer_id], i);
  65. #else
  66. intptr_t timer_ptr;
  67. sscanf(line, "timer@0x%x", &timer_ptr);
  68. for (size_t j = 0; j < num_timers; ++j) {
  69. if (indices[j] == i) {
  70. TEST_ASSERT_EQUAL_PTR(handles[j], timer_ptr);
  71. break;
  72. }
  73. }
  74. #endif
  75. }
  76. fclose(stream);
  77. }
  78. static const int test_time_sec = 10;
  79. static void set_alarm_task(void* arg)
  80. {
  81. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  82. int64_t start = esp_timer_impl_get_time();
  83. int64_t now = start;
  84. int count = 0;
  85. const int delays[] = {50, 5000, 10000000};
  86. const int delays_count = sizeof(delays)/sizeof(delays[0]);
  87. while (now - start < test_time_sec * 1000000) {
  88. now = esp_timer_impl_get_time();
  89. esp_timer_impl_set_alarm(now + delays[count % delays_count]);
  90. ++count;
  91. }
  92. xSemaphoreGive(done);
  93. vTaskDelete(NULL);
  94. }
  95. TEST_CASE("esp_timer_impl_set_alarm stress test", "[esp_timer]")
  96. {
  97. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  98. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_0", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 0);
  99. #if portNUM_PROCESSORS == 2
  100. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_1", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 1);
  101. #endif
  102. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  103. #if portNUM_PROCESSORS == 2
  104. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  105. #endif
  106. vSemaphoreDelete(done);
  107. }
  108. static void test_correct_delay_timer_func(void* arg)
  109. {
  110. int64_t* p_end = (int64_t*) arg;
  111. *p_end = ref_clock_get();
  112. }
  113. TEST_CASE("esp_timer produces correct delay", "[esp_timer]")
  114. {
  115. int64_t t_end;
  116. esp_timer_handle_t timer1;
  117. esp_timer_create_args_t args = {
  118. .callback = &test_correct_delay_timer_func,
  119. .arg = &t_end,
  120. .name = "timer1"
  121. };
  122. TEST_ESP_OK(esp_timer_create(&args, &timer1));
  123. const int delays_ms[] = {20, 100, 200, 250};
  124. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  125. ref_clock_init();
  126. for (size_t i = 0; i < delays_count; ++i) {
  127. t_end = 0;
  128. int64_t t_start = ref_clock_get();
  129. TEST_ESP_OK(esp_timer_start_once(timer1, delays_ms[i] * 1000));
  130. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  131. TEST_ASSERT(t_end != 0);
  132. int32_t ms_diff = (t_end - t_start) / 1000;
  133. printf("%d %d\n", delays_ms[i], ms_diff);
  134. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  135. }
  136. ref_clock_deinit();
  137. TEST_ESP_OK( esp_timer_dump(stdout) );
  138. esp_timer_delete(timer1);
  139. }
  140. // no, we can't make this a const size_t (§6.7.5.2)
  141. #define NUM_INTERVALS 16
  142. typedef struct {
  143. esp_timer_handle_t timer;
  144. size_t cur_interval;
  145. int intervals[NUM_INTERVALS];
  146. int64_t t_start;
  147. SemaphoreHandle_t done;
  148. } test_periodic_correct_delays_args_t;
  149. static void test_periodic_correct_delays_timer_func(void* arg)
  150. {
  151. test_periodic_correct_delays_args_t* p_args = (test_periodic_correct_delays_args_t*) arg;
  152. int64_t t_end = ref_clock_get();
  153. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  154. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  155. p_args->intervals[p_args->cur_interval++] = ms_diff;
  156. // Deliberately make timer handler run longer.
  157. // We check that this doesn't affect the result.
  158. esp_rom_delay_us(10*1000);
  159. if (p_args->cur_interval == NUM_INTERVALS) {
  160. printf("done\n");
  161. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  162. xSemaphoreGive(p_args->done);
  163. }
  164. }
  165. TEST_CASE("periodic esp_timer produces correct delays", "[esp_timer]")
  166. {
  167. const int delay_ms = 100;
  168. test_periodic_correct_delays_args_t args = {0};
  169. esp_timer_handle_t timer1;
  170. esp_timer_create_args_t create_args = {
  171. .callback = &test_periodic_correct_delays_timer_func,
  172. .arg = &args,
  173. .name = "timer1",
  174. };
  175. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  176. ref_clock_init();
  177. args.timer = timer1;
  178. args.t_start = ref_clock_get();
  179. args.done = xSemaphoreCreateBinary();
  180. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  181. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  182. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  183. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  184. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  185. }
  186. ref_clock_deinit();
  187. TEST_ESP_OK( esp_timer_dump(stdout) );
  188. TEST_ESP_OK( esp_timer_delete(timer1) );
  189. vSemaphoreDelete(args.done);
  190. }
  191. #undef NUM_INTERVALS
  192. #define N 5
  193. typedef struct {
  194. const int order[N * 3];
  195. size_t count;
  196. } test_timers_ordered_correctly_common_t;
  197. typedef struct {
  198. int timer_index;
  199. const int intervals[N];
  200. size_t intervals_count;
  201. esp_timer_handle_t timer;
  202. test_timers_ordered_correctly_common_t* common;
  203. bool pass;
  204. SemaphoreHandle_t done;
  205. int64_t t_start;
  206. } test_timers_ordered_correctly_args_t;
  207. static void test_timers_ordered_correctly_timer_func(void* arg)
  208. {
  209. test_timers_ordered_correctly_args_t* p_args = (test_timers_ordered_correctly_args_t*) arg;
  210. // check order
  211. size_t count = p_args->common->count;
  212. int expected_index = p_args->common->order[count];
  213. int ms_since_start = (ref_clock_get() - p_args->t_start) / 1000;
  214. printf("Time %dms, at count %d, expected timer %d, got timer %d\n",
  215. ms_since_start, count, expected_index, p_args->timer_index);
  216. if (expected_index != p_args->timer_index) {
  217. p_args->pass = false;
  218. esp_timer_stop(p_args->timer);
  219. xSemaphoreGive(p_args->done);
  220. return;
  221. }
  222. p_args->common->count++;
  223. if (++p_args->intervals_count == N) {
  224. esp_timer_stop(p_args->timer);
  225. xSemaphoreGive(p_args->done);
  226. return;
  227. }
  228. int next_interval = p_args->intervals[p_args->intervals_count];
  229. printf("starting timer %d interval #%d, %d ms\n",
  230. p_args->timer_index, p_args->intervals_count, next_interval);
  231. esp_timer_start_once(p_args->timer, next_interval * 1000);
  232. }
  233. TEST_CASE("multiple timers are ordered correctly", "[esp_timer]")
  234. {
  235. test_timers_ordered_correctly_common_t common = {
  236. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  237. .count = 0
  238. };
  239. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  240. ref_clock_init();
  241. int64_t now = ref_clock_get();
  242. test_timers_ordered_correctly_args_t args1 = {
  243. .timer_index = 1,
  244. .intervals = {10, 40, 20, 40, 30},
  245. .common = &common,
  246. .pass = true,
  247. .done = done,
  248. .t_start = now
  249. };
  250. test_timers_ordered_correctly_args_t args2 = {
  251. .timer_index = 2,
  252. .intervals = {20, 20, 60, 30, 40},
  253. .common = &common,
  254. .pass = true,
  255. .done = done,
  256. .t_start = now
  257. };
  258. test_timers_ordered_correctly_args_t args3 = {
  259. .timer_index = 3,
  260. .intervals = {30, 30, 60, 30, 10},
  261. .common = &common,
  262. .pass = true,
  263. .done = done,
  264. .t_start = now
  265. };
  266. esp_timer_create_args_t create_args = {
  267. .callback = &test_timers_ordered_correctly_timer_func,
  268. .arg = &args1,
  269. .name = "1"
  270. };
  271. TEST_ESP_OK(esp_timer_create(&create_args, &args1.timer));
  272. create_args.name = "2";
  273. create_args.arg = &args2;
  274. TEST_ESP_OK(esp_timer_create(&create_args, &args2.timer));
  275. create_args.name = "3";
  276. create_args.arg = &args3;
  277. TEST_ESP_OK(esp_timer_create(&create_args, &args3.timer));
  278. esp_timer_start_once(args1.timer, args1.intervals[0] * 1000);
  279. esp_timer_start_once(args2.timer, args2.intervals[0] * 1000);
  280. esp_timer_start_once(args3.timer, args3.intervals[0] * 1000);
  281. for (int i = 0; i < 3; ++i) {
  282. int result = xSemaphoreTake(done, 1000 / portTICK_PERIOD_MS);
  283. TEST_ASSERT_TRUE(result == pdPASS);
  284. }
  285. TEST_ASSERT_TRUE(args1.pass);
  286. TEST_ASSERT_TRUE(args2.pass);
  287. TEST_ASSERT_TRUE(args3.pass);
  288. ref_clock_deinit();
  289. TEST_ESP_OK( esp_timer_dump(stdout) );
  290. TEST_ESP_OK( esp_timer_delete(args1.timer) );
  291. TEST_ESP_OK( esp_timer_delete(args2.timer) );
  292. TEST_ESP_OK( esp_timer_delete(args3.timer) );
  293. }
  294. #undef N
  295. static void test_short_intervals_timer_func(void* arg) {
  296. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  297. xSemaphoreGive(done);
  298. printf(".");
  299. }
  300. /* Create two timers, start them around the same time, and search through
  301. * timeout delta values to reproduce the case when timeouts occur close to
  302. * each other, testing the "multiple timers triggered" code path in timer_process_alarm.
  303. */
  304. TEST_CASE("esp_timer for very short intervals", "[esp_timer]")
  305. {
  306. SemaphoreHandle_t semaphore = xSemaphoreCreateCounting(2, 0);
  307. esp_timer_create_args_t timer_args = {
  308. .callback = &test_short_intervals_timer_func,
  309. .arg = (void*) semaphore,
  310. .name = "foo"
  311. };
  312. esp_timer_handle_t timer1, timer2;
  313. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer1) );
  314. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer2) );
  315. const int timeout_ms = 10;
  316. for (int timeout_delta_us = -150; timeout_delta_us < 150; timeout_delta_us++) {
  317. printf("delta=%d", timeout_delta_us);
  318. ESP_ERROR_CHECK( esp_timer_start_once(timer1, timeout_ms * 1000) );
  319. ESP_ERROR_CHECK( esp_timer_start_once(timer2, timeout_ms * 1000 + timeout_delta_us) );
  320. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  321. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  322. printf("\n");
  323. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer1));
  324. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer2));
  325. }
  326. vSemaphoreDelete(semaphore);
  327. TEST_ESP_OK(esp_timer_delete(timer1));
  328. TEST_ESP_OK(esp_timer_delete(timer2));
  329. }
  330. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  331. //IDF-5052
  332. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  333. {
  334. int64_t begin = esp_timer_get_time();
  335. volatile int64_t end;
  336. const int iter_count = 10000;
  337. for (int i = 0; i < iter_count; ++i) {
  338. end = esp_timer_get_time();
  339. }
  340. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  341. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  342. }
  343. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  344. static int64_t IRAM_ATTR __attribute__((noinline)) get_clock_diff(void)
  345. {
  346. uint64_t hs_time = esp_timer_get_time();
  347. uint64_t ref_time = ref_clock_get();
  348. return hs_time - ref_time;
  349. }
  350. typedef struct {
  351. SemaphoreHandle_t done;
  352. bool pass;
  353. int test_cnt;
  354. int error_cnt;
  355. int64_t max_error;
  356. int64_t avg_diff;
  357. int64_t dummy;
  358. } test_monotonic_values_state_t;
  359. static void timer_test_monotonic_values_task(void* arg) {
  360. test_monotonic_values_state_t* state = (test_monotonic_values_state_t*) arg;
  361. state->pass = true;
  362. /* make sure both functions are in cache */
  363. state->dummy = get_clock_diff();
  364. /* calculate the difference between the two clocks */
  365. portDISABLE_INTERRUPTS();
  366. int64_t delta = get_clock_diff();
  367. portENABLE_INTERRUPTS();
  368. int64_t start_time = ref_clock_get();
  369. int error_repeat_cnt = 0;
  370. while (ref_clock_get() - start_time < 10000000) { /* 10 seconds */
  371. /* Get values of both clocks again, and check that they are close to 'delta'.
  372. * We don't disable interrupts here, because esp_timer_get_time doesn't lock
  373. * interrupts internally, so we check if it can get "broken" by a well placed
  374. * interrupt.
  375. */
  376. int64_t diff = get_clock_diff() - delta;
  377. /* Allow some difference due to rtos tick interrupting task between
  378. * getting 'hs_now' and 'now'.
  379. */
  380. if (llabs(diff) > 100) {
  381. error_repeat_cnt++;
  382. state->error_cnt++;
  383. } else {
  384. error_repeat_cnt = 0;
  385. }
  386. if (error_repeat_cnt > 2) {
  387. printf("diff=%lld\n", diff);
  388. state->pass = false;
  389. }
  390. state->avg_diff += diff;
  391. state->max_error = MAX(state->max_error, llabs(diff));
  392. state->test_cnt++;
  393. }
  394. state->avg_diff /= state->test_cnt;
  395. xSemaphoreGive(state->done);
  396. vTaskDelete(NULL);
  397. }
  398. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer]")
  399. {
  400. ref_clock_init();
  401. test_monotonic_values_state_t states[portNUM_PROCESSORS] = {0};
  402. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  403. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  404. states[i].done = done;
  405. xTaskCreatePinnedToCore(&timer_test_monotonic_values_task, "test", 4096, &states[i], 6, NULL, i);
  406. }
  407. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  408. TEST_ASSERT_TRUE( xSemaphoreTake(done, portMAX_DELAY) );
  409. printf("CPU%d: %s test_cnt=%d error_cnt=%d avg_diff=%d |max_error|=%d\n",
  410. i, states[i].pass ? "PASS" : "FAIL",
  411. states[i].test_cnt, states[i].error_cnt,
  412. (int) states[i].avg_diff, (int) states[i].max_error);
  413. }
  414. vSemaphoreDelete(done);
  415. ref_clock_deinit();
  416. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  417. TEST_ASSERT(states[i].pass);
  418. }
  419. }
  420. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  421. {
  422. esp_timer_dump(stdout);
  423. }
  424. typedef struct {
  425. SemaphoreHandle_t notify_from_timer_cb;
  426. esp_timer_handle_t timer;
  427. } test_delete_from_callback_arg_t;
  428. static void test_delete_from_callback_timer_func(void* varg)
  429. {
  430. test_delete_from_callback_arg_t arg = *(test_delete_from_callback_arg_t*) varg;
  431. esp_timer_delete(arg.timer);
  432. printf("Timer %p is deleted\n", arg.timer);
  433. xSemaphoreGive(arg.notify_from_timer_cb);
  434. }
  435. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  436. {
  437. test_delete_from_callback_arg_t args = {
  438. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  439. };
  440. esp_timer_create_args_t timer_args = {
  441. .callback = &test_delete_from_callback_timer_func,
  442. .arg = &args,
  443. .name = "self_deleter"
  444. };
  445. esp_timer_create(&timer_args, &args.timer);
  446. esp_timer_start_once(args.timer, 10000);
  447. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  448. printf("Checking heap at %p\n", args.timer);
  449. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  450. vSemaphoreDelete(args.notify_from_timer_cb);
  451. }
  452. typedef struct {
  453. SemaphoreHandle_t delete_start;
  454. SemaphoreHandle_t delete_done;
  455. SemaphoreHandle_t test_done;
  456. esp_timer_handle_t timer;
  457. } timer_delete_test_args_t;
  458. static void timer_delete_task(void* arg)
  459. {
  460. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  461. xSemaphoreTake(args->delete_start, portMAX_DELAY);
  462. printf("Deleting the timer\n");
  463. esp_timer_delete(args->timer);
  464. printf("Timer deleted\n");
  465. xSemaphoreGive(args->delete_done);
  466. vTaskDelete(NULL);
  467. }
  468. static void timer_delete_test_callback(void* arg)
  469. {
  470. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  471. printf("Timer callback called\n");
  472. xSemaphoreGive(args->delete_start);
  473. xSemaphoreTake(args->delete_done, portMAX_DELAY);
  474. printf("Callback complete\n");
  475. xSemaphoreGive(args->test_done);
  476. }
  477. TEST_CASE("Can delete timer from a separate task, triggered from callback", "[esp_timer]")
  478. {
  479. timer_delete_test_args_t args = {
  480. .delete_start = xSemaphoreCreateBinary(),
  481. .delete_done = xSemaphoreCreateBinary(),
  482. .test_done = xSemaphoreCreateBinary(),
  483. };
  484. esp_timer_create_args_t timer_args = {
  485. .callback = &timer_delete_test_callback,
  486. .arg = &args
  487. };
  488. esp_timer_handle_t timer;
  489. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  490. args.timer = timer;
  491. xTaskCreate(timer_delete_task, "deleter", 4096, &args, 5, NULL);
  492. esp_timer_start_once(timer, 100);
  493. TEST_ASSERT(xSemaphoreTake(args.test_done, pdMS_TO_TICKS(1000)));
  494. vSemaphoreDelete(args.delete_done);
  495. vSemaphoreDelete(args.delete_start);
  496. vSemaphoreDelete(args.test_done);
  497. }
  498. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  499. {
  500. int64_t t0 = esp_timer_get_time();
  501. const int64_t diff_us = 1000000;
  502. esp_timer_impl_advance(diff_us);
  503. int64_t t1 = esp_timer_get_time();
  504. int64_t t_delta = t1 - t0;
  505. printf("diff_us=%lld t0=%lld t1=%lld t1-t0=%lld\n", diff_us, t0, t1, t_delta);
  506. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  507. }
  508. typedef struct {
  509. int64_t cb_time;
  510. } test_run_when_expected_state_t;
  511. static void test_run_when_expected_timer_func(void* varg) {
  512. test_run_when_expected_state_t* arg = (test_run_when_expected_state_t*) varg;
  513. arg->cb_time = ref_clock_get();
  514. }
  515. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  516. {
  517. ref_clock_init();
  518. test_run_when_expected_state_t state = { 0 };
  519. esp_timer_create_args_t timer_args = {
  520. .callback = &test_run_when_expected_timer_func,
  521. .arg = &state
  522. };
  523. esp_timer_handle_t timer;
  524. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  525. const int64_t interval = 10000;
  526. const int64_t advance = 2000;
  527. printf("test 1\n");
  528. int64_t t_start = ref_clock_get();
  529. esp_timer_start_once(timer, interval);
  530. esp_timer_impl_advance(advance);
  531. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  532. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  533. printf("test 2\n");
  534. state.cb_time = 0;
  535. t_start = ref_clock_get();
  536. esp_timer_start_once(timer, interval);
  537. esp_timer_impl_advance(interval);
  538. vTaskDelay(1);
  539. TEST_ASSERT(state.cb_time > t_start);
  540. ref_clock_deinit();
  541. TEST_ESP_OK(esp_timer_delete(timer));
  542. }
  543. static esp_timer_handle_t timer1;
  544. static SemaphoreHandle_t sem;
  545. static void IRAM_ATTR test_tick_hook(void)
  546. {
  547. static int i;
  548. const int iterations = 16;
  549. if (++i <= iterations) {
  550. if (i & 0x1) {
  551. TEST_ESP_OK(esp_timer_start_once(timer1, 5000));
  552. } else {
  553. TEST_ESP_OK(esp_timer_stop(timer1));
  554. }
  555. } else {
  556. xSemaphoreGiveFromISR(sem, 0);
  557. }
  558. }
  559. static void test_start_stop_timer_func(void* arg)
  560. {
  561. printf("timer cb\n");
  562. }
  563. TEST_CASE("Can start/stop timer from ISR context", "[esp_timer]")
  564. {
  565. esp_timer_create_args_t create_args = {
  566. .callback = &test_start_stop_timer_func,
  567. };
  568. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  569. sem = xSemaphoreCreateBinary();
  570. esp_register_freertos_tick_hook(test_tick_hook);
  571. TEST_ASSERT(xSemaphoreTake(sem, portMAX_DELAY));
  572. esp_deregister_freertos_tick_hook(test_tick_hook);
  573. TEST_ESP_OK( esp_timer_delete(timer1) );
  574. vSemaphoreDelete(sem);
  575. }
  576. #if !defined(CONFIG_FREERTOS_UNICORE) && SOC_DPORT_WORKAROUND
  577. #include "dport_access.h"
  578. static bool task_stop;
  579. static bool time_jumped;
  580. static void task_check_time(void *p)
  581. {
  582. int64_t t1 = 0, t2 = 0;
  583. while (task_stop == false) {
  584. t1 = t2;
  585. t2 = esp_timer_get_time();
  586. if (t1 > t2) {
  587. int64_t shift_us = t2 - t1;
  588. time_jumped = true;
  589. printf("System clock jumps back: %lli us\n", shift_us);
  590. }
  591. vTaskDelay(1);
  592. }
  593. vTaskDelete(NULL);
  594. }
  595. static void timer_callback(void* arg)
  596. {
  597. }
  598. static void dport_task(void *pvParameters)
  599. {
  600. while (task_stop == false) {
  601. DPORT_STALL_OTHER_CPU_START();
  602. esp_rom_delay_us(3);
  603. DPORT_STALL_OTHER_CPU_END();
  604. }
  605. vTaskDelete(NULL);
  606. }
  607. TEST_CASE("esp_timer_impl_set_alarm does not set an alarm below the current time", "[esp_timer][timeout=62]")
  608. {
  609. const int max_timers = 2;
  610. time_jumped = false;
  611. task_stop = false;
  612. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  613. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  614. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  615. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  616. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  617. const esp_timer_create_args_t periodic_timer_args = {
  618. .callback = &timer_callback,
  619. };
  620. esp_timer_handle_t periodic_timer[max_timers];
  621. printf("timers created\n");
  622. esp_timer_create(&periodic_timer_args, &periodic_timer[0]);
  623. esp_timer_start_periodic(periodic_timer[0], 9000);
  624. esp_timer_create(&periodic_timer_args, &periodic_timer[1]);
  625. esp_timer_start_periodic(periodic_timer[1], 9000);
  626. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  627. task_stop = true;
  628. esp_timer_stop(periodic_timer[0]);
  629. esp_timer_delete(periodic_timer[0]);
  630. esp_timer_stop(periodic_timer[1]);
  631. esp_timer_delete(periodic_timer[1]);
  632. printf("timers deleted\n");
  633. vTaskDelay(1000 / portTICK_PERIOD_MS);
  634. TEST_ASSERT(time_jumped == false);
  635. }
  636. static esp_timer_handle_t oneshot_timer;
  637. static void oneshot_timer_callback(void* arg)
  638. {
  639. esp_timer_start_once(oneshot_timer, 5000);
  640. }
  641. static const esp_timer_create_args_t oneshot_timer_args = {
  642. .callback = &oneshot_timer_callback,
  643. };
  644. TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the System time jumps back", "[esp_timer][timeout=62]")
  645. {
  646. time_jumped = false;
  647. task_stop = false;
  648. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  649. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  650. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  651. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  652. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  653. const esp_timer_create_args_t periodic_timer_args = {
  654. .callback = &timer_callback,
  655. };
  656. esp_timer_handle_t periodic_timer;
  657. esp_timer_create(&periodic_timer_args, &periodic_timer);
  658. esp_timer_start_periodic(periodic_timer, 5000);
  659. esp_timer_create(&oneshot_timer_args, &oneshot_timer);
  660. esp_timer_start_once(oneshot_timer, 9990);
  661. printf("timers created\n");
  662. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  663. task_stop = true;
  664. esp_timer_stop(oneshot_timer);
  665. esp_timer_delete(oneshot_timer);
  666. esp_timer_stop(periodic_timer);
  667. esp_timer_delete(periodic_timer);
  668. printf("timers deleted\n");
  669. vTaskDelay(1000 / portTICK_PERIOD_MS);
  670. TEST_ASSERT(time_jumped == false);
  671. }
  672. #endif // !defined(CONFIG_FREERTOS_UNICORE) && SOC_DPORT_WORKAROUND
  673. TEST_CASE("Test case when esp_timer_impl_set_alarm needs set timer < now_time", "[esp_timer]")
  674. {
  675. esp_timer_impl_advance(50331648); // 0xefffffff/80 = 50331647
  676. esp_rom_delay_us(2);
  677. portDISABLE_INTERRUPTS();
  678. esp_timer_impl_set_alarm(50331647);
  679. uint64_t alarm_reg = esp_timer_impl_get_alarm_reg();
  680. uint64_t count_reg = esp_timer_impl_get_counter_reg();
  681. portENABLE_INTERRUPTS();
  682. const uint32_t offset = 2;
  683. printf("alarm_reg = 0x%llx, count_reg 0x%llx\n", alarm_reg, count_reg);
  684. TEST_ASSERT(alarm_reg <= (count_reg + offset));
  685. }
  686. static void timer_callback5(void* arg)
  687. {
  688. *(int64_t *)arg = esp_timer_get_time();
  689. }
  690. TEST_CASE("Test a latency between a call of callback and real event", "[esp_timer]")
  691. {
  692. int64_t callback_time = 0;
  693. const esp_timer_create_args_t periodic_timer_args = {
  694. .arg = &callback_time,
  695. .callback = &timer_callback5,
  696. };
  697. esp_timer_handle_t periodic_timer;
  698. TEST_ESP_OK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  699. int interval_ms = 50;
  700. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, interval_ms * 1000));
  701. for (int i = 0; i < 5; ++i) {
  702. int64_t expected_time = esp_timer_get_next_alarm();
  703. int64_t saved_callback_time = callback_time;
  704. while (saved_callback_time == callback_time) {
  705. vTaskDelay(10 / portTICK_PERIOD_MS);
  706. }
  707. int diff = callback_time - expected_time;
  708. printf("%d us\n", diff);
  709. #ifndef CONFIG_IDF_ENV_FPGA
  710. if (i != 0) {
  711. // skip the first measurement
  712. // if CPU_FREQ = 240MHz. 14 - 16us
  713. TEST_ASSERT_LESS_OR_EQUAL(50, diff);
  714. }
  715. #endif // not CONFIG_IDF_ENV_FPGA
  716. }
  717. TEST_ESP_OK(esp_timer_dump(stdout));
  718. TEST_ESP_OK(esp_timer_stop(periodic_timer));
  719. TEST_ESP_OK(esp_timer_delete(periodic_timer));
  720. }
  721. static void test_periodic_timer_feed(void* timer1_fed)
  722. {
  723. *((int*) timer1_fed) = 1;
  724. }
  725. /**
  726. * Feed function is not part of the esp_timer header file: it's a public in the sense that it is not static,
  727. * but it is only meant to be used in IDF components.
  728. */
  729. esp_err_t esp_timer_feed(esp_timer_handle_t timer);
  730. TEST_CASE("periodic esp_timer can be fed", "[esp_timer]")
  731. {
  732. const int delay_ms = 100;
  733. int timer_fed = 0;
  734. esp_timer_handle_t timer1;
  735. esp_timer_create_args_t create_args = {
  736. .callback = &test_periodic_timer_feed,
  737. .arg = &timer_fed,
  738. .name = "timer1",
  739. };
  740. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  741. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  742. /* Sleep for delay_ms/2 and feed the timer */
  743. vTaskDelay((delay_ms / 2) * portTICK_PERIOD_MS);
  744. /* Check that the alarm was not triggered */
  745. TEST_ASSERT_EQUAL(0, timer_fed);
  746. /* Reaching this point, the timer will be triggered in delay_ms/2.
  747. * Let's feed the timer now. */
  748. TEST_ESP_OK(esp_timer_feed(timer1));
  749. /* Sleep for a bit more than delay_ms/2 */
  750. vTaskDelay(((delay_ms / 2) + 1) * portTICK_PERIOD_MS);
  751. /* If the alarm was triggered, feed didn't work */
  752. TEST_ASSERT_EQUAL(0, timer_fed);
  753. /* Else, wait for another delay_ms/2, which should trigger the alarm */
  754. vTaskDelay(((delay_ms / 2) + 1) * portTICK_PERIOD_MS);
  755. TEST_ASSERT_EQUAL(1, timer_fed);
  756. TEST_ESP_OK( esp_timer_stop(timer1) );
  757. TEST_ESP_OK( esp_timer_delete(timer1) );
  758. }
  759. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  760. static int64_t old_time[2];
  761. static void timer_isr_callback(void* arg)
  762. {
  763. int num_timer = *((int*)arg);
  764. int64_t now = esp_timer_get_time();
  765. int64_t dt = now - old_time[num_timer];
  766. old_time[num_timer] = now;
  767. if (num_timer == 1) {
  768. esp_rom_printf("(%lld): \t\t\t\t timer ISR, dt: %lld us\n", now, dt);
  769. assert(xPortInIsrContext());
  770. } else {
  771. esp_rom_printf("(%lld): timer TASK, dt: %lld us\n", now, dt);
  772. assert(!xPortInIsrContext());
  773. }
  774. }
  775. TEST_CASE("Test ESP_TIMER_ISR dispatch method", "[esp_timer]")
  776. {
  777. TEST_ESP_OK(esp_timer_dump(stdout));
  778. int timer[2]= {1, 2};
  779. const esp_timer_create_args_t periodic_timer1_args = {
  780. .callback = &timer_isr_callback,
  781. .dispatch_method = ESP_TIMER_ISR,
  782. .arg = &timer[0],
  783. .name = "ISR",
  784. };
  785. esp_timer_handle_t periodic_timer1;
  786. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  787. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 400000));
  788. const esp_timer_create_args_t periodic_timer2_args = {
  789. .callback = &timer_isr_callback,
  790. .dispatch_method = ESP_TIMER_TASK,
  791. .arg = &timer[1],
  792. .name = "TASK",
  793. };
  794. esp_timer_handle_t periodic_timer2;
  795. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  796. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 500000));
  797. printf("timers created\n");
  798. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  799. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  800. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  801. TEST_ESP_OK(esp_timer_dump(stdout));
  802. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  803. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  804. printf("timers deleted\n");
  805. TEST_ESP_OK(esp_timer_dump(stdout));
  806. }
  807. static void dump_task(void* arg)
  808. {
  809. bool* stop_dump_task = (bool*) arg;
  810. while (*stop_dump_task == false) {
  811. TEST_ESP_OK(esp_timer_dump(NULL));
  812. }
  813. vTaskDelete(NULL);
  814. }
  815. static void isr_callback(void* arg)
  816. {
  817. assert(xPortInIsrContext());
  818. }
  819. static void task_callback(void* arg)
  820. {
  821. assert(!xPortInIsrContext());
  822. }
  823. TEST_CASE("Test ESP_TIMER_ISR dispatch method is not blocked", "[esp_timer]")
  824. {
  825. const esp_timer_create_args_t periodic_timer1_args = {
  826. .callback = &isr_callback,
  827. .dispatch_method = ESP_TIMER_ISR,
  828. .arg = NULL,
  829. .name = "ISR",
  830. };
  831. esp_timer_handle_t periodic_timer1;
  832. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  833. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 500));
  834. const esp_timer_create_args_t periodic_timer2_args = {
  835. .callback = &task_callback,
  836. .dispatch_method = ESP_TIMER_TASK,
  837. .arg = NULL,
  838. .name = "TASK",
  839. };
  840. esp_timer_handle_t periodic_timer2;
  841. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  842. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 5000));
  843. printf("timers created\n");
  844. bool stop_dump_task = false;
  845. xTaskCreatePinnedToCore(&dump_task, "dump", 4096, &stop_dump_task, UNITY_FREERTOS_PRIORITY, NULL, 0);
  846. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  847. stop_dump_task = true;
  848. vTaskDelay(100 / portTICK_PERIOD_MS);
  849. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  850. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  851. TEST_ESP_OK(esp_timer_dump(stdout));
  852. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  853. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  854. printf("timer deleted\n");
  855. }
  856. static void isr_callback1(void* arg)
  857. {
  858. assert(xPortInIsrContext());
  859. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  860. esp_rom_printf("isr_callback1: timer ISR\n");
  861. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  862. xSemaphoreGiveFromISR(done, &xHigherPriorityTaskWoken);
  863. if (xHigherPriorityTaskWoken) {
  864. esp_timer_isr_dispatch_need_yield();
  865. }
  866. }
  867. static void task_callback1(void* arg)
  868. {
  869. assert(0);
  870. }
  871. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if TASK timer list is empty", "[esp_timer]")
  872. {
  873. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  874. const esp_timer_create_args_t timer1_args = {
  875. .callback = &isr_callback1,
  876. .dispatch_method = ESP_TIMER_ISR,
  877. .arg = &done,
  878. .name = "ISR",
  879. };
  880. esp_timer_handle_t timer1;
  881. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  882. TEST_ESP_OK(esp_timer_start_periodic(timer1, 5 * SEC));
  883. const esp_timer_create_args_t timer2_args = {
  884. .callback = &task_callback1,
  885. .dispatch_method = ESP_TIMER_TASK,
  886. .arg = NULL,
  887. .name = "TASK",
  888. };
  889. esp_timer_handle_t timer2;
  890. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  891. TEST_ESP_OK(esp_timer_start_once(timer2, 3 * SEC));
  892. printf("timers created\n");
  893. printf("stop timer2\n");
  894. TEST_ESP_OK(esp_timer_stop(timer2));
  895. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  896. printf("stop timer1\n");
  897. TEST_ESP_OK(esp_timer_stop(timer1));
  898. TEST_ESP_OK(esp_timer_dump(stdout));
  899. TEST_ESP_OK(esp_timer_delete(timer1));
  900. TEST_ESP_OK(esp_timer_delete(timer2));
  901. vSemaphoreDelete(done);
  902. printf("timer deleted\n");
  903. }
  904. static void isr_callback2(void* arg)
  905. {
  906. assert(0);
  907. }
  908. static void task_callback2(void* arg)
  909. {
  910. assert(!xPortInIsrContext());
  911. esp_rom_printf("task_callback2: timer TASK\n");
  912. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  913. xSemaphoreGive(done);
  914. }
  915. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if ISR timer list is empty", "[esp_timer]")
  916. {
  917. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  918. const esp_timer_create_args_t timer1_args = {
  919. .callback = &isr_callback2,
  920. .dispatch_method = ESP_TIMER_ISR,
  921. .arg = NULL,
  922. .name = "ISR",
  923. };
  924. esp_timer_handle_t timer1;
  925. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  926. TEST_ESP_OK(esp_timer_start_once(timer1, 3 * SEC));
  927. const esp_timer_create_args_t timer2_args = {
  928. .callback = &task_callback2,
  929. .dispatch_method = ESP_TIMER_TASK,
  930. .arg = &done,
  931. .name = "TASK",
  932. };
  933. esp_timer_handle_t timer2;
  934. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  935. TEST_ESP_OK(esp_timer_start_periodic(timer2, 5 * SEC));
  936. printf("timers created\n");
  937. printf("stop timer1\n");
  938. TEST_ESP_OK(esp_timer_stop(timer1));
  939. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  940. printf("stop timer2\n");
  941. TEST_ESP_OK(esp_timer_stop(timer2));
  942. TEST_ESP_OK(esp_timer_dump(stdout));
  943. TEST_ESP_OK(esp_timer_delete(timer1));
  944. TEST_ESP_OK(esp_timer_delete(timer2));
  945. vSemaphoreDelete(done);
  946. printf("timer deleted\n");
  947. }
  948. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD