mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-06 00:45:22 +08:00
Remove old hwtimer/ktime/cputime subsystems and fix formatting
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
This commit is contained in:
@@ -5,7 +5,6 @@ rsource "ipc/Kconfig"
|
|||||||
|
|
||||||
rsource "serial/Kconfig"
|
rsource "serial/Kconfig"
|
||||||
rsource "can/Kconfig"
|
rsource "can/Kconfig"
|
||||||
rsource "cputime/Kconfig"
|
|
||||||
rsource "i2c/Kconfig"
|
rsource "i2c/Kconfig"
|
||||||
rsource "phy/Kconfig"
|
rsource "phy/Kconfig"
|
||||||
rsource "misc/Kconfig"
|
rsource "misc/Kconfig"
|
||||||
@@ -47,9 +46,7 @@ rsource "pic/Kconfig"
|
|||||||
rsource "pin/Kconfig"
|
rsource "pin/Kconfig"
|
||||||
rsource "pinctrl/Kconfig"
|
rsource "pinctrl/Kconfig"
|
||||||
rsource "clock_time/Kconfig"
|
rsource "clock_time/Kconfig"
|
||||||
rsource "ktime/Kconfig"
|
|
||||||
rsource "clk/Kconfig"
|
rsource "clk/Kconfig"
|
||||||
rsource "hwtimer/Kconfig"
|
|
||||||
rsource "usb/Kconfig"
|
rsource "usb/Kconfig"
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
|||||||
@@ -25,26 +25,4 @@ if RT_USING_CLOCK_TIME
|
|||||||
help
|
help
|
||||||
Enable system boottime (monotonic time since boot) APIs.
|
Enable system boottime (monotonic time since boot) APIs.
|
||||||
|
|
||||||
# Backward compatibility options
|
|
||||||
config RT_USING_HWTIMER
|
|
||||||
bool
|
|
||||||
default y
|
|
||||||
help
|
|
||||||
Legacy option for backward compatibility with hwtimer.
|
|
||||||
Automatically enabled when RT_USING_CLOCK_TIME is enabled.
|
|
||||||
|
|
||||||
config RT_USING_KTIME
|
|
||||||
bool
|
|
||||||
default y if RT_USING_CLOCK_HRTIMER
|
|
||||||
help
|
|
||||||
Legacy option for backward compatibility with ktime.
|
|
||||||
Automatically enabled when RT_USING_CLOCK_HRTIMER is enabled.
|
|
||||||
|
|
||||||
config RT_USING_CPUTIME
|
|
||||||
bool
|
|
||||||
default y if RT_USING_CLOCK_CPUTIME
|
|
||||||
help
|
|
||||||
Legacy option for backward compatibility with cputime.
|
|
||||||
Automatically enabled when RT_USING_CLOCK_CPUTIME is enabled.
|
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
config RT_USING_CPUTIME
|
|
||||||
bool "Enable CPU time for high resolution clock counter (DEPRECATED - use RT_USING_CLOCK_TIME)"
|
|
||||||
default n
|
|
||||||
depends on !RT_USING_CLOCK_TIME
|
|
||||||
help
|
|
||||||
DEPRECATED: This option is maintained for backward compatibility only.
|
|
||||||
New projects should use RT_USING_CLOCK_TIME instead.
|
|
||||||
|
|
||||||
When enable this option, the BSP should provide a rt_clock_cputime_ops
|
|
||||||
for CPU time by:
|
|
||||||
const static struct rt_clock_cputime_ops _ops = {...};
|
|
||||||
clock_cpu_setops(&_ops);
|
|
||||||
|
|
||||||
Then user can use high resolution clock counter with:
|
|
||||||
|
|
||||||
ts1 = clock_cpu_gettime();
|
|
||||||
ts2 = clock_cpu_gettime();
|
|
||||||
|
|
||||||
/* and get the ms of delta tick with API: */
|
|
||||||
ms_tick = clock_cpu_millisecond(t2 - t1);
|
|
||||||
us_tick = clock_cpu_microsecond(t2 - t1);
|
|
||||||
|
|
||||||
if RT_USING_CPUTIME
|
|
||||||
config RT_USING_CPUTIME_CORTEXM
|
|
||||||
bool "Support Cortex-M CPU"
|
|
||||||
default y
|
|
||||||
depends on ARCH_ARM_CORTEX_M0 || ARCH_ARM_CORTEX_M3 || ARCH_ARM_CORTEX_M4 || ARCH_ARM_CORTEX_M7
|
|
||||||
select PKG_USING_PERF_COUNTER
|
|
||||||
config RT_USING_CPUTIME_RISCV
|
|
||||||
bool "Use rdtime instructions for CPU time"
|
|
||||||
default y
|
|
||||||
depends on ARCH_RISCV64
|
|
||||||
help
|
|
||||||
Some RISCV64 MCU Use rdtime instructions read CPU time.
|
|
||||||
config CPUTIME_TIMER_FREQ
|
|
||||||
int "CPUTIME timer freq"
|
|
||||||
default 0
|
|
||||||
endif
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
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')
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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);
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#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);
|
|
||||||
@@ -1,339 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
menuconfig RT_USING_HWTIMER
|
|
||||||
bool "Using Hardware Timer device drivers (DEPRECATED - use RT_USING_CLOCK_TIME)"
|
|
||||||
default n
|
|
||||||
depends on !RT_USING_CLOCK_TIME
|
|
||||||
help
|
|
||||||
DEPRECATED: This option is maintained for backward compatibility only.
|
|
||||||
New projects should use RT_USING_CLOCK_TIME instead.
|
|
||||||
|
|
||||||
config RT_HWTIMER_ARM_ARCH
|
|
||||||
bool "ARM ARCH Timer"
|
|
||||||
depends on RT_USING_DM
|
|
||||||
depends on RT_USING_HWTIMER
|
|
||||||
depends on ARCH_ARM_CORTEX_A || ARCH_ARMV8
|
|
||||||
default n
|
|
||||||
|
|
||||||
if RT_USING_DM && RT_USING_HWTIMER
|
|
||||||
osource "$(SOC_DM_HWTIMER_DIR)/Kconfig"
|
|
||||||
endif
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
from building import *
|
|
||||||
|
|
||||||
group = []
|
|
||||||
|
|
||||||
if not GetDepend(['RT_USING_HWTIMER']):
|
|
||||||
Return('group')
|
|
||||||
|
|
||||||
cwd = GetCurrentDir()
|
|
||||||
CPPPATH = [cwd + '/../include']
|
|
||||||
|
|
||||||
src = ['hwtimer.c']
|
|
||||||
|
|
||||||
if GetDepend(['RT_HWTIMER_ARM_ARCH']):
|
|
||||||
src += ['hwtimer-arm_arch.c']
|
|
||||||
|
|
||||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
|
||||||
|
|
||||||
Return('group')
|
|
||||||
@@ -1,387 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2021-12-20 GuEe-GUI first version
|
|
||||||
* 2022-08-24 GuEe-GUI Add OFW support
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <rthw.h>
|
|
||||||
#include <rtthread.h>
|
|
||||||
#include <rtdevice.h>
|
|
||||||
|
|
||||||
/* support registers access and timer registers in libcpu */
|
|
||||||
#include <cpu.h>
|
|
||||||
#include <cpuport.h>
|
|
||||||
|
|
||||||
typedef void (*timer_ctrl_handle)(rt_bool_t enable);
|
|
||||||
typedef rt_uint64_t (*timer_value_handle)(rt_uint64_t val);
|
|
||||||
|
|
||||||
static volatile rt_uint64_t timer_step;
|
|
||||||
|
|
||||||
static int arm_arch_timer_irq = -1;
|
|
||||||
static timer_ctrl_handle arm_arch_timer_ctrl_handle = RT_NULL;
|
|
||||||
static timer_value_handle arm_arch_timer_value_handle = RT_NULL;
|
|
||||||
|
|
||||||
/* CTL */
|
|
||||||
static void mon_ptimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTPS_CTL, !!enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hyp_s_ptimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
rt_hw_sysreg_write(CNTHPS_CTL, !!enable);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hyp_ns_ptimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTHP_CTL, !!enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hyp_s_vtimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
rt_hw_sysreg_write(CNTHVS_CTL, !!enable);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hyp_ns_vtimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
rt_hw_sysreg_write(CNTHV_CTL, !!enable);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void os_ptimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTP_CTL, !!enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void os_vtimer_ctrl(rt_bool_t enable)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTV_CTL, !!enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TVAL */
|
|
||||||
static rt_uint64_t mon_ptimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTPS_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTPS_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t hyp_s_ptimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTHPS_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTHPS_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t hyp_ns_ptimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTHP_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTHP_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t hyp_s_vtimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTHVS_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTHVS_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t hyp_ns_vtimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
#if ARCH_ARMV8_EXTENSIONS > 1
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTHV_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTHV_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t os_ptimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTP_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTP_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_uint64_t os_vtimer_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_write(CNTV_TVAL, val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_sysreg_read(CNTV_TVAL, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
static timer_ctrl_handle ctrl_handle[] =
|
|
||||||
{
|
|
||||||
mon_ptimer_ctrl,
|
|
||||||
hyp_s_ptimer_ctrl,
|
|
||||||
hyp_ns_ptimer_ctrl,
|
|
||||||
hyp_s_vtimer_ctrl,
|
|
||||||
hyp_ns_vtimer_ctrl,
|
|
||||||
os_ptimer_ctrl,
|
|
||||||
os_vtimer_ctrl,
|
|
||||||
};
|
|
||||||
|
|
||||||
static timer_value_handle value_handle[] =
|
|
||||||
{
|
|
||||||
mon_ptimer_value,
|
|
||||||
hyp_s_ptimer_value,
|
|
||||||
hyp_ns_ptimer_value,
|
|
||||||
hyp_s_vtimer_value,
|
|
||||||
hyp_ns_vtimer_value,
|
|
||||||
os_ptimer_value,
|
|
||||||
os_vtimer_value,
|
|
||||||
};
|
|
||||||
|
|
||||||
rt_err_t arm_arch_timer_local_enable(void)
|
|
||||||
{
|
|
||||||
rt_err_t ret = RT_EOK;
|
|
||||||
|
|
||||||
if (arm_arch_timer_irq >= 0)
|
|
||||||
{
|
|
||||||
arm_arch_timer_ctrl_handle(RT_FALSE);
|
|
||||||
arm_arch_timer_value_handle(timer_step);
|
|
||||||
|
|
||||||
rt_hw_interrupt_umask(arm_arch_timer_irq);
|
|
||||||
|
|
||||||
arm_arch_timer_ctrl_handle(RT_TRUE);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t arm_arch_timer_local_disable(void)
|
|
||||||
{
|
|
||||||
rt_err_t ret = RT_EOK;
|
|
||||||
|
|
||||||
if (arm_arch_timer_ctrl_handle)
|
|
||||||
{
|
|
||||||
arm_arch_timer_ctrl_handle(RT_FALSE);
|
|
||||||
rt_hw_interrupt_mask(arm_arch_timer_irq);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t arm_arch_timer_set_frequency(rt_uint64_t frq)
|
|
||||||
{
|
|
||||||
rt_err_t ret = RT_EOK;
|
|
||||||
|
|
||||||
#ifdef ARCH_SUPPORT_TEE
|
|
||||||
rt_hw_isb();
|
|
||||||
rt_hw_sysreg_write(CNTFRQ, frq);
|
|
||||||
rt_hw_dsb();
|
|
||||||
#else
|
|
||||||
ret = -RT_ENOSYS;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_uint64_t arm_arch_timer_get_frequency(void)
|
|
||||||
{
|
|
||||||
rt_uint64_t frq;
|
|
||||||
|
|
||||||
rt_hw_isb();
|
|
||||||
rt_hw_sysreg_read(CNTFRQ, frq);
|
|
||||||
rt_hw_isb();
|
|
||||||
|
|
||||||
return frq;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t arm_arch_timer_set_value(rt_uint64_t val)
|
|
||||||
{
|
|
||||||
rt_err_t ret = RT_EOK;
|
|
||||||
|
|
||||||
if (arm_arch_timer_value_handle)
|
|
||||||
{
|
|
||||||
val = arm_arch_timer_value_handle(val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_uint64_t arm_arch_timer_get_value(void)
|
|
||||||
{
|
|
||||||
rt_uint64_t val = 0;
|
|
||||||
|
|
||||||
if (arm_arch_timer_value_handle)
|
|
||||||
{
|
|
||||||
val = arm_arch_timer_value_handle(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_uint64_t arm_arch_timer_get_count(void)
|
|
||||||
{
|
|
||||||
rt_uint64_t cntpct;
|
|
||||||
|
|
||||||
rt_hw_sysreg_read(CNTPCT, cntpct);
|
|
||||||
|
|
||||||
return cntpct;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void arm_arch_timer_isr(int vector, void *param)
|
|
||||||
{
|
|
||||||
arm_arch_timer_set_value(timer_step);
|
|
||||||
|
|
||||||
rt_tick_increase();
|
|
||||||
}
|
|
||||||
|
|
||||||
static int arm_arch_timer_post_init(void)
|
|
||||||
{
|
|
||||||
arm_arch_timer_local_enable();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
INIT_SECONDARY_CPU_EXPORT(arm_arch_timer_post_init);
|
|
||||||
|
|
||||||
static rt_err_t arm_arch_timer_probe(struct rt_platform_device *pdev)
|
|
||||||
{
|
|
||||||
int mode_idx, irq_idx;
|
|
||||||
const char *irq_name[] =
|
|
||||||
{
|
|
||||||
"phys", /* Secure Phys IRQ */
|
|
||||||
"virt", /* Non-secure Phys IRQ */
|
|
||||||
"hyp-phys", /* Virt IRQ */
|
|
||||||
"hyp-virt", /* Hyp IRQ */
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(ARCH_SUPPORT_TEE)
|
|
||||||
mode_idx = 0;
|
|
||||||
irq_idx = 0;
|
|
||||||
#elif defined(ARCH_SUPPORT_HYP)
|
|
||||||
mode_idx = 2;
|
|
||||||
irq_idx = 3;
|
|
||||||
#else
|
|
||||||
mode_idx = 5;
|
|
||||||
irq_idx = 1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
arm_arch_timer_irq = rt_dm_dev_get_irq_by_name(&pdev->parent, irq_name[irq_idx]);
|
|
||||||
|
|
||||||
if (arm_arch_timer_irq < 0)
|
|
||||||
{
|
|
||||||
arm_arch_timer_irq = rt_dm_dev_get_irq(&pdev->parent, irq_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arm_arch_timer_irq < 0)
|
|
||||||
{
|
|
||||||
return -RT_EEMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
arm_arch_timer_ctrl_handle = ctrl_handle[mode_idx];
|
|
||||||
arm_arch_timer_value_handle = value_handle[mode_idx];
|
|
||||||
|
|
||||||
rt_hw_interrupt_install(arm_arch_timer_irq, arm_arch_timer_isr, RT_NULL, "tick-arm-timer");
|
|
||||||
|
|
||||||
timer_step = arm_arch_timer_get_frequency() / RT_TICK_PER_SECOND;
|
|
||||||
|
|
||||||
arm_arch_timer_local_enable();
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct rt_ofw_node_id arm_arch_timer_ofw_ids[] =
|
|
||||||
{
|
|
||||||
{ .compatible = "arm,armv7-timer", },
|
|
||||||
{ .compatible = "arm,armv8-timer", },
|
|
||||||
{ /* sentinel */ }
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct rt_platform_driver arm_arch_timer_driver =
|
|
||||||
{
|
|
||||||
.name = "arm-arch-timer",
|
|
||||||
.ids = arm_arch_timer_ofw_ids,
|
|
||||||
|
|
||||||
.probe = arm_arch_timer_probe,
|
|
||||||
};
|
|
||||||
|
|
||||||
static int arm_arch_timer_drv_register(void)
|
|
||||||
{
|
|
||||||
rt_platform_driver_register(&arm_arch_timer_driver);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
INIT_SUBSYS_EXPORT(arm_arch_timer_drv_register);
|
|
||||||
@@ -1,417 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2015-08-31 heyuanjie87 first version
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <rtdevice.h>
|
|
||||||
#include <rthw.h>
|
|
||||||
|
|
||||||
#define DBG_TAG "hwtimer"
|
|
||||||
#define DBG_LVL DBG_INFO
|
|
||||||
#include <rtdbg.h>
|
|
||||||
|
|
||||||
#ifdef RT_USING_DM
|
|
||||||
void (*rt_device_hwtimer_us_delay)(rt_uint32_t us) = RT_NULL;
|
|
||||||
|
|
||||||
void rt_hw_us_delay(rt_uint32_t us)
|
|
||||||
{
|
|
||||||
if (rt_device_hwtimer_us_delay)
|
|
||||||
{
|
|
||||||
rt_device_hwtimer_us_delay(us);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG_E("Implemented at least in the libcpu");
|
|
||||||
|
|
||||||
RT_ASSERT(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif /* RT_USING_DM */
|
|
||||||
|
|
||||||
rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
|
|
||||||
{
|
|
||||||
float overflow;
|
|
||||||
float timeout;
|
|
||||||
rt_uint32_t counter;
|
|
||||||
int i, index = 0;
|
|
||||||
float tv_sec;
|
|
||||||
float devi_min = 1;
|
|
||||||
float devi;
|
|
||||||
|
|
||||||
/* changed to second */
|
|
||||||
overflow = timer->info->maxcnt/(float)timer->freq;
|
|
||||||
tv_sec = tv->sec + tv->usec/(float)1000000;
|
|
||||||
|
|
||||||
if (tv_sec < (1/(float)timer->freq))
|
|
||||||
{
|
|
||||||
/* little timeout */
|
|
||||||
i = 0;
|
|
||||||
timeout = 1/(float)timer->freq;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i = 1; i > 0; i ++)
|
|
||||||
{
|
|
||||||
timeout = tv_sec/i;
|
|
||||||
|
|
||||||
if (timeout <= overflow)
|
|
||||||
{
|
|
||||||
counter = (rt_uint32_t)(timeout * timer->freq);
|
|
||||||
devi = tv_sec - (counter / (float)timer->freq) * i;
|
|
||||||
/* Minimum calculation error */
|
|
||||||
if (devi > devi_min)
|
|
||||||
{
|
|
||||||
i = index;
|
|
||||||
timeout = tv_sec/i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (devi == 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (devi < devi_min)
|
|
||||||
{
|
|
||||||
devi_min = devi;
|
|
||||||
index = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
timer->cycles = i;
|
|
||||||
timer->reload = i;
|
|
||||||
timer->period_sec = timeout;
|
|
||||||
counter = (rt_uint32_t)(timeout * timer->freq);
|
|
||||||
|
|
||||||
return counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_err_t rt_hwtimer_init(struct rt_device *dev)
|
|
||||||
{
|
|
||||||
rt_err_t result = RT_EOK;
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t *)dev;
|
|
||||||
/* try to change to 1MHz */
|
|
||||||
if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq))
|
|
||||||
{
|
|
||||||
timer->freq = 1000000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
timer->freq = timer->info->minfreq;
|
|
||||||
}
|
|
||||||
timer->mode = HWTIMER_MODE_ONESHOT;
|
|
||||||
timer->cycles = 0;
|
|
||||||
timer->overflow = 0;
|
|
||||||
|
|
||||||
if (timer->ops->init)
|
|
||||||
{
|
|
||||||
timer->ops->init(timer, 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_err_t rt_hwtimer_open(struct rt_device *dev, rt_uint16_t oflag)
|
|
||||||
{
|
|
||||||
rt_err_t result = RT_EOK;
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t *)dev;
|
|
||||||
if (timer->ops->control != RT_NULL)
|
|
||||||
{
|
|
||||||
timer->ops->control(timer, HWTIMER_CTRL_FREQ_SET, &timer->freq);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_err_t rt_hwtimer_close(struct rt_device *dev)
|
|
||||||
{
|
|
||||||
rt_err_t result = RT_EOK;
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t*)dev;
|
|
||||||
if (timer->ops->init != RT_NULL)
|
|
||||||
{
|
|
||||||
timer->ops->init(timer, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
|
|
||||||
dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
|
|
||||||
dev->rx_indicate = RT_NULL;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_ssize_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
|
|
||||||
{
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
rt_hwtimerval_t tv;
|
|
||||||
rt_uint32_t cnt;
|
|
||||||
rt_base_t level;
|
|
||||||
rt_int32_t overflow;
|
|
||||||
float t;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t *)dev;
|
|
||||||
if (timer->ops->count_get == RT_NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
cnt = timer->ops->count_get(timer);
|
|
||||||
overflow = timer->overflow;
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
if (timer->info->cntmode == HWTIMER_CNTMODE_DW)
|
|
||||||
{
|
|
||||||
cnt = (rt_uint32_t)(timer->freq * timer->period_sec) - cnt;
|
|
||||||
}
|
|
||||||
if (timer->mode == HWTIMER_MODE_ONESHOT)
|
|
||||||
{
|
|
||||||
overflow = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
t = overflow * timer->period_sec + cnt/(float)timer->freq;
|
|
||||||
tv.sec = (rt_int32_t)t;
|
|
||||||
tv.usec = (rt_int32_t)((t - tv.sec) * 1000000);
|
|
||||||
size = size > sizeof(tv)? sizeof(tv) : size;
|
|
||||||
rt_memcpy(buffer, &tv, size);
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_ssize_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
rt_uint32_t t;
|
|
||||||
rt_hwtimer_mode_t opm = HWTIMER_MODE_PERIOD;
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t *)dev;
|
|
||||||
if ((timer->ops->start == RT_NULL) || (timer->ops->stop == RT_NULL))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (size != sizeof(rt_hwtimerval_t))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
timer->ops->stop(timer);
|
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
timer->overflow = 0;
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
t = timeout_calc(timer, (rt_hwtimerval_t*)buffer);
|
|
||||||
if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
|
|
||||||
{
|
|
||||||
opm = HWTIMER_MODE_ONESHOT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->ops->start(timer, t, opm) != RT_EOK)
|
|
||||||
size = 0;
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
rt_err_t result = RT_EOK;
|
|
||||||
rt_hwtimer_t *timer;
|
|
||||||
|
|
||||||
timer = (rt_hwtimer_t *)dev;
|
|
||||||
|
|
||||||
switch (cmd)
|
|
||||||
{
|
|
||||||
case HWTIMER_CTRL_STOP:
|
|
||||||
{
|
|
||||||
if (timer->ops->stop != RT_NULL)
|
|
||||||
{
|
|
||||||
timer->ops->stop(timer);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HWTIMER_CTRL_FREQ_SET:
|
|
||||||
{
|
|
||||||
rt_int32_t *f;
|
|
||||||
|
|
||||||
if (args == RT_NULL)
|
|
||||||
{
|
|
||||||
result = -RT_EEMPTY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
f = (rt_int32_t*)args;
|
|
||||||
if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
|
|
||||||
{
|
|
||||||
LOG_W("frequency setting out of range! It will maintain at %d Hz", timer->freq);
|
|
||||||
result = -RT_EINVAL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->ops->control != RT_NULL)
|
|
||||||
{
|
|
||||||
result = timer->ops->control(timer, cmd, args);
|
|
||||||
if (result == RT_EOK)
|
|
||||||
{
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
timer->freq = *f;
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HWTIMER_CTRL_INFO_GET:
|
|
||||||
{
|
|
||||||
if (args == RT_NULL)
|
|
||||||
{
|
|
||||||
result = -RT_EEMPTY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
*((struct rt_hwtimer_info*)args) = *timer->info;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HWTIMER_CTRL_MODE_SET:
|
|
||||||
{
|
|
||||||
rt_hwtimer_mode_t *m;
|
|
||||||
|
|
||||||
if (args == RT_NULL)
|
|
||||||
{
|
|
||||||
result = -RT_EEMPTY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
m = (rt_hwtimer_mode_t*)args;
|
|
||||||
|
|
||||||
if ((*m != HWTIMER_MODE_ONESHOT) && (*m != HWTIMER_MODE_PERIOD))
|
|
||||||
{
|
|
||||||
result = -RT_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
timer->mode = *m;
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
if (timer->ops->control != RT_NULL)
|
|
||||||
{
|
|
||||||
result = timer->ops->control(timer, cmd, args);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = -RT_ENOSYS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
|
|
||||||
timer->overflow ++;
|
|
||||||
|
|
||||||
if (timer->cycles != 0)
|
|
||||||
{
|
|
||||||
timer->cycles --;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->cycles == 0)
|
|
||||||
{
|
|
||||||
timer->cycles = timer->reload;
|
|
||||||
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
if (timer->mode == HWTIMER_MODE_ONESHOT)
|
|
||||||
{
|
|
||||||
if (timer->ops->stop != RT_NULL)
|
|
||||||
{
|
|
||||||
timer->ops->stop(timer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->parent.rx_indicate != RT_NULL)
|
|
||||||
{
|
|
||||||
timer->parent.rx_indicate(&timer->parent, sizeof(struct rt_hwtimerval));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef RT_USING_DEVICE_OPS
|
|
||||||
const static struct rt_device_ops hwtimer_ops =
|
|
||||||
{
|
|
||||||
rt_hwtimer_init,
|
|
||||||
rt_hwtimer_open,
|
|
||||||
rt_hwtimer_close,
|
|
||||||
rt_hwtimer_read,
|
|
||||||
rt_hwtimer_write,
|
|
||||||
rt_hwtimer_control
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data)
|
|
||||||
{
|
|
||||||
struct rt_device *device;
|
|
||||||
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
RT_ASSERT(timer->ops != RT_NULL);
|
|
||||||
RT_ASSERT(timer->info != RT_NULL);
|
|
||||||
|
|
||||||
device = &(timer->parent);
|
|
||||||
|
|
||||||
device->type = RT_Device_Class_Timer;
|
|
||||||
device->rx_indicate = RT_NULL;
|
|
||||||
device->tx_complete = RT_NULL;
|
|
||||||
|
|
||||||
#ifdef RT_USING_DEVICE_OPS
|
|
||||||
device->ops = &hwtimer_ops;
|
|
||||||
#else
|
|
||||||
device->init = rt_hwtimer_init;
|
|
||||||
device->open = rt_hwtimer_open;
|
|
||||||
device->close = rt_hwtimer_close;
|
|
||||||
device->read = rt_hwtimer_read;
|
|
||||||
device->write = rt_hwtimer_write;
|
|
||||||
device->control = rt_hwtimer_control;
|
|
||||||
#endif
|
|
||||||
device->user_data = user_data;
|
|
||||||
|
|
||||||
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CPUTIME_H__
|
|
||||||
#define CPUTIME_H__
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "cputimer.h"
|
|
||||||
|
|
||||||
struct rt_clock_cputime_ops
|
|
||||||
{
|
|
||||||
uint64_t (*cputime_getres)(void);
|
|
||||||
uint64_t (*cputime_gettime)(void);
|
|
||||||
int (*cputime_settimeout)(uint64_t tick, void (*timeout)(void *param), void *param);
|
|
||||||
};
|
|
||||||
|
|
||||||
uint64_t clock_cpu_getres(void);
|
|
||||||
uint64_t clock_cpu_gettime(void);
|
|
||||||
int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param);
|
|
||||||
int clock_cpu_issettimeout(void);
|
|
||||||
|
|
||||||
uint64_t clock_cpu_microsecond(uint64_t cpu_tick);
|
|
||||||
uint64_t clock_cpu_millisecond(uint64_t cpu_tick);
|
|
||||||
|
|
||||||
int clock_cpu_setops(const struct rt_clock_cputime_ops *ops);
|
|
||||||
|
|
||||||
#ifdef RT_USING_CPUTIME_RISCV
|
|
||||||
int riscv_cputime_init(void);
|
|
||||||
#endif /* RT_USING_CPUTIME_RISCV */
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CPUTIMER_H__
|
|
||||||
#define CPUTIMER_H__
|
|
||||||
|
|
||||||
#include <rtthread.h>
|
|
||||||
|
|
||||||
struct rt_cputimer
|
|
||||||
{
|
|
||||||
struct rt_object parent; /**< inherit from rt_object */
|
|
||||||
rt_list_t row;
|
|
||||||
void (*timeout_func)(void *parameter);
|
|
||||||
void *parameter;
|
|
||||||
rt_uint64_t init_tick;
|
|
||||||
rt_uint64_t timeout_tick;
|
|
||||||
struct rt_semaphore sem;
|
|
||||||
};
|
|
||||||
typedef struct rt_cputimer *rt_cputimer_t;
|
|
||||||
|
|
||||||
rt_err_t rt_cputimer_detach(rt_cputimer_t timer);
|
|
||||||
|
|
||||||
#ifdef RT_USING_HEAP
|
|
||||||
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);
|
|
||||||
rt_err_t rt_cputimer_delete(rt_cputimer_t timer);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
rt_err_t rt_cputimer_start(rt_cputimer_t timer);
|
|
||||||
rt_err_t rt_cputimer_stop(rt_cputimer_t timer);
|
|
||||||
rt_err_t rt_cputimer_control(rt_cputimer_t timer, int cmd, void *arg);
|
|
||||||
rt_err_t rt_cputime_sleep(rt_uint64_t tick);
|
|
||||||
rt_err_t rt_cputime_ndelay(rt_uint64_t ns);
|
|
||||||
rt_err_t rt_cputime_udelay(rt_uint64_t us);
|
|
||||||
rt_err_t rt_cputime_mdelay(rt_uint64_t ms);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
*/
|
|
||||||
#ifndef __HWTIMER_H__
|
|
||||||
#define __HWTIMER_H__
|
|
||||||
|
|
||||||
#include <rtthread.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Timer Control Command */
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /* set the count frequency */
|
|
||||||
HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /* stop timer */
|
|
||||||
HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /* get a timer feature information */
|
|
||||||
HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /* Setting the timing mode(oneshot/period) */
|
|
||||||
} rt_hwtimer_ctrl_t;
|
|
||||||
|
|
||||||
/* Timing Mode */
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
HWTIMER_MODE_ONESHOT = 0x01,
|
|
||||||
HWTIMER_MODE_PERIOD
|
|
||||||
} rt_hwtimer_mode_t;
|
|
||||||
|
|
||||||
/* Time Value */
|
|
||||||
typedef struct rt_hwtimerval
|
|
||||||
{
|
|
||||||
rt_int32_t sec; /* second */
|
|
||||||
rt_int32_t usec; /* microsecond */
|
|
||||||
} rt_hwtimerval_t;
|
|
||||||
|
|
||||||
#define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
|
|
||||||
#define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
|
|
||||||
|
|
||||||
struct rt_hwtimer_device;
|
|
||||||
|
|
||||||
struct rt_hwtimer_ops
|
|
||||||
{
|
|
||||||
void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
|
|
||||||
rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
|
|
||||||
void (*stop)(struct rt_hwtimer_device *timer);
|
|
||||||
rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
|
|
||||||
rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Timer Feature Information */
|
|
||||||
struct rt_hwtimer_info
|
|
||||||
{
|
|
||||||
rt_int32_t maxfreq; /* the maximum count frequency timer support */
|
|
||||||
rt_int32_t minfreq; /* the minimum count frequency timer support */
|
|
||||||
rt_uint32_t maxcnt; /* counter maximum value */
|
|
||||||
rt_uint8_t cntmode; /* count mode (inc/dec) */
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct rt_hwtimer_device
|
|
||||||
{
|
|
||||||
struct rt_device parent;
|
|
||||||
const struct rt_hwtimer_ops *ops;
|
|
||||||
const struct rt_hwtimer_info *info;
|
|
||||||
|
|
||||||
rt_int32_t freq; /* counting frequency set by the user */
|
|
||||||
rt_int32_t overflow; /* timer overflows */
|
|
||||||
float period_sec;
|
|
||||||
rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
|
|
||||||
rt_int32_t reload; /* reload cycles(using in period mode) */
|
|
||||||
rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
|
|
||||||
} rt_hwtimer_t;
|
|
||||||
|
|
||||||
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
|
|
||||||
void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
|
|
||||||
|
|
||||||
#ifdef RT_USING_DM
|
|
||||||
extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2025, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2025-01-01 RT-Thread Compatibility layer for legacy hwtimer API
|
|
||||||
*
|
|
||||||
* DEPRECATED: This header provides backward compatibility for the legacy hwtimer API.
|
|
||||||
* New code should use the unified clock_time subsystem instead.
|
|
||||||
* This compatibility layer will be removed in a future release.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __HWTIMER_H__
|
|
||||||
#define __HWTIMER_H__
|
|
||||||
|
|
||||||
#include <rtthread.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
* For backward compatibility, we keep the hwtimer types and APIs.
|
|
||||||
* If RT_USING_CLOCK_TIME is defined, these will map to clock_time.
|
|
||||||
* Otherwise, we include the original hwtimer implementation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef RT_USING_CLOCK_TIME
|
|
||||||
|
|
||||||
/* Timer Control Command */
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /* set the count frequency */
|
|
||||||
HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /* stop timer */
|
|
||||||
HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /* get a timer feature information */
|
|
||||||
HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /* Setting the timing mode(oneshot/period) */
|
|
||||||
} rt_hwtimer_ctrl_t;
|
|
||||||
|
|
||||||
/* Timing Mode */
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
HWTIMER_MODE_ONESHOT = 0x01,
|
|
||||||
HWTIMER_MODE_PERIOD
|
|
||||||
} rt_hwtimer_mode_t;
|
|
||||||
|
|
||||||
/* Time Value */
|
|
||||||
typedef struct rt_hwtimerval
|
|
||||||
{
|
|
||||||
rt_int32_t sec; /* second */
|
|
||||||
rt_int32_t usec; /* microsecond */
|
|
||||||
} rt_hwtimerval_t;
|
|
||||||
|
|
||||||
#define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
|
|
||||||
#define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
|
|
||||||
|
|
||||||
struct rt_hwtimer_device;
|
|
||||||
|
|
||||||
struct rt_hwtimer_ops
|
|
||||||
{
|
|
||||||
void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
|
|
||||||
rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
|
|
||||||
void (*stop)(struct rt_hwtimer_device *timer);
|
|
||||||
rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
|
|
||||||
rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Timer Feature Information */
|
|
||||||
struct rt_hwtimer_info
|
|
||||||
{
|
|
||||||
rt_int32_t maxfreq; /* the maximum count frequency timer support */
|
|
||||||
rt_int32_t minfreq; /* the minimum count frequency timer support */
|
|
||||||
rt_uint32_t maxcnt; /* counter maximum value */
|
|
||||||
rt_uint8_t cntmode; /* count mode (inc/dec) */
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct rt_hwtimer_device
|
|
||||||
{
|
|
||||||
struct rt_device parent;
|
|
||||||
const struct rt_hwtimer_ops *ops;
|
|
||||||
const struct rt_hwtimer_info *info;
|
|
||||||
|
|
||||||
rt_int32_t freq; /* counting frequency set by the user */
|
|
||||||
rt_int32_t overflow; /* timer overflows */
|
|
||||||
float period_sec;
|
|
||||||
rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
|
|
||||||
rt_int32_t reload; /* reload cycles(using in period mode) */
|
|
||||||
rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
|
|
||||||
} rt_hwtimer_t;
|
|
||||||
|
|
||||||
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
|
|
||||||
void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
|
|
||||||
|
|
||||||
#ifdef RT_USING_DM
|
|
||||||
extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else /* !RT_USING_CLOCK_TIME */
|
|
||||||
|
|
||||||
#warning "RT_USING_HWTIMER is deprecated. Please migrate to RT_USING_CLOCK_TIME. Include <drivers/clock_time.h> instead and use rt_clock_time_* APIs."
|
|
||||||
|
|
||||||
/* If clock_time is not enabled, this means the old hwtimer module should still exist */
|
|
||||||
/* The build system should handle this by including the old hwtimer directory */
|
|
||||||
|
|
||||||
#endif /* RT_USING_CLOCK_TIME */
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* __HWTIMER_H__ */
|
|
||||||
31
components/drivers/include/ktime.h
Normal file
31
components/drivers/include/ktime.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2006-2025, RT-Thread Development Team
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Change Logs:
|
||||||
|
* Date Author Notes
|
||||||
|
* 2025-01-01 RT-Thread Compatibility layer for legacy ktime API
|
||||||
|
*
|
||||||
|
* COMPATIBILITY HEADER:
|
||||||
|
* This header provides backward compatibility for code using the old ktime API.
|
||||||
|
* All rt_ktime_* APIs are now redirected to rt_clock_* APIs.
|
||||||
|
*
|
||||||
|
* The old ktime subsystem has been removed and replaced with the unified
|
||||||
|
* clock_time subsystem. Include <drivers/clock_time.h> for new code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __KTIME_H__
|
||||||
|
#define __KTIME_H__
|
||||||
|
|
||||||
|
#ifdef RT_USING_CLOCK_TIME
|
||||||
|
/* Include the unified clock_time header which provides all APIs */
|
||||||
|
#include <drivers/clock_time.h>
|
||||||
|
|
||||||
|
/* All rt_ktime_* APIs are already defined as macros in clock_time.h */
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error "ktime subsystem has been removed. Please enable RT_USING_CLOCK_TIME in menuconfig."
|
||||||
|
#endif /* RT_USING_CLOCK_TIME */
|
||||||
|
|
||||||
|
#endif /* __KTIME_H__ */
|
||||||
@@ -246,18 +246,10 @@ extern "C" {
|
|||||||
#include "drivers/clock_time.h"
|
#include "drivers/clock_time.h"
|
||||||
#endif /* RT_USING_CLOCK_TIME */
|
#endif /* RT_USING_CLOCK_TIME */
|
||||||
|
|
||||||
#ifdef RT_USING_HWTIMER
|
|
||||||
#include "drivers/hwtimer.h"
|
|
||||||
#endif /* RT_USING_HWTIMER */
|
|
||||||
|
|
||||||
#ifdef RT_USING_AUDIO
|
#ifdef RT_USING_AUDIO
|
||||||
#include "drivers/dev_audio.h"
|
#include "drivers/dev_audio.h"
|
||||||
#endif /* RT_USING_AUDIO */
|
#endif /* RT_USING_AUDIO */
|
||||||
|
|
||||||
#ifdef RT_USING_CPUTIME
|
|
||||||
#include "drivers/cputime.h"
|
|
||||||
#endif /* RT_USING_CPUTIME */
|
|
||||||
|
|
||||||
#ifdef RT_USING_ADC
|
#ifdef RT_USING_ADC
|
||||||
#include "drivers/adc.h"
|
#include "drivers/adc.h"
|
||||||
#endif /* RT_USING_ADC */
|
#endif /* RT_USING_ADC */
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
menuconfig RT_USING_KTIME
|
|
||||||
bool "Ktime: kernel time (DEPRECATED - use RT_USING_CLOCK_TIME)"
|
|
||||||
default n
|
|
||||||
depends on !RT_USING_CLOCK_TIME
|
|
||||||
help
|
|
||||||
DEPRECATED: This option is maintained for backward compatibility only.
|
|
||||||
New projects should use RT_USING_CLOCK_TIME instead.
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
# ktime
|
|
||||||
|
|
||||||
## 1、介绍
|
|
||||||
|
|
||||||
ktime 为 kernel time,为内核时间子系统,实现了内核启动时间以及芯片内核 cputimer 时间管理以及一个 ns 精度的高精度定时器,
|
|
||||||
|
|
||||||
## 2、如何打开 ktime
|
|
||||||
|
|
||||||
使用 ktime 需要在 RT-Thread 的 menuconfig 中选择它,具体路径如下:
|
|
||||||
|
|
||||||
```
|
|
||||||
RT-Thread Components
|
|
||||||
[*] Ktime: kernel time
|
|
||||||
```
|
|
||||||
|
|
||||||
## 3、使用 ktime
|
|
||||||
|
|
||||||
> 函数的功能以及参数类型已经写在头文件的注释之中,本文不再赘述
|
|
||||||
|
|
||||||
### 3.1、boottime
|
|
||||||
|
|
||||||
boottime 为系统启动时间,即为系统从上电开始到现在运行的时间,默认的时间基准为芯片内核的 cputimer 的 cnt 值,已经适配了 aarch64 与 riscv64 平台,例如 stm32 等平台需要在自己的 bsp 里面进行适配(boottime 里面函数都为 weak function),需要注意 tick 从中断到设置中间的时延
|
|
||||||
|
|
||||||
**此值应当为 Readonly**
|
|
||||||
|
|
||||||
### 3.2、cputimer
|
|
||||||
|
|
||||||
cputimer 为芯片内核的 cputimer,也可以认为是 os tick 来源的那个定时器,cputimer 主要是提供了一个统一的接口去获得其分辨率,频率,cnt 值
|
|
||||||
|
|
||||||
**此值应当为 Readonly**
|
|
||||||
|
|
||||||
### 3.3、hrtimer
|
|
||||||
|
|
||||||
> TODO: hrtimer 目前还是使用优先级链表的方式进行管理,在遇到任务的大规模并发时还是存在部分性能问题,待内核有一个统一的红黑树组件后,再进行优化
|
|
||||||
|
|
||||||
hrtimer 为高精度定时器,需要重写其 weak 函数(需要对接到硬件定时器,否则默认走的是软件定时器,分辨率只有 os tick 的值)才能正常使用,其主要使用方法:
|
|
||||||
|
|
||||||
#### 3.3.1、延时
|
|
||||||
|
|
||||||
hrtimer 的延时并不是 while(1)式死等,它会将一个线程挂起,睡眠多少时间后通过硬件定时器将其唤醒(注:延时 ns 并不是真的能准确的延时这么多,而是在保证性能的情况下尽可能的延时)
|
|
||||||
|
|
||||||
- rt_ktime_hrtimer_sleep:单位为 cputimer 的 tick 值
|
|
||||||
- rt_ktime_hrtimer_ndelay:单位为 ns
|
|
||||||
- rt_ktime_hrtimer_udelay:单位为 us
|
|
||||||
- rt_ktime_hrtimer_mdelay:单位为 ms
|
|
||||||
|
|
||||||
#### 3.3.1、定时器
|
|
||||||
|
|
||||||
hrtimer 还提供了一套 rt_timer 风格的 api
|
|
||||||
|
|
||||||
- rt_ktime_hrtimer_init
|
|
||||||
- rt_ktime_hrtimer_delete
|
|
||||||
- rt_ktime_hrtimer_start
|
|
||||||
- rt_ktime_hrtimer_stop
|
|
||||||
- rt_ktime_hrtimer_control
|
|
||||||
- rt_ktime_hrtimer_detach
|
|
||||||
|
|
||||||
需要注意,此定时器回调函数依旧处于中断之中,不能做一些耗时的任务
|
|
||||||
|
|
||||||
## 5、联系方式
|
|
||||||
|
|
||||||
- 维护:xqyjlj
|
|
||||||
- 主页:https://github.com/xqyjlj
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
import os
|
|
||||||
from building import *
|
|
||||||
|
|
||||||
Import('rtconfig')
|
|
||||||
|
|
||||||
cwd = GetCurrentDir()
|
|
||||||
|
|
||||||
src = Glob('src/*.c')
|
|
||||||
list = os.listdir(cwd + "/src")
|
|
||||||
if rtconfig.ARCH in list:
|
|
||||||
if os.path.exists(cwd + "/src/" + rtconfig.ARCH + "/" + rtconfig.CPU):
|
|
||||||
src += Glob("src/" + rtconfig.ARCH + "/" + rtconfig.CPU + "/*.c")
|
|
||||||
else:
|
|
||||||
src += Glob("src/" + rtconfig.ARCH + "/*.c")
|
|
||||||
CPPPATH = [cwd, cwd + "/inc"]
|
|
||||||
LOCAL_CCFLAGS = ''
|
|
||||||
if rtconfig.PLATFORM in ['gcc', 'armclang']:
|
|
||||||
LOCAL_CCFLAGS += ' -std=gnu99'
|
|
||||||
elif rtconfig.PLATFORM in ['armcc']:
|
|
||||||
LOCAL_CCFLAGS += ' --c99 --gnu'
|
|
||||||
|
|
||||||
group = DefineGroup('DeviceDrivers', src, depend=['RT_USING_KTIME'], CPPPATH=CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS)
|
|
||||||
|
|
||||||
Return('group')
|
|
||||||
@@ -1,192 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2025, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
* 2024-04-26 Shell Improve ipc performance
|
|
||||||
* 2025-01-01 RT-Thread Compatibility layer for unified clock_time
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __KTIME_H__
|
|
||||||
#define __KTIME_H__
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <ipc/completion.h>
|
|
||||||
|
|
||||||
#include "rtthread.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* COMPATIBILITY LAYER:
|
|
||||||
* When RT_USING_CLOCK_TIME is enabled, this header redirects to the
|
|
||||||
* unified clock_time subsystem. All rt_ktime_* APIs are mapped to
|
|
||||||
* rt_clock_* APIs via macros defined in clock_time.h.
|
|
||||||
*
|
|
||||||
* When RT_USING_CLOCK_TIME is not enabled, the original ktime
|
|
||||||
* implementation is used.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef RT_USING_CLOCK_TIME
|
|
||||||
|
|
||||||
/* Include the unified clock_time header which provides all APIs */
|
|
||||||
#include <drivers/clock_time.h>
|
|
||||||
|
|
||||||
/* All rt_ktime_* APIs are already defined as macros in clock_time.h */
|
|
||||||
/* No additional definitions needed here */
|
|
||||||
|
|
||||||
#else /* !RT_USING_CLOCK_TIME - Original ktime implementation */
|
|
||||||
|
|
||||||
#define RT_KTIME_RESMUL (1000000ULL)
|
|
||||||
|
|
||||||
struct rt_ktime_hrtimer
|
|
||||||
{
|
|
||||||
rt_uint8_t flag; /**< compatible to tick timer's flag */
|
|
||||||
char name[RT_NAME_MAX];
|
|
||||||
rt_list_t node;
|
|
||||||
void *parameter;
|
|
||||||
unsigned long delay_cnt;
|
|
||||||
unsigned long timeout_cnt;
|
|
||||||
rt_err_t error;
|
|
||||||
struct rt_completion completion;
|
|
||||||
void (*timeout_func)(void *parameter);
|
|
||||||
};
|
|
||||||
typedef struct rt_ktime_hrtimer *rt_ktime_hrtimer_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get boottime with us precision
|
|
||||||
*
|
|
||||||
* @param tv: timeval
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_boottime_get_us(struct timeval *tv);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get boottime with s precision
|
|
||||||
*
|
|
||||||
* @param t: time_t
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_boottime_get_s(time_t *t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get boottime with ns precision
|
|
||||||
*
|
|
||||||
* @param ts: timespec
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get cputimer resolution
|
|
||||||
*
|
|
||||||
* @return (resolution * RT_KTIME_RESMUL)
|
|
||||||
*/
|
|
||||||
rt_uint64_t rt_ktime_cputimer_getres(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get cputimer frequency
|
|
||||||
*
|
|
||||||
* @return frequency
|
|
||||||
*/
|
|
||||||
unsigned long rt_ktime_cputimer_getfrq(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get cputimer the value of the cnt counter
|
|
||||||
*
|
|
||||||
* @return cnt
|
|
||||||
*/
|
|
||||||
unsigned long rt_ktime_cputimer_getcnt(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Init cputimer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void rt_ktime_cputimer_init(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get hrtimer resolution
|
|
||||||
*
|
|
||||||
* @return (resolution * RT_KTIME_RESMUL)
|
|
||||||
*/
|
|
||||||
rt_uint64_t rt_ktime_hrtimer_getres(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get hrtimer frequency
|
|
||||||
*
|
|
||||||
* @return frequency
|
|
||||||
*/
|
|
||||||
unsigned long rt_ktime_hrtimer_getfrq(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief set hrtimer interrupt timeout count (cnt), you should re-implemented it in hrtimer device driver
|
|
||||||
*
|
|
||||||
* @param cnt: hrtimer requires a timing cnt value
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief called in hrtimer device driver isr routinue, it will process the timeouts
|
|
||||||
*/
|
|
||||||
void rt_ktime_hrtimer_process(void);
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
|
|
||||||
const char *name,
|
|
||||||
rt_uint8_t flag,
|
|
||||||
void (*timeout)(void *parameter),
|
|
||||||
void *parameter);
|
|
||||||
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long cnt);
|
|
||||||
rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer);
|
|
||||||
rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg);
|
|
||||||
rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer);
|
|
||||||
|
|
||||||
rt_inline void rt_ktime_hrtimer_keep_errno(rt_ktime_hrtimer_t timer, rt_err_t err)
|
|
||||||
{
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
|
|
||||||
timer->error = err;
|
|
||||||
rt_set_errno(-err);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer);
|
|
||||||
void rt_ktime_hrtimer_delay_detach(struct rt_ktime_hrtimer *timer);
|
|
||||||
void rt_ktime_hrtimer_process(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief sleep by the cputimer cnt value
|
|
||||||
*
|
|
||||||
* @param cnt: the cputimer cnt value
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cnt);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief sleep by ns
|
|
||||||
*
|
|
||||||
* @param ns: ns
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief sleep by us
|
|
||||||
*
|
|
||||||
* @param us: us
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_hrtimer_udelay(struct rt_ktime_hrtimer *timer, unsigned long us);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief sleep by ms
|
|
||||||
*
|
|
||||||
* @param ms: ms
|
|
||||||
* @return rt_err_t
|
|
||||||
*/
|
|
||||||
rt_err_t rt_ktime_hrtimer_mdelay(struct rt_ktime_hrtimer *timer, unsigned long ms);
|
|
||||||
|
|
||||||
#endif /* !RT_USING_CLOCK_TIME */
|
|
||||||
|
|
||||||
#endif /* __KTIME_H__ */
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "gtimer.h"
|
|
||||||
#include "ktime.h"
|
|
||||||
|
|
||||||
static volatile unsigned long _init_cnt = 0;
|
|
||||||
|
|
||||||
rt_uint64_t rt_ktime_cputimer_getres(void)
|
|
||||||
{
|
|
||||||
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long rt_ktime_cputimer_getfrq(void)
|
|
||||||
{
|
|
||||||
return rt_hw_get_gtimer_frq();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long rt_ktime_cputimer_getcnt(void)
|
|
||||||
{
|
|
||||||
return rt_hw_get_cntpct_val() - _init_cnt;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_cputimer_init(void)
|
|
||||||
{
|
|
||||||
_init_cnt = rt_hw_get_cntpct_val();
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ktime.h"
|
|
||||||
|
|
||||||
#define __KTIME_MUL ((1000ULL * 1000 * 1000) / RT_TICK_PER_SECOND)
|
|
||||||
|
|
||||||
rt_weak rt_err_t rt_ktime_boottime_get_us(struct timeval *tv)
|
|
||||||
{
|
|
||||||
RT_ASSERT(tv != RT_NULL);
|
|
||||||
|
|
||||||
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
|
|
||||||
|
|
||||||
tv->tv_sec = ns / (1000ULL * 1000 * 1000);
|
|
||||||
tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak rt_err_t rt_ktime_boottime_get_s(time_t *t)
|
|
||||||
{
|
|
||||||
RT_ASSERT(t != RT_NULL);
|
|
||||||
|
|
||||||
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
|
|
||||||
|
|
||||||
*t = ns / (1000ULL * 1000 * 1000);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts)
|
|
||||||
{
|
|
||||||
RT_ASSERT(ts != RT_NULL);
|
|
||||||
|
|
||||||
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
|
|
||||||
|
|
||||||
ts->tv_sec = ns / (1000ULL * 1000 * 1000);
|
|
||||||
ts->tv_nsec = ns % (1000ULL * 1000 * 1000);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ktime.h"
|
|
||||||
|
|
||||||
rt_weak rt_uint64_t rt_ktime_cputimer_getres(void)
|
|
||||||
{
|
|
||||||
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak unsigned long rt_ktime_cputimer_getfrq(void)
|
|
||||||
{
|
|
||||||
return RT_TICK_PER_SECOND;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak unsigned long rt_ktime_cputimer_getcnt(void)
|
|
||||||
{
|
|
||||||
return rt_tick_get();
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak void rt_ktime_cputimer_init(void)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
@@ -1,386 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <rtdevice.h>
|
|
||||||
#include <rthw.h>
|
|
||||||
#include <rtthread.h>
|
|
||||||
|
|
||||||
#define DBG_SECTION_NAME "drv.ktime"
|
|
||||||
#define DBG_LEVEL DBG_INFO
|
|
||||||
#include <rtdbg.h>
|
|
||||||
|
|
||||||
#include "ktime.h"
|
|
||||||
|
|
||||||
#ifdef ARCH_CPU_64BIT
|
|
||||||
#define _HRTIMER_MAX_CNT UINT64_MAX
|
|
||||||
#else
|
|
||||||
#define _HRTIMER_MAX_CNT UINT32_MAX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static rt_list_t _timer_list = RT_LIST_OBJECT_INIT(_timer_list);
|
|
||||||
static RT_DEFINE_SPINLOCK(_spinlock);
|
|
||||||
|
|
||||||
rt_inline rt_ktime_hrtimer_t _first_hrtimer(void)
|
|
||||||
{
|
|
||||||
return rt_list_isempty(&_timer_list) ? RT_NULL : rt_list_first_entry(&_timer_list, struct rt_ktime_hrtimer, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak rt_uint64_t rt_ktime_hrtimer_getres(void)
|
|
||||||
{
|
|
||||||
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak unsigned long rt_ktime_hrtimer_getfrq(void)
|
|
||||||
{
|
|
||||||
return RT_TICK_PER_SECOND;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt)
|
|
||||||
{
|
|
||||||
static rt_timer_t timer = RT_NULL;
|
|
||||||
static struct rt_timer _sh_rtimer;
|
|
||||||
|
|
||||||
RT_ASSERT(cnt > 0);
|
|
||||||
|
|
||||||
if (timer == RT_NULL)
|
|
||||||
{
|
|
||||||
timer = &_sh_rtimer;
|
|
||||||
rt_timer_init(timer, "shrtimer", (void (*)(void *))rt_ktime_hrtimer_process, RT_NULL, cnt, RT_TIMER_FLAG_ONE_SHOT);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rt_tick_t tick = cnt;
|
|
||||||
rt_timer_control(timer, RT_TIMER_CTRL_SET_TIME, &tick);
|
|
||||||
rt_timer_control(timer, RT_TIMER_CTRL_SET_PARM, RT_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
|
|
||||||
{
|
|
||||||
rt_timer_stop(timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_timer_start(timer);
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief convert cnt from cputimer cnt to hrtimer cnt
|
|
||||||
*
|
|
||||||
* @param cnt
|
|
||||||
* @return unsigned long
|
|
||||||
*/
|
|
||||||
static unsigned long _cnt_convert(unsigned long cnt)
|
|
||||||
{
|
|
||||||
unsigned long rtn = 0;
|
|
||||||
unsigned long count = cnt - rt_ktime_cputimer_getcnt();
|
|
||||||
if (count > (_HRTIMER_MAX_CNT / 2))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
rtn = (count * rt_ktime_cputimer_getres()) / rt_ktime_hrtimer_getres();
|
|
||||||
return rtn == 0 ? 1 : rtn; /* at least 1 */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _sleep_timeout(void *parameter)
|
|
||||||
{
|
|
||||||
struct rt_ktime_hrtimer *timer = parameter;
|
|
||||||
rt_completion_done(&timer->completion);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _insert_timer_to_list_locked(rt_ktime_hrtimer_t timer)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_t iter;
|
|
||||||
|
|
||||||
rt_list_for_each_entry(iter, &_timer_list, node)
|
|
||||||
{
|
|
||||||
if (iter->timeout_cnt > timer->timeout_cnt)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rt_list_insert_before(&iter->node, &(timer->node));
|
|
||||||
|
|
||||||
timer->flag |= RT_TIMER_FLAG_ACTIVATED;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _hrtimer_process_locked(void)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_t timer;
|
|
||||||
|
|
||||||
for (timer = _first_hrtimer();
|
|
||||||
(timer != RT_NULL) && (timer->timeout_cnt <= rt_ktime_cputimer_getcnt());
|
|
||||||
timer = _first_hrtimer())
|
|
||||||
{
|
|
||||||
rt_list_remove(&(timer->node));
|
|
||||||
|
|
||||||
if (timer->flag & RT_TIMER_FLAG_PERIODIC)
|
|
||||||
{
|
|
||||||
timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt();
|
|
||||||
_insert_timer_to_list_locked(timer);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timer->timeout_func)
|
|
||||||
{
|
|
||||||
timer->timeout_func(timer->parameter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _set_next_timeout_locked(void)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_t timer;
|
|
||||||
rt_ubase_t next_timeout_hrtimer_cnt;
|
|
||||||
rt_bool_t find_next;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
find_next = RT_FALSE;
|
|
||||||
if ((timer = _first_hrtimer()) != RT_NULL)
|
|
||||||
{
|
|
||||||
next_timeout_hrtimer_cnt = _cnt_convert(timer->timeout_cnt);
|
|
||||||
if (next_timeout_hrtimer_cnt > 0)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_settimeout(next_timeout_hrtimer_cnt);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_hrtimer_process_locked();
|
|
||||||
find_next = RT_TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (find_next);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_process(void)
|
|
||||||
{
|
|
||||||
rt_base_t level = rt_spin_lock_irqsave(&_spinlock);
|
|
||||||
|
|
||||||
_hrtimer_process_locked();
|
|
||||||
_set_next_timeout_locked();
|
|
||||||
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
|
|
||||||
const char *name,
|
|
||||||
rt_uint8_t flag,
|
|
||||||
void (*timeout)(void *parameter),
|
|
||||||
void *parameter)
|
|
||||||
{
|
|
||||||
/* parameter check */
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
RT_ASSERT(timeout != RT_NULL);
|
|
||||||
|
|
||||||
rt_memset(timer, 0, sizeof(struct rt_ktime_hrtimer));
|
|
||||||
|
|
||||||
timer->flag = flag & ~RT_TIMER_FLAG_ACTIVATED;
|
|
||||||
timer->timeout_func = timeout;
|
|
||||||
timer->parameter = parameter;
|
|
||||||
rt_strncpy(timer->name, name, RT_NAME_MAX - 1);
|
|
||||||
rt_list_init(&(timer->node));
|
|
||||||
rt_completion_init(&timer->completion);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long delay_cnt)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
|
|
||||||
/* parameter check */
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
RT_ASSERT(delay_cnt < (_HRTIMER_MAX_CNT / 2));
|
|
||||||
|
|
||||||
timer->delay_cnt = delay_cnt;
|
|
||||||
timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt();
|
|
||||||
|
|
||||||
level = rt_spin_lock_irqsave(&_spinlock);
|
|
||||||
|
|
||||||
if (timer->flag & RT_TIMER_FLAG_ACTIVATED)
|
|
||||||
{
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
return -RT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
_insert_timer_to_list_locked(timer);
|
|
||||||
_set_next_timeout_locked();
|
|
||||||
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
|
|
||||||
RT_ASSERT(timer != RT_NULL); /* timer check */
|
|
||||||
|
|
||||||
level = rt_spin_lock_irqsave(&_spinlock);
|
|
||||||
|
|
||||||
if (!(timer->flag & RT_TIMER_FLAG_ACTIVATED))
|
|
||||||
{
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
return -RT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_list_remove(&timer->node);
|
|
||||||
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
||||||
_set_next_timeout_locked();
|
|
||||||
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
|
|
||||||
/* parameter check */
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
|
|
||||||
level = rt_spin_lock_irqsave(&_spinlock);
|
|
||||||
switch (cmd)
|
|
||||||
{
|
|
||||||
|
|
||||||
case RT_TIMER_CTRL_GET_TIME:
|
|
||||||
*(unsigned long *)arg = timer->delay_cnt;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RT_TIMER_CTRL_SET_TIME:
|
|
||||||
RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
|
|
||||||
timer->delay_cnt = *(unsigned long *)arg;
|
|
||||||
timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RT_TIMER_CTRL_SET_ONESHOT:
|
|
||||||
timer->flag &= ~RT_TIMER_FLAG_PERIODIC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RT_TIMER_CTRL_SET_PERIODIC:
|
|
||||||
timer->flag |= RT_TIMER_FLAG_PERIODIC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RT_TIMER_CTRL_GET_STATE:
|
|
||||||
if (timer->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:
|
|
||||||
*(unsigned long *)arg = timer->timeout_cnt;
|
|
||||||
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_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer)
|
|
||||||
{
|
|
||||||
rt_base_t level;
|
|
||||||
|
|
||||||
/* parameter check */
|
|
||||||
RT_ASSERT(timer != RT_NULL);
|
|
||||||
|
|
||||||
/* notify the timer stop event */
|
|
||||||
rt_completion_wakeup_by_errno(&timer->completion, RT_ERROR);
|
|
||||||
|
|
||||||
level = rt_spin_lock_irqsave(&_spinlock);
|
|
||||||
|
|
||||||
/* stop timer */
|
|
||||||
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
||||||
/* when interrupted */
|
|
||||||
if (timer->error == -RT_EINTR || timer->error == RT_EINTR)
|
|
||||||
{
|
|
||||||
rt_list_remove(&timer->node);
|
|
||||||
_set_next_timeout_locked();
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_spin_unlock_irqrestore(&_spinlock, level);
|
|
||||||
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/************************** delay ***************************/
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_init(timer, "hrtimer_sleep", RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
|
|
||||||
_sleep_timeout, timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_hrtimer_delay_detach(struct rt_ktime_hrtimer *timer)
|
|
||||||
{
|
|
||||||
rt_ktime_hrtimer_detach(timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cnt)
|
|
||||||
{
|
|
||||||
rt_err_t err;
|
|
||||||
|
|
||||||
if (cnt == 0)
|
|
||||||
return -RT_EINVAL;
|
|
||||||
|
|
||||||
err = rt_ktime_hrtimer_start(timer, cnt);
|
|
||||||
if (err)
|
|
||||||
return err;
|
|
||||||
|
|
||||||
err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER,
|
|
||||||
RT_INTERRUPTIBLE);
|
|
||||||
rt_ktime_hrtimer_keep_errno(timer, err);
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns)
|
|
||||||
{
|
|
||||||
rt_uint64_t res = rt_ktime_cputimer_getres();
|
|
||||||
return rt_ktime_hrtimer_sleep(timer, (ns * RT_KTIME_RESMUL) / res);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_udelay(struct rt_ktime_hrtimer *timer, unsigned long us)
|
|
||||||
{
|
|
||||||
return rt_ktime_hrtimer_ndelay(timer, us * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
rt_err_t rt_ktime_hrtimer_mdelay(struct rt_ktime_hrtimer *timer, unsigned long ms)
|
|
||||||
{
|
|
||||||
return rt_ktime_hrtimer_ndelay(timer, ms * 1000000);
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*
|
|
||||||
* Change Logs:
|
|
||||||
* Date Author Notes
|
|
||||||
* 2023-07-10 xqyjlj The first version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ktime.h"
|
|
||||||
|
|
||||||
static volatile unsigned long _init_cnt = 0;
|
|
||||||
|
|
||||||
rt_uint64_t rt_ktime_cputimer_getres(void)
|
|
||||||
{
|
|
||||||
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long rt_ktime_cputimer_getfrq(void)
|
|
||||||
{
|
|
||||||
return CPUTIME_TIMER_FREQ;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long rt_ktime_cputimer_getcnt(void)
|
|
||||||
{
|
|
||||||
unsigned long time_elapsed;
|
|
||||||
__asm__ __volatile__("rdtime %0" : "=r"(time_elapsed));
|
|
||||||
return time_elapsed - _init_cnt;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rt_ktime_cputimer_init(void)
|
|
||||||
{
|
|
||||||
__asm__ __volatile__("rdtime %0" : "=r"(_init_cnt));
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user