ccomp_timer_impl_riscv.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <stdint.h>
  15. #include "freertos/portmacro.h"
  16. #include "esp_freertos_hooks.h"
  17. #include "soc/soc_caps.h"
  18. #include "hal/cpu_hal.h"
  19. #include "esp_rom_sys.h"
  20. #if CONFIG_IDF_TARGET_ESP32C3
  21. #include "esp32c3/clk.h"
  22. #elif CONFIG_IDF_TARGET_ESP32H2
  23. #include "esp32h2/clk.h"
  24. #endif
  25. typedef enum {
  26. PERF_TIMER_UNINIT = 0, // timer has not been initialized yet
  27. PERF_TIMER_IDLE, // timer has been initialized but is not tracking elapsed time
  28. PERF_TIMER_ACTIVE // timer is tracking elapsed time
  29. } ccomp_timer_state_t;
  30. typedef struct {
  31. uint32_t last_ccount; // last CCOUNT value, updated every os tick
  32. ccomp_timer_state_t state; // state of the timer
  33. int64_t ccount; // accumulated processors cycles during the time when timer is active
  34. } ccomp_timer_status_t;
  35. // Each core has its independent timer
  36. ccomp_timer_status_t s_status[SOC_CPU_CORES_NUM];
  37. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  38. static void IRAM_ATTR update_ccount(void)
  39. {
  40. if (s_status[cpu_hal_get_core_id()].state == PERF_TIMER_ACTIVE) {
  41. int64_t new_ccount = cpu_hal_get_cycle_count();
  42. if (new_ccount > s_status[cpu_hal_get_core_id()].last_ccount) {
  43. s_status[cpu_hal_get_core_id()].ccount += new_ccount - s_status[cpu_hal_get_core_id()].last_ccount;
  44. } else {
  45. // CCOUNT has wrapped around
  46. s_status[cpu_hal_get_core_id()].ccount += new_ccount + (UINT32_MAX - s_status[cpu_hal_get_core_id()].last_ccount);
  47. }
  48. s_status[cpu_hal_get_core_id()].last_ccount = new_ccount;
  49. }
  50. }
  51. esp_err_t ccomp_timer_impl_init(void)
  52. {
  53. s_status[cpu_hal_get_core_id()].state = PERF_TIMER_IDLE;
  54. return ESP_OK;
  55. }
  56. esp_err_t ccomp_timer_impl_deinit(void)
  57. {
  58. s_status[cpu_hal_get_core_id()].state = PERF_TIMER_UNINIT;
  59. return ESP_OK;
  60. }
  61. esp_err_t ccomp_timer_impl_start(void)
  62. {
  63. s_status[cpu_hal_get_core_id()].state = PERF_TIMER_ACTIVE;
  64. s_status[cpu_hal_get_core_id()].last_ccount = cpu_hal_get_cycle_count();
  65. // Update elapsed cycles every OS tick
  66. esp_register_freertos_tick_hook_for_cpu(update_ccount, cpu_hal_get_core_id());
  67. return ESP_OK;
  68. }
  69. esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
  70. {
  71. esp_deregister_freertos_tick_hook_for_cpu(update_ccount, cpu_hal_get_core_id());
  72. update_ccount();
  73. s_status[cpu_hal_get_core_id()].state = PERF_TIMER_IDLE;
  74. return ESP_OK;
  75. }
  76. int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
  77. {
  78. update_ccount();
  79. int64_t cycles = s_status[cpu_hal_get_core_id()].ccount;
  80. return (cycles * 1000000) / esp_clk_cpu_freq();
  81. }
  82. esp_err_t ccomp_timer_impl_reset(void)
  83. {
  84. s_status[cpu_hal_get_core_id()].ccount = 0;
  85. s_status[cpu_hal_get_core_id()].last_ccount = 0;
  86. return ESP_OK;
  87. }
  88. bool ccomp_timer_impl_is_init(void)
  89. {
  90. return s_status[cpu_hal_get_core_id()].state != PERF_TIMER_UNINIT;
  91. }
  92. bool IRAM_ATTR ccomp_timer_impl_is_active(void)
  93. {
  94. return s_status[cpu_hal_get_core_id()].state == PERF_TIMER_ACTIVE;
  95. }
  96. void IRAM_ATTR ccomp_timer_impl_lock(void)
  97. {
  98. portENTER_CRITICAL(&s_lock);
  99. }
  100. void IRAM_ATTR ccomp_timer_impl_unlock(void)
  101. {
  102. portEXIT_CRITICAL(&s_lock);
  103. }