test_esp_timer_light_sleep.c 1.7 KB

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