2015-09-02 22:00:24 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2015-09-02 22:00:24 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-09-02 22:00:24 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2015-08-31 heyuanjie87 first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtdevice.h>
|
2020-11-21 17:44:52 +08:00
|
|
|
#include <rthw.h>
|
2015-09-02 22:00:24 +08:00
|
|
|
|
2022-05-09 22:59:27 +08:00
|
|
|
#define DBG_TAG "hwtimer"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2024-02-29 22:27:38 +08:00
|
|
|
#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 */
|
|
|
|
|
2015-09-06 15:05:47 +08:00
|
|
|
rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
|
|
|
float overflow;
|
|
|
|
float timeout;
|
|
|
|
rt_uint32_t counter;
|
2018-09-18 15:30:52 +08:00
|
|
|
int i, index = 0;
|
2015-09-02 22:00:24 +08:00
|
|
|
float tv_sec;
|
|
|
|
float devi_min = 1;
|
|
|
|
float devi;
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
/* changed to second */
|
2015-09-02 22:00:24 +08:00
|
|
|
overflow = timer->info->maxcnt/(float)timer->freq;
|
|
|
|
tv_sec = tv->sec + tv->usec/(float)1000000;
|
|
|
|
|
|
|
|
if (tv_sec < (1/(float)timer->freq))
|
|
|
|
{
|
2018-06-10 17:59:17 +08:00
|
|
|
/* little timeout */
|
2015-09-02 22:00:24 +08:00
|
|
|
i = 0;
|
|
|
|
timeout = 1/(float)timer->freq;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 1; i > 0; i ++)
|
|
|
|
{
|
|
|
|
timeout = tv_sec/i;
|
|
|
|
|
|
|
|
if (timeout <= overflow)
|
|
|
|
{
|
2022-04-17 21:39:26 +08:00
|
|
|
counter = (rt_uint32_t)(timeout * timer->freq);
|
|
|
|
devi = tv_sec - (counter / (float)timer->freq) * i;
|
2018-06-10 17:59:17 +08:00
|
|
|
/* Minimum calculation error */
|
2015-09-02 22:00:24 +08:00
|
|
|
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;
|
2022-04-17 21:39:26 +08:00
|
|
|
counter = (rt_uint32_t)(timeout * timer->freq);
|
2015-09-02 22:00:24 +08:00
|
|
|
|
2015-09-06 15:05:47 +08:00
|
|
|
return counter;
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-06-10 17:59:17 +08:00
|
|
|
/* try to change to 1MHz */
|
2015-09-02 22:00:24 +08:00
|
|
|
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;
|
2015-09-06 15:05:47 +08:00
|
|
|
timer->overflow = 0;
|
2015-09-02 22:00:24 +08:00
|
|
|
|
|
|
|
if (timer->ops->init)
|
|
|
|
{
|
2015-09-06 15:05:47 +08:00
|
|
|
timer->ops->init(timer, 1);
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
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;
|
2015-09-06 15:05:47 +08:00
|
|
|
if (timer->ops->init != RT_NULL)
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
2015-09-06 15:05:47 +08:00
|
|
|
timer->ops->init(timer, 0);
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
|
|
|
|
dev->rx_indicate = RT_NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
|
|
|
rt_hwtimer_t *timer;
|
|
|
|
rt_hwtimerval_t tv;
|
|
|
|
rt_uint32_t cnt;
|
2020-11-21 17:44:52 +08:00
|
|
|
rt_base_t level;
|
|
|
|
rt_int32_t overflow;
|
2015-09-02 22:00:24 +08:00
|
|
|
float t;
|
|
|
|
|
|
|
|
timer = (rt_hwtimer_t *)dev;
|
|
|
|
if (timer->ops->count_get == RT_NULL)
|
|
|
|
return 0;
|
|
|
|
|
2020-11-21 17:44:52 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2015-09-02 22:00:24 +08:00
|
|
|
cnt = timer->ops->count_get(timer);
|
2020-11-21 17:44:52 +08:00
|
|
|
overflow = timer->overflow;
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
if (timer->info->cntmode == HWTIMER_CNTMODE_DW)
|
|
|
|
{
|
2022-04-17 21:39:26 +08:00
|
|
|
cnt = (rt_uint32_t)(timer->freq * timer->period_sec) - cnt;
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
2023-07-18 10:52:56 +08:00
|
|
|
if (timer->mode == HWTIMER_MODE_ONESHOT)
|
|
|
|
{
|
|
|
|
overflow = 0;
|
|
|
|
}
|
2015-09-02 22:00:24 +08:00
|
|
|
|
2020-11-21 17:44:52 +08:00
|
|
|
t = overflow * timer->period_sec + cnt/(float)timer->freq;
|
2022-04-17 21:39:26 +08:00
|
|
|
tv.sec = (rt_int32_t)t;
|
|
|
|
tv.usec = (rt_int32_t)((t - tv.sec) * 1000000);
|
2015-09-02 22:00:24 +08:00
|
|
|
size = size > sizeof(tv)? sizeof(tv) : size;
|
|
|
|
rt_memcpy(buffer, &tv, size);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_base_t level;
|
2015-09-06 15:05:47 +08:00
|
|
|
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;
|
2015-09-02 22:00:24 +08:00
|
|
|
|
2018-03-06 10:38:35 +08:00
|
|
|
timer->ops->stop(timer);
|
2022-05-08 12:18:48 +08:00
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
2018-03-06 10:38:35 +08:00
|
|
|
timer->overflow = 0;
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2018-03-06 10:38:35 +08:00
|
|
|
|
|
|
|
t = timeout_calc(timer, (rt_hwtimerval_t*)buffer);
|
2015-09-06 15:05:47 +08:00
|
|
|
if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
2015-09-06 15:05:47 +08:00
|
|
|
opm = HWTIMER_MODE_ONESHOT;
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
|
2015-09-06 15:05:47 +08:00
|
|
|
if (timer->ops->start(timer, t, opm) != RT_EOK)
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
return size;
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
|
2015-09-02 22:00:24 +08:00
|
|
|
{
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_base_t level;
|
2015-09-02 22:00:24 +08:00
|
|
|
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:
|
|
|
|
{
|
2022-09-20 22:31:11 +08:00
|
|
|
rt_int32_t *f;
|
2015-09-02 22:00:24 +08:00
|
|
|
|
|
|
|
if (args == RT_NULL)
|
|
|
|
{
|
|
|
|
result = -RT_EEMPTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-20 22:31:11 +08:00
|
|
|
f = (rt_int32_t*)args;
|
2015-09-02 22:00:24 +08:00
|
|
|
if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
|
|
|
|
{
|
2022-05-09 22:59:27 +08:00
|
|
|
LOG_W("frequency setting out of range! It will maintain at %d Hz", timer->freq);
|
|
|
|
result = -RT_EINVAL;
|
2015-09-02 22:00:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->ops->control != RT_NULL)
|
|
|
|
{
|
|
|
|
result = timer->ops->control(timer, cmd, args);
|
|
|
|
if (result == RT_EOK)
|
|
|
|
{
|
2022-05-08 12:18:48 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2015-09-02 22:00:24 +08:00
|
|
|
timer->freq = *f;
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2020-05-15 09:46:01 +08:00
|
|
|
break;
|
2015-09-02 22:00:24 +08:00
|
|
|
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;
|
|
|
|
}
|
2022-05-08 12:18:48 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2015-09-02 22:00:24 +08:00
|
|
|
timer->mode = *m;
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
result = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
|
|
|
|
{
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_base_t level;
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
2022-05-08 12:18:48 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
timer->overflow ++;
|
|
|
|
|
|
|
|
if (timer->cycles != 0)
|
|
|
|
{
|
|
|
|
timer->cycles --;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->cycles == 0)
|
|
|
|
{
|
|
|
|
timer->cycles = timer->reload;
|
|
|
|
|
2022-05-08 12:18:48 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:18:48 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2015-09-02 22:00:24 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2021-03-08 18:19:04 +08:00
|
|
|
const static struct rt_device_ops hwtimer_ops =
|
2018-06-10 17:59:17 +08:00
|
|
|
{
|
|
|
|
rt_hwtimer_init,
|
|
|
|
rt_hwtimer_open,
|
|
|
|
rt_hwtimer_close,
|
|
|
|
rt_hwtimer_read,
|
|
|
|
rt_hwtimer_write,
|
|
|
|
rt_hwtimer_control
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
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);
|
|
|
|
|
2015-09-06 15:05:47 +08:00
|
|
|
device->type = RT_Device_Class_Timer;
|
2015-09-02 22:00:24 +08:00
|
|
|
device->rx_indicate = RT_NULL;
|
|
|
|
device->tx_complete = RT_NULL;
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
device->ops = &hwtimer_ops;
|
|
|
|
#else
|
2015-09-02 22:00:24 +08:00
|
|
|
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;
|
2018-06-10 17:59:17 +08:00
|
|
|
#endif
|
2015-09-02 22:00:24 +08:00
|
|
|
device->user_data = user_data;
|
|
|
|
|
|
|
|
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
|
|
|
}
|