test_ets_timer.c 7.7 KB

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