test_esp_timer.c 26 KB

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