test_ets_timer.c 7.7 KB

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