ccomp_timer.c 2.2 KB

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