test_esp_timer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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_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();
  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()
  21. {
  22. s_old_overflow_val = esp_timer_impl_get_overflow_val();
  23. esp_timer_impl_set_overflow_val(0x7fffff); /* overflow every ~0.1 sec */}
  24. static void teardown_overflow()
  25. {
  26. esp_timer_impl_set_overflow_val(s_old_overflow_val);
  27. }
  28. TEST_CASE("esp_timer orders timers correctly", "[esp_timer]")
  29. {
  30. void dummy_cb(void* arg)
  31. {
  32. }
  33. uint64_t timeouts[] = { 10000, 1000, 10000, 5000, 20000, 1000 };
  34. size_t indices[] = { 3, 0, 4, 2, 5, 1 };
  35. const size_t num_timers = sizeof(timeouts)/sizeof(timeouts[0]);
  36. esp_timer_handle_t handles[num_timers];
  37. char* names[num_timers];
  38. setup_overflow();
  39. for (size_t i = 0; i < num_timers; ++i) {
  40. asprintf(&names[i], "timer%d", i);
  41. esp_timer_create_args_t args = {
  42. .callback = &dummy_cb,
  43. .name = names[i]
  44. };
  45. TEST_ESP_OK(esp_timer_create(&args, &handles[i]));
  46. TEST_ESP_OK(esp_timer_start_once(handles[i], timeouts[i] * 100));
  47. }
  48. teardown_overflow();
  49. char* stream_str[1024];
  50. FILE* stream = fmemopen(stream_str, sizeof(stream_str), "r+");
  51. TEST_ESP_OK(esp_timer_dump(stream));
  52. for (size_t i = 0; i < num_timers; ++i) {
  53. TEST_ESP_OK(esp_timer_stop(handles[i]));
  54. TEST_ESP_OK(esp_timer_delete(handles[i]));
  55. free(names[i]);
  56. }
  57. fflush(stream);
  58. fseek(stream, 0, SEEK_SET);
  59. for (size_t i = 0; i < num_timers; ++i) {
  60. char line[128];
  61. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  62. #if WITH_PROFILING
  63. int timer_id;
  64. sscanf(line, "timer%d", &timer_id);
  65. TEST_ASSERT_EQUAL(indices[timer_id], i);
  66. #else
  67. intptr_t timer_ptr;
  68. sscanf(line, "timer@0x%x", &timer_ptr);
  69. for (size_t j = 0; j < num_timers; ++j) {
  70. if (indices[j] == i) {
  71. TEST_ASSERT_EQUAL_PTR(handles[j], timer_ptr);
  72. break;
  73. }
  74. }
  75. #endif
  76. }
  77. fclose(stream);
  78. }
  79. TEST_CASE("esp_timer_impl_set_alarm stress test", "[esp_timer]")
  80. {
  81. const int test_time_sec = 10;
  82. void set_alarm_task(void* arg)
  83. {
  84. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  85. uint64_t start = esp_timer_impl_get_time();
  86. uint64_t now = start;
  87. int count = 0;
  88. const int delays[] = {50, 5000, 10000000};
  89. const int delays_count = sizeof(delays)/sizeof(delays[0]);
  90. while (now - start < test_time_sec * 1000000) {
  91. now = esp_timer_impl_get_time();
  92. esp_timer_impl_set_alarm(now + delays[count % delays_count]);
  93. ++count;
  94. }
  95. xSemaphoreGive(done);
  96. vTaskDelete(NULL);
  97. }
  98. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  99. setup_overflow();
  100. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_0", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 0);
  101. #if portNUM_PROCESSORS == 2
  102. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_1", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 1);
  103. #endif
  104. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  105. #if portNUM_PROCESSORS == 2
  106. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  107. #endif
  108. teardown_overflow();
  109. vSemaphoreDelete(done);
  110. }
  111. TEST_CASE("esp_timer produces correct delay", "[esp_timer]")
  112. {
  113. void timer_func(void* arg)
  114. {
  115. int64_t* p_end = (int64_t*) arg;
  116. *p_end = ref_clock_get();
  117. }
  118. int64_t t_end;
  119. esp_timer_handle_t timer1;
  120. esp_timer_create_args_t args = {
  121. .callback = &timer_func,
  122. .arg = &t_end,
  123. .name = "timer1"
  124. };
  125. TEST_ESP_OK(esp_timer_create(&args, &timer1));
  126. const int delays_ms[] = {20, 100, 200, 250};
  127. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  128. ref_clock_init();
  129. setup_overflow();
  130. for (size_t i = 0; i < delays_count; ++i) {
  131. t_end = 0;
  132. int64_t t_start = ref_clock_get();
  133. TEST_ESP_OK(esp_timer_start_once(timer1, delays_ms[i] * 1000));
  134. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  135. TEST_ASSERT(t_end != 0);
  136. int32_t ms_diff = (t_end - t_start) / 1000;
  137. printf("%d %d\n", delays_ms[i], ms_diff);
  138. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  139. }
  140. teardown_overflow();
  141. ref_clock_deinit();
  142. TEST_ESP_OK( esp_timer_dump(stdout) );
  143. esp_timer_delete(timer1);
  144. }
  145. TEST_CASE("periodic esp_timer produces correct delays", "[esp_timer]")
  146. {
  147. // no, we can't make this a const size_t (§6.7.5.2)
  148. #define NUM_INTERVALS 16
  149. typedef struct {
  150. esp_timer_handle_t timer;
  151. size_t cur_interval;
  152. int intervals[NUM_INTERVALS];
  153. int64_t t_start;
  154. SemaphoreHandle_t done;
  155. } test_args_t;
  156. void timer_func(void* arg)
  157. {
  158. test_args_t* p_args = (test_args_t*) arg;
  159. int64_t t_end = ref_clock_get();
  160. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  161. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  162. p_args->intervals[p_args->cur_interval++] = ms_diff;
  163. // Deliberately make timer handler run longer.
  164. // We check that this doesn't affect the result.
  165. ets_delay_us(10*1000);
  166. if (p_args->cur_interval == NUM_INTERVALS) {
  167. printf("done\n");
  168. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  169. xSemaphoreGive(p_args->done);
  170. }
  171. }
  172. const int delay_ms = 100;
  173. test_args_t args = {0};
  174. esp_timer_handle_t timer1;
  175. esp_timer_create_args_t create_args = {
  176. .callback = &timer_func,
  177. .arg = &args,
  178. .name = "timer1",
  179. };
  180. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  181. ref_clock_init();
  182. setup_overflow();
  183. args.timer = timer1;
  184. args.t_start = ref_clock_get();
  185. args.done = xSemaphoreCreateBinary();
  186. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  187. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  188. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  189. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  190. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  191. }
  192. teardown_overflow();
  193. ref_clock_deinit();
  194. TEST_ESP_OK( esp_timer_dump(stdout) );
  195. TEST_ESP_OK( esp_timer_delete(timer1) );
  196. vSemaphoreDelete(args.done);
  197. #undef NUM_INTERVALS
  198. }
  199. TEST_CASE("multiple timers are ordered correctly", "[esp_timer]")
  200. {
  201. #define N 5
  202. typedef struct {
  203. const int order[N * 3];
  204. size_t count;
  205. } test_common_t;
  206. typedef struct {
  207. int timer_index;
  208. const int intervals[N];
  209. size_t intervals_count;
  210. esp_timer_handle_t timer;
  211. test_common_t* common;
  212. bool pass;
  213. SemaphoreHandle_t done;
  214. int64_t t_start;
  215. } test_args_t;
  216. void timer_func(void* arg)
  217. {
  218. test_args_t* p_args = (test_args_t*) arg;
  219. // check order
  220. size_t count = p_args->common->count;
  221. int expected_index = p_args->common->order[count];
  222. int ms_since_start = (ref_clock_get() - p_args->t_start) / 1000;
  223. printf("Time %dms, at count %d, expected timer %d, got timer %d\n",
  224. ms_since_start, count, expected_index, p_args->timer_index);
  225. if (expected_index != p_args->timer_index) {
  226. p_args->pass = false;
  227. esp_timer_stop(p_args->timer);
  228. xSemaphoreGive(p_args->done);
  229. return;
  230. }
  231. p_args->common->count++;
  232. if (++p_args->intervals_count == N) {
  233. esp_timer_stop(p_args->timer);
  234. xSemaphoreGive(p_args->done);
  235. return;
  236. }
  237. int next_interval = p_args->intervals[p_args->intervals_count];
  238. printf("starting timer %d interval #%d, %d ms\n",
  239. p_args->timer_index, p_args->intervals_count, next_interval);
  240. esp_timer_start_once(p_args->timer, next_interval * 1000);
  241. }
  242. test_common_t common = {
  243. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  244. .count = 0
  245. };
  246. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  247. ref_clock_init();
  248. int64_t now = ref_clock_get();
  249. test_args_t args1 = {
  250. .timer_index = 1,
  251. .intervals = {10, 40, 20, 40, 30},
  252. .common = &common,
  253. .pass = true,
  254. .done = done,
  255. .t_start = now
  256. };
  257. test_args_t args2 = {
  258. .timer_index = 2,
  259. .intervals = {20, 20, 60, 30, 40},
  260. .common = &common,
  261. .pass = true,
  262. .done = done,
  263. .t_start = now
  264. };
  265. test_args_t args3 = {
  266. .timer_index = 3,
  267. .intervals = {30, 30, 60, 30, 10},
  268. .common = &common,
  269. .pass = true,
  270. .done = done,
  271. .t_start = now
  272. };
  273. esp_timer_create_args_t create_args = {
  274. .callback = &timer_func,
  275. .arg = &args1,
  276. .name = "1"
  277. };
  278. TEST_ESP_OK(esp_timer_create(&create_args, &args1.timer));
  279. create_args.name = "2";
  280. create_args.arg = &args2;
  281. TEST_ESP_OK(esp_timer_create(&create_args, &args2.timer));
  282. create_args.name = "3";
  283. create_args.arg = &args3;
  284. TEST_ESP_OK(esp_timer_create(&create_args, &args3.timer));
  285. esp_timer_start_once(args1.timer, args1.intervals[0] * 1000);
  286. esp_timer_start_once(args2.timer, args2.intervals[0] * 1000);
  287. esp_timer_start_once(args3.timer, args3.intervals[0] * 1000);
  288. for (int i = 0; i < 3; ++i) {
  289. int result = xSemaphoreTake(done, 1000 / portTICK_PERIOD_MS);
  290. TEST_ASSERT_TRUE(result == pdPASS);
  291. }
  292. TEST_ASSERT_TRUE(args1.pass);
  293. TEST_ASSERT_TRUE(args2.pass);
  294. TEST_ASSERT_TRUE(args3.pass);
  295. ref_clock_deinit();
  296. TEST_ESP_OK( esp_timer_dump(stdout) );
  297. TEST_ESP_OK( esp_timer_delete(args1.timer) );
  298. TEST_ESP_OK( esp_timer_delete(args2.timer) );
  299. TEST_ESP_OK( esp_timer_delete(args3.timer) );
  300. #undef N
  301. }
  302. /* Create two timers, start them around the same time, and search through
  303. * timeout delta values to reproduce the case when timeouts occur close to
  304. * each other, testing the "multiple timers triggered" code path in timer_process_alarm.
  305. */
  306. TEST_CASE("esp_timer for very short intervals", "[esp_timer]")
  307. {
  308. SemaphoreHandle_t semaphore = xSemaphoreCreateCounting(2, 0);
  309. void timer_func(void* arg) {
  310. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  311. xSemaphoreGive(done);
  312. printf(".");
  313. }
  314. esp_timer_create_args_t timer_args = {
  315. .callback = &timer_func,
  316. .arg = (void*) semaphore,
  317. .name = "foo"
  318. };
  319. esp_timer_handle_t timer1, timer2;
  320. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer1) );
  321. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer2) );
  322. setup_overflow();
  323. const int timeout_ms = 10;
  324. for (int timeout_delta_us = -150; timeout_delta_us < 150; timeout_delta_us++) {
  325. printf("delta=%d", timeout_delta_us);
  326. ESP_ERROR_CHECK( esp_timer_start_once(timer1, timeout_ms * 1000) );
  327. ESP_ERROR_CHECK( esp_timer_start_once(timer2, timeout_ms * 1000 + timeout_delta_us) );
  328. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  329. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  330. printf("\n");
  331. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer1));
  332. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer2));
  333. }
  334. teardown_overflow();
  335. vSemaphoreDelete(semaphore);
  336. }
  337. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  338. {
  339. int64_t begin = esp_timer_get_time();
  340. volatile int64_t end;
  341. const int iter_count = 10000;
  342. for (int i = 0; i < iter_count; ++i) {
  343. end = esp_timer_get_time();
  344. }
  345. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  346. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  347. }
  348. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer]")
  349. {
  350. typedef struct {
  351. SemaphoreHandle_t done;
  352. bool pass;
  353. int test_cnt;
  354. int error_cnt;
  355. int64_t total_sq_error;
  356. int64_t max_error;
  357. } test_state_t;
  358. void timer_test_task(void* arg) {
  359. test_state_t* state = (test_state_t*) arg;
  360. state->pass = true;
  361. int64_t start_time = ref_clock_get();
  362. int64_t delta = esp_timer_get_time() - start_time;
  363. int64_t now = start_time;
  364. int error_repeat_cnt = 0;
  365. while (now - start_time < 10000000) { /* 10 seconds */
  366. int64_t hs_now = esp_timer_get_time();
  367. now = ref_clock_get();
  368. int64_t diff = hs_now - (now + delta);
  369. /* Allow some difference due to rtos tick interrupting task between
  370. * getting 'now' and 'ref_now'.
  371. */
  372. if (abs(diff) > 100) {
  373. error_repeat_cnt++;
  374. state->error_cnt++;
  375. } else {
  376. error_repeat_cnt = 0;
  377. }
  378. if (error_repeat_cnt > 2) {
  379. printf("diff=%lld\n", diff);
  380. state->pass = false;
  381. }
  382. state->max_error = MAX(state->max_error, abs(diff));
  383. state->test_cnt++;
  384. state->total_sq_error += diff * diff;
  385. }
  386. xSemaphoreGive(state->done);
  387. vTaskDelete(NULL);
  388. }
  389. ref_clock_init();
  390. setup_overflow();
  391. test_state_t states[portNUM_PROCESSORS] = {0};
  392. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  393. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  394. states[i].done = done;
  395. xTaskCreatePinnedToCore(&timer_test_task, "test", 4096, &states[i], 6, NULL, i);
  396. }
  397. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  398. TEST_ASSERT_TRUE( xSemaphoreTake(done, portMAX_DELAY) );
  399. printf("CPU%d: %s test_cnt=%d error_cnt=%d std_error=%d |max_error|=%d\n",
  400. i, states[i].pass ? "PASS" : "FAIL",
  401. states[i].test_cnt, states[i].error_cnt,
  402. (int) sqrt(states[i].total_sq_error / states[i].test_cnt), (int) states[i].max_error);
  403. }
  404. vSemaphoreDelete(done);
  405. teardown_overflow();
  406. ref_clock_deinit();
  407. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  408. TEST_ASSERT(states[i].pass);
  409. }
  410. }
  411. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  412. {
  413. esp_timer_dump(stdout);
  414. }
  415. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  416. {
  417. typedef struct {
  418. SemaphoreHandle_t notify_from_timer_cb;
  419. esp_timer_handle_t timer;
  420. } test_arg_t;
  421. void timer_func(void* varg)
  422. {
  423. test_arg_t arg = *(test_arg_t*) varg;
  424. esp_timer_delete(arg.timer);
  425. printf("Timer %p is deleted\n", arg.timer);
  426. xSemaphoreGive(arg.notify_from_timer_cb);
  427. }
  428. test_arg_t args = {
  429. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  430. };
  431. esp_timer_create_args_t timer_args = {
  432. .callback = &timer_func,
  433. .arg = &args,
  434. .name = "self_deleter"
  435. };
  436. esp_timer_create(&timer_args, &args.timer);
  437. esp_timer_start_once(args.timer, 10000);
  438. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  439. printf("Checking heap at %p\n", args.timer);
  440. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  441. vSemaphoreDelete(args.notify_from_timer_cb);
  442. }
  443. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  444. {
  445. ref_clock_init();
  446. int64_t t0 = esp_timer_get_time();
  447. const int64_t diff_us = 1000000;
  448. esp_timer_impl_advance(diff_us);
  449. int64_t t1 = esp_timer_get_time();
  450. int64_t t_delta = t1 - t0;
  451. printf("diff_us=%lld t1-t0=%lld\n", diff_us, t_delta);
  452. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  453. ref_clock_deinit();
  454. }
  455. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  456. {
  457. typedef struct {
  458. int64_t cb_time;
  459. } test_state_t;
  460. void timer_func(void* varg) {
  461. test_state_t* arg = (test_state_t*) varg;
  462. arg->cb_time = ref_clock_get();
  463. }
  464. ref_clock_init();
  465. test_state_t state = { 0 };
  466. esp_timer_create_args_t timer_args = {
  467. .callback = &timer_func,
  468. .arg = &state
  469. };
  470. esp_timer_handle_t timer;
  471. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  472. const int64_t interval = 10000;
  473. const int64_t advance = 2000;
  474. printf("test 1\n");
  475. int64_t t_start = ref_clock_get();
  476. esp_timer_start_once(timer, interval);
  477. esp_timer_impl_advance(advance);
  478. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  479. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  480. printf("test 2\n");
  481. state.cb_time = 0;
  482. t_start = ref_clock_get();
  483. esp_timer_start_once(timer, interval);
  484. esp_timer_impl_advance(interval);
  485. vTaskDelay(1);
  486. TEST_ASSERT(state.cb_time > t_start);
  487. ref_clock_deinit();
  488. }