test_esp_timer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <sys/param.h>
  6. #include "unity.h"
  7. #include "esp_timer.h"
  8. #include "esp_heap_caps.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/semphr.h"
  12. #include "test_utils.h"
  13. #include "esp_private/esp_timer_impl.h"
  14. #ifdef CONFIG_ESP_TIMER_PROFILING
  15. #define WITH_PROFILING 1
  16. #endif
  17. extern uint32_t esp_timer_impl_get_overflow_val(void);
  18. extern void esp_timer_impl_set_overflow_val(uint32_t overflow_val);
  19. static uint32_t s_old_overflow_val;
  20. static void setup_overflow(void)
  21. {
  22. s_old_overflow_val = esp_timer_impl_get_overflow_val();
  23. /* Overflow every 0.1 sec.
  24. * Chosen so that it is 0 modulo s_timer_ticks_per_us (which is 80),
  25. * to prevent roundoff error on each overflow.
  26. */
  27. esp_timer_impl_set_overflow_val(8000000);
  28. }
  29. static void teardown_overflow(void)
  30. {
  31. esp_timer_impl_set_overflow_val(s_old_overflow_val);
  32. }
  33. TEST_CASE("esp_timer orders timers correctly", "[esp_timer]")
  34. {
  35. void dummy_cb(void* arg)
  36. {
  37. }
  38. uint64_t timeouts[] = { 10000, 1000, 10000, 5000, 20000, 1000 };
  39. size_t indices[] = { 3, 0, 4, 2, 5, 1 };
  40. const size_t num_timers = sizeof(timeouts)/sizeof(timeouts[0]);
  41. esp_timer_handle_t handles[num_timers];
  42. char* names[num_timers];
  43. setup_overflow();
  44. for (size_t i = 0; i < num_timers; ++i) {
  45. asprintf(&names[i], "timer%d", i);
  46. esp_timer_create_args_t args = {
  47. .callback = &dummy_cb,
  48. .name = names[i]
  49. };
  50. TEST_ESP_OK(esp_timer_create(&args, &handles[i]));
  51. TEST_ESP_OK(esp_timer_start_once(handles[i], timeouts[i] * 100));
  52. }
  53. teardown_overflow();
  54. char* stream_str[1024];
  55. FILE* stream = fmemopen(stream_str, sizeof(stream_str), "r+");
  56. TEST_ESP_OK(esp_timer_dump(stream));
  57. for (size_t i = 0; i < num_timers; ++i) {
  58. TEST_ESP_OK(esp_timer_stop(handles[i]));
  59. TEST_ESP_OK(esp_timer_delete(handles[i]));
  60. free(names[i]);
  61. }
  62. fflush(stream);
  63. fseek(stream, 0, SEEK_SET);
  64. for (size_t i = 0; i < num_timers; ++i) {
  65. char line[128];
  66. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  67. #if WITH_PROFILING
  68. int timer_id;
  69. sscanf(line, "timer%d", &timer_id);
  70. TEST_ASSERT_EQUAL(indices[timer_id], i);
  71. #else
  72. intptr_t timer_ptr;
  73. sscanf(line, "timer@0x%x", &timer_ptr);
  74. for (size_t j = 0; j < num_timers; ++j) {
  75. if (indices[j] == i) {
  76. TEST_ASSERT_EQUAL_PTR(handles[j], timer_ptr);
  77. break;
  78. }
  79. }
  80. #endif
  81. }
  82. fclose(stream);
  83. }
  84. TEST_CASE("esp_timer_impl_set_alarm stress test", "[esp_timer]")
  85. {
  86. const int test_time_sec = 10;
  87. void set_alarm_task(void* arg)
  88. {
  89. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  90. uint64_t start = esp_timer_impl_get_time();
  91. uint64_t now = start;
  92. int count = 0;
  93. const int delays[] = {50, 5000, 10000000};
  94. const int delays_count = sizeof(delays)/sizeof(delays[0]);
  95. while (now - start < test_time_sec * 1000000) {
  96. now = esp_timer_impl_get_time();
  97. esp_timer_impl_set_alarm(now + delays[count % delays_count]);
  98. ++count;
  99. }
  100. xSemaphoreGive(done);
  101. vTaskDelete(NULL);
  102. }
  103. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  104. setup_overflow();
  105. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_0", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 0);
  106. #if portNUM_PROCESSORS == 2
  107. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_1", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 1);
  108. #endif
  109. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  110. #if portNUM_PROCESSORS == 2
  111. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  112. #endif
  113. teardown_overflow();
  114. vSemaphoreDelete(done);
  115. }
  116. TEST_CASE("esp_timer produces correct delay", "[esp_timer]")
  117. {
  118. void timer_func(void* arg)
  119. {
  120. int64_t* p_end = (int64_t*) arg;
  121. *p_end = ref_clock_get();
  122. }
  123. int64_t t_end;
  124. esp_timer_handle_t timer1;
  125. esp_timer_create_args_t args = {
  126. .callback = &timer_func,
  127. .arg = &t_end,
  128. .name = "timer1"
  129. };
  130. TEST_ESP_OK(esp_timer_create(&args, &timer1));
  131. const int delays_ms[] = {20, 100, 200, 250};
  132. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  133. ref_clock_init();
  134. setup_overflow();
  135. for (size_t i = 0; i < delays_count; ++i) {
  136. t_end = 0;
  137. int64_t t_start = ref_clock_get();
  138. TEST_ESP_OK(esp_timer_start_once(timer1, delays_ms[i] * 1000));
  139. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  140. TEST_ASSERT(t_end != 0);
  141. int32_t ms_diff = (t_end - t_start) / 1000;
  142. printf("%d %d\n", delays_ms[i], ms_diff);
  143. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  144. }
  145. teardown_overflow();
  146. ref_clock_deinit();
  147. TEST_ESP_OK( esp_timer_dump(stdout) );
  148. esp_timer_delete(timer1);
  149. }
  150. TEST_CASE("periodic esp_timer produces correct delays", "[esp_timer]")
  151. {
  152. // no, we can't make this a const size_t (§6.7.5.2)
  153. #define NUM_INTERVALS 16
  154. typedef struct {
  155. esp_timer_handle_t timer;
  156. size_t cur_interval;
  157. int intervals[NUM_INTERVALS];
  158. int64_t t_start;
  159. SemaphoreHandle_t done;
  160. } test_args_t;
  161. void timer_func(void* arg)
  162. {
  163. test_args_t* p_args = (test_args_t*) arg;
  164. int64_t t_end = ref_clock_get();
  165. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  166. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  167. p_args->intervals[p_args->cur_interval++] = ms_diff;
  168. // Deliberately make timer handler run longer.
  169. // We check that this doesn't affect the result.
  170. ets_delay_us(10*1000);
  171. if (p_args->cur_interval == NUM_INTERVALS) {
  172. printf("done\n");
  173. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  174. xSemaphoreGive(p_args->done);
  175. }
  176. }
  177. const int delay_ms = 100;
  178. test_args_t args = {0};
  179. esp_timer_handle_t timer1;
  180. esp_timer_create_args_t create_args = {
  181. .callback = &timer_func,
  182. .arg = &args,
  183. .name = "timer1",
  184. };
  185. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  186. ref_clock_init();
  187. setup_overflow();
  188. args.timer = timer1;
  189. args.t_start = ref_clock_get();
  190. args.done = xSemaphoreCreateBinary();
  191. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  192. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  193. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  194. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  195. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  196. }
  197. teardown_overflow();
  198. ref_clock_deinit();
  199. TEST_ESP_OK( esp_timer_dump(stdout) );
  200. TEST_ESP_OK( esp_timer_delete(timer1) );
  201. vSemaphoreDelete(args.done);
  202. #undef NUM_INTERVALS
  203. }
  204. TEST_CASE("multiple timers are ordered correctly", "[esp_timer]")
  205. {
  206. #define N 5
  207. typedef struct {
  208. const int order[N * 3];
  209. size_t count;
  210. } test_common_t;
  211. typedef struct {
  212. int timer_index;
  213. const int intervals[N];
  214. size_t intervals_count;
  215. esp_timer_handle_t timer;
  216. test_common_t* common;
  217. bool pass;
  218. SemaphoreHandle_t done;
  219. int64_t t_start;
  220. } test_args_t;
  221. void timer_func(void* arg)
  222. {
  223. test_args_t* p_args = (test_args_t*) arg;
  224. // check order
  225. size_t count = p_args->common->count;
  226. int expected_index = p_args->common->order[count];
  227. int ms_since_start = (ref_clock_get() - p_args->t_start) / 1000;
  228. printf("Time %dms, at count %d, expected timer %d, got timer %d\n",
  229. ms_since_start, count, expected_index, p_args->timer_index);
  230. if (expected_index != p_args->timer_index) {
  231. p_args->pass = false;
  232. esp_timer_stop(p_args->timer);
  233. xSemaphoreGive(p_args->done);
  234. return;
  235. }
  236. p_args->common->count++;
  237. if (++p_args->intervals_count == N) {
  238. esp_timer_stop(p_args->timer);
  239. xSemaphoreGive(p_args->done);
  240. return;
  241. }
  242. int next_interval = p_args->intervals[p_args->intervals_count];
  243. printf("starting timer %d interval #%d, %d ms\n",
  244. p_args->timer_index, p_args->intervals_count, next_interval);
  245. esp_timer_start_once(p_args->timer, next_interval * 1000);
  246. }
  247. test_common_t common = {
  248. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  249. .count = 0
  250. };
  251. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  252. ref_clock_init();
  253. int64_t now = ref_clock_get();
  254. test_args_t args1 = {
  255. .timer_index = 1,
  256. .intervals = {10, 40, 20, 40, 30},
  257. .common = &common,
  258. .pass = true,
  259. .done = done,
  260. .t_start = now
  261. };
  262. test_args_t args2 = {
  263. .timer_index = 2,
  264. .intervals = {20, 20, 60, 30, 40},
  265. .common = &common,
  266. .pass = true,
  267. .done = done,
  268. .t_start = now
  269. };
  270. test_args_t args3 = {
  271. .timer_index = 3,
  272. .intervals = {30, 30, 60, 30, 10},
  273. .common = &common,
  274. .pass = true,
  275. .done = done,
  276. .t_start = now
  277. };
  278. esp_timer_create_args_t create_args = {
  279. .callback = &timer_func,
  280. .arg = &args1,
  281. .name = "1"
  282. };
  283. TEST_ESP_OK(esp_timer_create(&create_args, &args1.timer));
  284. create_args.name = "2";
  285. create_args.arg = &args2;
  286. TEST_ESP_OK(esp_timer_create(&create_args, &args2.timer));
  287. create_args.name = "3";
  288. create_args.arg = &args3;
  289. TEST_ESP_OK(esp_timer_create(&create_args, &args3.timer));
  290. esp_timer_start_once(args1.timer, args1.intervals[0] * 1000);
  291. esp_timer_start_once(args2.timer, args2.intervals[0] * 1000);
  292. esp_timer_start_once(args3.timer, args3.intervals[0] * 1000);
  293. for (int i = 0; i < 3; ++i) {
  294. int result = xSemaphoreTake(done, 1000 / portTICK_PERIOD_MS);
  295. TEST_ASSERT_TRUE(result == pdPASS);
  296. }
  297. TEST_ASSERT_TRUE(args1.pass);
  298. TEST_ASSERT_TRUE(args2.pass);
  299. TEST_ASSERT_TRUE(args3.pass);
  300. ref_clock_deinit();
  301. TEST_ESP_OK( esp_timer_dump(stdout) );
  302. TEST_ESP_OK( esp_timer_delete(args1.timer) );
  303. TEST_ESP_OK( esp_timer_delete(args2.timer) );
  304. TEST_ESP_OK( esp_timer_delete(args3.timer) );
  305. #undef N
  306. }
  307. /* Create two timers, start them around the same time, and search through
  308. * timeout delta values to reproduce the case when timeouts occur close to
  309. * each other, testing the "multiple timers triggered" code path in timer_process_alarm.
  310. */
  311. TEST_CASE("esp_timer for very short intervals", "[esp_timer]")
  312. {
  313. SemaphoreHandle_t semaphore = xSemaphoreCreateCounting(2, 0);
  314. void timer_func(void* arg) {
  315. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  316. xSemaphoreGive(done);
  317. printf(".");
  318. }
  319. esp_timer_create_args_t timer_args = {
  320. .callback = &timer_func,
  321. .arg = (void*) semaphore,
  322. .name = "foo"
  323. };
  324. esp_timer_handle_t timer1, timer2;
  325. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer1) );
  326. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer2) );
  327. setup_overflow();
  328. const int timeout_ms = 10;
  329. for (int timeout_delta_us = -150; timeout_delta_us < 150; timeout_delta_us++) {
  330. printf("delta=%d", timeout_delta_us);
  331. ESP_ERROR_CHECK( esp_timer_start_once(timer1, timeout_ms * 1000) );
  332. ESP_ERROR_CHECK( esp_timer_start_once(timer2, timeout_ms * 1000 + timeout_delta_us) );
  333. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  334. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  335. printf("\n");
  336. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer1));
  337. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer2));
  338. }
  339. teardown_overflow();
  340. vSemaphoreDelete(semaphore);
  341. }
  342. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  343. {
  344. int64_t begin = esp_timer_get_time();
  345. volatile int64_t end;
  346. const int iter_count = 10000;
  347. for (int i = 0; i < iter_count; ++i) {
  348. end = esp_timer_get_time();
  349. }
  350. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  351. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  352. }
  353. static int64_t IRAM_ATTR __attribute__((noinline)) get_clock_diff(void)
  354. {
  355. uint64_t hs_time = esp_timer_get_time();
  356. uint64_t ref_time = ref_clock_get();
  357. return hs_time - ref_time;
  358. }
  359. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer]")
  360. {
  361. typedef struct {
  362. SemaphoreHandle_t done;
  363. bool pass;
  364. int test_cnt;
  365. int error_cnt;
  366. int64_t max_error;
  367. int64_t avg_diff;
  368. int64_t dummy;
  369. } test_state_t;
  370. void timer_test_task(void* arg) {
  371. test_state_t* state = (test_state_t*) arg;
  372. state->pass = true;
  373. /* make sure both functions are in cache */
  374. state->dummy = get_clock_diff();
  375. /* calculate the difference between the two clocks */
  376. portDISABLE_INTERRUPTS();
  377. int64_t delta = get_clock_diff();
  378. portENABLE_INTERRUPTS();
  379. int64_t start_time = ref_clock_get();
  380. int error_repeat_cnt = 0;
  381. while (ref_clock_get() - start_time < 10000000) { /* 10 seconds */
  382. /* Get values of both clocks again, and check that they are close to 'delta'.
  383. * We don't disable interrupts here, because esp_timer_get_time doesn't lock
  384. * interrupts internally, so we check if it can get "broken" by a well placed
  385. * interrupt.
  386. */
  387. int64_t diff = get_clock_diff() - delta;
  388. /* Allow some difference due to rtos tick interrupting task between
  389. * getting 'hs_now' and 'now'.
  390. */
  391. if (abs(diff) > 100) {
  392. error_repeat_cnt++;
  393. state->error_cnt++;
  394. } else {
  395. error_repeat_cnt = 0;
  396. }
  397. if (error_repeat_cnt > 2) {
  398. printf("diff=%lld\n", diff);
  399. state->pass = false;
  400. }
  401. state->avg_diff += diff;
  402. state->max_error = MAX(state->max_error, abs(diff));
  403. state->test_cnt++;
  404. }
  405. state->avg_diff /= state->test_cnt;
  406. xSemaphoreGive(state->done);
  407. vTaskDelete(NULL);
  408. }
  409. ref_clock_init();
  410. setup_overflow();
  411. test_state_t states[portNUM_PROCESSORS] = {0};
  412. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  413. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  414. states[i].done = done;
  415. xTaskCreatePinnedToCore(&timer_test_task, "test", 4096, &states[i], 6, NULL, i);
  416. }
  417. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  418. TEST_ASSERT_TRUE( xSemaphoreTake(done, portMAX_DELAY) );
  419. printf("CPU%d: %s test_cnt=%d error_cnt=%d avg_diff=%d |max_error|=%d\n",
  420. i, states[i].pass ? "PASS" : "FAIL",
  421. states[i].test_cnt, states[i].error_cnt,
  422. (int) states[i].avg_diff, (int) states[i].max_error);
  423. }
  424. vSemaphoreDelete(done);
  425. teardown_overflow();
  426. ref_clock_deinit();
  427. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  428. TEST_ASSERT(states[i].pass);
  429. }
  430. }
  431. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  432. {
  433. esp_timer_dump(stdout);
  434. }
  435. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  436. {
  437. typedef struct {
  438. SemaphoreHandle_t notify_from_timer_cb;
  439. esp_timer_handle_t timer;
  440. } test_arg_t;
  441. void timer_func(void* varg)
  442. {
  443. test_arg_t arg = *(test_arg_t*) varg;
  444. esp_timer_delete(arg.timer);
  445. printf("Timer %p is deleted\n", arg.timer);
  446. xSemaphoreGive(arg.notify_from_timer_cb);
  447. }
  448. test_arg_t args = {
  449. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  450. };
  451. esp_timer_create_args_t timer_args = {
  452. .callback = &timer_func,
  453. .arg = &args,
  454. .name = "self_deleter"
  455. };
  456. esp_timer_create(&timer_args, &args.timer);
  457. esp_timer_start_once(args.timer, 10000);
  458. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  459. printf("Checking heap at %p\n", args.timer);
  460. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  461. vSemaphoreDelete(args.notify_from_timer_cb);
  462. }
  463. typedef struct {
  464. SemaphoreHandle_t delete_start;
  465. SemaphoreHandle_t delete_done;
  466. SemaphoreHandle_t test_done;
  467. esp_timer_handle_t timer;
  468. } timer_delete_test_args_t;
  469. static void timer_delete_task(void* arg)
  470. {
  471. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  472. xSemaphoreTake(args->delete_start, portMAX_DELAY);
  473. printf("Deleting the timer\n");
  474. esp_timer_delete(args->timer);
  475. printf("Timer deleted\n");
  476. xSemaphoreGive(args->delete_done);
  477. vTaskDelete(NULL);
  478. }
  479. static void timer_delete_test_callback(void* arg)
  480. {
  481. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  482. printf("Timer callback called\n");
  483. xSemaphoreGive(args->delete_start);
  484. xSemaphoreTake(args->delete_done, portMAX_DELAY);
  485. printf("Callback complete\n");
  486. xSemaphoreGive(args->test_done);
  487. }
  488. TEST_CASE("Can delete timer from a separate task, triggered from callback", "[esp_timer]")
  489. {
  490. timer_delete_test_args_t args = {
  491. .delete_start = xSemaphoreCreateBinary(),
  492. .delete_done = xSemaphoreCreateBinary(),
  493. .test_done = xSemaphoreCreateBinary(),
  494. };
  495. esp_timer_create_args_t timer_args = {
  496. .callback = &timer_delete_test_callback,
  497. .arg = &args
  498. };
  499. esp_timer_handle_t timer;
  500. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  501. args.timer = timer;
  502. xTaskCreate(timer_delete_task, "deleter", 4096, &args, 5, NULL);
  503. esp_timer_start_once(timer, 100);
  504. TEST_ASSERT(xSemaphoreTake(args.test_done, pdMS_TO_TICKS(1000)));
  505. vSemaphoreDelete(args.delete_done);
  506. vSemaphoreDelete(args.delete_start);
  507. vSemaphoreDelete(args.test_done);
  508. }
  509. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  510. {
  511. ref_clock_init();
  512. int64_t t0 = esp_timer_get_time();
  513. const int64_t diff_us = 1000000;
  514. esp_timer_impl_advance(diff_us);
  515. int64_t t1 = esp_timer_get_time();
  516. int64_t t_delta = t1 - t0;
  517. printf("diff_us=%lld t1-t0=%lld\n", diff_us, t_delta);
  518. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  519. ref_clock_deinit();
  520. }
  521. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  522. {
  523. typedef struct {
  524. int64_t cb_time;
  525. } test_state_t;
  526. void timer_func(void* varg) {
  527. test_state_t* arg = (test_state_t*) varg;
  528. arg->cb_time = ref_clock_get();
  529. }
  530. ref_clock_init();
  531. test_state_t state = { 0 };
  532. esp_timer_create_args_t timer_args = {
  533. .callback = &timer_func,
  534. .arg = &state
  535. };
  536. esp_timer_handle_t timer;
  537. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  538. const int64_t interval = 10000;
  539. const int64_t advance = 2000;
  540. printf("test 1\n");
  541. int64_t t_start = ref_clock_get();
  542. esp_timer_start_once(timer, interval);
  543. esp_timer_impl_advance(advance);
  544. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  545. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  546. printf("test 2\n");
  547. state.cb_time = 0;
  548. t_start = ref_clock_get();
  549. esp_timer_start_once(timer, interval);
  550. esp_timer_impl_advance(interval);
  551. vTaskDelay(1);
  552. TEST_ASSERT(state.cb_time > t_start);
  553. ref_clock_deinit();
  554. }
  555. #if !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)
  556. #include "soc/dport_reg.h"
  557. #include "soc/frc_timer_reg.h"
  558. #include "esp_ipc.h"
  559. static bool task_stop;
  560. static bool time_jumped;
  561. static void task_check_time(void *p)
  562. {
  563. int64_t t1 = 0, t2 = 0;
  564. while (task_stop == false) {
  565. t1 = t2;
  566. t2 = esp_timer_get_time();
  567. if (t1 > t2) {
  568. int64_t shift_us = t2 - t1;
  569. time_jumped = true;
  570. printf("System clock jumps back: %lli us\n", shift_us);
  571. }
  572. vTaskDelay(1);
  573. }
  574. vTaskDelete(NULL);
  575. }
  576. static void timer_callback(void* arg)
  577. {
  578. }
  579. static void dport_task(void *pvParameters)
  580. {
  581. while (task_stop == false) {
  582. DPORT_STALL_OTHER_CPU_START();
  583. ets_delay_us(3);
  584. DPORT_STALL_OTHER_CPU_END();
  585. }
  586. vTaskDelete(NULL);
  587. }
  588. TEST_CASE("esp_timer_impl_set_alarm does not set an alarm below the current time", "[esp_timer][timeout=62]")
  589. {
  590. const int max_timers = 2;
  591. time_jumped = false;
  592. task_stop = false;
  593. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  594. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  595. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  596. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  597. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  598. const esp_timer_create_args_t periodic_timer_args = {
  599. .callback = &timer_callback,
  600. };
  601. esp_timer_handle_t periodic_timer[max_timers];
  602. printf("timers created\n");
  603. esp_timer_create(&periodic_timer_args, &periodic_timer[0]);
  604. esp_timer_start_periodic(periodic_timer[0], 9000);
  605. esp_timer_create(&periodic_timer_args, &periodic_timer[1]);
  606. esp_timer_start_periodic(periodic_timer[1], 9000);
  607. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  608. task_stop = true;
  609. esp_timer_stop(periodic_timer[0]);
  610. esp_timer_delete(periodic_timer[0]);
  611. esp_timer_stop(periodic_timer[1]);
  612. esp_timer_delete(periodic_timer[1]);
  613. printf("timers deleted\n");
  614. vTaskDelay(1000 / portTICK_PERIOD_MS);
  615. TEST_ASSERT(time_jumped == false);
  616. }
  617. static esp_timer_handle_t oneshot_timer;
  618. static void oneshot_timer_callback(void* arg)
  619. {
  620. esp_timer_start_once(oneshot_timer, 5000);
  621. }
  622. static const esp_timer_create_args_t oneshot_timer_args = {
  623. .callback = &oneshot_timer_callback,
  624. };
  625. TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the System time jumps back", "[esp_timer][timeout=62]")
  626. {
  627. time_jumped = false;
  628. task_stop = false;
  629. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  630. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  631. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  632. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  633. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  634. const esp_timer_create_args_t periodic_timer_args = {
  635. .callback = &timer_callback,
  636. };
  637. esp_timer_handle_t periodic_timer;
  638. esp_timer_create(&periodic_timer_args, &periodic_timer);
  639. esp_timer_start_periodic(periodic_timer, 5000);
  640. esp_timer_create(&oneshot_timer_args, &oneshot_timer);
  641. esp_timer_start_once(oneshot_timer, 9990);
  642. printf("timers created\n");
  643. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  644. task_stop = true;
  645. esp_timer_stop(oneshot_timer);
  646. esp_timer_delete(oneshot_timer);
  647. printf("timers deleted\n");
  648. vTaskDelay(1000 / portTICK_PERIOD_MS);
  649. TEST_ASSERT(time_jumped == false);
  650. }
  651. #endif // !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)