test_esp_timer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include "unity.h"
  6. #include "esp_timer.h"
  7. #include "esp_heap_caps.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "test_utils.h"
  12. #include "../esp_timer_impl.h"
  13. #ifdef CONFIG_ESP_TIMER_PROFILING
  14. #define WITH_PROFILING 1
  15. #endif
  16. TEST_CASE("esp_timer orders timers correctly", "[esp_timer]")
  17. {
  18. void dummy_cb(void* arg)
  19. {
  20. }
  21. uint64_t timeouts[] = { 10000, 1000, 10000, 5000, 20000, 1000 };
  22. size_t indices[] = { 3, 0, 4, 2, 5, 1 };
  23. const size_t num_timers = sizeof(timeouts)/sizeof(timeouts[0]);
  24. esp_timer_handle_t handles[num_timers];
  25. char* names[num_timers];
  26. for (size_t i = 0; i < num_timers; ++i) {
  27. asprintf(&names[i], "timer%d", i);
  28. esp_timer_create_args_t args = {
  29. .callback = &dummy_cb,
  30. .name = names[i]
  31. };
  32. TEST_ESP_OK(esp_timer_create(&args, &handles[i]));
  33. TEST_ESP_OK(esp_timer_start_once(handles[i], timeouts[i] * 100));
  34. }
  35. char* stream_str[1024];
  36. FILE* stream = fmemopen(stream_str, sizeof(stream_str), "r+");
  37. TEST_ESP_OK(esp_timer_dump(stream));
  38. for (size_t i = 0; i < num_timers; ++i) {
  39. TEST_ESP_OK(esp_timer_stop(handles[i]));
  40. TEST_ESP_OK(esp_timer_delete(handles[i]));
  41. free(names[i]);
  42. }
  43. fflush(stream);
  44. fseek(stream, 0, SEEK_SET);
  45. for (size_t i = 0; i < num_timers; ++i) {
  46. char line[128];
  47. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  48. #if WITH_PROFILING
  49. int timer_id;
  50. sscanf(line, "timer%d", &timer_id);
  51. TEST_ASSERT_EQUAL(indices[timer_id], i);
  52. #else
  53. intptr_t timer_ptr;
  54. sscanf(line, "timer@0x%x", &timer_ptr);
  55. for (size_t j = 0; j < num_timers; ++j) {
  56. if (indices[j] == i) {
  57. TEST_ASSERT_EQUAL_PTR(handles[j], timer_ptr);
  58. break;
  59. }
  60. }
  61. #endif
  62. }
  63. fclose(stream);
  64. }
  65. TEST_CASE("esp_timer produces correct delay", "[esp_timer]")
  66. {
  67. void timer_func(void* arg)
  68. {
  69. int64_t* p_end = (int64_t*) arg;
  70. *p_end = ref_clock_get();
  71. }
  72. int64_t t_end;
  73. esp_timer_handle_t timer1;
  74. esp_timer_create_args_t args = {
  75. .callback = &timer_func,
  76. .arg = &t_end,
  77. .name = "timer1"
  78. };
  79. TEST_ESP_OK(esp_timer_create(&args, &timer1));
  80. const int delays_ms[] = {20, 100, 200, 250};
  81. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  82. ref_clock_init();
  83. for (size_t i = 0; i < delays_count; ++i) {
  84. t_end = 0;
  85. int64_t t_start = ref_clock_get();
  86. TEST_ESP_OK(esp_timer_start_once(timer1, delays_ms[i] * 1000));
  87. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  88. TEST_ASSERT(t_end != 0);
  89. int32_t ms_diff = (t_end - t_start) / 1000;
  90. printf("%d %d\n", delays_ms[i], ms_diff);
  91. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  92. }
  93. ref_clock_deinit();
  94. TEST_ESP_OK( esp_timer_dump(stdout) );
  95. esp_timer_delete(timer1);
  96. }
  97. TEST_CASE("periodic esp_timer produces correct delays", "[esp_timer]")
  98. {
  99. // no, we can't make this a const size_t (§6.7.5.2)
  100. #define NUM_INTERVALS 16
  101. typedef struct {
  102. esp_timer_handle_t timer;
  103. size_t cur_interval;
  104. int intervals[NUM_INTERVALS];
  105. int64_t t_start;
  106. SemaphoreHandle_t done;
  107. } test_args_t;
  108. void timer_func(void* arg)
  109. {
  110. test_args_t* p_args = (test_args_t*) arg;
  111. int64_t t_end = ref_clock_get();
  112. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  113. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  114. p_args->intervals[p_args->cur_interval++] = ms_diff;
  115. // Deliberately make timer handler run longer.
  116. // We check that this doesn't affect the result.
  117. ets_delay_us(10*1000);
  118. if (p_args->cur_interval == NUM_INTERVALS) {
  119. printf("done\n");
  120. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  121. xSemaphoreGive(p_args->done);
  122. }
  123. }
  124. const int delay_ms = 100;
  125. test_args_t args = {0};
  126. esp_timer_handle_t timer1;
  127. esp_timer_create_args_t create_args = {
  128. .callback = &timer_func,
  129. .arg = &args,
  130. .name = "timer1",
  131. };
  132. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  133. ref_clock_init();
  134. args.timer = timer1;
  135. args.t_start = ref_clock_get();
  136. args.done = xSemaphoreCreateBinary();
  137. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  138. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  139. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  140. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  141. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  142. }
  143. ref_clock_deinit();
  144. TEST_ESP_OK( esp_timer_dump(stdout) );
  145. TEST_ESP_OK( esp_timer_delete(timer1) );
  146. vSemaphoreDelete(args.done);
  147. #undef NUM_INTERVALS
  148. }
  149. TEST_CASE("multiple timers are ordered correctly", "[esp_timer]")
  150. {
  151. #define N 5
  152. typedef struct {
  153. const int order[N * 3];
  154. size_t count;
  155. } test_common_t;
  156. typedef struct {
  157. int timer_index;
  158. const int intervals[N];
  159. size_t intervals_count;
  160. esp_timer_handle_t timer;
  161. test_common_t* common;
  162. bool pass;
  163. SemaphoreHandle_t done;
  164. int64_t t_start;
  165. } test_args_t;
  166. void timer_func(void* arg)
  167. {
  168. test_args_t* p_args = (test_args_t*) arg;
  169. // check order
  170. size_t count = p_args->common->count;
  171. int expected_index = p_args->common->order[count];
  172. int ms_since_start = (ref_clock_get() - p_args->t_start) / 1000;
  173. printf("Time %dms, at count %d, expected timer %d, got timer %d\n",
  174. ms_since_start, count, expected_index, p_args->timer_index);
  175. if (expected_index != p_args->timer_index) {
  176. p_args->pass = false;
  177. esp_timer_stop(p_args->timer);
  178. xSemaphoreGive(p_args->done);
  179. return;
  180. }
  181. p_args->common->count++;
  182. if (++p_args->intervals_count == N) {
  183. esp_timer_stop(p_args->timer);
  184. xSemaphoreGive(p_args->done);
  185. return;
  186. }
  187. int next_interval = p_args->intervals[p_args->intervals_count];
  188. printf("starting timer %d interval #%d, %d ms\n",
  189. p_args->timer_index, p_args->intervals_count, next_interval);
  190. esp_timer_start_once(p_args->timer, next_interval * 1000);
  191. }
  192. test_common_t common = {
  193. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  194. .count = 0
  195. };
  196. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  197. ref_clock_init();
  198. int64_t now = ref_clock_get();
  199. test_args_t args1 = {
  200. .timer_index = 1,
  201. .intervals = {10, 40, 20, 40, 30},
  202. .common = &common,
  203. .pass = true,
  204. .done = done,
  205. .t_start = now
  206. };
  207. test_args_t args2 = {
  208. .timer_index = 2,
  209. .intervals = {20, 20, 60, 30, 40},
  210. .common = &common,
  211. .pass = true,
  212. .done = done,
  213. .t_start = now
  214. };
  215. test_args_t args3 = {
  216. .timer_index = 3,
  217. .intervals = {30, 30, 60, 30, 10},
  218. .common = &common,
  219. .pass = true,
  220. .done = done,
  221. .t_start = now
  222. };
  223. esp_timer_create_args_t create_args = {
  224. .callback = &timer_func,
  225. .arg = &args1,
  226. .name = "1"
  227. };
  228. TEST_ESP_OK(esp_timer_create(&create_args, &args1.timer));
  229. create_args.name = "2";
  230. create_args.arg = &args2;
  231. TEST_ESP_OK(esp_timer_create(&create_args, &args2.timer));
  232. create_args.name = "3";
  233. create_args.arg = &args3;
  234. TEST_ESP_OK(esp_timer_create(&create_args, &args3.timer));
  235. esp_timer_start_once(args1.timer, args1.intervals[0] * 1000);
  236. esp_timer_start_once(args2.timer, args2.intervals[0] * 1000);
  237. esp_timer_start_once(args3.timer, args3.intervals[0] * 1000);
  238. for (int i = 0; i < 3; ++i) {
  239. int result = xSemaphoreTake(done, 1000 / portTICK_PERIOD_MS);
  240. TEST_ASSERT_TRUE(result == pdPASS);
  241. }
  242. TEST_ASSERT_TRUE(args1.pass);
  243. TEST_ASSERT_TRUE(args2.pass);
  244. TEST_ASSERT_TRUE(args3.pass);
  245. ref_clock_deinit();
  246. TEST_ESP_OK( esp_timer_dump(stdout) );
  247. TEST_ESP_OK( esp_timer_delete(args1.timer) );
  248. TEST_ESP_OK( esp_timer_delete(args2.timer) );
  249. TEST_ESP_OK( esp_timer_delete(args3.timer) );
  250. #undef N
  251. }
  252. /* Create two timers, start them around the same time, and search through
  253. * timeout delta values to reproduce the case when timeouts occur close to
  254. * each other, testing the "multiple timers triggered" code path in timer_process_alarm.
  255. */
  256. TEST_CASE("esp_timer for very short intervals", "[esp_timer]")
  257. {
  258. SemaphoreHandle_t semaphore = xSemaphoreCreateCounting(2, 0);
  259. void timer_func(void* arg) {
  260. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  261. xSemaphoreGive(done);
  262. printf(".");
  263. }
  264. esp_timer_create_args_t timer_args = {
  265. .callback = &timer_func,
  266. .arg = (void*) semaphore,
  267. .name = "foo"
  268. };
  269. esp_timer_handle_t timer1, timer2;
  270. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer1) );
  271. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer2) );
  272. const int timeout_ms = 10;
  273. for (int timeout_delta_us = -150; timeout_delta_us < 150; timeout_delta_us++) {
  274. printf("delta=%d", timeout_delta_us);
  275. ESP_ERROR_CHECK( esp_timer_start_once(timer1, timeout_ms * 1000) );
  276. ESP_ERROR_CHECK( esp_timer_start_once(timer2, timeout_ms * 1000 + timeout_delta_us) );
  277. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  278. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  279. printf("\n");
  280. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer1));
  281. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer2));
  282. }
  283. vSemaphoreDelete(semaphore);
  284. }
  285. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  286. {
  287. int64_t begin = esp_timer_get_time();
  288. volatile int64_t end;
  289. const int iter_count = 10000;
  290. for (int i = 0; i < iter_count; ++i) {
  291. end = esp_timer_get_time();
  292. }
  293. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  294. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  295. }
  296. /* This test runs for about 10 minutes and is disabled in CI.
  297. * Such run time is needed to have FRC2 timer overflow a few times.
  298. */
  299. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer][ignore]")
  300. {
  301. void timer_test_task(void* arg) {
  302. int64_t delta = esp_timer_get_time() - ref_clock_get();
  303. const int iter_count = 1000000000;
  304. for (int i = 0; i < iter_count; ++i) {
  305. int64_t now = esp_timer_get_time();
  306. int64_t ref_now = ref_clock_get();
  307. int64_t diff = now - (ref_now + delta);
  308. /* Allow some difference due to rtos tick interrupting task between
  309. * getting 'now' and 'ref_now'.
  310. */
  311. TEST_ASSERT_INT32_WITHIN(100, 0, (int) diff);
  312. }
  313. xSemaphoreGive((SemaphoreHandle_t) arg);
  314. vTaskDelete(NULL);
  315. }
  316. ref_clock_init();
  317. SemaphoreHandle_t done_1 = xSemaphoreCreateBinary();
  318. SemaphoreHandle_t done_2 = xSemaphoreCreateBinary();
  319. xTaskCreatePinnedToCore(&timer_test_task, "t1", 4096, (void*) done_1, 6, NULL, 0);
  320. xTaskCreatePinnedToCore(&timer_test_task, "t2", 4096, (void*) done_2, 6, NULL, 1);
  321. TEST_ASSERT_TRUE( xSemaphoreTake(done_1, portMAX_DELAY) );
  322. TEST_ASSERT_TRUE( xSemaphoreTake(done_2, portMAX_DELAY) );
  323. vSemaphoreDelete(done_1);
  324. vSemaphoreDelete(done_2);
  325. ref_clock_deinit();
  326. }
  327. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  328. {
  329. esp_timer_dump(stdout);
  330. }
  331. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  332. {
  333. typedef struct {
  334. SemaphoreHandle_t notify_from_timer_cb;
  335. esp_timer_handle_t timer;
  336. } test_arg_t;
  337. void timer_func(void* varg)
  338. {
  339. test_arg_t arg = *(test_arg_t*) varg;
  340. esp_timer_delete(arg.timer);
  341. printf("Timer %p is deleted\n", arg.timer);
  342. xSemaphoreGive(arg.notify_from_timer_cb);
  343. }
  344. test_arg_t args = {
  345. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  346. };
  347. esp_timer_create_args_t timer_args = {
  348. .callback = &timer_func,
  349. .arg = &args,
  350. .name = "self_deleter"
  351. };
  352. esp_timer_create(&timer_args, &args.timer);
  353. esp_timer_start_once(args.timer, 10000);
  354. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  355. printf("Checking heap at %p\n", args.timer);
  356. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  357. vSemaphoreDelete(args.notify_from_timer_cb);
  358. }
  359. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  360. {
  361. ref_clock_init();
  362. int64_t t0 = esp_timer_get_time();
  363. const int64_t diff_us = 1000000;
  364. esp_timer_impl_advance(diff_us);
  365. int64_t t1 = esp_timer_get_time();
  366. int64_t t_delta = t1 - t0;
  367. printf("diff_us=%lld t1-t0=%lld\n", diff_us, t_delta);
  368. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  369. ref_clock_deinit();
  370. }
  371. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  372. {
  373. typedef struct {
  374. int64_t cb_time;
  375. } test_state_t;
  376. void timer_func(void* varg) {
  377. test_state_t* arg = (test_state_t*) varg;
  378. arg->cb_time = ref_clock_get();
  379. }
  380. ref_clock_init();
  381. test_state_t state = { 0 };
  382. esp_timer_create_args_t timer_args = {
  383. .callback = &timer_func,
  384. .arg = &state
  385. };
  386. esp_timer_handle_t timer;
  387. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  388. const int64_t interval = 10000;
  389. const int64_t advance = 2000;
  390. printf("test 1\n");
  391. int64_t t_start = ref_clock_get();
  392. esp_timer_start_once(timer, interval);
  393. esp_timer_impl_advance(advance);
  394. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  395. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  396. printf("test 2\n");
  397. state.cb_time = 0;
  398. t_start = ref_clock_get();
  399. esp_timer_start_once(timer, interval);
  400. esp_timer_impl_advance(interval);
  401. vTaskDelay(1);
  402. TEST_ASSERT(state.cb_time > t_start);
  403. ref_clock_deinit();
  404. }