2017-12-23 23:34:23 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2017-12-23 23:34:23 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-12-23 23:34:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2017-12-23 Bernard first version
|
2022-06-14 18:50:31 +08:00
|
|
|
* 2022-06-14 Meco Man suuport pref_counter
|
2017-12-23 23:34:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#include <board.h>
|
2022-06-14 18:50:31 +08:00
|
|
|
#ifdef PKG_USING_PERF_COUNTER
|
|
|
|
#include <perf_counter.h>
|
|
|
|
#endif
|
2017-12-23 23:34:23 +08:00
|
|
|
|
|
|
|
/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
|
2023-04-13 16:00:19 +08:00
|
|
|
static uint64_t cortexm_cputime_getres(void)
|
2017-12-23 23:34:23 +08:00
|
|
|
{
|
2023-04-13 16:00:19 +08:00
|
|
|
uint64_t ret = 1000UL * 1000 * 1000;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2023-04-13 16:00:19 +08:00
|
|
|
ret = (ret * (1000UL * 1000)) / SystemCoreClock;
|
2017-12-31 14:44:04 +08:00
|
|
|
return ret;
|
2017-12-23 23:34:23 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 18:49:30 +08:00
|
|
|
static uint64_t cortexm_cputime_gettime(void)
|
2017-12-23 23:34:23 +08:00
|
|
|
{
|
2022-06-14 18:50:31 +08:00
|
|
|
#ifdef PKG_USING_PERF_COUNTER
|
|
|
|
return get_system_ticks();
|
|
|
|
#else
|
2017-12-23 23:34:23 +08:00
|
|
|
return DWT->CYCCNT;
|
2022-06-14 18:50:31 +08:00
|
|
|
#endif
|
2017-12-23 23:34:23 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
const static struct rt_clock_cputime_ops _cortexm_ops =
|
2017-12-23 23:34:23 +08:00
|
|
|
{
|
|
|
|
cortexm_cputime_getres,
|
|
|
|
cortexm_cputime_gettime
|
|
|
|
};
|
|
|
|
|
2022-06-14 18:50:31 +08:00
|
|
|
|
2017-12-23 23:34:23 +08:00
|
|
|
int cortexm_cputime_init(void)
|
|
|
|
{
|
2022-06-14 18:50:31 +08:00
|
|
|
#ifdef PKG_USING_PERF_COUNTER
|
|
|
|
clock_cpu_setops(&_cortexm_ops);
|
|
|
|
#else
|
2017-12-23 23:34:23 +08:00
|
|
|
/* check support bit */
|
2021-03-08 18:19:04 +08:00
|
|
|
if ((DWT->CTRL & (1UL << DWT_CTRL_NOCYCCNT_Pos)) == 0)
|
2017-12-23 23:34:23 +08:00
|
|
|
{
|
2018-06-04 14:54:07 +08:00
|
|
|
/* enable trace*/
|
|
|
|
CoreDebug->DEMCR |= (1UL << CoreDebug_DEMCR_TRCENA_Pos);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2017-12-23 23:34:23 +08:00
|
|
|
/* whether cycle counter not enabled */
|
2021-03-08 18:19:04 +08:00
|
|
|
if ((DWT->CTRL & (1UL << DWT_CTRL_CYCCNTENA_Pos)) == 0)
|
2017-12-23 23:34:23 +08:00
|
|
|
{
|
|
|
|
/* enable cycle counter */
|
|
|
|
DWT->CTRL |= (1UL << DWT_CTRL_CYCCNTENA_Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_cpu_setops(&_cortexm_ops);
|
|
|
|
}
|
2022-06-14 18:50:31 +08:00
|
|
|
#endif /* PKG_USING_PERF_COUNTER */
|
2017-12-23 23:34:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(cortexm_cputime_init);
|