test_esp_timer.c 24 KB

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