hwtimer.c 8.4 KB
Newer Older
H
heyuanjie87 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
H
heyuanjie87 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
H
heyuanjie87 已提交
5 6 7 8 9 10 11
 *
 * Change Logs:
 * Date           Author         Notes
 * 2015-08-31     heyuanjie87    first version
 */

#include <rtdevice.h>
12
#include <rthw.h>
H
heyuanjie87 已提交
13

14 15 16 17
#define DBG_TAG "hwtimer"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

H
heyuanjie87 已提交
18
rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
H
heyuanjie87 已提交
19 20 21 22
{
    float overflow;
    float timeout;
    rt_uint32_t counter;
23
    int i, index = 0;
H
heyuanjie87 已提交
24 25 26 27
    float tv_sec;
    float devi_min = 1;
    float devi;

B
Bernard Xiong 已提交
28
    /* changed to second */
H
heyuanjie87 已提交
29 30 31 32 33
    overflow = timer->info->maxcnt/(float)timer->freq;
    tv_sec = tv->sec + tv->usec/(float)1000000;

    if (tv_sec < (1/(float)timer->freq))
    {
B
Bernard Xiong 已提交
34
        /* little timeout */
H
heyuanjie87 已提交
35 36 37 38 39 40 41 42 43 44 45
        i = 0;
        timeout = 1/(float)timer->freq;
    }
    else
    {
        for (i = 1; i > 0; i ++)
        {
            timeout = tv_sec/i;

            if (timeout <= overflow)
            {
46 47
                counter = (rt_uint32_t)(timeout * timer->freq);
                devi = tv_sec - (counter / (float)timer->freq) * i;
B
Bernard Xiong 已提交
48
                /* Minimum calculation error */
H
heyuanjie87 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
                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;
71
    counter = (rt_uint32_t)(timeout * timer->freq);
H
heyuanjie87 已提交
72

H
heyuanjie87 已提交
73
    return counter;
H
heyuanjie87 已提交
74 75 76 77 78 79 80 81
}

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;
B
Bernard Xiong 已提交
82
    /* try to change to 1MHz */
H
heyuanjie87 已提交
83 84 85 86 87 88 89 90 91 92
    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;
H
heyuanjie87 已提交
93
    timer->overflow = 0;
H
heyuanjie87 已提交
94 95 96

    if (timer->ops->init)
    {
H
heyuanjie87 已提交
97
        timer->ops->init(timer, 1);
H
heyuanjie87 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
    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;
H
heyuanjie87 已提交
131
    if (timer->ops->init != RT_NULL)
H
heyuanjie87 已提交
132
    {
H
heyuanjie87 已提交
133
        timer->ops->init(timer, 0);
H
heyuanjie87 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }
    else
    {
        result = -RT_ENOSYS;
    }

    dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
    dev->rx_indicate = RT_NULL;

    return result;
}

static rt_size_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;
151 152
    rt_base_t level;
    rt_int32_t overflow;
H
heyuanjie87 已提交
153 154 155 156 157 158
    float t;

    timer = (rt_hwtimer_t *)dev;
    if (timer->ops->count_get == RT_NULL)
        return 0;

159
    level = rt_hw_interrupt_disable();
H
heyuanjie87 已提交
160
    cnt = timer->ops->count_get(timer);
161 162 163
    overflow = timer->overflow;
    rt_hw_interrupt_enable(level);

H
heyuanjie87 已提交
164 165
    if (timer->info->cntmode == HWTIMER_CNTMODE_DW)
    {
166
        cnt = (rt_uint32_t)(timer->freq * timer->period_sec) - cnt;
H
heyuanjie87 已提交
167 168
    }

169
    t = overflow * timer->period_sec + cnt/(float)timer->freq;
170 171
    tv.sec = (rt_int32_t)t;
    tv.usec = (rt_int32_t)((t - tv.sec) * 1000000);
H
heyuanjie87 已提交
172 173 174 175 176 177 178 179
    size = size > sizeof(tv)? sizeof(tv) : size;
    rt_memcpy(buffer, &tv, size);

    return size;
}

static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
180
    rt_base_t level;
H
heyuanjie87 已提交
181 182 183 184 185 186 187 188 189 190
    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;
H
heyuanjie87 已提交
191

192
    timer->ops->stop(timer);
193 194

    level = rt_hw_interrupt_disable();
195
    timer->overflow = 0;
196
    rt_hw_interrupt_enable(level);
197 198

    t = timeout_calc(timer, (rt_hwtimerval_t*)buffer);
H
heyuanjie87 已提交
199
    if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
H
heyuanjie87 已提交
200
    {
H
heyuanjie87 已提交
201
        opm = HWTIMER_MODE_ONESHOT;
H
heyuanjie87 已提交
202 203
    }

H
heyuanjie87 已提交
204 205 206 207
    if (timer->ops->start(timer, t, opm) != RT_EOK)
        size = 0;

    return size;
H
heyuanjie87 已提交
208 209
}

B
bernard 已提交
210
static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
H
heyuanjie87 已提交
211
{
212
    rt_base_t level;
H
heyuanjie87 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    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_uint32_t *f;

        if (args == RT_NULL)
        {
            result = -RT_EEMPTY;
            break;
        }

        f = (rt_uint32_t*)args;
        if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
        {
245 246
            LOG_W("frequency setting out of range! It will maintain at %d Hz", timer->freq);
            result = -RT_EINVAL;
H
heyuanjie87 已提交
247 248 249 250 251 252 253 254
            break;
        }

        if (timer->ops->control != RT_NULL)
        {
            result = timer->ops->control(timer, cmd, args);
            if (result == RT_EOK)
            {
255
                level = rt_hw_interrupt_disable();
H
heyuanjie87 已提交
256
                timer->freq = *f;
257
                rt_hw_interrupt_enable(level);
H
heyuanjie87 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
            }
        }
        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;
    }
276
    break;
H
heyuanjie87 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    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;
        }
