test_esp_timer_light_sleep.c 1.6 KB

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