rt-thread-official/components/drivers/cputime/cputime_cortexm.c

70 lines
1.5 KiB
C
Raw Normal View History

2017-12-23 23:34:23 +08:00
/*
2021-03-08 18:19:04 +08:00
* Copyright (c) 2006-2021, 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-01-09 15:21:38 +08:00
static double cortexm_cputime_getres(void)
2017-12-23 23:34:23 +08:00
{
2023-01-09 15:21:38 +08:00
double ret = 1000UL * 1000 * 1000;
2021-03-08 18:19:04 +08:00
ret = ret / SystemCoreClock;
return ret;
2017-12-23 23:34:23 +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
{
/* 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);