timer.c 14.9 KB
Newer Older
1 2 3
/*
 * File      : timer.c
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5 6 7
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-04-29     Bernard      implement thread timer
 * 2006-06-04     Bernard      implement rt_timer_control
 * 2006-08-10     Bernard      fix the periodic timer bug
 * 2006-09-03     Bernard      implement rt_timer_detach
17
 * 2009-11-11     LiJin        add soft timer
18
 * 2010-05-12     Bernard      fix the timer check bug.
19
 * 2010-11-02     Charlie      re-implement tick overflow issue
20
 * 2012-12-15     Bernard      fix the next timeout issue in soft timer
21 22 23 24 25
 */

#include <rtthread.h>
#include <rthw.h>

26
/* hard timer list */
27
static rt_list_t rt_timer_list = RT_LIST_OBJECT_INIT(rt_timer_list);
28

29
#ifdef RT_USING_TIMER_SOFT
30
#ifndef RT_TIMER_THREAD_STACK_SIZE
D
dzzxzz@gmail.com 已提交
31
#define RT_TIMER_THREAD_STACK_SIZE     512
32 33 34
#endif

#ifndef RT_TIMER_THREAD_PRIO
D
dzzxzz@gmail.com 已提交
35
#define RT_TIMER_THREAD_PRIO           0
36 37
#endif

38
/* soft timer list */
39
static rt_list_t rt_soft_timer_list;
40 41 42
static struct rt_thread timer_thread;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
43
#endif
44

45
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
46 47 48
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
49 50 51 52

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
53

54 55 56 57 58
/*@{*/

/**
 * This function will set a hook function, which will be invoked when timer
 * is timeout.
59
 *
60 61
 * @param hook the hook function
 */
D
dzzxzz 已提交
62
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
63
{
64
    rt_timer_timeout_hook = hook;
65 66 67 68 69
}

/*@}*/
#endif

70
static void _rt_timer_init(rt_timer_t timer,
D
dzzxzz@gmail.com 已提交
71 72 73 74
                           void (*timeout)(void *parameter),
                           void      *parameter,
                           rt_tick_t  time,
                           rt_uint8_t flag)
75
{
76 77
    /* set flag */
    timer->parent.flag  = flag;
78

79 80
    /* set deactivated */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
81

82 83
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
84

85 86
    timer->timeout_tick = 0;
    timer->init_tick    = time;
87

88 89
    /* initialize timer list */
    rt_list_init(&(timer->list));
90 91
}

D
dzzxzz@gmail.com 已提交
92
static rt_tick_t rt_timer_list_next_timeout(rt_list_t *timer_list)
93
{
94
    struct rt_timer *timer;
D
dzzxzz@gmail.com 已提交
95

96 97 98 99
    if (rt_list_isempty(timer_list))
        return RT_TICK_MAX;
    
    timer = rt_list_entry(timer_list->next, struct rt_timer, list);
D
dzzxzz@gmail.com 已提交
100

101
    return timer->timeout_tick;
102 103
}

104 105 106
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
107

108 109 110
/*@{*/

/**
111 112
 * This function will initialize a timer, normally this function is used to
 * initialize a static timer object.
113 114 115 116 117 118 119 120
 *
 * @param timer the static timer object
 * @param name the name of timer
 * @param timeout the timeout function
 * @param parameter the parameter of timeout function
 * @param time the tick of timer
 * @param flag the flag of timer
 */
D
dzzxzz@gmail.com 已提交
121
void rt_timer_init(rt_timer_t  timer,
122
                   const char *name,
D
dzzxzz@gmail.com 已提交
123 124 125 126
                   void (*timeout)(void *parameter),
                   void       *parameter,
                   rt_tick_t   time,
                   rt_uint8_t  flag)
127
{
128 129
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
130

131 132
    /* timer object initialization */
    rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
133

134
    _rt_timer_init(timer, timeout, parameter, time, flag);
135
}
136
RTM_EXPORT(rt_timer_init);
137

138
/**
139 140 141
 * This function will detach a timer from timer management.
 *
 * @param timer the static timer object
142
 *
143 144 145 146
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
147
    register rt_base_t level;
148

149 150
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
151

152 153
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
154

155 156
    /* remove it from timer list */
    rt_list_remove(&(timer->list));
157

158 159
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
160

161
    rt_object_detach((rt_object_t)timer);
162

163
    return -RT_EOK;
164
}
165
RTM_EXPORT(rt_timer_detach);
166 167 168 169 170 171 172 173 174 175 176 177 178

