import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
18
components/drivers/cputime/SConscript
Normal file
18
components/drivers/cputime/SConscript
Normal file
|
@ -0,0 +1,18 @@
|
|||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
CPPPATH = [cwd + '/../include']
|
||||
src = Split('''
|
||||
cputime.c
|
||||
cputimer.c
|
||||
''')
|
||||
|
||||
if GetDepend('RT_USING_CPUTIME_CORTEXM'):
|
||||
src += ['cputime_cortexm.c']
|
||||
|
||||
if GetDepend('RT_USING_CPUTIME_RISCV'):
|
||||
src += ['cputime_riscv.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_CPUTIME'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
116
components/drivers/cputime/cputime.c
Normal file
116
components/drivers/cputime/cputime.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, 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(x (1000UL * 1000))
|
||||
*/
|
||||
uint64_t 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_settimeout() fucntion set timeout time and timeout callback function
|
||||
* The timeout callback function will be called when the timeout time is reached
|
||||
*
|
||||
* @param tick the Timeout tick
|
||||
* @param timeout the Timeout function
|
||||
* @param parameter the Parameters of timeout function
|
||||
*
|
||||
*/
|
||||
int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param)
|
||||
{
|
||||
if (_cputime_ops)
|
||||
return _cputime_ops->cputime_settimeout(tick, timeout, param);
|
||||
|
||||
rt_set_errno(ENOSYS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int clock_cpu_issettimeout(void)
|
||||
{
|
||||
if (_cputime_ops)
|
||||
return _cputime_ops->cputime_settimeout != RT_NULL;
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_microsecond() fucntion shall return the microsecond according to
|
||||
* cpu_tick parameter.
|
||||
*
|
||||
* @param cpu_tick the cpu tick
|
||||
*
|
||||
* @return the microsecond
|
||||
*/
|
||||
uint64_t clock_cpu_microsecond(uint64_t cpu_tick)
|
||||
{
|
||||
uint64_t unit = clock_cpu_getres();
|
||||
|
||||
return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* The clock_cpu_microsecond() fucntion shall return the millisecond according to
|
||||
* cpu_tick parameter.
|
||||
*
|
||||
* @param cpu_tick the cpu tick
|
||||
*
|
||||
* @return the millisecond
|
||||
*/
|
||||
uint64_t clock_cpu_millisecond(uint64_t cpu_tick)
|
||||
{
|
||||
uint64_t unit = clock_cpu_getres();
|
||||
|
||||
return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / (1000UL * 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
components/drivers/cputime/cputime_cortexm.c
Normal file
69
components/drivers/cputime/cputime_cortexm.c
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, 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 uint64_t cortexm_cputime_getres(void)
|
||||
{
|
||||
uint64_t ret = 1000UL * 1000 * 1000;
|
||||
|
||||
ret = (ret * (1000UL * 1000)) / 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);
|
37
components/drivers/cputime/cputime_riscv.c
Normal file
37
components/drivers/cputime/cputime_riscv.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
#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 uint64_t riscv_cputime_getres(void)
|
||||
{
|
||||
uint64_t ret = 1000UL * 1000 * 1000;
|
||||
|
||||
ret = (ret * (1000UL * 1000)) / CPUTIME_TIMER_FREQ;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint64_t riscv_cputime_gettime(void)
|
||||
{
|
||||
uint64_t time_elapsed;
|
||||
__asm__ __volatile__(
|
||||
"rdtime %0"
|
||||
: "=r"(time_elapsed));
|
||||
return time_elapsed;
|
||||
}
|
||||
|
||||
const static struct rt_clock_cputime_ops _riscv_ops =
|
||||
{
|
||||
riscv_cputime_getres,
|
||||
riscv_cputime_gettime
|
||||
};
|
||||
|
||||
int riscv_cputime_init(void)
|
||||
{
|
||||
clock_cpu_setops(&_riscv_ops);
|
||||
return 0;
|
||||
}
|
||||
INIT_BOARD_EXPORT(riscv_cputime_init);
|
339
components/drivers/cputime/cputimer.c
Normal file
339
components/drivers/cputime/cputimer.c
Normal file
|
@ -0,0 +1,339 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-13 zhkag first version
|
||||
* 2023-04-03 xqyjlj fix cputimer in multithreading
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
static rt_list_t _cputimer_list = RT_LIST_OBJECT_INIT(_cputimer_list);
|
||||
static struct rt_cputimer *_cputimer_nowtimer = RT_NULL;
|
||||
|
||||
static void _cputime_sleep_timeout(void *parameter)
|
||||
{
|
||||
struct rt_semaphore *sem;
|
||||
sem = (struct rt_semaphore *)parameter;
|
||||
rt_sem_release(sem);
|
||||
}
|
||||
|
||||
static void _cputime_timeout_callback(void *parameter)
|
||||
{
|
||||
struct rt_cputimer *timer;
|
||||
timer = (struct rt_cputimer *)parameter;
|
||||
rt_base_t level;
|
||||
level = rt_hw_interrupt_disable();
|
||||
_cputimer_nowtimer = RT_NULL;
|
||||
rt_list_remove(&(timer->row));
|
||||
rt_hw_interrupt_enable(level);
|
||||
timer->timeout_func(timer->parameter);
|
||||
|
||||
if (&_cputimer_list != _cputimer_list.prev)
|
||||
{
|
||||
struct rt_cputimer *t;
|
||||
t = rt_list_entry(_cputimer_list.next, struct rt_cputimer, row);
|
||||
clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t);
|
||||
}
|
||||
else
|
||||
{
|
||||
clock_cpu_settimeout(RT_NULL, RT_NULL, RT_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void _set_next_timeout()
|
||||
{
|
||||
struct rt_cputimer *t;
|
||||
|
||||
if (&_cputimer_list != _cputimer_list.prev)
|
||||
{
|
||||
t = rt_list_entry((&_cputimer_list)->next, struct rt_cputimer, row);
|
||||
if (_cputimer_nowtimer != RT_NULL)
|
||||
{
|
||||
if (t != _cputimer_nowtimer && t->timeout_tick < _cputimer_nowtimer->timeout_tick)
|
||||
{
|
||||
_cputimer_nowtimer = t;
|
||||
clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_cputimer_nowtimer = t;
|
||||
clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_cputimer_nowtimer = RT_NULL;
|
||||
clock_cpu_settimeout(RT_NULL, RT_NULL, RT_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void rt_cputimer_init(rt_cputimer_t timer,
|
||||
const char *name,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_uint64_t tick,
|
||||
rt_uint8_t flag)
|
||||
{
|
||||
/* parameter check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(timeout != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
/* set flag */
|
||||
timer->parent.flag = flag;
|
||||
|
||||
/* set deactivated */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
timer->timeout_func = timeout;
|
||||
timer->parameter = parameter;
|
||||
timer->timeout_tick = tick + clock_cpu_gettime();
|
||||
timer->init_tick = tick;
|
||||
|
||||
rt_list_init(&(timer->row));
|
||||
rt_sem_init(&(timer->sem), "cputime", 0, RT_IPC_FLAG_PRIO);
|
||||
}
|
||||
|
||||
rt_err_t rt_cputimer_delete(rt_cputimer_t timer)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rt_list_remove(&timer->row);
|
||||
/* stop timer */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
_set_next_timeout();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputimer_start(rt_cputimer_t timer)
|
||||
{
|
||||
rt_list_t *timer_list;
|
||||
rt_base_t level;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
/* stop timer firstly */
|
||||
level = rt_hw_interrupt_disable();
|
||||
/* remove timer from list */
|
||||
|
||||
rt_list_remove(&timer->row);
|
||||
/* change status of timer */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
timer_list = &_cputimer_list;
|
||||
|
||||
for (; timer_list != _cputimer_list.prev;
|
||||
timer_list = timer_list->next)
|
||||
{
|
||||
struct rt_cputimer *t;
|
||||
rt_list_t *p = timer_list->next;
|
||||
|
||||
t = rt_list_entry(p, struct rt_cputimer, row);
|
||||
|
||||
if ((t->timeout_tick - timer->timeout_tick) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if ((t->timeout_tick - timer->timeout_tick) < 0x7fffffffffffffff)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rt_list_insert_after(timer_list, &(timer->row));
|
||||
|
||||
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
_set_next_timeout();
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputimer_stop(rt_cputimer_t timer)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_list_remove(&timer->row);
|
||||
/* change status */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
_set_next_timeout();
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputimer_control(rt_cputimer_t timer, int cmd, void *arg)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_TIMER_CTRL_GET_TIME:
|
||||
*(rt_uint64_t *)arg = timer->init_tick;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_TIME:
|
||||
RT_ASSERT((*(rt_uint64_t *)arg) < 0x7fffffffffffffff);
|
||||
timer->init_tick = *(rt_uint64_t *)arg;
|
||||
timer->timeout_tick = *(rt_uint64_t *)arg + clock_cpu_gettime();
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_ONESHOT:
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_PERIODIC:
|
||||
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_GET_STATE:
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
|
||||
{
|
||||
/*timer is start and run*/
|
||||
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*timer is stop*/
|
||||
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
|
||||
}
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_GET_REMAIN_TIME:
|
||||
*(rt_uint64_t *)arg = timer->timeout_tick;
|
||||
break;
|
||||
case RT_TIMER_CTRL_GET_FUNC:
|
||||
arg = (void *)timer->timeout_func;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_FUNC:
|
||||
timer->timeout_func = (void (*)(void *))arg;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_GET_PARM:
|
||||
*(void **)arg = timer->parameter;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_PARM:
|
||||
timer->parameter = arg;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputimer_detach(rt_cputimer_t timer)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE);
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rt_list_remove(&timer->row);
|
||||
/* stop timer */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
_set_next_timeout();
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_sem_detach(&(timer->sem));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputime_sleep(rt_uint64_t tick)
|
||||
{
|
||||
rt_base_t level;
|
||||
struct rt_cputimer cputimer;
|
||||
|
||||
if (!clock_cpu_issettimeout())
|
||||
{
|
||||
rt_int32_t ms = clock_cpu_millisecond(tick);
|
||||
return rt_thread_delay(rt_tick_from_millisecond(ms));
|
||||
}
|
||||
|
||||
if (tick == 0)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_cputimer_init(&cputimer, "cputime_sleep", _cputime_sleep_timeout, &(cputimer.sem), tick,
|
||||
RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rt_cputimer_start(&cputimer); /* reset the timeout of thread timer and start it */
|
||||
rt_hw_interrupt_enable(level);
|
||||
rt_sem_take_interruptible(&(cputimer.sem), RT_WAITING_FOREVER);
|
||||
|
||||
rt_cputimer_detach(&cputimer);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_cputime_ndelay(rt_uint64_t ns)
|
||||
{
|
||||
uint64_t unit = clock_cpu_getres();
|
||||
return rt_cputime_sleep(ns * (1000UL * 1000) / unit);
|
||||
}
|
||||
|
||||
rt_err_t rt_cputime_udelay(rt_uint64_t us)
|
||||
{
|
||||
return rt_cputime_ndelay(us * 1000);
|
||||
}
|
||||
|
||||
rt_err_t rt_cputime_mdelay(rt_uint64_t ms)
|
||||
{
|
||||
return rt_cputime_ndelay(ms * 1000000);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue