test_ets_timer.c 7.8 KB

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