test_ets_timer.c 6.8 KB

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