test_esp_timer.c 26 KB

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