test_esp_timer.c 27 KB

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