test_esp_timer_light_sleep.c 1.5 KB

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