test_esp_timer.c 13 KB

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