ccomp_timer.c 2.1 KB

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