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