test_esp_timer.c 26 KB

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