1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <sys/time.h>
5 #include <sys/param.h>
6 #include "esp_timer.h"
7 #include "unity.h"
8 #include "esp_rom_sys.h"
9 #include "esp_sleep.h"
10
11
timer_cb1(void * arg)12 static void timer_cb1(void *arg)
13 {
14 ++*((int*) arg);
15 }
16
17 TEST_CASE("Test the periodic timer does not handle lost events during light sleep", "[esp_timer][timeout=20]")
18 {
19
20 int count_calls;
21 const esp_timer_create_args_t timer_args = {
22 .name = "timer_cb1",
23 .arg = &count_calls,
24 .callback = &timer_cb1,
25 .skip_unhandled_events = true,
26 };
27 esp_timer_handle_t periodic_timer;
28 esp_timer_create(&timer_args, &periodic_timer);
29
30 int period_cb_ms = 10;
31 int interval_ms = 50;
32 TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, period_cb_ms * 1000));
33 TEST_ESP_OK(esp_sleep_enable_timer_wakeup(interval_ms * 1000));
34 printf("Run light sleep\n");
35 printf("count_calls should be around = %d\n", interval_ms / period_cb_ms);
36 for (int i = 0; i < 3; i++) {
37 count_calls = 0;
38 TEST_ESP_OK(esp_light_sleep_start());
39 esp_rom_delay_us(interval_ms * 1000);
40 printf("count_calls = %d\n", count_calls);
41 TEST_ASSERT_INT_WITHIN(2, interval_ms / period_cb_ms, count_calls);
42 TEST_ESP_OK(esp_timer_dump(stdout));
43 }
44 TEST_ESP_OK(esp_timer_stop(periodic_timer));
45 // times_skipped is about 12 (4 from each sleep time).
46 TEST_ESP_OK(esp_timer_dump(stdout));
47 TEST_ESP_OK(esp_timer_delete(periodic_timer));
48 }
49