test_ets_timer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <inttypes.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include "unity.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/semphr.h"
  15. #include "spi_flash_mmap.h"
  16. #include "esp_rom_sys.h"
  17. #include "esp_private/spi_flash_os.h"
  18. #include "rom/ets_sys.h"
  19. static void test_correct_delay_timer_func(void* arg)
  20. {
  21. struct timeval* ptv = (struct timeval*) arg;
  22. gettimeofday(ptv, NULL);
  23. }
  24. TEST_CASE("ets_timer produces correct delay", "[ets_timer]")
  25. {
  26. ETSTimer timer1 = {0};
  27. const int delays_ms[] = {20, 100, 200, 250};
  28. const size_t delays_count = sizeof(delays_ms) / sizeof(delays_ms[0]);
  29. for (size_t i = 0; i < delays_count; ++i) {
  30. struct timeval tv_end = {0};
  31. ets_timer_setfn(&timer1, &test_correct_delay_timer_func, &tv_end);
  32. struct timeval tv_start;
  33. gettimeofday(&tv_start, NULL);
  34. ets_timer_arm(&timer1, delays_ms[i], false);
  35. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  36. int32_t ms_diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
  37. (tv_end.tv_usec - tv_start.tv_usec) / 1000;
  38. printf("%d %"PRIi32"\n", delays_ms[i], ms_diff);
  39. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  40. }
  41. ets_timer_disarm(&timer1);
  42. ets_timer_done(&timer1);
  43. }
  44. // no, we can't make this a const size_t (§6.7.5.2)
  45. #define NUM_INTERVALS 16
  46. typedef struct {
  47. ETSTimer *timer;
  48. size_t cur_interval;
  49. int intervals[NUM_INTERVALS];
  50. struct timeval tv_start;
  51. } test_periodic_correct_delays_args_t;
  52. static void test_periodic_correct_delays_timer_func(void* arg)
  53. {
  54. test_periodic_correct_delays_args_t *p_args = (test_periodic_correct_delays_args_t *) arg;
  55. struct timeval tv_now;
  56. gettimeofday(&tv_now, NULL);
  57. int32_t ms_diff = (tv_now.tv_sec - p_args->tv_start.tv_sec) * 1000 +
  58. (tv_now.tv_usec - p_args->tv_start.tv_usec) / 1000;
  59. printf("timer #%d %"PRIi32"ms\n", p_args->cur_interval, ms_diff);
  60. p_args->intervals[p_args->cur_interval++] = ms_diff;
  61. // Deliberately make timer handler run longer.
  62. // We check that this doesn't affect the result.
  63. esp_rom_delay_us(10 * 1000);
  64. if (p_args->cur_interval == NUM_INTERVALS) {
  65. printf("done\n");
  66. ets_timer_disarm(p_args->timer);
  67. }
  68. }
  69. TEST_CASE("periodic ets_timer produces correct delays", "[ets_timer]")
  70. {
  71. const int delay_ms = 100;
  72. ETSTimer timer1 = {0};
  73. test_periodic_correct_delays_args_t args = {0};
  74. args.timer = &timer1;
  75. gettimeofday(&args.tv_start, NULL);
  76. ets_timer_setfn(&timer1, &test_periodic_correct_delays_timer_func, &args);
  77. ets_timer_arm(&timer1, delay_ms, true);
  78. vTaskDelay(delay_ms * (NUM_INTERVALS + 1));
  79. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  80. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  81. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  82. }
  83. ets_timer_done(&timer1);
  84. }
  85. #undef NUM_INTERVALS
  86. #define N 5
  87. typedef struct {
  88. const int order[N * 3];
  89. size_t count;
  90. } test_timers_ordered_correctly_common_t;
  91. typedef struct {
  92. int timer_index;
  93. const int intervals[N];
  94. size_t intervals_count;
  95. ETSTimer* timer;
  96. test_timers_ordered_correctly_common_t* common;
  97. bool pass;
  98. SemaphoreHandle_t done;
  99. } test_timers_ordered_correctly_args_t;
  100. static void test_timers_ordered_correctly_timer_func(void* arg)
  101. {
  102. test_timers_ordered_correctly_args_t* p_args = (test_timers_ordered_correctly_args_t*) arg;
  103. // check order
  104. size_t count = p_args->common->count;
  105. int expected_index = p_args->common->order[count];
  106. printf("At count %d, expected timer %d, got timer %d\n",
  107. count, expected_index, p_args->timer_index);
  108. if (expected_index != p_args->timer_index) {
  109. p_args->pass = false;
  110. ets_timer_disarm(p_args->timer);
  111. xSemaphoreGive(p_args->done);
  112. return;
  113. }
  114. p_args->common->count++;
  115. if (++p_args->intervals_count == N) {
  116. ets_timer_disarm(p_args->timer);
  117. xSemaphoreGive(p_args->done);
  118. return;
  119. }
  120. int next_interval = p_args->intervals[p_args->intervals_count];
  121. printf("timer %d interval #%d, %d ms\n",
  122. p_args->timer_index, p_args->intervals_count, next_interval);
  123. ets_timer_arm(p_args->timer, next_interval, false);
  124. }
  125. TEST_CASE("multiple ETSTimers are ordered correctly", "[ets_timer]")
  126. {
  127. ETSTimer timer1;
  128. ETSTimer timer2;
  129. ETSTimer timer3;
  130. test_timers_ordered_correctly_common_t common = {
  131. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  132. .count = 0
  133. };
  134. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  135. test_timers_ordered_correctly_args_t args1 = {
  136. .timer_index = 1,
  137. .intervals = {10, 40, 20, 40, 30},
  138. .timer = &timer1,
  139. .common = &common,
  140. .pass = true,
  141. .done = done
  142. };
  143. test_timers_ordered_correctly_args_t args2 = {
  144. .timer_index = 2,
  145. .intervals = {20, 20, 60, 30, 40},
  146. .timer = &timer2,
  147. .common = &common,
  148. .pass = true,
  149. .done = done
  150. };
  151. test_timers_ordered_correctly_args_t args3 = {
  152. .timer_index = 3,
  153. .intervals = {30, 30, 60, 30, 10},
  154. .timer = &timer3,
  155. .common = &common,
  156. .pass = true,
  157. .done = done
  158. };
  159. ets_timer_setfn(&timer1, &test_timers_ordered_correctly_timer_func, &args1);
  160. ets_timer_setfn(&timer2, &test_timers_ordered_correctly_timer_func, &args2);
  161. ets_timer_setfn(&timer3, &test_timers_ordered_correctly_timer_func, &args3);
  162. ets_timer_arm(&timer1, args1.intervals[0], false);
  163. ets_timer_arm(&timer2, args2.intervals[0], false);
  164. ets_timer_arm(&timer3, args3.intervals[0], false);
  165. for (int i = 0; i < 3; ++i) {
  166. int result = xSemaphoreTake(done, 180 / portTICK_PERIOD_MS);
  167. TEST_ASSERT_TRUE(result == pdPASS);
  168. }
  169. TEST_ASSERT_TRUE(args1.pass);
  170. TEST_ASSERT_TRUE(args2.pass);
  171. TEST_ASSERT_TRUE(args3.pass);
  172. ets_timer_done(&timer1);
  173. ets_timer_done(&timer2);
  174. ets_timer_done(&timer3);
  175. }
  176. #undef N
  177. static void IRAM_ATTR test_iram_timer_func(void* arg)
  178. {
  179. volatile bool *b = (volatile bool *)arg;
  180. *b = true;
  181. }
  182. /* WiFi/BT coexistence will sometimes arm/disarm
  183. timers from an ISR where flash may be disabled. */
  184. IRAM_ATTR TEST_CASE("ETSTimers arm & disarm run from IRAM", "[ets_timer]")
  185. {
  186. volatile bool flag = false;
  187. ETSTimer timer1;
  188. const int INTERVAL = 5;
  189. ets_timer_setfn(&timer1, &test_iram_timer_func, (void *)&flag);
  190. /* arm a disabled timer, then disarm a live timer */
  191. spi_flash_guard_get()->start(); // Disables flash cache
  192. ets_timer_arm(&timer1, INTERVAL, false);
  193. // redundant call is deliberate (test code path if already armed)
  194. ets_timer_arm(&timer1, INTERVAL, false);
  195. ets_timer_disarm(&timer1);
  196. spi_flash_guard_get()->end(); // Re-enables flash cache
  197. TEST_ASSERT_FALSE(flag); // didn't expire yet
  198. /* do the same thing but wait for the timer to expire */
  199. spi_flash_guard_get()->start();
  200. ets_timer_arm(&timer1, INTERVAL, false);
  201. spi_flash_guard_get()->end();
  202. vTaskDelay(2 * INTERVAL / portTICK_PERIOD_MS);
  203. TEST_ASSERT_TRUE(flag);
  204. spi_flash_guard_get()->start();
  205. ets_timer_disarm(&timer1);
  206. spi_flash_guard_get()->end();
  207. ets_timer_done(&timer1);
  208. }