#ifdef RT_USING_HEAP
/**
 * This function will create a timer
 *
 * @param name the name of timer
 * @param timeout the timeout function
 * @param parameter the parameter of timeout function
 * @param time the tick of timer
 * @param flag the flag of timer
 *
 * @return the created timer object
 */
D
dzzxzz@gmail.com 已提交
179 180 181 182 183
rt_timer_t rt_timer_create(const char *name,
                           void (*timeout)(void *parameter),
                           void       *parameter,
                           rt_tick_t   time,
                           rt_uint8_t  flag)
184
{
185
    struct rt_timer *timer;
186

187 188 189 190 191 192
    /* allocate a object */
    timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
    if (timer == RT_NULL)
    {
        return RT_NULL;
    }
193

194
    _rt_timer_init(timer, timeout, parameter, time, flag);
195

196
    return timer;
197
}
198
RTM_EXPORT(rt_timer_create);
199 200 201 202 203 204 205 206 207 208

/**
 * This function will delete a timer and release timer memory
 *
 * @param timer the timer to be deleted
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_delete(rt_timer_t timer)
{
209
    register rt_base_t level;
210

211 212
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
213

214 215
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
216

217 218
    /* remove it from timer list */
    rt_list_remove(&(timer->list));
219

220 221
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
222

223
    rt_object_delete((rt_object_t)timer);
224

225
    return -RT_EOK;
226
}
227
RTM_EXPORT(rt_timer_delete);
228 229 230 231 232 233 234
#endif

/**
 * This function will start the timer
 *
 * @param timer the timer to be started
 *
B
bernard.xiong@gmail.com 已提交
235
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
236 237 238
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
239 240 241
    struct rt_timer *t;
    register rt_base_t level;
    rt_list_t *n, *timer_list;
242

243 244 245 246
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
    if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
        return -RT_ERROR;
247

248
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
249

D
dzzxzz@gmail.com 已提交
250 251 252 253 254
    /*
     * get timeout tick,
     * the max timeout tick shall not great than RT_TICK_MAX/2
     */
    RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
255
    timer->timeout_tick = rt_tick_get() + timer->init_tick;
256

257 258
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
259

260
#ifdef RT_USING_TIMER_SOFT
261 262 263 264 265 266
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* insert timer to soft timer list */
        timer_list = &rt_soft_timer_list;
    }
    else
267
#endif
268 269 270 271 272 273 274 275
    {
        /* insert timer to system timer list */
        timer_list = &rt_timer_list;
    }

    for (n = timer_list->next; n != timer_list; n = n->next)
    {
        t = rt_list_entry(n, struct rt_timer, list);
G
Grissiom 已提交
276

277 278
        /*
         * It supposes that the new tick shall less than the half duration of
G
Grissiom 已提交
279 280
         * tick max. And if we have two timers that timeout at the same time,
         * it's prefered that the timer inserted early get called early.
281
         */
G
Grissiom 已提交
282 283 284 285 286 287
        if ((t->timeout_tick - timer->timeout_tick) == 0)
        {
            rt_list_insert_after(n, &(timer->list));
            break;
        }
        else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        {
            rt_list_insert_before(n, &(timer->list));
            break;
        }
    }
    /* no found suitable position in timer list */
    if (n == timer_list)
    {
        rt_list_insert_before(n, &(timer->list));
    }

    timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

    /* enable interrupt */
    rt_hw_interrupt_enable(level);
303

304
#ifdef RT_USING_TIMER_SOFT
305 306 307 308 309 310 311 312 313 314
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* check whether timer thread is ready */
        if (timer_thread.stat != RT_THREAD_READY)
        {
            /* resume timer thread to check soft timer */
            rt_thread_resume(&timer_thread);
            rt_schedule();
        }
    }
315 316
#endif

317
    return -RT_EOK;
318
}
319
RTM_EXPORT(rt_timer_start);
320 321 322 323 324 325

/**
 * This function will stop the timer
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
326
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
327 328 329
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
330
    register rt_base_t level;
331

332 333 334 335
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
    if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
        return -RT_ERROR;
336

337
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
338

339 340
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
341

342 343
    /* remove it from timer list */
    rt_list_remove(&(timer->list));
344

345 346
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
347

348 349
    /* change stat */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
350

351
    return RT_EOK;
352
}
353
RTM_EXPORT(rt_timer_stop);
354 355 356 357 358 359 360 361

/**
 * This function will get or set some options of the timer
 *
 * @param timer the timer to be get or set
 * @param cmd the control command
 * @param arg the argument
 *
B
bernard.xiong@gmail.com 已提交
362
 * @return RT_EOK
363
 */
