test_esp_timer.c 25 KB

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