ccomp_timer_impl_riscv.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include "freertos/portmacro.h"
  8. #include "esp_freertos_hooks.h"
  9. #include "soc/soc_caps.h"
  10. #include "esp_rom_sys.h"
  11. #include "esp_cpu.h"
  12. #include "esp_private/esp_clk.h"
  13. typedef enum {
  14. PERF_TIMER_UNINIT = 0, // timer has not been initialized yet
  15. PERF_TIMER_IDLE, // timer has been initialized but is not tracking elapsed time
  16. PERF_TIMER_ACTIVE // timer is tracking elapsed time
  17. } ccomp_timer_state_t;
  18. typedef struct {
  19. uint32_t last_ccount; // last CCOUNT value, updated every os tick
  20. ccomp_timer_state_t state; // state of the timer
  21. int64_t ccount; // accumulated processors cycles during the time when timer is active
  22. } ccomp_timer_status_t;
  23. // Each core has its independent timer
  24. ccomp_timer_status_t s_status[SOC_CPU_CORES_NUM];
  25. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  26. static void IRAM_ATTR update_ccount(void)
  27. {
  28. if (s_status[esp_cpu_get_core_id()].state == PERF_TIMER_ACTIVE) {
  29. int64_t new_ccount = esp_cpu_get_cycle_count();
  30. if (new_ccount > s_status[esp_cpu_get_core_id()].last_ccount) {
  31. s_status[esp_cpu_get_core_id()].ccount += new_ccount - s_status[esp_cpu_get_core_id()].last_ccount;
  32. } else {
  33. // CCOUNT has wrapped around
  34. s_status[esp_cpu_get_core_id()].ccount += new_ccount + (UINT32_MAX - s_status[esp_cpu_get_core_id()].last_ccount);
  35. }
  36. s_status[esp_cpu_get_core_id()].last_ccount = new_ccount;
  37. }
  38. }
  39. esp_err_t ccomp_timer_impl_init(void)
  40. {
  41. s_status[esp_cpu_get_core_id()].state = PERF_TIMER_IDLE;
  42. return ESP_OK;
  43. }
  44. esp_err_t ccomp_timer_impl_deinit(void)
  45. {
  46. s_status[esp_cpu_get_core_id()].state = PERF_TIMER_UNINIT;
  47. return ESP_OK;
  48. }
  49. esp_err_t ccomp_timer_impl_start(void)
  50. {
  51. s_status[esp_cpu_get_core_id()].state = PERF_TIMER_ACTIVE;
  52. s_status[esp_cpu_get_core_id()].last_ccount = esp_cpu_get_cycle_count();
  53. // Update elapsed cycles every OS tick
  54. esp_register_freertos_tick_hook_for_cpu(update_ccount, esp_cpu_get_core_id());
  55. return ESP_OK;
  56. }
  57. esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
  58. {
  59. esp_deregister_freertos_tick_hook_for_cpu(update_ccount, esp_cpu_get_core_id());
  60. update_ccount();
  61. s_status[esp_cpu_get_core_id()].state = PERF_TIMER_IDLE;
  62. return ESP_OK;
  63. }
  64. int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
  65. {
  66. update_ccount();
  67. int64_t cycles = s_status[esp_cpu_get_core_id()].ccount;
  68. return (cycles * 1000000) / esp_clk_cpu_freq();
  69. }
  70. esp_err_t ccomp_timer_impl_reset(void)
  71. {
  72. s_status[esp_cpu_get_core_id()].ccount = 0;
  73. s_status[esp_cpu_get_core_id()].last_ccount = 0;
  74. return ESP_OK;
  75. }
  76. bool ccomp_timer_impl_is_init(void)
  77. {
  78. return s_status[esp_cpu_get_core_id()].state != PERF_TIMER_UNINIT;
  79. }
  80. bool IRAM_ATTR ccomp_timer_impl_is_active(void)
  81. {
  82. return s_status[esp_cpu_get_core_id()].state == PERF_TIMER_ACTIVE;
  83. }
  84. void IRAM_ATTR ccomp_timer_impl_lock(void)
  85. {
  86. portENTER_CRITICAL(&s_lock);
  87. }
  88. void IRAM_ATTR ccomp_timer_impl_unlock(void)
  89. {
  90. portEXIT_CRITICAL(&s_lock);
  91. }