14
rt-thread/components/drivers/cputime/SConscript
Normal file
14
rt-thread/components/drivers/cputime/SConscript
Normal file
@@ -0,0 +1,14 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
CPPPATH = [cwd + '/../include']
|
||||
src = Split('''
|
||||
cputime.c
|
||||
''')
|
||||
|
||||
if GetDepend('RT_USING_CPUTIME_CORTEXM'):
|
||||
src += ['cputime_cortexm.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_CPUTIME'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
91
rt-thread/components/drivers/cputime/cputime.c
Normal file
91
rt-thread/components/drivers/cputime/cputime.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017-12-23 Bernard first version
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
#include <rtthread.h>
|
||||
#include <sys/errno.h>
|
||||
|
||||
static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL;
|
||||
|
||||
/**
|
||||
* The clock_cpu_getres() function shall return the resolution of CPU time, the
|
||||
* number of nanosecond per tick.
|
||||
*
|
||||
* @return the number of nanosecond per tick
|
||||
*/
|
||||
float clock_cpu_getres(void)
|
||||
{
|
||||
if (_cputime_ops)
|
||||
return _cputime_ops->cputime_getres();
|
||||
|
||||
rt_set_errno(ENOSYS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_gettime() function shall return the current value of cpu time tick.
|
||||
*
|
||||
* @return the cpu tick
|
||||
*/
|
||||
uint64_t clock_cpu_gettime(void)
|
||||
{
|
||||
if (_cputime_ops)
|
||||
return _cputime_ops->cputime_gettime();
|
||||
|
||||
rt_set_errno(ENOSYS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_microsecond() fucntion shall return the microsecond according to
|
||||
* cpu_tick parameter.
|
||||
*
|
||||
* @param cpu_tick the cpu tick
|
||||
*
|
||||
* @return the microsecond
|
||||
*/
|
||||
uint32_t clock_cpu_microsecond(uint32_t cpu_tick)
|
||||
{
|
||||
float unit = clock_cpu_getres();
|
||||
|
||||
return (uint32_t)((cpu_tick * unit) / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_microsecond() fucntion shall return the millisecond according to
|
||||
* cpu_tick parameter.
|
||||
*
|
||||
* @param cpu_tick the cpu tick
|
||||
*
|
||||
* @return the millisecond
|
||||
*/
|
||||
uint32_t clock_cpu_millisecond(uint32_t cpu_tick)
|
||||
{
|
||||
float unit = clock_cpu_getres();
|
||||
|
||||
return (uint32_t)((cpu_tick * unit) / (1000 * 1000));
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_seops() function shall set the ops of cpu time.
|
||||
*
|
||||
* @return always return 0.
|
||||
*/
|
||||
int clock_cpu_setops(const struct rt_clock_cputime_ops *ops)
|
||||
{
|
||||
_cputime_ops = ops;
|
||||
if (ops)
|
||||
{
|
||||
RT_ASSERT(ops->cputime_getres != RT_NULL);
|
||||
RT_ASSERT(ops->cputime_gettime != RT_NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
69
rt-thread/components/drivers/cputime/cputime_cortexm.c
Normal file
69
rt-thread/components/drivers/cputime/cputime_cortexm.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017-12-23 Bernard first version
|
||||
* 2022-06-14 Meco Man suuport pref_counter
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtdevice.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <board.h>
|
||||
#ifdef PKG_USING_PERF_COUNTER
|
||||
#include <perf_counter.h>
|
||||
#endif
|
||||
|
||||
/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
|
||||
static float cortexm_cputime_getres(void)
|
||||
{
|
||||
float ret = 1000 * 1000 * 1000;
|
||||
|
||||
ret = ret / SystemCoreClock;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint64_t cortexm_cputime_gettime(void)
|
||||
{
|
||||
#ifdef PKG_USING_PERF_COUNTER
|
||||
return get_system_ticks();
|
||||
#else
|
||||
return DWT->CYCCNT;
|
||||
#endif
|
||||
}
|
||||
|
||||
const static struct rt_clock_cputime_ops _cortexm_ops =
|
||||
{
|
||||
cortexm_cputime_getres,
|
||||
cortexm_cputime_gettime
|
||||
};
|
||||
|
||||
|
||||
int cortexm_cputime_init(void)
|
||||
{
|
||||
#ifdef PKG_USING_PERF_COUNTER
|
||||
clock_cpu_setops(&_cortexm_ops);
|
||||
#else
|
||||
/* check support bit */
|
||||
if ((DWT->CTRL & (1UL << DWT_CTRL_NOCYCCNT_Pos)) == 0)
|
||||
{
|
||||
/* enable trace*/
|
||||
CoreDebug->DEMCR |= (1UL << CoreDebug_DEMCR_TRCENA_Pos);
|
||||
|
||||
/* whether cycle counter not enabled */
|
||||
if ((DWT->CTRL & (1UL << DWT_CTRL_CYCCNTENA_Pos)) == 0)
|
||||
{
|
||||
/* enable cycle counter */
|
||||
DWT->CTRL |= (1UL << DWT_CTRL_CYCCNTENA_Pos);
|
||||
}
|
||||
|
||||
clock_cpu_setops(&_cortexm_ops);
|
||||
}
|
||||
#endif /* PKG_USING_PERF_COUNTER */
|
||||
return 0;
|
||||
}
|
||||
INIT_BOARD_EXPORT(cortexm_cputime_init);
|
Reference in New Issue
Block a user