test_esp_timer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  326. {
  327. int64_t begin = esp_timer_get_time();
  328. volatile int64_t end;
  329. const int iter_count = 10000;
  330. for (int i = 0; i < iter_count; ++i) {
  331. end = esp_timer_get_time();
  332. }
  333. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  334. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  335. }
  336. static int64_t IRAM_ATTR __attribute__((noinline)) get_clock_diff(void)
  337. {
  338. uint64_t hs_time = esp_timer_get_time();
  339. uint64_t ref_time = ref_clock_get();
  340. return hs_time - ref_time;
  341. }
  342. typedef struct {
  343. SemaphoreHandle_t done;
  344. bool pass;
  345. int test_cnt;
  346. int error_cnt;
  347. int64_t max_error;
  348. int64_t avg_diff;
  349. int64_t dummy;
  350. } test_monotonic_values_state_t;
  351. static void timer_test_monotonic_values_task(void* arg) {
  352. test_monotonic_values_state_t* state = (test_monotonic_values_state_t*) arg;
  353. state->pass = true;
  354. /* make sure both functions are in cache */
  355. state->dummy = get_clock_diff();
  356. /* calculate the difference between the two clocks */
  357. portDISABLE_INTERRUPTS();
  358. int64_t delta = get_clock_diff();
  359. portENABLE_INTERRUPTS();
  360. int64_t start_time = ref_clock_get();
  361. int error_repeat_cnt = 0;
  362. while (ref_clock_get() - start_time < 10000000) { /* 10 seconds */
  363. /* Get values of both clocks again, and check that they are close to 'delta'.
  364. * We don't disable interrupts here, because esp_timer_get_time doesn't lock
  365. * interrupts internally, so we check if it can get "broken" by a well placed
  366. * interrupt.
  367. */
  368. int64_t diff = get_clock_diff() - delta;
  369. /* Allow some difference due to rtos tick interrupting task between
  370. * getting 'hs_now' and '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->avg_diff += diff;
  383. state->max_error = MAX(state->max_error, abs(diff));
  384. state->test_cnt++;
  385. }
  386. state->avg_diff /= state->test_cnt;
  387. xSemaphoreGive(state->done);
  388. vTaskDelete(NULL);
  389. }
  390. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer]")
  391. {
  392. ref_clock_init();
  393. test_monotonic_values_state_t states[portNUM_PROCESSORS] = {0};
  394. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  395. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  396. states[i].done = done;
  397. xTaskCreatePinnedToCore(&timer_test_monotonic_values_task, "test", 4096, &states[i], 6, NULL, i);
  398. }
  399. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  400. TEST_ASSERT_TRUE( xSemaphoreTake(done, portMAX_DELAY) );
  401. printf("CPU%d: %s test_cnt=%d error_cnt=%d avg_diff=%d |max_error|=%d\n",
  402. i, states[i].pass ? "PASS" : "FAIL",
  403. states[i].test_cnt, states[i].error_cnt,
  404. (int) states[i].avg_diff, (int) states[i].max_error);
  405. }
  406. vSemaphoreDelete(done);
  407. ref_clock_deinit();
  408. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  409. TEST_ASSERT(states[i].pass);
  410. }
  411. }
  412. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  413. {
  414. esp_timer_dump(stdout);
  415. }
  416. typedef struct {
  417. SemaphoreHandle_t notify_from_timer_cb;
  418. esp_timer_handle_t timer;
  419. } test_delete_from_callback_arg_t;
  420. static void test_delete_from_callback_timer_func(void* varg)
  421. {
  422. test_delete_from_callback_arg_t arg = *(test_delete_from_callback_arg_t*) varg;
  423. esp_timer_delete(arg.timer);
  424. printf("Timer %p is deleted\n", arg.timer);
  425. xSemaphoreGive(arg.notify_from_timer_cb);
  426. }
  427. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  428. {
  429. test_delete_from_callback_arg_t args = {
  430. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  431. };
  432. esp_timer_create_args_t timer_args = {
  433. .callback = &test_delete_from_callback_timer_func,
  434. .arg = &args,
  435. .name = "self_deleter"
  436. };
  437. esp_timer_create(&timer_args, &args.timer);
  438. esp_timer_start_once(args.timer, 10000);
  439. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  440. printf("Checking heap at %p\n", args.timer);
  441. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  442. vSemaphoreDelete(args.notify_from_timer_cb);
  443. }
  444. typedef struct {
  445. SemaphoreHandle_t delete_start;
  446. SemaphoreHandle_t delete_done;
  447. SemaphoreHandle_t test_done;
  448. esp_timer_handle_t timer;
  449. } timer_delete_test_args_t;
  450. static void timer_delete_task(void* arg)
  451. {
  452. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  453. xSemaphoreTake(args->delete_start, portMAX_DELAY);
  454. printf("Deleting the timer\n");
  455. esp_timer_delete(args->timer);
  456. printf("Timer deleted\n");
  457. xSemaphoreGive(args->delete_done);
  458. vTaskDelete(NULL);
  459. }
  460. static void timer_delete_test_callback(void* arg)
  461. {
  462. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  463. printf("Timer callback called\n");
  464. xSemaphoreGive(args->delete_start);
  465. xSemaphoreTake(args->delete_done, portMAX_DELAY);
  466. printf("Callback complete\n");
  467. xSemaphoreGive(args->test_done);
  468. }
  469. TEST_CASE("Can delete timer from a separate task, triggered from callback", "[esp_timer]")
  470. {
  471. timer_delete_test_args_t args = {
  472. .delete_start = xSemaphoreCreateBinary(),
  473. .delete_done = xSemaphoreCreateBinary(),
  474. .test_done = xSemaphoreCreateBinary(),
  475. };
  476. esp_timer_create_args_t timer_args = {
  477. .callback = &timer_delete_test_callback,
  478. .arg = &args
  479. };
  480. esp_timer_handle_t timer;
  481. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  482. args.timer = timer;
  483. xTaskCreate(timer_delete_task, "deleter", 4096, &args, 5, NULL);
  484. esp_timer_start_once(timer, 100);
  485. TEST_ASSERT(xSemaphoreTake(args.test_done, pdMS_TO_TICKS(1000)));
  486. vSemaphoreDelete(args.delete_done);
  487. vSemaphoreDelete(args.delete_start);
  488. vSemaphoreDelete(args.test_done);
  489. }
  490. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  491. {
  492. int64_t t0 = esp_timer_get_time();
  493. const int64_t diff_us = 1000000;
  494. esp_timer_impl_advance(diff_us);
  495. int64_t t1 = esp_timer_get_time();
  496. int64_t t_delta = t1 - t0;
  497. printf("diff_us=%lld t0=%lld t1=%lld t1-t0=%lld\n", diff_us, t0, t1, t_delta);
  498. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  499. }
  500. typedef struct {
  501. int64_t cb_time;
  502. } test_run_when_expected_state_t;
  503. static void test_run_when_expected_timer_func(void* varg) {
  504. test_run_when_expected_state_t* arg = (test_run_when_expected_state_t*) varg;
  505. arg->cb_time = ref_clock_get();
  506. }
  507. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  508. {
  509. ref_clock_init();
  510. test_run_when_expected_state_t state = { 0 };
  511. esp_timer_create_args_t timer_args = {
  512. .callback = &test_run_when_expected_timer_func,
  513. .arg = &state
  514. };
  515. esp_timer_handle_t timer;
  516. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  517. const int64_t interval = 10000;
  518. const int64_t advance = 2000;
  519. printf("test 1\n");
  520. int64_t t_start = ref_clock_get();
  521. esp_timer_start_once(timer, interval);
  522. esp_timer_impl_advance(advance);
  523. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  524. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  525. printf("test 2\n");
  526. state.cb_time = 0;
  527. t_start = ref_clock_get();
  528. esp_timer_start_once(timer, interval);
  529. esp_timer_impl_advance(interval);
  530. vTaskDelay(1);
  531. TEST_ASSERT(state.cb_time > t_start);
  532. ref_clock_deinit();
  533. TEST_ESP_OK(esp_timer_delete(timer));
  534. }
  535. static esp_timer_handle_t timer1;
  536. static SemaphoreHandle_t sem;
  537. static void IRAM_ATTR test_tick_hook(void)
  538. {
  539. static int i;
  540. const int iterations = 16;
  541. if (++i <= iterations) {
  542. if (i & 0x1) {
  543. TEST_ESP_OK(esp_timer_start_once(timer1, 5000));
  544. } else {
  545. TEST_ESP_OK(esp_timer_stop(timer1));
  546. }
  547. } else {
  548. xSemaphoreGiveFromISR(sem, 0);
  549. }
  550. }
  551. static void test_start_stop_timer_func(void* arg)
  552. {
  553. printf("timer cb\n");
  554. }
  555. TEST_CASE("Can start/stop timer from ISR context", "[esp_timer]")
  556. {
  557. esp_timer_create_args_t create_args = {
  558. .callback = &test_start_stop_timer_func,
  559. };
  560. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  561. sem = xSemaphoreCreateBinary();
  562. esp_register_freertos_tick_hook(test_tick_hook);
  563. TEST_ASSERT(xSemaphoreTake(sem, portMAX_DELAY));
  564. esp_deregister_freertos_tick_hook(test_tick_hook);
  565. TEST_ESP_OK( esp_timer_delete(timer1) );
  566. vSemaphoreDelete(sem);
  567. }
  568. #if !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)
  569. #include "soc/dport_reg.h"
  570. static bool task_stop;
  571. static bool time_jumped;
  572. static void task_check_time(void *p)
  573. {
  574. int64_t t1 = 0, t2 = 0;
  575. while (task_stop == false) {
  576. t1 = t2;
  577. t2 = esp_timer_get_time();
  578. if (t1 > t2) {
  579. int64_t shift_us = t2 - t1;
  580. time_jumped = true;
  581. printf("System clock jumps back: %lli us\n", shift_us);
  582. }
  583. vTaskDelay(1);
  584. }
  585. vTaskDelete(NULL);
  586. }
  587. static void timer_callback(void* arg)
  588. {
  589. }
  590. static void dport_task(void *pvParameters)
  591. {
  592. while (task_stop == false) {
  593. DPORT_STALL_OTHER_CPU_START();
  594. esp_rom_delay_us(3);
  595. DPORT_STALL_OTHER_CPU_END();
  596. }
  597. vTaskDelete(NULL);
  598. }
  599. TEST_CASE("esp_timer_impl_set_alarm does not set an alarm below the current time", "[esp_timer][timeout=62]")
  600. {
  601. const int max_timers = 2;
  602. time_jumped = false;
  603. task_stop = false;
  604. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  605. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  606. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  607. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  608. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  609. const esp_timer_create_args_t periodic_timer_args = {
  610. .callback = &timer_callback,
  611. };
  612. esp_timer_handle_t periodic_timer[max_timers];
  613. printf("timers created\n");
  614. esp_timer_create(&periodic_timer_args, &periodic_timer[0]);
  615. esp_timer_start_periodic(periodic_timer[0], 9000);
  616. esp_timer_create(&periodic_timer_args, &periodic_timer[1]);
  617. esp_timer_start_periodic(periodic_timer[1], 9000);
  618. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  619. task_stop = true;
  620. esp_timer_stop(periodic_timer[0]);
  621. esp_timer_delete(periodic_timer[0]);
  622. esp_timer_stop(periodic_timer[1]);
  623. esp_timer_delete(periodic_timer[1]);
  624. printf("timers deleted\n");
  625. vTaskDelay(1000 / portTICK_PERIOD_MS);
  626. TEST_ASSERT(time_jumped == false);
  627. }
  628. static esp_timer_handle_t oneshot_timer;
  629. static void oneshot_timer_callback(void* arg)
  630. {
  631. esp_timer_start_once(oneshot_timer, 5000);
  632. }
  633. static const esp_timer_create_args_t oneshot_timer_args = {
  634. .callback = &oneshot_timer_callback,
  635. };
  636. TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the System time jumps back", "[esp_timer][timeout=62]")
  637. {
  638. time_jumped = false;
  639. task_stop = false;
  640. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  641. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  642. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  643. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  644. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  645. const esp_timer_create_args_t periodic_timer_args = {
  646. .callback = &timer_callback,
  647. };
  648. esp_timer_handle_t periodic_timer;
  649. esp_timer_create(&periodic_timer_args, &periodic_timer);
  650. esp_timer_start_periodic(periodic_timer, 5000);
  651. esp_timer_create(&oneshot_timer_args, &oneshot_timer);
  652. esp_timer_start_once(oneshot_timer, 9990);
  653. printf("timers created\n");
  654. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  655. task_stop = true;
  656. esp_timer_stop(oneshot_timer);
  657. esp_timer_delete(oneshot_timer);
  658. esp_timer_stop(periodic_timer);
  659. esp_timer_delete(periodic_timer);
  660. printf("timers deleted\n");
  661. vTaskDelay(1000 / portTICK_PERIOD_MS);
  662. TEST_ASSERT(time_jumped == false);
  663. }
  664. #endif // !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)
  665. TEST_CASE("Test case when esp_timer_impl_set_alarm needs set timer < now_time", "[esp_timer]")
  666. {
  667. esp_timer_impl_advance(50331648); // 0xefffffff/80 = 50331647
  668. esp_rom_delay_us(2);
  669. portDISABLE_INTERRUPTS();
  670. esp_timer_impl_set_alarm(50331647);
  671. uint64_t alarm_reg = esp_timer_impl_get_alarm_reg();
  672. uint64_t count_reg = esp_timer_impl_get_counter_reg();
  673. portENABLE_INTERRUPTS();
  674. const uint32_t offset = 2;
  675. printf("alarm_reg = 0x%llx, count_reg 0x%llx\n", alarm_reg, count_reg);
  676. TEST_ASSERT(alarm_reg <= (count_reg + offset));
  677. }
  678. static void timer_callback5(void* arg)
  679. {
  680. *(int64_t *)arg = esp_timer_get_time();
  681. }
  682. TEST_CASE("Test a latency between a call of callback and real event", "[esp_timer]")
  683. {
  684. int64_t callback_time = 0;
  685. const esp_timer_create_args_t periodic_timer_args = {
  686. .arg = &callback_time,
  687. .callback = &timer_callback5,
  688. };
  689. esp_timer_handle_t periodic_timer;
  690. TEST_ESP_OK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  691. int interval_ms = 50;
  692. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, interval_ms * 1000));
  693. for (int i = 0; i < 5; ++i) {
  694. int64_t expected_time = esp_timer_get_next_alarm();
  695. int64_t saved_callback_time = callback_time;
  696. while (saved_callback_time == callback_time) {
  697. vTaskDelay(10 / portTICK_PERIOD_MS);
  698. }
  699. int diff = callback_time - expected_time;
  700. printf("%d us\n", diff);
  701. #ifndef CONFIG_IDF_ENV_FPGA
  702. if (i != 0) {
  703. // skip the first measurement
  704. // if CPU_FREQ = 240MHz. 14 - 16us
  705. TEST_ASSERT_LESS_OR_EQUAL(50, diff);
  706. }
  707. #endif // not CONFIG_IDF_ENV_FPGA
  708. }
  709. TEST_ESP_OK(esp_timer_dump(stdout));
  710. TEST_ESP_OK(esp_timer_stop(periodic_timer));
  711. TEST_ESP_OK(esp_timer_delete(periodic_timer));
  712. }
  713. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  714. static int64_t old_time[2];
  715. static void timer_isr_callback(void* arg)
  716. {
  717. int num_timer = *((int*)arg);
  718. int64_t now = esp_timer_get_time();
  719. int64_t dt = now - old_time[num_timer];
  720. old_time[num_timer] = now;
  721. if (num_timer == 1) {
  722. esp_rom_printf("(%lld): \t\t\t\t timer ISR, dt: %lld us\n", now, dt);
  723. assert(xPortInIsrContext());
  724. } else {
  725. esp_rom_printf("(%lld): timer TASK, dt: %lld us\n", now, dt);
  726. assert(!xPortInIsrContext());
  727. }
  728. }
  729. TEST_CASE("Test ESP_TIMER_ISR dispatch method", "[esp_timer]")
  730. {
  731. TEST_ESP_OK(esp_timer_dump(stdout));
  732. int timer[2]= {1, 2};
  733. const esp_timer_create_args_t periodic_timer1_args = {
  734. .callback = &timer_isr_callback,
  735. .dispatch_method = ESP_TIMER_ISR,
  736. .arg = &timer[0],
  737. .name = "ISR",
  738. };
  739. esp_timer_handle_t periodic_timer1;
  740. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  741. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 400000));
  742. const esp_timer_create_args_t periodic_timer2_args = {
  743. .callback = &timer_isr_callback,
  744. .dispatch_method = ESP_TIMER_TASK,
  745. .arg = &timer[1],
  746. .name = "TASK",
  747. };
  748. esp_timer_handle_t periodic_timer2;
  749. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  750. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 500000));
  751. printf("timers created\n");
  752. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  753. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  754. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  755. TEST_ESP_OK(esp_timer_dump(stdout));
  756. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  757. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  758. printf("timers deleted\n");
  759. TEST_ESP_OK(esp_timer_dump(stdout));
  760. }
  761. static void dump_task(void* arg)
  762. {
  763. bool* stop_dump_task = (bool*) arg;
  764. while (*stop_dump_task == false) {
  765. TEST_ESP_OK(esp_timer_dump(NULL));
  766. }
  767. vTaskDelete(NULL);
  768. }
  769. static void isr_callback(void* arg)
  770. {
  771. assert(xPortInIsrContext());
  772. }
  773. static void task_callback(void* arg)
  774. {
  775. assert(!xPortInIsrContext());
  776. }
  777. TEST_CASE("Test ESP_TIMER_ISR dispatch method is not blocked", "[esp_timer]")
  778. {
  779. const esp_timer_create_args_t periodic_timer1_args = {
  780. .callback = &isr_callback,
  781. .dispatch_method = ESP_TIMER_ISR,
  782. .arg = NULL,
  783. .name = "ISR",
  784. };
  785. esp_timer_handle_t periodic_timer1;
  786. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  787. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 500));
  788. const esp_timer_create_args_t periodic_timer2_args = {
  789. .callback = &task_callback,
  790. .dispatch_method = ESP_TIMER_TASK,
  791. .arg = NULL,
  792. .name = "TASK",
  793. };
  794. esp_timer_handle_t periodic_timer2;
  795. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  796. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 5000));
  797. printf("timers created\n");
  798. bool stop_dump_task = false;
  799. xTaskCreatePinnedToCore(&dump_task, "dump", 4096, &stop_dump_task, UNITY_FREERTOS_PRIORITY, NULL, 0);
  800. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  801. stop_dump_task = true;
  802. vTaskDelay(100 / portTICK_PERIOD_MS);
  803. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  804. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  805. TEST_ESP_OK(esp_timer_dump(stdout));
  806. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  807. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  808. printf("timer deleted\n");
  809. }
  810. static void isr_callback1(void* arg)
  811. {
  812. assert(xPortInIsrContext());
  813. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  814. esp_rom_printf("isr_callback1: timer ISR\n");
  815. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  816. xSemaphoreGiveFromISR(done, &xHigherPriorityTaskWoken);
  817. if (xHigherPriorityTaskWoken) {
  818. esp_timer_isr_dispatch_need_yield();
  819. }
  820. }
  821. static void task_callback1(void* arg)
  822. {
  823. assert(0);
  824. }
  825. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if TASK timer list is empty", "[esp_timer]")
  826. {
  827. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  828. const esp_timer_create_args_t timer1_args = {
  829. .callback = &isr_callback1,
  830. .dispatch_method = ESP_TIMER_ISR,
  831. .arg = &done,
  832. .name = "ISR",
  833. };
  834. esp_timer_handle_t timer1;
  835. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  836. TEST_ESP_OK(esp_timer_start_periodic(timer1, 5 * SEC));
  837. const esp_timer_create_args_t timer2_args = {
  838. .callback = &task_callback1,
  839. .dispatch_method = ESP_TIMER_TASK,
  840. .arg = NULL,
  841. .name = "TASK",
  842. };
  843. esp_timer_handle_t timer2;
  844. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  845. TEST_ESP_OK(esp_timer_start_once(timer2, 3 * SEC));
  846. printf("timers created\n");
  847. printf("stop timer2\n");
  848. TEST_ESP_OK(esp_timer_stop(timer2));
  849. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  850. printf("stop timer1\n");
  851. TEST_ESP_OK(esp_timer_stop(timer1));
  852. TEST_ESP_OK(esp_timer_dump(stdout));
  853. TEST_ESP_OK(esp_timer_delete(timer1));
  854. TEST_ESP_OK(esp_timer_delete(timer2));
  855. vSemaphoreDelete(done);
  856. printf("timer deleted\n");
  857. }
  858. static void isr_callback2(void* arg)
  859. {
  860. assert(0);
  861. }
  862. static void task_callback2(void* arg)
  863. {
  864. assert(!xPortInIsrContext());
  865. esp_rom_printf("task_callback2: timer TASK\n");
  866. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  867. xSemaphoreGive(done);
  868. }
  869. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if ISR timer list is empty", "[esp_timer]")
  870. {
  871. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  872. const esp_timer_create_args_t timer1_args = {
  873. .callback = &isr_callback2,
  874. .dispatch_method = ESP_TIMER_ISR,
  875. .arg = NULL,
  876. .name = "ISR",
  877. };
  878. esp_timer_handle_t timer1;
  879. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  880. TEST_ESP_OK(esp_timer_start_once(timer1, 3 * SEC));
  881. const esp_timer_create_args_t timer2_args = {
  882. .callback = &task_callback2,
  883. .dispatch_method = ESP_TIMER_TASK,
  884. .arg = &done,
  885. .name = "TASK",
  886. };
  887. esp_timer_handle_t timer2;
  888. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  889. TEST_ESP_OK(esp_timer_start_periodic(timer2, 5 * SEC));
  890. printf("timers created\n");
  891. printf("stop timer1\n");
  892. TEST_ESP_OK(esp_timer_stop(timer1));
  893. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  894. printf("stop timer2\n");
  895. TEST_ESP_OK(esp_timer_stop(timer2));
  896. TEST_ESP_OK(esp_timer_dump(stdout));
  897. TEST_ESP_OK(esp_timer_delete(timer1));
  898. TEST_ESP_OK(esp_timer_delete(timer2));
  899. vSemaphoreDelete(done);
  900. printf("timer deleted\n");
  901. }
  902. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD