ccomp_timer.c 2.3 KB

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