test_ets_timer.c 7.9 KB

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