test_ets_timer.c 7.3 KB

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