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

59 lines
1.2 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
*/
#include <rthw.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <board.h>
/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
static float cortexm_cputime_getres(void)
2017-12-23 23:34:23 +08:00
{
float ret = 1000 * 1000 * 1000;
2021-03-08 18:19:04 +08:00
ret = ret / SystemCoreClock;
return ret;
2017-12-23 23:34:23 +08:00
}
static uint32_t cortexm_cputime_gettime(void)
{
return DWT->CYCCNT;
}
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
};
int cortexm_cputime_init(void)
{
/* 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);
}
return 0;
}
INIT_BOARD_EXPORT(cortexm_cputime_init);