294
        level = rt_hw_interrupt_disable();
H
heyuanjie87 已提交
295
        timer->mode = *m;
296
        rt_hw_interrupt_enable(level);
H
heyuanjie87 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310
    }
    break;
    default:
    {
        result = -RT_ENOSYS;
    }
    break;
    }

    return result;
}

void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
{
311 312
    rt_base_t level;

H
heyuanjie87 已提交
313 314
    RT_ASSERT(timer != RT_NULL);

315 316
    level = rt_hw_interrupt_disable();

H
heyuanjie87 已提交
317 318 319 320 321 322 323 324 325 326 327
    timer->overflow ++;

    if (timer->cycles != 0)
    {
        timer->cycles --;
    }

    if (timer->cycles == 0)
    {
        timer->cycles = timer->reload;

328 329
        rt_hw_interrupt_enable(level);

H
heyuanjie87 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342
        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));
        }
    }
343 344 345 346
    else
    {
        rt_hw_interrupt_enable(level);
    }
H
heyuanjie87 已提交
347 348
}

B
Bernard Xiong 已提交
349
#ifdef RT_USING_DEVICE_OPS
350
const static struct rt_device_ops hwtimer_ops =
B
Bernard Xiong 已提交
351 352 353 354 355 356 357 358 359 360
{
    rt_hwtimer_init,
    rt_hwtimer_open,
    rt_hwtimer_close,
    rt_hwtimer_read,
    rt_hwtimer_write,
    rt_hwtimer_control
};
#endif

H
heyuanjie87 已提交
361 362 363 364 365 366 367 368 369 370
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);

H
heyuanjie87 已提交
371
    device->type        = RT_Device_Class_Timer;
H
heyuanjie87 已提交
372 373 374
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

B
Bernard Xiong 已提交
375 376 377
#ifdef RT_USING_DEVICE_OPS
    device->ops         = &hwtimer_ops;
#else
H
heyuanjie87 已提交
378 379 380 381 382 383
    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;
B
Bernard Xiong 已提交
384
#endif
H
heyuanjie87 已提交
385 386 387 388
    device->user_data   = user_data;

    return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}