test_esp_timer.c 33 KB

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