mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-21 10:36:04 +08:00
[Kernel] Add device ops feature.
This commit is contained in:
@@ -35,13 +35,13 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
|
||||
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;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
|
||||
{
|
||||
counter = timeout*timer->freq;
|
||||
devi = tv_sec - (counter/(float)timer->freq)*i;
|
||||
/* 计算最小误差 */
|
||||
/* Minimum calculation error */
|
||||
if (devi > devi_min)
|
||||
{
|
||||
i = index;
|
||||
@@ -89,7 +89,7 @@ static rt_err_t rt_hwtimer_init(struct rt_device *dev)
|
||||
rt_hwtimer_t *timer;
|
||||
|
||||
timer = (rt_hwtimer_t *)dev;
|
||||
/* 尝试将默认计数频率设为1Mhz */
|
||||
/* try to change to 1MHz */
|
||||
if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq))
|
||||
{
|
||||
timer->freq = 1000000;
|
||||
@@ -330,6 +330,18 @@ void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
|
||||
}
|
||||
}
|
||||
|
||||
#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;
|
||||
@@ -344,12 +356,16 @@ rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user