test_esp_timer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifdef __cpp_exceptions
  15. #include "unity.h"
  16. #include "unity_cxx.hpp"
  17. #include <limits>
  18. #include <stdio.h>
  19. #include <iostream>
  20. #include "test_utils.h" // ref clock
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "freertos/semphr.h"
  24. #include "esp_timer_cxx.hpp"
  25. #include "esp_exception.hpp"
  26. using namespace std;
  27. using namespace idf;
  28. using namespace idf::esp_timer;
  29. struct RefClock {
  30. RefClock()
  31. {
  32. ref_clock_init();
  33. };
  34. ~RefClock()
  35. {
  36. ref_clock_deinit();
  37. }
  38. };
  39. TEST_CASE("ESPTimer produces correct delay", "[ESPTimer]")
  40. {
  41. int64_t t_end;
  42. RefClock ref_clock;
  43. function<void()> timer_cb = [&t_end]() {
  44. t_end = ref_clock_get();
  45. };
  46. ESPTimer timer(timer_cb, "timer1");
  47. const int delays_ms[] = {20, 100, 200, 250};
  48. const size_t delays_count = sizeof(delays_ms)/sizeof(delays_ms[0]);
  49. for (size_t i = 0; i < delays_count; ++i) {
  50. t_end = 0;
  51. int64_t t_start = ref_clock_get();
  52. timer.start(chrono::microseconds(delays_ms[i] * 1000));
  53. vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
  54. TEST_ASSERT(t_end != 0);
  55. int32_t ms_diff = (t_end - t_start) / 1000;
  56. printf("%d %d\n", delays_ms[i], ms_diff);
  57. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
  58. }
  59. }
  60. TEST_CASE("ESPtimer produces correct periodic delays", "[ESPTimer]")
  61. {
  62. const size_t NUM_INTERVALS = 3u;
  63. size_t cur_interval = 0;
  64. int intervals[NUM_INTERVALS];
  65. int64_t t_start;
  66. SemaphoreHandle_t done;
  67. const int DELAY_MS = 100;
  68. function<void()> timer_cb = [&]() {
  69. int64_t t_end = ref_clock_get();
  70. int32_t ms_diff = (t_end - t_start) / 1000;
  71. printf("timer #%d %dms\n", cur_interval, ms_diff);
  72. if (cur_interval < NUM_INTERVALS) {
  73. intervals[cur_interval++] = ms_diff;
  74. }
  75. // Deliberately make timer handler run longer.
  76. // We check that this doesn't affect the result.
  77. esp_rom_delay_us(10*1000);
  78. if (cur_interval == NUM_INTERVALS) {
  79. printf("done\n");
  80. xSemaphoreGive(done);
  81. }
  82. };
  83. ESPTimer timer(timer_cb, "timer1");
  84. RefClock ref_clock;
  85. t_start = ref_clock_get();
  86. done = xSemaphoreCreateBinary();
  87. timer.start_periodic(chrono::microseconds(DELAY_MS * 1000));
  88. TEST_ASSERT(xSemaphoreTake(done, DELAY_MS * NUM_INTERVALS * 2));
  89. timer.stop();
  90. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, cur_interval);
  91. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  92. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * DELAY_MS, intervals[i]);
  93. }
  94. TEST_ESP_OK(esp_timer_dump(stdout));
  95. vSemaphoreDelete(done);
  96. #undef NUM_INTERVALS
  97. }
  98. #endif // __cpp_exceptions