test_ets_timer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #endif
  20. TEST_CASE("ets_timer produces correct delay", "[ets_timer]")
  21. {
  22. void timer_func(void* arg)
  23. {
  24. struct timeval* ptv = (struct timeval*) arg;
  25. gettimeofday(ptv, NULL);
  26. }
  27. ETSTimer timer1 = {0};
  28. const int delays_ms[] = {20, 100, 200, 250};
  29. const size_t delays_count = sizeof(delays_ms) / sizeof(delays_ms[0]);
  30. for (size_t i = 0; i < delays_count; ++i) {
  31. struct timeval tv_end = {0};
  32. ets_timer_setfn(&timer1, &timer_func, &tv_end);
  33. struct timeval tv_start;
  34. gettimeofday(&tv_start, NULL);
  35. ets_timer_arm(&timer1, delays_ms[i], false);
  36. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  37. int32_t ms_diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
  38. (tv_end.tv_usec - tv_start.tv_usec) / 1000;
  39. printf("%d %d\n", delays_ms[i], ms_diff);
  40. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  41. }
  42. ets_timer_disarm(&timer1);
  43. ets_timer_done(&timer1);
  44. }
  45. TEST_CASE("periodic ets_timer produces correct delays", "[ets_timer]")
  46. {
  47. // no, we can't make this a const size_t (§6.7.5.2)
  48. #define NUM_INTERVALS 16
  49. typedef struct {
  50. ETSTimer *timer;
  51. size_t cur_interval;
  52. int intervals[NUM_INTERVALS];
  53. struct timeval tv_start;
  54. } test_args_t;
  55. void timer_func(void *arg) {
  56. test_args_t *p_args = (test_args_t *) arg;
  57. struct timeval tv_now;
  58. gettimeofday(&tv_now, NULL);
  59. int32_t ms_diff = (tv_now.tv_sec - p_args->tv_start.tv_sec) * 1000 +
  60. (tv_now.tv_usec - p_args->tv_start.tv_usec) / 1000;
  61. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  62. p_args->intervals[p_args->cur_interval++] = ms_diff;
  63. // Deliberately make timer handler run longer.
  64. // We check that this doesn't affect the result.
  65. esp_rom_delay_us(10 * 1000);
  66. if (p_args->cur_interval == NUM_INTERVALS) {
  67. printf("done\n");
  68. ets_timer_disarm(p_args->timer);
  69. }
  70. }
  71. const int delay_ms = 100;
  72. ETSTimer timer1 = {0};
  73. test_args_t args = {0};
  74. args.timer = &timer1;
  75. gettimeofday(&args.tv_start, NULL);
  76. ets_timer_setfn(&timer1, &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. #undef NUM_INTERVALS
  85. }
  86. TEST_CASE("multiple ETSTimers are ordered correctly", "[ets_timer]")
  87. {
  88. #define N 5
  89. typedef struct {
  90. const int order[N * 3];
  91. size_t count;
  92. } test_common_t;
  93. typedef struct {
  94. int timer_index;
  95. const int intervals[N];
  96. size_t intervals_count;
  97. ETSTimer* timer;
  98. test_common_t* common;
  99. bool pass;
  100. SemaphoreHandle_t done;
  101. } test_args_t;
  102. void timer_func(void* arg)
  103. {
  104. test_args_t* p_args = (test_args_t*) arg;
  105. // check order
  106. size_t count = p_args->common->count;
  107. int expected_index = p_args->common->order[count];
  108. printf("At count %d, expected timer %d, got timer %d\n",
  109. count, expected_index, p_args->timer_index);
  110. if (expected_index != p_args->timer_index) {
  111. p_args->pass = false;
  112. ets_timer_disarm(p_args->timer);
  113. xSemaphoreGive(p_args->done);
  114. return;
  115. }
  116. p_args->common->count++;
  117. if (++p_args->intervals_count == N) {
  118. ets_timer_disarm(p_args->timer);
  119. xSemaphoreGive(p_args->done);
  120. return;
  121. }
  122. int next_interval = p_args->intervals[p_args->intervals_count];
  123. printf("timer %d interval #%d, %d ms\n",
  124. p_args->timer_index, p_args->intervals_count, next_interval);
  125. ets_timer_arm(p_args->timer, next_interval, false);
  126. }
  127. ETSTimer timer1;
  128. ETSTimer timer2;
  129. ETSTimer timer3;
  130. test_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_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_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_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, &timer_func, &args1);
  160. ets_timer_setfn(&timer2, &timer_func, &args2);
  161. ets_timer_setfn(&timer3, &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. #undef N
  176. }
  177. /* WiFi/BT coexistence will sometimes arm/disarm
  178. timers from an ISR where flash may be disabled. */
  179. IRAM_ATTR TEST_CASE("ETSTimers arm & disarm run from IRAM", "[ets_timer]")
  180. {
  181. void timer_func(void* arg)
  182. {
  183. volatile bool *b = (volatile bool *)arg;
  184. *b = true;
  185. }
  186. volatile bool flag = false;
  187. ETSTimer timer1;
  188. const int INTERVAL = 5;
  189. ets_timer_setfn(&timer1, &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. }