test_ets_timer.c 7.0 KB

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