test_esp_timer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. #define SEC (1000000)
  18. #if CONFIG_ESP_TIMER_IMPL_FRC2
  19. #include "soc/frc_timer_reg.h"
  20. #endif
  21. #ifdef CONFIG_ESP_TIMER_PROFILING
  22. #define WITH_PROFILING 1
  23. #endif
  24. #ifdef CONFIG_ESP_TIMER_IMPL_FRC2
  25. extern uint32_t esp_timer_impl_get_overflow_val(void);
  26. extern void esp_timer_impl_set_overflow_val(uint32_t overflow_val);
  27. static uint32_t s_old_overflow_val;
  28. static void setup_overflow(void)
  29. {
  30. s_old_overflow_val = esp_timer_impl_get_overflow_val();
  31. /* Overflow every 0.1 sec.
  32. * Chosen so that it is 0 modulo s_timer_ticks_per_us (which is 80),
  33. * to prevent roundoff error on each overflow.
  34. */
  35. esp_timer_impl_set_overflow_val(8000000);
  36. }
  37. static void teardown_overflow(void)
  38. {
  39. esp_timer_impl_set_overflow_val(s_old_overflow_val);
  40. }
  41. #else
  42. static void setup_overflow(void)
  43. {
  44. }
  45. static void teardown_overflow(void)
  46. {
  47. }
  48. #endif // CONFIG_ESP_TIMER_IMPL_FRC2
  49. TEST_CASE("esp_timer orders timers correctly", "[esp_timer]")
  50. {
  51. void dummy_cb(void* arg)
  52. {
  53. }
  54. uint64_t timeouts[] = { 10000, 1000, 10000, 5000, 20000, 1000 };
  55. size_t indices[] = { 3, 0, 4, 2, 5, 1 };
  56. const size_t num_timers = sizeof(timeouts)/sizeof(timeouts[0]);
  57. esp_timer_handle_t handles[num_timers];
  58. char* names[num_timers];
  59. setup_overflow();
  60. for (size_t i = 0; i < num_timers; ++i) {
  61. asprintf(&names[i], "timer%d", i);
  62. esp_timer_create_args_t args = {
  63. .callback = &dummy_cb,
  64. .name = names[i]
  65. };
  66. TEST_ESP_OK(esp_timer_create(&args, &handles[i]));
  67. TEST_ESP_OK(esp_timer_start_once(handles[i], timeouts[i] * 100));
  68. }
  69. teardown_overflow();
  70. char* stream_str[1024];
  71. FILE* stream = fmemopen(stream_str, sizeof(stream_str), "r+");
  72. TEST_ESP_OK(esp_timer_dump(stream));
  73. for (size_t i = 0; i < num_timers; ++i) {
  74. TEST_ESP_OK(esp_timer_stop(handles[i]));
  75. TEST_ESP_OK(esp_timer_delete(handles[i]));
  76. free(names[i]);
  77. }
  78. fflush(stream);
  79. fseek(stream, 0, SEEK_SET);
  80. /* Discard header lines */
  81. char line[128];
  82. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  83. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  84. for (size_t i = 0; i < num_timers; ++i) {
  85. TEST_ASSERT_NOT_NULL(fgets(line, sizeof(line), stream));
  86. #if WITH_PROFILING
  87. int timer_id;
  88. sscanf(line, "timer%d", &timer_id);
  89. TEST_ASSERT_EQUAL(indices[timer_id], i);
  90. #else
  91. intptr_t timer_ptr;
  92. sscanf(line, "timer@0x%x", &timer_ptr);
  93. for (size_t j = 0; j < num_timers; ++j) {
  94. if (indices[j] == i) {
  95. TEST_ASSERT_EQUAL_PTR(handles[j], timer_ptr);
  96. break;
  97. }
  98. }
  99. #endif
  100. }
  101. fclose(stream);
  102. }
  103. TEST_CASE("esp_timer_impl_set_alarm stress test", "[esp_timer]")
  104. {
  105. const int test_time_sec = 10;
  106. void set_alarm_task(void* arg)
  107. {
  108. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  109. int64_t start = esp_timer_impl_get_time();
  110. int64_t now = start;
  111. int count = 0;
  112. const int delays[] = {50, 5000, 10000000};
  113. const int delays_count = sizeof(delays)/sizeof(delays[0]);
  114. while (now - start < test_time_sec * 1000000) {
  115. now = esp_timer_impl_get_time();
  116. esp_timer_impl_set_alarm(now + delays[count % delays_count]);
  117. ++count;
  118. }
  119. xSemaphoreGive(done);
  120. vTaskDelete(NULL);
  121. }
  122. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  123. setup_overflow();
  124. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_0", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 0);
  125. #if portNUM_PROCESSORS == 2
  126. xTaskCreatePinnedToCore(&set_alarm_task, "set_alarm_1", 4096, done, UNITY_FREERTOS_PRIORITY, NULL, 1);
  127. #endif
  128. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  129. #if portNUM_PROCESSORS == 2
  130. TEST_ASSERT(xSemaphoreTake(done, test_time_sec * 2 * 1000 / portTICK_PERIOD_MS));
  131. #endif
  132. teardown_overflow();
  133. vSemaphoreDelete(done);
  134. }
  135. TEST_CASE("esp_timer produces correct delay", "[esp_timer]")
  136. {
  137. void timer_func(void* arg)
  138. {
  139. int64_t* p_end = (int64_t*) arg;
  140. *p_end = ref_clock_get();
  141. }
  142. int64_t t_end;
  143. esp_timer_handle_t timer1;
  144. esp_timer_create_args_t args = {
  145. .callback = &timer_func,
  146. .arg = &t_end,
  147. .name = "timer1"
  148. };
  149. TEST_ESP_OK(esp_timer_create(&args, &timer1));
  150. const int delays_ms[] = {20, 100, 200, 250};
  151. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  152. ref_clock_init();
  153. setup_overflow();
  154. for (size_t i = 0; i < delays_count; ++i) {
  155. t_end = 0;
  156. int64_t t_start = ref_clock_get();
  157. TEST_ESP_OK(esp_timer_start_once(timer1, delays_ms[i] * 1000));
  158. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  159. TEST_ASSERT(t_end != 0);
  160. int32_t ms_diff = (t_end - t_start) / 1000;
  161. printf("%d %d\n", delays_ms[i], ms_diff);
  162. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  163. }
  164. teardown_overflow();
  165. ref_clock_deinit();
  166. TEST_ESP_OK( esp_timer_dump(stdout) );
  167. esp_timer_delete(timer1);
  168. }
  169. TEST_CASE("periodic esp_timer produces correct delays", "[esp_timer]")
  170. {
  171. // no, we can't make this a const size_t (§6.7.5.2)
  172. #define NUM_INTERVALS 16
  173. typedef struct {
  174. esp_timer_handle_t timer;
  175. size_t cur_interval;
  176. int intervals[NUM_INTERVALS];
  177. int64_t t_start;
  178. SemaphoreHandle_t done;
  179. } test_args_t;
  180. void timer_func(void* arg)
  181. {
  182. test_args_t* p_args = (test_args_t*) arg;
  183. int64_t t_end = ref_clock_get();
  184. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  185. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  186. p_args->intervals[p_args->cur_interval++] = ms_diff;
  187. // Deliberately make timer handler run longer.
  188. // We check that this doesn't affect the result.
  189. esp_rom_delay_us(10*1000);
  190. if (p_args->cur_interval == NUM_INTERVALS) {
  191. printf("done\n");
  192. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  193. xSemaphoreGive(p_args->done);
  194. }
  195. }
  196. const int delay_ms = 100;
  197. test_args_t args = {0};
  198. esp_timer_handle_t timer1;
  199. esp_timer_create_args_t create_args = {
  200. .callback = &timer_func,
  201. .arg = &args,
  202. .name = "timer1",
  203. };
  204. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  205. ref_clock_init();
  206. setup_overflow();
  207. args.timer = timer1;
  208. args.t_start = ref_clock_get();
  209. args.done = xSemaphoreCreateBinary();
  210. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  211. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  212. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  213. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  214. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  215. }
  216. teardown_overflow();
  217. ref_clock_deinit();
  218. TEST_ESP_OK( esp_timer_dump(stdout) );
  219. TEST_ESP_OK( esp_timer_delete(timer1) );
  220. vSemaphoreDelete(args.done);
  221. #undef NUM_INTERVALS
  222. }
  223. TEST_CASE("multiple timers are ordered correctly", "[esp_timer]")
  224. {
  225. #define N 5
  226. typedef struct {
  227. const int order[N * 3];
  228. size_t count;
  229. } test_common_t;
  230. typedef struct {
  231. int timer_index;
  232. const int intervals[N];
  233. size_t intervals_count;
  234. esp_timer_handle_t timer;
  235. test_common_t* common;
  236. bool pass;
  237. SemaphoreHandle_t done;
  238. int64_t t_start;
  239. } test_args_t;
  240. void timer_func(void* arg)
  241. {
  242. test_args_t* p_args = (test_args_t*) arg;
  243. // check order
  244. size_t count = p_args->common->count;
  245. int expected_index = p_args->common->order[count];
  246. int ms_since_start = (ref_clock_get() - p_args->t_start) / 1000;
  247. printf("Time %dms, at count %d, expected timer %d, got timer %d\n",
  248. ms_since_start, count, expected_index, p_args->timer_index);
  249. if (expected_index != p_args->timer_index) {
  250. p_args->pass = false;
  251. esp_timer_stop(p_args->timer);
  252. xSemaphoreGive(p_args->done);
  253. return;
  254. }
  255. p_args->common->count++;
  256. if (++p_args->intervals_count == N) {
  257. esp_timer_stop(p_args->timer);
  258. xSemaphoreGive(p_args->done);
  259. return;
  260. }
  261. int next_interval = p_args->intervals[p_args->intervals_count];
  262. printf("starting timer %d interval #%d, %d ms\n",
  263. p_args->timer_index, p_args->intervals_count, next_interval);
  264. esp_timer_start_once(p_args->timer, next_interval * 1000);
  265. }
  266. test_common_t common = {
  267. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  268. .count = 0
  269. };
  270. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  271. ref_clock_init();
  272. int64_t now = ref_clock_get();
  273. test_args_t args1 = {
  274. .timer_index = 1,
  275. .intervals = {10, 40, 20, 40, 30},
  276. .common = &common,
  277. .pass = true,
  278. .done = done,
  279. .t_start = now
  280. };
  281. test_args_t args2 = {
  282. .timer_index = 2,
  283. .intervals = {20, 20, 60, 30, 40},
  284. .common = &common,
  285. .pass = true,
  286. .done = done,
  287. .t_start = now
  288. };
  289. test_args_t args3 = {
  290. .timer_index = 3,
  291. .intervals = {30, 30, 60, 30, 10},
  292. .common = &common,
  293. .pass = true,
  294. .done = done,
  295. .t_start = now
  296. };
  297. esp_timer_create_args_t create_args = {
  298. .callback = &timer_func,
  299. .arg = &args1,
  300. .name = "1"
  301. };
  302. TEST_ESP_OK(esp_timer_create(&create_args, &args1.timer));
  303. create_args.name = "2";
  304. create_args.arg = &args2;
  305. TEST_ESP_OK(esp_timer_create(&create_args, &args2.timer));
  306. create_args.name = "3";
  307. create_args.arg = &args3;
  308. TEST_ESP_OK(esp_timer_create(&create_args, &args3.timer));
  309. esp_timer_start_once(args1.timer, args1.intervals[0] * 1000);
  310. esp_timer_start_once(args2.timer, args2.intervals[0] * 1000);
  311. esp_timer_start_once(args3.timer, args3.intervals[0] * 1000);
  312. for (int i = 0; i < 3; ++i) {
  313. int result = xSemaphoreTake(done, 1000 / portTICK_PERIOD_MS);
  314. TEST_ASSERT_TRUE(result == pdPASS);
  315. }
  316. TEST_ASSERT_TRUE(args1.pass);
  317. TEST_ASSERT_TRUE(args2.pass);
  318. TEST_ASSERT_TRUE(args3.pass);
  319. ref_clock_deinit();
  320. TEST_ESP_OK( esp_timer_dump(stdout) );
  321. TEST_ESP_OK( esp_timer_delete(args1.timer) );
  322. TEST_ESP_OK( esp_timer_delete(args2.timer) );
  323. TEST_ESP_OK( esp_timer_delete(args3.timer) );
  324. #undef N
  325. }
  326. /* Create two timers, start them around the same time, and search through
  327. * timeout delta values to reproduce the case when timeouts occur close to
  328. * each other, testing the "multiple timers triggered" code path in timer_process_alarm.
  329. */
  330. TEST_CASE("esp_timer for very short intervals", "[esp_timer]")
  331. {
  332. SemaphoreHandle_t semaphore = xSemaphoreCreateCounting(2, 0);
  333. void timer_func(void* arg) {
  334. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  335. xSemaphoreGive(done);
  336. printf(".");
  337. }
  338. esp_timer_create_args_t timer_args = {
  339. .callback = &timer_func,
  340. .arg = (void*) semaphore,
  341. .name = "foo"
  342. };
  343. esp_timer_handle_t timer1, timer2;
  344. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer1) );
  345. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer2) );
  346. setup_overflow();
  347. const int timeout_ms = 10;
  348. for (int timeout_delta_us = -150; timeout_delta_us < 150; timeout_delta_us++) {
  349. printf("delta=%d", timeout_delta_us);
  350. ESP_ERROR_CHECK( esp_timer_start_once(timer1, timeout_ms * 1000) );
  351. ESP_ERROR_CHECK( esp_timer_start_once(timer2, timeout_ms * 1000 + timeout_delta_us) );
  352. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  353. TEST_ASSERT_EQUAL(pdPASS, xSemaphoreTake(semaphore, timeout_ms * 2));
  354. printf("\n");
  355. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer1));
  356. TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_timer_stop(timer2));
  357. }
  358. teardown_overflow();
  359. vSemaphoreDelete(semaphore);
  360. TEST_ESP_OK(esp_timer_delete(timer1));
  361. TEST_ESP_OK(esp_timer_delete(timer2));
  362. }
  363. TEST_CASE("esp_timer_get_time call takes less than 1us", "[esp_timer]")
  364. {
  365. int64_t begin = esp_timer_get_time();
  366. volatile int64_t end;
  367. const int iter_count = 10000;
  368. for (int i = 0; i < iter_count; ++i) {
  369. end = esp_timer_get_time();
  370. }
  371. int ns_per_call = (int) ((end - begin) * 1000 / iter_count);
  372. TEST_PERFORMANCE_LESS_THAN(ESP_TIMER_GET_TIME_PER_CALL, "%dns", ns_per_call);
  373. }
  374. static int64_t IRAM_ATTR __attribute__((noinline)) get_clock_diff(void)
  375. {
  376. uint64_t hs_time = esp_timer_get_time();
  377. uint64_t ref_time = ref_clock_get();
  378. return hs_time - ref_time;
  379. }
  380. TEST_CASE("esp_timer_get_time returns monotonic values", "[esp_timer]")
  381. {
  382. typedef struct {
  383. SemaphoreHandle_t done;
  384. bool pass;
  385. int test_cnt;
  386. int error_cnt;
  387. int64_t max_error;
  388. int64_t avg_diff;
  389. int64_t dummy;
  390. } test_state_t;
  391. void timer_test_task(void* arg) {
  392. test_state_t* state = (test_state_t*) arg;
  393. state->pass = true;
  394. /* make sure both functions are in cache */
  395. state->dummy = get_clock_diff();
  396. /* calculate the difference between the two clocks */
  397. portDISABLE_INTERRUPTS();
  398. int64_t delta = get_clock_diff();
  399. portENABLE_INTERRUPTS();
  400. int64_t start_time = ref_clock_get();
  401. int error_repeat_cnt = 0;
  402. while (ref_clock_get() - start_time < 10000000) { /* 10 seconds */
  403. /* Get values of both clocks again, and check that they are close to 'delta'.
  404. * We don't disable interrupts here, because esp_timer_get_time doesn't lock
  405. * interrupts internally, so we check if it can get "broken" by a well placed
  406. * interrupt.
  407. */
  408. int64_t diff = get_clock_diff() - delta;
  409. /* Allow some difference due to rtos tick interrupting task between
  410. * getting 'hs_now' and 'now'.
  411. */
  412. if (abs(diff) > 100) {
  413. error_repeat_cnt++;
  414. state->error_cnt++;
  415. } else {
  416. error_repeat_cnt = 0;
  417. }
  418. if (error_repeat_cnt > 2) {
  419. printf("diff=%lld\n", diff);
  420. state->pass = false;
  421. }
  422. state->avg_diff += diff;
  423. state->max_error = MAX(state->max_error, abs(diff));
  424. state->test_cnt++;
  425. }
  426. state->avg_diff /= state->test_cnt;
  427. xSemaphoreGive(state->done);
  428. vTaskDelete(NULL);
  429. }
  430. ref_clock_init();
  431. setup_overflow();
  432. test_state_t states[portNUM_PROCESSORS] = {0};
  433. SemaphoreHandle_t done = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  434. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  435. states[i].done = done;
  436. xTaskCreatePinnedToCore(&timer_test_task, "test", 4096, &states[i], 6, NULL, i);
  437. }
  438. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  439. TEST_ASSERT_TRUE( xSemaphoreTake(done, portMAX_DELAY) );
  440. printf("CPU%d: %s test_cnt=%d error_cnt=%d avg_diff=%d |max_error|=%d\n",
  441. i, states[i].pass ? "PASS" : "FAIL",
  442. states[i].test_cnt, states[i].error_cnt,
  443. (int) states[i].avg_diff, (int) states[i].max_error);
  444. }
  445. vSemaphoreDelete(done);
  446. teardown_overflow();
  447. ref_clock_deinit();
  448. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  449. TEST_ASSERT(states[i].pass);
  450. }
  451. }
  452. TEST_CASE("Can dump esp_timer stats", "[esp_timer]")
  453. {
  454. esp_timer_dump(stdout);
  455. }
  456. TEST_CASE("Can delete timer from callback", "[esp_timer]")
  457. {
  458. typedef struct {
  459. SemaphoreHandle_t notify_from_timer_cb;
  460. esp_timer_handle_t timer;
  461. } test_arg_t;
  462. void timer_func(void* varg)
  463. {
  464. test_arg_t arg = *(test_arg_t*) varg;
  465. esp_timer_delete(arg.timer);
  466. printf("Timer %p is deleted\n", arg.timer);
  467. xSemaphoreGive(arg.notify_from_timer_cb);
  468. }
  469. test_arg_t args = {
  470. .notify_from_timer_cb = xSemaphoreCreateBinary(),
  471. };
  472. esp_timer_create_args_t timer_args = {
  473. .callback = &timer_func,
  474. .arg = &args,
  475. .name = "self_deleter"
  476. };
  477. esp_timer_create(&timer_args, &args.timer);
  478. esp_timer_start_once(args.timer, 10000);
  479. TEST_ASSERT_TRUE(xSemaphoreTake(args.notify_from_timer_cb, 1000 / portTICK_PERIOD_MS));
  480. printf("Checking heap at %p\n", args.timer);
  481. TEST_ASSERT_TRUE(heap_caps_check_integrity_addr((intptr_t) args.timer, true));
  482. vSemaphoreDelete(args.notify_from_timer_cb);
  483. }
  484. typedef struct {
  485. SemaphoreHandle_t delete_start;
  486. SemaphoreHandle_t delete_done;
  487. SemaphoreHandle_t test_done;
  488. esp_timer_handle_t timer;
  489. } timer_delete_test_args_t;
  490. static void timer_delete_task(void* arg)
  491. {
  492. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  493. xSemaphoreTake(args->delete_start, portMAX_DELAY);
  494. printf("Deleting the timer\n");
  495. esp_timer_delete(args->timer);
  496. printf("Timer deleted\n");
  497. xSemaphoreGive(args->delete_done);
  498. vTaskDelete(NULL);
  499. }
  500. static void timer_delete_test_callback(void* arg)
  501. {
  502. timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg;
  503. printf("Timer callback called\n");
  504. xSemaphoreGive(args->delete_start);
  505. xSemaphoreTake(args->delete_done, portMAX_DELAY);
  506. printf("Callback complete\n");
  507. xSemaphoreGive(args->test_done);
  508. }
  509. TEST_CASE("Can delete timer from a separate task, triggered from callback", "[esp_timer]")
  510. {
  511. timer_delete_test_args_t args = {
  512. .delete_start = xSemaphoreCreateBinary(),
  513. .delete_done = xSemaphoreCreateBinary(),
  514. .test_done = xSemaphoreCreateBinary(),
  515. };
  516. esp_timer_create_args_t timer_args = {
  517. .callback = &timer_delete_test_callback,
  518. .arg = &args
  519. };
  520. esp_timer_handle_t timer;
  521. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  522. args.timer = timer;
  523. xTaskCreate(timer_delete_task, "deleter", 4096, &args, 5, NULL);
  524. esp_timer_start_once(timer, 100);
  525. TEST_ASSERT(xSemaphoreTake(args.test_done, pdMS_TO_TICKS(1000)));
  526. vSemaphoreDelete(args.delete_done);
  527. vSemaphoreDelete(args.delete_start);
  528. vSemaphoreDelete(args.test_done);
  529. }
  530. TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]")
  531. {
  532. int64_t t0 = esp_timer_get_time();
  533. const int64_t diff_us = 1000000;
  534. esp_timer_impl_advance(diff_us);
  535. int64_t t1 = esp_timer_get_time();
  536. int64_t t_delta = t1 - t0;
  537. printf("diff_us=%lld t0=%lld t1=%lld t1-t0=%lld\n", diff_us, t0, t1, t_delta);
  538. TEST_ASSERT_INT_WITHIN(1000, diff_us, (int) t_delta);
  539. }
  540. TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer]")
  541. {
  542. typedef struct {
  543. int64_t cb_time;
  544. } test_state_t;
  545. void timer_func(void* varg) {
  546. test_state_t* arg = (test_state_t*) varg;
  547. arg->cb_time = ref_clock_get();
  548. }
  549. ref_clock_init();
  550. test_state_t state = { 0 };
  551. esp_timer_create_args_t timer_args = {
  552. .callback = &timer_func,
  553. .arg = &state
  554. };
  555. esp_timer_handle_t timer;
  556. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  557. const int64_t interval = 10000;
  558. const int64_t advance = 2000;
  559. printf("test 1\n");
  560. int64_t t_start = ref_clock_get();
  561. esp_timer_start_once(timer, interval);
  562. esp_timer_impl_advance(advance);
  563. vTaskDelay(2 * interval / 1000 / portTICK_PERIOD_MS);
  564. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 1000, interval - advance, state.cb_time - t_start);
  565. printf("test 2\n");
  566. state.cb_time = 0;
  567. t_start = ref_clock_get();
  568. esp_timer_start_once(timer, interval);
  569. esp_timer_impl_advance(interval);
  570. vTaskDelay(1);
  571. TEST_ASSERT(state.cb_time > t_start);
  572. ref_clock_deinit();
  573. TEST_ESP_OK(esp_timer_delete(timer));
  574. }
  575. static esp_timer_handle_t timer1;
  576. static SemaphoreHandle_t sem;
  577. static void IRAM_ATTR test_tick_hook(void)
  578. {
  579. static int i;
  580. const int iterations = 16;
  581. if (++i <= iterations) {
  582. if (i & 0x1) {
  583. TEST_ESP_OK(esp_timer_start_once(timer1, 5000));
  584. } else {
  585. TEST_ESP_OK(esp_timer_stop(timer1));
  586. }
  587. } else {
  588. xSemaphoreGiveFromISR(sem, 0);
  589. }
  590. }
  591. TEST_CASE("Can start/stop timer from ISR context", "[esp_timer]")
  592. {
  593. void timer_func(void* arg)
  594. {
  595. printf("timer cb\n");
  596. }
  597. esp_timer_create_args_t create_args = {
  598. .callback = &timer_func,
  599. };
  600. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  601. sem = xSemaphoreCreateBinary();
  602. esp_register_freertos_tick_hook(test_tick_hook);
  603. TEST_ASSERT(xSemaphoreTake(sem, portMAX_DELAY));
  604. esp_deregister_freertos_tick_hook(test_tick_hook);
  605. TEST_ESP_OK( esp_timer_delete(timer1) );
  606. vSemaphoreDelete(sem);
  607. }
  608. #if !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)
  609. #include "soc/dport_reg.h"
  610. #include "soc/frc_timer_reg.h"
  611. static bool task_stop;
  612. static bool time_jumped;
  613. static void task_check_time(void *p)
  614. {
  615. int64_t t1 = 0, t2 = 0;
  616. while (task_stop == false) {
  617. t1 = t2;
  618. t2 = esp_timer_get_time();
  619. if (t1 > t2) {
  620. int64_t shift_us = t2 - t1;
  621. time_jumped = true;
  622. printf("System clock jumps back: %lli us\n", shift_us);
  623. }
  624. vTaskDelay(1);
  625. }
  626. vTaskDelete(NULL);
  627. }
  628. static void timer_callback(void* arg)
  629. {
  630. }
  631. static void dport_task(void *pvParameters)
  632. {
  633. while (task_stop == false) {
  634. DPORT_STALL_OTHER_CPU_START();
  635. esp_rom_delay_us(3);
  636. DPORT_STALL_OTHER_CPU_END();
  637. }
  638. vTaskDelete(NULL);
  639. }
  640. TEST_CASE("esp_timer_impl_set_alarm does not set an alarm below the current time", "[esp_timer][timeout=62]")
  641. {
  642. const int max_timers = 2;
  643. time_jumped = false;
  644. task_stop = false;
  645. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  646. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  647. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  648. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  649. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  650. const esp_timer_create_args_t periodic_timer_args = {
  651. .callback = &timer_callback,
  652. };
  653. esp_timer_handle_t periodic_timer[max_timers];
  654. printf("timers created\n");
  655. esp_timer_create(&periodic_timer_args, &periodic_timer[0]);
  656. esp_timer_start_periodic(periodic_timer[0], 9000);
  657. esp_timer_create(&periodic_timer_args, &periodic_timer[1]);
  658. esp_timer_start_periodic(periodic_timer[1], 9000);
  659. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  660. task_stop = true;
  661. esp_timer_stop(periodic_timer[0]);
  662. esp_timer_delete(periodic_timer[0]);
  663. esp_timer_stop(periodic_timer[1]);
  664. esp_timer_delete(periodic_timer[1]);
  665. printf("timers deleted\n");
  666. vTaskDelay(1000 / portTICK_PERIOD_MS);
  667. TEST_ASSERT(time_jumped == false);
  668. }
  669. static esp_timer_handle_t oneshot_timer;
  670. static void oneshot_timer_callback(void* arg)
  671. {
  672. esp_timer_start_once(oneshot_timer, 5000);
  673. }
  674. static const esp_timer_create_args_t oneshot_timer_args = {
  675. .callback = &oneshot_timer_callback,
  676. };
  677. TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the System time jumps back", "[esp_timer][timeout=62]")
  678. {
  679. time_jumped = false;
  680. task_stop = false;
  681. xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0);
  682. // dport_task is used here to interrupt the esp_timer_impl_set_alarm function.
  683. // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0.
  684. // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0.
  685. xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1);
  686. const esp_timer_create_args_t periodic_timer_args = {
  687. .callback = &timer_callback,
  688. };
  689. esp_timer_handle_t periodic_timer;
  690. esp_timer_create(&periodic_timer_args, &periodic_timer);
  691. esp_timer_start_periodic(periodic_timer, 5000);
  692. esp_timer_create(&oneshot_timer_args, &oneshot_timer);
  693. esp_timer_start_once(oneshot_timer, 9990);
  694. printf("timers created\n");
  695. vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
  696. task_stop = true;
  697. esp_timer_stop(oneshot_timer);
  698. esp_timer_delete(oneshot_timer);
  699. esp_timer_stop(periodic_timer);
  700. esp_timer_delete(periodic_timer);
  701. printf("timers deleted\n");
  702. vTaskDelay(1000 / portTICK_PERIOD_MS);
  703. TEST_ASSERT(time_jumped == false);
  704. }
  705. #endif // !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)
  706. TEST_CASE("Test case when esp_timer_impl_set_alarm needs set timer < now_time", "[esp_timer]")
  707. {
  708. #ifdef CONFIG_ESP_TIMER_IMPL_FRC2
  709. REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
  710. #endif
  711. esp_timer_impl_advance(50331648); // 0xefffffff/80 = 50331647
  712. esp_rom_delay_us(2);
  713. portDISABLE_INTERRUPTS();
  714. esp_timer_impl_set_alarm(50331647);
  715. uint64_t alarm_reg = esp_timer_impl_get_alarm_reg();
  716. uint64_t count_reg = esp_timer_impl_get_counter_reg();
  717. portENABLE_INTERRUPTS();
  718. #ifdef CONFIG_ESP_TIMER_IMPL_FRC2
  719. const uint32_t offset = 80 * 2; // s_timer_ticks_per_us
  720. #else
  721. const uint32_t offset = 2;
  722. #endif
  723. printf("alarm_reg = 0x%llx, count_reg 0x%llx\n", alarm_reg, count_reg);
  724. TEST_ASSERT(alarm_reg <= (count_reg + offset));
  725. }
  726. #ifdef CONFIG_ESP_TIMER_IMPL_FRC2
  727. TEST_CASE("Test esp_timer_impl_set_alarm when the counter is near an overflow value", "[esp_timer]")
  728. {
  729. for (int i = 0; i < 1024; ++i) {
  730. uint32_t count_reg = 0xeffffe00 + i;
  731. REG_WRITE(FRC_TIMER_LOAD_REG(1), count_reg);
  732. printf("%d) count_reg = 0x%x\n", i, count_reg);
  733. esp_timer_impl_set_alarm(1); // timestamp is expired
  734. }
  735. }
  736. #endif
  737. static void timer_callback5(void* arg)
  738. {
  739. *(int64_t *)arg = esp_timer_get_time();
  740. }
  741. TEST_CASE("Test a latency between a call of callback and real event", "[esp_timer]")
  742. {
  743. int64_t callback_time = 0;
  744. const esp_timer_create_args_t periodic_timer_args = {
  745. .arg = &callback_time,
  746. .callback = &timer_callback5,
  747. };
  748. esp_timer_handle_t periodic_timer;
  749. TEST_ESP_OK(esp_timer_create(&periodic_timer_args, &periodic_timer));
  750. int interval_ms = 50;
  751. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, interval_ms * 1000));
  752. for (int i = 0; i < 5; ++i) {
  753. int64_t expected_time = esp_timer_get_next_alarm();
  754. int64_t saved_callback_time = callback_time;
  755. while (saved_callback_time == callback_time) {
  756. vTaskDelay(10 / portTICK_PERIOD_MS);
  757. }
  758. int diff = callback_time - expected_time;
  759. printf("%d us\n", diff);
  760. #ifndef CONFIG_IDF_ENV_FPGA
  761. if (i != 0) {
  762. // skip the first measurement
  763. // if CPU_FREQ = 240MHz. 14 - 16us
  764. TEST_ASSERT_LESS_OR_EQUAL(50, diff);
  765. }
  766. #endif // not CONFIG_IDF_ENV_FPGA
  767. }
  768. TEST_ESP_OK(esp_timer_dump(stdout));
  769. TEST_ESP_OK(esp_timer_stop(periodic_timer));
  770. TEST_ESP_OK(esp_timer_delete(periodic_timer));
  771. }
  772. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  773. static int64_t old_time[2];
  774. static void timer_isr_callback(void* arg)
  775. {
  776. int num_timer = *((int*)arg);
  777. int64_t now = esp_timer_get_time();
  778. int64_t dt = now - old_time[num_timer];
  779. old_time[num_timer] = now;
  780. if (num_timer == 1) {
  781. esp_rom_printf("(%lld): \t\t\t\t timer ISR, dt: %lld us\n", now, dt);
  782. assert(xPortInIsrContext());
  783. } else {
  784. esp_rom_printf("(%lld): timer TASK, dt: %lld us\n", now, dt);
  785. assert(!xPortInIsrContext());
  786. }
  787. }
  788. TEST_CASE("Test ESP_TIMER_ISR dispatch method", "[esp_timer]")
  789. {
  790. TEST_ESP_OK(esp_timer_dump(stdout));
  791. int timer[2]= {1, 2};
  792. const esp_timer_create_args_t periodic_timer1_args = {
  793. .callback = &timer_isr_callback,
  794. .dispatch_method = ESP_TIMER_ISR,
  795. .arg = &timer[0],
  796. .name = "ISR",
  797. };
  798. esp_timer_handle_t periodic_timer1;
  799. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  800. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 400000));
  801. const esp_timer_create_args_t periodic_timer2_args = {
  802. .callback = &timer_isr_callback,
  803. .dispatch_method = ESP_TIMER_TASK,
  804. .arg = &timer[1],
  805. .name = "TASK",
  806. };
  807. esp_timer_handle_t periodic_timer2;
  808. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  809. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 500000));
  810. printf("timers created\n");
  811. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  812. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  813. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  814. TEST_ESP_OK(esp_timer_dump(stdout));
  815. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  816. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  817. printf("timers deleted\n");
  818. TEST_ESP_OK(esp_timer_dump(stdout));
  819. }
  820. static void dump_task(void* arg)
  821. {
  822. bool* stop_dump_task = (bool*) arg;
  823. while (*stop_dump_task == false) {
  824. TEST_ESP_OK(esp_timer_dump(NULL));
  825. }
  826. vTaskDelete(NULL);
  827. }
  828. static void isr_callback(void* arg)
  829. {
  830. assert(xPortInIsrContext());
  831. }
  832. static void task_callback(void* arg)
  833. {
  834. assert(!xPortInIsrContext());
  835. }
  836. TEST_CASE("Test ESP_TIMER_ISR dispatch method is not blocked", "[esp_timer]")
  837. {
  838. const esp_timer_create_args_t periodic_timer1_args = {
  839. .callback = &isr_callback,
  840. .dispatch_method = ESP_TIMER_ISR,
  841. .arg = NULL,
  842. .name = "ISR",
  843. };
  844. esp_timer_handle_t periodic_timer1;
  845. TEST_ESP_OK(esp_timer_create(&periodic_timer1_args, &periodic_timer1));
  846. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer1, 500));
  847. const esp_timer_create_args_t periodic_timer2_args = {
  848. .callback = &task_callback,
  849. .dispatch_method = ESP_TIMER_TASK,
  850. .arg = NULL,
  851. .name = "TASK",
  852. };
  853. esp_timer_handle_t periodic_timer2;
  854. TEST_ESP_OK(esp_timer_create(&periodic_timer2_args, &periodic_timer2));
  855. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer2, 5000));
  856. printf("timers created\n");
  857. bool stop_dump_task = false;
  858. xTaskCreatePinnedToCore(&dump_task, "dump", 4096, &stop_dump_task, UNITY_FREERTOS_PRIORITY, NULL, 0);
  859. vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
  860. stop_dump_task = true;
  861. vTaskDelay(100 / portTICK_PERIOD_MS);
  862. TEST_ESP_OK(esp_timer_stop(periodic_timer1));
  863. TEST_ESP_OK(esp_timer_stop(periodic_timer2));
  864. TEST_ESP_OK(esp_timer_dump(stdout));
  865. TEST_ESP_OK(esp_timer_delete(periodic_timer1));
  866. TEST_ESP_OK(esp_timer_delete(periodic_timer2));
  867. printf("timer deleted\n");
  868. }
  869. static void isr_callback1(void* arg)
  870. {
  871. assert(xPortInIsrContext());
  872. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  873. esp_rom_printf("isr_callback1: timer ISR\n");
  874. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  875. xSemaphoreGiveFromISR(done, &xHigherPriorityTaskWoken);
  876. if (xHigherPriorityTaskWoken) {
  877. esp_timer_isr_dispatch_need_yield();
  878. }
  879. }
  880. static void task_callback1(void* arg)
  881. {
  882. assert(0);
  883. }
  884. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if TASK timer list is empty", "[esp_timer]")
  885. {
  886. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  887. const esp_timer_create_args_t timer1_args = {
  888. .callback = &isr_callback1,
  889. .dispatch_method = ESP_TIMER_ISR,
  890. .arg = &done,
  891. .name = "ISR",
  892. };
  893. esp_timer_handle_t timer1;
  894. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  895. TEST_ESP_OK(esp_timer_start_periodic(timer1, 5 * SEC));
  896. const esp_timer_create_args_t timer2_args = {
  897. .callback = &task_callback1,
  898. .dispatch_method = ESP_TIMER_TASK,
  899. .arg = NULL,
  900. .name = "TASK",
  901. };
  902. esp_timer_handle_t timer2;
  903. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  904. TEST_ESP_OK(esp_timer_start_once(timer2, 3 * SEC));
  905. printf("timers created\n");
  906. printf("stop timer2\n");
  907. TEST_ESP_OK(esp_timer_stop(timer2));
  908. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  909. printf("stop timer1\n");
  910. TEST_ESP_OK(esp_timer_stop(timer1));
  911. TEST_ESP_OK(esp_timer_dump(stdout));
  912. TEST_ESP_OK(esp_timer_delete(timer1));
  913. TEST_ESP_OK(esp_timer_delete(timer2));
  914. vSemaphoreDelete(done);
  915. printf("timer deleted\n");
  916. }
  917. static void isr_callback2(void* arg)
  918. {
  919. assert(0);
  920. }
  921. static void task_callback2(void* arg)
  922. {
  923. assert(!xPortInIsrContext());
  924. esp_rom_printf("task_callback2: timer TASK\n");
  925. SemaphoreHandle_t done = *(SemaphoreHandle_t*) arg;
  926. xSemaphoreGive(done);
  927. }
  928. TEST_CASE("Test ESP_TIMER_ISR, stop API cleans alarm reg if ISR timer list is empty", "[esp_timer]")
  929. {
  930. SemaphoreHandle_t done = xSemaphoreCreateBinary();
  931. const esp_timer_create_args_t timer1_args = {
  932. .callback = &isr_callback2,
  933. .dispatch_method = ESP_TIMER_ISR,
  934. .arg = NULL,
  935. .name = "ISR",
  936. };
  937. esp_timer_handle_t timer1;
  938. TEST_ESP_OK(esp_timer_create(&timer1_args, &timer1));
  939. TEST_ESP_OK(esp_timer_start_once(timer1, 3 * SEC));
  940. const esp_timer_create_args_t timer2_args = {
  941. .callback = &task_callback2,
  942. .dispatch_method = ESP_TIMER_TASK,
  943. .arg = &done,
  944. .name = "TASK",
  945. };
  946. esp_timer_handle_t timer2;
  947. TEST_ESP_OK(esp_timer_create(&timer2_args, &timer2));
  948. TEST_ESP_OK(esp_timer_start_periodic(timer2, 5 * SEC));
  949. printf("timers created\n");
  950. printf("stop timer1\n");
  951. TEST_ESP_OK(esp_timer_stop(timer1));
  952. TEST_ASSERT(xSemaphoreTake(done, 6 * 1000 / portTICK_PERIOD_MS));
  953. printf("stop timer2\n");
  954. TEST_ESP_OK(esp_timer_stop(timer2));
  955. TEST_ESP_OK(esp_timer_dump(stdout));
  956. TEST_ESP_OK(esp_timer_delete(timer1));
  957. TEST_ESP_OK(esp_timer_delete(timer2));
  958. vSemaphoreDelete(done);
  959. printf("timer deleted\n");
  960. }
  961. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD