test_ets_timer.c 7.9 KB

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