test_ets_timer.c 7.1 KB

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