test_esp_timer.c 33 KB

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