test_ets_timer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include "unity.h"
  6. #include "rom/ets_sys.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. TEST_CASE("ets_timer produces correct delay", "[ets_timer]")
  11. {
  12. void timer_func(void* arg)
  13. {
  14. struct timeval* ptv = (struct timeval*) arg;
  15. gettimeofday(ptv, NULL);
  16. }
  17. ETSTimer timer1 = {0};
  18. const int delays_ms[] = {20, 100, 200, 250};
  19. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  20. for (size_t i = 0; i < delays_count; ++i) {
  21. struct timeval tv_end = {0};
  22. ets_timer_setfn(&timer1, &timer_func, &tv_end);
  23. struct timeval tv_start;
  24. gettimeofday(&tv_start, NULL);
  25. ets_timer_arm(&timer1, delays_ms[i], false);
  26. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  27. int32_t ms_diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
  28. (tv_end.tv_usec - tv_start.tv_usec) / 1000;
  29. printf("%d %d\n", delays_ms[i], ms_diff);
  30. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  31. }
  32. ets_timer_disarm(&timer1);
  33. }
  34. TEST_CASE("periodic ets_timer produces correct delays", "[ets_timer]")
  35. {
  36. // no, we can't make this a const size_t (§6.7.5.2)
  37. #define NUM_INTERVALS 16
  38. typedef struct {
  39. ETSTimer* timer;
  40. size_t cur_interval;
  41. int intervals[NUM_INTERVALS];
  42. struct timeval tv_start;
  43. } test_args_t;
  44. void timer_func(void* arg)
  45. {
  46. test_args_t* p_args = (test_args_t*) arg;
  47. struct timeval tv_now;
  48. gettimeofday(&tv_now, NULL);
  49. int32_t ms_diff = (tv_now.tv_sec - p_args->tv_start.tv_sec) * 1000 +
  50. (tv_now.tv_usec - p_args->tv_start.tv_usec) / 1000;
  51. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  52. p_args->intervals[p_args->cur_interval++] = ms_diff;
  53. // Deliberately make timer handler run longer.
  54. // We check that this doesn't affect the result.
  55. ets_delay_us(10*1000);
  56. if (p_args->cur_interval == NUM_INTERVALS) {
  57. printf("done\n");
  58. ets_timer_disarm(p_args->timer);
  59. }
  60. }
  61. const int delay_ms = 100;
  62. ETSTimer timer1 = {0};
  63. test_args_t args = {0};
  64. args.timer = &timer1;
  65. gettimeofday(&args.tv_start, NULL);
  66. ets_timer_setfn(&timer1, &timer_func, &args);
  67. ets_timer_arm(&timer1, delay_ms, true);
  68. vTaskDelay(delay_ms * (NUM_INTERVALS + 1));
  69. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  70. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  71. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  72. }
  73. #undef NUM_INTERVALS
  74. }
  75. TEST_CASE("multiple ETSTimers are ordered correctly", "[ets_timer]")
  76. {
  77. #define N 5
  78. typedef struct {
  79. const int order[N * 3];
  80. size_t count;
  81. } test_common_t;
  82. typedef struct {
  83. int timer_index;
  84. const int intervals[N];
  85. size_t intervals_count;
  86. ETSTimer* timer;
  87. test_common_t* common;
  88. bool pass;
  89. SemaphoreHandle_t done;
  90. } test_args_t;
  91. void timer_func(void* arg)
  92. {
  93. test_args_t* p_args = (test_args_t*) arg;
  94. // check order
  95. size_t count = p_args->common->count;
  96. int expected_index = p_args->common->order[count];
  97. printf("At count %d, expected timer %d, got timer %d\n",
  98. count, expected_index, p_args->timer_index);
  99. if (expected_index != p_args->timer_index) {
  100. p_args->pass = false;
  101. ets_timer_disarm(p_args->timer);
  102. xSemaphoreGive(p_args->done);
  103. return;
  104. }
  105. p_args->common->count++;
  106. if (++p_args->intervals_count == N) {
  107. ets_timer_disarm(p_args->timer);
  108. xSemaphoreGive(p_args->done);
  109. return;
  110. }
  111. int next_interval = p_args->intervals[p_args->intervals_count];
  112. printf("timer %d interval #%d, %d ms\n",
  113. p_args->timer_index, p_args->intervals_count, next_interval);
  114. ets_timer_arm(p_args->timer, next_interval, false);
  115. }
  116. ETSTimer timer1;
  117. ETSTimer timer2;
  118. ETSTimer timer3;
  119. test_common_t common = {
  120. .order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
  121. .count = 0
  122. };
  123. SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
  124. test_args_t args1 = {
  125. .timer_index = 1,
  126. .intervals = {10, 40, 20, 40, 30},
  127. .timer = &timer1,
  128. .common = &common,
  129. .pass = true,
  130. .done = done
  131. };
  132. test_args_t args2 = {
  133. .timer_index = 2,
  134. .intervals = {20, 20, 60, 30, 40},
  135. .timer = &timer2,
  136. .common = &common,
  137. .pass = true,
  138. .done = done
  139. };
  140. test_args_t args3 = {
  141. .timer_index = 3,
  142. .intervals = {30, 30, 60, 30, 10},
  143. .timer = &timer3,
  144. .common = &common,
  145. .pass = true,
  146. .done = done
  147. };
  148. ets_timer_setfn(&timer1, &timer_func, &args1);
  149. ets_timer_setfn(&timer2, &timer_func, &args2);
  150. ets_timer_setfn(&timer3, &timer_func, &args3);
  151. ets_timer_arm(&timer1, args1.intervals[0], false);
  152. ets_timer_arm(&timer2, args2.intervals[0], false);
  153. ets_timer_arm(&timer3, args3.intervals[0], false);
  154. for (int i = 0; i < 3; ++i) {
  155. int result = xSemaphoreTake(done, 180 / portTICK_PERIOD_MS);
  156. TEST_ASSERT_TRUE(result == pdPASS);
  157. }
  158. TEST_ASSERT_TRUE(args1.pass);
  159. TEST_ASSERT_TRUE(args2.pass);
  160. TEST_ASSERT_TRUE(args3.pass);
  161. #undef N
  162. }