Files
mr-library/device/timer.c

252 lines
7.5 KiB
C
Raw Normal View History

/*
2024-01-02 00:02:48 +08:00
* @copyright (c) 2023-2024, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-15 MacRsh First version
*/
2024-01-02 00:02:48 +08:00
#include "include/device/mr_timer.h"
#ifdef MR_USING_TIMER
2023-11-29 15:17:02 +08:00
static int timer_calculate(struct mr_timer *timer, uint32_t timeout)
{
uint32_t clk = timer->info->clk, psc_max = timer->info->prescaler_max, per_max = timer->info
->period_max;
uint32_t psc_best = 1, per_best = 1, reload_best = 1;
2023-11-29 15:17:02 +08:00
if ((clk == 0) || (timeout == 0)) {
2023-11-29 15:17:02 +08:00
return MR_EINVAL;
}
/* Take the timeout as the product of the prescaler and period */
uint32_t product = timeout;
2023-11-29 15:17:02 +08:00
/* If the product is within the maximum period, set it as the period */
if (product <= per_max) {
psc_best = clk / 1000000;
per_best = MR_BOUND(product, 1, per_max);
} else {
int error_min = INT32_MAX;
/* Calculate the least error prescaler and period */
for (uint32_t psc = MR_BOUND((product / per_max), 1, product); psc < UINT32_MAX; psc++) {
uint32_t per = MR_BOUND(product / psc, 1, per_max);
int error = (int)(timeout - (per * psc));
/* Found a valid and optimal solution */
if (error <= 1) {
psc_best = psc;
per_best = per;
2023-11-29 15:17:02 +08:00
break;
} else if (error < error_min) {
error_min = error;
psc_best = psc;
per_best = per;
2023-11-29 15:17:02 +08:00
}
}
/* Calculate the reload and prescaler product */
product = psc_best * (clk / 1000000);
error_min = INT32_MAX;
/* Calculate the least error reload and prescaler */
for (uint32_t reload = MR_BOUND(product / psc_max, 1, product);
reload < product;
reload++) {
uint32_t psc = MR_BOUND(product / reload, 1, psc_max);
int error = (int)product - (int)(reload * psc);
2023-11-29 15:17:02 +08:00
/* Found a valid and optimal solution */
if (error <= 1) {
reload_best = reload;
psc_best = psc;
break;
} else if (error < error_min) {
error_min = error;
reload_best = reload;
psc_best = psc;
2023-11-29 15:17:02 +08:00
}
}
/* If period can take reload value, lower interrupts by loading reload to period */
if (per_best <= (per_max / reload_best)) {
per_best *= reload_best;
reload_best = 1;
/* If the reload is less than the prescaler, swap them */
} else if ((reload_best > per_best) && (reload_best < per_max)) {
MR_SWAP(reload_best, per_best);
}
2023-11-29 15:17:02 +08:00
}
timer->prescaler = psc_best;
timer->period = per_best;
timer->reload = reload_best;
timer->count = timer->reload;
timer->timeout = timeout / timer->reload;
2023-11-29 15:17:02 +08:00
return MR_EOK;
}
static int mr_timer_open(struct mr_dev *dev)
{
struct mr_timer *timer = (struct mr_timer *)dev;
struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops;
return ops->configure(timer, MR_ENABLE);
}
static int mr_timer_close(struct mr_dev *dev)
{
struct mr_timer *timer = (struct mr_timer *)dev;
struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops;
return ops->configure(timer, MR_DISABLE);
}
static ssize_t mr_timer_read(struct mr_dev *dev, void *buf, size_t count)
2023-11-25 23:37:24 +08:00
{
struct mr_timer *timer = (struct mr_timer *)dev;
struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops;
uint32_t *rd_buf = (uint32_t *)buf;
2023-12-31 16:32:01 +08:00
ssize_t rd_size;
2023-11-25 23:37:24 +08:00
for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) {
uint32_t cnt = ops->get_count(timer);
2023-11-25 23:37:24 +08:00
2023-11-30 22:13:07 +08:00
*rd_buf = (timer->reload - timer->count) * timer->timeout +
(uint32_t)(((float)cnt / (float)timer->period) * (float)timer->timeout);
2023-11-29 15:17:02 +08:00
rd_buf++;
2023-11-25 23:37:24 +08:00
}
2023-11-29 15:17:02 +08:00
return rd_size;
2023-11-25 23:37:24 +08:00
}
static ssize_t mr_timer_write(struct mr_dev *dev, const void *buf, size_t count)
{
struct mr_timer *timer = (struct mr_timer *)dev;
struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops;
uint32_t *wr_buf = (uint32_t *)buf;
2023-11-29 15:17:02 +08:00
uint32_t timeout = 0;
2023-12-31 16:32:01 +08:00
ssize_t wr_size;
2023-11-29 15:17:02 +08:00
/* Only the last write is valid */
for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) {
2023-11-29 15:17:02 +08:00
timeout = *wr_buf;
wr_buf++;
}
2023-11-29 15:17:02 +08:00
/* Reset timer */
ops->stop(timer);
timer->count = timer->reload;
if (timeout != 0) {
2023-11-29 15:17:02 +08:00
/* Calculate prescaler and period */
int ret = timer_calculate(timer, timeout);
if (ret < 0) {
2023-11-29 15:17:02 +08:00
return ret;
2023-11-25 23:37:24 +08:00
}
2023-11-25 23:37:24 +08:00
/* Start timer */
ops->start(timer, timer->prescaler, timer->period);
}
return wr_size;
}
static int mr_timer_ioctl(struct mr_dev *dev, int cmd, void *args)
{
struct mr_timer *timer = (struct mr_timer *)dev;
switch (cmd) {
case MR_IOC_TIMER_SET_MODE: {
if (args != MR_NULL) {
2023-11-29 15:17:02 +08:00
struct mr_timer_config config = *(struct mr_timer_config *)args;
2023-11-29 15:17:02 +08:00
timer->config = config;
2024-01-16 04:03:40 +08:00
return sizeof(config);
}
return MR_EINVAL;
}
case MR_IOC_TIMER_GET_MODE: {
if (args != MR_NULL) {
2023-11-29 15:17:02 +08:00
struct mr_timer_config *config = (struct mr_timer_config *)args;
2023-11-25 23:37:24 +08:00
2023-11-29 15:17:02 +08:00
*config = timer->config;
2024-01-16 04:03:40 +08:00
return sizeof(*config);
2023-11-25 23:37:24 +08:00
}
return MR_EINVAL;
}
default: {
2023-11-29 15:17:02 +08:00
return MR_ENOTSUP;
}
}
}
static ssize_t mr_timer_isr(struct mr_dev *dev, int event, void *args)
{
struct mr_timer *timer = (struct mr_timer *)dev;
struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops;
switch (event) {
case MR_ISR_TIMER_TIMEOUT_INT: {
2023-11-29 15:17:02 +08:00
timer->count--;
if (timer->count == 0) {
2023-11-29 15:17:02 +08:00
timer->count = timer->reload;
if (timer->config.mode == MR_TIMER_MODE_ONESHOT) {
2023-11-29 15:17:02 +08:00
ops->stop(timer);
2023-11-25 23:37:24 +08:00
}
return MR_EOK;
}
2023-11-29 15:17:02 +08:00
return MR_EBUSY;
}
default: {
return MR_ENOTSUP;
}
}
}
2023-11-29 15:17:02 +08:00
/**
* @brief This function register a timer.
*
* @param timer The timer.
* @param path The path of the timer.
2023-11-29 15:17:02 +08:00
* @param drv The driver of the timer.
* @param info The information of the timer.
*
* @return 0 on success, otherwise an error code.
2023-11-29 15:17:02 +08:00
*/
int mr_timer_register(struct mr_timer *timer,
const char *path,
struct mr_drv *drv,
struct mr_timer_info *info)
{
static struct mr_dev_ops ops = {mr_timer_open,
mr_timer_close,
mr_timer_read,
mr_timer_write,
mr_timer_ioctl,
mr_timer_isr};
struct mr_timer_config default_config = MR_TIMER_CONFIG_DEFAULT;
MR_ASSERT(timer != MR_NULL);
MR_ASSERT(path != MR_NULL);
MR_ASSERT(drv != MR_NULL);
MR_ASSERT(drv->ops != MR_NULL);
MR_ASSERT(info != MR_NULL);
/* Initialize the fields */
timer->config = default_config;
2023-11-29 15:17:02 +08:00
timer->reload = 0;
timer->count = 0;
timer->timeout = 0;
timer->period = 0;
timer->prescaler = 0;
timer->info = info;
2023-12-27 23:47:57 +08:00
/* Register the timer */
return mr_dev_register(&timer->dev, path, MR_DEV_TYPE_TIMER, MR_O_RDWR, &ops, drv);
}
#endif /* MR_USING_TIMER */