test_esp_timer.c 27 KB

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