test_esp_timer_light_sleep.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <sys/param.h>
  11. #include "esp_timer.h"
  12. #include "unity.h"
  13. #include "esp_rom_sys.h"
  14. #include "esp_sleep.h"
  15. static void timer_cb1(void *arg)
  16. {
  17. ++*((int*) arg);
  18. }
  19. TEST_CASE("Test the periodic timer does not handle lost events during light sleep", "[esp_timer][timeout=20]")
  20. {
  21. int count_calls;
  22. const esp_timer_create_args_t timer_args = {
  23. .name = "timer_cb1",
  24. .arg = &count_calls,
  25. .callback = &timer_cb1,
  26. .skip_unhandled_events = true,
  27. };
  28. esp_timer_handle_t periodic_timer;
  29. esp_timer_create(&timer_args, &periodic_timer);
  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. }