2021-11-18 22:42:01 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-11-05 23:00:07 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "freertos/portmacro.h"
|
|
|
|
#include "esp_freertos_hooks.h"
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
#include "esp_rom_sys.h"
|
2022-07-21 07:24:42 -04:00
|
|
|
#include "esp_cpu.h"
|
2021-11-18 22:42:01 -05:00
|
|
|
#include "esp_private/esp_clk.h"
|
2020-11-05 23:00:07 -05:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PERF_TIMER_UNINIT = 0, // timer has not been initialized yet
|
|
|
|
PERF_TIMER_IDLE, // timer has been initialized but is not tracking elapsed time
|
|
|
|
PERF_TIMER_ACTIVE // timer is tracking elapsed time
|
|
|
|
} ccomp_timer_state_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t last_ccount; // last CCOUNT value, updated every os tick
|
|
|
|
ccomp_timer_state_t state; // state of the timer
|
|
|
|
int64_t ccount; // accumulated processors cycles during the time when timer is active
|
|
|
|
} ccomp_timer_status_t;
|
|
|
|
|
|
|
|
// Each core has its independent timer
|
|
|
|
ccomp_timer_status_t s_status[SOC_CPU_CORES_NUM];
|
|
|
|
|
|
|
|
static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
|
|
|
static void IRAM_ATTR update_ccount(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
if (s_status[esp_cpu_get_core_id()].state == PERF_TIMER_ACTIVE) {
|
|
|
|
int64_t new_ccount = esp_cpu_get_cycle_count();
|
|
|
|
if (new_ccount > s_status[esp_cpu_get_core_id()].last_ccount) {
|
|
|
|
s_status[esp_cpu_get_core_id()].ccount += new_ccount - s_status[esp_cpu_get_core_id()].last_ccount;
|
2020-11-05 23:00:07 -05:00
|
|
|
} else {
|
|
|
|
// CCOUNT has wrapped around
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].ccount += new_ccount + (UINT32_MAX - s_status[esp_cpu_get_core_id()].last_ccount);
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].last_ccount = new_ccount;
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ccomp_timer_impl_init(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].state = PERF_TIMER_IDLE;
|
2020-11-05 23:00:07 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ccomp_timer_impl_deinit(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].state = PERF_TIMER_UNINIT;
|
2020-11-05 23:00:07 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ccomp_timer_impl_start(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].state = PERF_TIMER_ACTIVE;
|
|
|
|
s_status[esp_cpu_get_core_id()].last_ccount = esp_cpu_get_cycle_count();
|
2020-11-05 23:00:07 -05:00
|
|
|
// Update elapsed cycles every OS tick
|
2022-07-21 07:24:42 -04:00
|
|
|
esp_register_freertos_tick_hook_for_cpu(update_ccount, esp_cpu_get_core_id());
|
2020-11-05 23:00:07 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR ccomp_timer_impl_stop(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
esp_deregister_freertos_tick_hook_for_cpu(update_ccount, esp_cpu_get_core_id());
|
2020-11-05 23:00:07 -05:00
|
|
|
update_ccount();
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].state = PERF_TIMER_IDLE;
|
2020-11-05 23:00:07 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t IRAM_ATTR ccomp_timer_impl_get_time(void)
|
|
|
|
{
|
|
|
|
update_ccount();
|
2022-07-21 07:24:42 -04:00
|
|
|
int64_t cycles = s_status[esp_cpu_get_core_id()].ccount;
|
2020-11-05 23:00:07 -05:00
|
|
|
return (cycles * 1000000) / esp_clk_cpu_freq();
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ccomp_timer_impl_reset(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
s_status[esp_cpu_get_core_id()].ccount = 0;
|
|
|
|
s_status[esp_cpu_get_core_id()].last_ccount = 0;
|
2020-11-05 23:00:07 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ccomp_timer_impl_is_init(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
return s_status[esp_cpu_get_core_id()].state != PERF_TIMER_UNINIT;
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IRAM_ATTR ccomp_timer_impl_is_active(void)
|
|
|
|
{
|
2022-07-21 07:24:42 -04:00
|
|
|
return s_status[esp_cpu_get_core_id()].state == PERF_TIMER_ACTIVE;
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR ccomp_timer_impl_lock(void)
|
|
|
|
{
|
|
|
|
portENTER_CRITICAL(&s_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR ccomp_timer_impl_unlock(void)
|
|
|
|
{
|
|
|
|
portEXIT_CRITICAL(&s_lock);
|
|
|
|
}
|