D
dzzxzz 已提交
364
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
365
{
366 367
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
368

369 370 371 372 373
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(rt_tick_t *)arg = timer->init_tick;
        break;
374

375 376 377
    case RT_TIMER_CTRL_SET_TIME:
        timer->init_tick = *(rt_tick_t *)arg;
        break;
378

379 380 381
    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;
382

383 384 385 386
    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
        break;
    }
387

388
    return RT_EOK;
389
}
390
RTM_EXPORT(rt_timer_control);
391 392

/**
393
 * This function will check timer list, if a timeout event happens, the
394 395
 * corresponding timeout function will be invoked.
 *
B
bernard.xiong@gmail.com 已提交
396
 * @note this function shall be invoked in operating system timer interrupt.
397
 */
398
void rt_timer_check(void)
399
{
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    struct rt_timer *t;
    rt_tick_t current_tick;
    register rt_base_t level;

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));

    current_tick = rt_tick_get();

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    while (!rt_list_isempty(&rt_timer_list))
    {
        t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
        {
            RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));

            /* remove timer from timer list firstly */
            rt_list_remove(&(t->list));

            /* call timeout function */
            t->timeout_func(t->parameter);

            /* re-get tick */
            current_tick = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
435
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
            else
            {
                /* stop timer */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
        }
        else
            break;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
455 456
}

457 458 459 460 461 462
/**
 * This function will return the next timeout tick in the system.
 *
 * @return the next timeout tick in the system
 */
rt_tick_t rt_timer_next_timeout_tick(void)
463
{
464
    return rt_timer_list_next_timeout(&rt_timer_list);
465 466
}

467
#ifdef RT_USING_TIMER_SOFT
468 469 470 471
/**
 * This function will check timer list, if a timeout event happens, the
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
472
void rt_soft_timer_check(void)
473
{
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    rt_tick_t current_tick;
    rt_list_t *n;
    struct rt_timer *t;

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));

    current_tick = rt_tick_get();

    for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
    {
        t = rt_list_entry(n, struct rt_timer, list);

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
D
dzzxzz@gmail.com 已提交
490
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
        {
            RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));

            /* move node to the next */
            n = n->next;

            /* remove timer from timer list firstly */
            rt_list_remove(&(t->list));

            /* call timeout function */
            t->timeout_func(t->parameter);

            /* re-get tick */
            current_tick = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
509
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
            else
            {
                /* stop timer */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
        }
        else break; /* not check anymore */
    }

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
525 526
}

527
/* system timer thread entry */
D
dzzxzz 已提交
528
static void rt_thread_timer_entry(void *parameter)
529
{
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    rt_tick_t next_timeout;
    
    while (1)
    {
        /* get the next timeout tick */
        next_timeout = rt_timer_list_next_timeout(&rt_soft_timer_list);
        if (next_timeout == RT_TICK_MAX)
        {
            /* no software timer exist, suspend self. */
            rt_thread_suspend(rt_thread_self());
            rt_schedule();
        }
        else
        {
            rt_tick_t current_tick;

            /* get current tick */
            current_tick = rt_tick_get();

            if ((next_timeout - current_tick) < RT_TICK_MAX/2)
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay(next_timeout);
            }
        }

        /* lock scheduler */
        rt_enter_critical();
        /* check software timer */
        rt_soft_timer_check();
        /* unlock scheduler */
        rt_exit_critical();
    }
564
}
565
#endif
566

567 568 569
/**
 * @ingroup SystemInit
 *
570
 * This function will initialize system timer
571 572 573
 *
 * @deprecated since 1.1.0, this function does not need to be invoked
 * in the system initialization.
574
 */
D
dzzxzz 已提交
575
void rt_system_timer_init(void)
576
{
B
bernard.xiong 已提交
577
}
578

B
bernard.xiong 已提交
579 580 581
/**
 * @ingroup SystemInit
 *
582
 * This function will initialize system timer thread
B
bernard.xiong 已提交
583
 */
D
dzzxzz 已提交
584
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
585 586
{
#ifdef RT_USING_TIMER_SOFT
587
    rt_list_init(&rt_soft_timer_list);
588

589 590 591
    /* start software timer thread */
    rt_thread_init(&timer_thread,
                   "timer",
D
dzzxzz@gmail.com 已提交
592 593 594 595 596 597
                   rt_thread_timer_entry,
                   RT_NULL,
                   &timer_thread_stack[0],
                   sizeof(timer_thread_stack),
                   RT_TIMER_THREAD_PRIO,
                   10);
598

599 600
    /* startup */
    rt_thread_startup(&timer_thread);
601
#endif
602
}
603

604
/*@}*/