test_ets_timer.c 7.6 KB

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