ccomp_timer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 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. #include "ccomp_timer.h"
  15. #include "ccomp_timer_impl.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/semphr.h"
  19. #include "esp_log.h"
  20. #include "esp_intr_alloc.h"
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "esp32/clk.h"
  23. #elif CONFIG_IDF_TARGET_ESP32S2
  24. #include "esp32s2/clk.h"
  25. #endif
  26. static const char TAG[] = "ccomp_timer";
  27. esp_err_t ccomp_timer_start(void)
  28. {
  29. esp_err_t err = ESP_OK;
  30. ccomp_timer_impl_lock();
  31. if (ccomp_timer_impl_is_init()) {
  32. if (ccomp_timer_impl_is_active()) {
  33. err = ESP_ERR_INVALID_STATE;
  34. }
  35. }
  36. else {
  37. err = ccomp_timer_impl_init();
  38. }
  39. ccomp_timer_impl_unlock();
  40. if (err != ESP_OK) {
  41. goto fail;
  42. }
  43. err = ccomp_timer_impl_reset();
  44. if (err != ESP_OK) {
  45. goto fail;
  46. }
  47. err = ccomp_timer_impl_start();
  48. if (err == ESP_OK) {
  49. return ESP_OK;
  50. }
  51. fail:
  52. ESP_LOGE(TAG, "Unable to start performance timer");
  53. return err;
  54. }
  55. int64_t IRAM_ATTR ccomp_timer_stop(void)
  56. {
  57. esp_err_t err = ESP_OK;
  58. ccomp_timer_impl_lock();
  59. if (!ccomp_timer_impl_is_active()) {
  60. err = ESP_ERR_INVALID_STATE;
  61. }
  62. ccomp_timer_impl_unlock();
  63. if (err != ESP_OK) {
  64. goto fail;
  65. }
  66. err = ccomp_timer_impl_stop();
  67. if (err != ESP_OK) {
  68. goto fail;
  69. }
  70. int64_t t = ccomp_timer_get_time();
  71. err = ccomp_timer_impl_deinit();
  72. if (err == ESP_OK && t != -1) {
  73. return t;
  74. }
  75. fail:
  76. ESP_LOGE(TAG, "Unable to stop performance timer");
  77. return -1;
  78. }
  79. int64_t IRAM_ATTR ccomp_timer_get_time(void)
  80. {
  81. return ccomp_timer_impl_get_time();
  82. }