test_ets_timer.c 6.9 KB

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