timer.c 24.2 KB
Newer Older
1
/*
S
Stanley 已提交
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12
 *
 * 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
13
 * 2009-11-11     LiJin        add soft timer
14
 * 2010-05-12     Bernard      fix the timer check bug.
15
 * 2010-11-02     Charlie      re-implement tick overflow issue
16
 * 2012-12-15     Bernard      fix the next timeout issue in soft timer
17
 * 2014-07-12     Bernard      does not lock scheduler when invoking soft-timer
18
 *                             timeout function.
Thomas_Fly's avatar
Thomas_Fly 已提交
19
 * 2021-08-15     supperthomas add the comment
20
 * 2022-01-07     Gabriel      Moving __on_rt_xxxxx_hook to timer.c
S
Stanley 已提交
21
 * 2022-04-19     Stanley      Correct descriptions
22 23 24 25 26
 */

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

27
/* hard timer list */
28
static rt_list_t _timer_list[RT_TIMER_SKIP_LIST_LEVEL];
29

30
#ifdef RT_USING_TIMER_SOFT
B
Bernard Xiong 已提交
31 32 33 34

#define RT_SOFT_TIMER_IDLE              1
#define RT_SOFT_TIMER_BUSY              0

35
#ifndef RT_TIMER_THREAD_STACK_SIZE
D
dzzxzz@gmail.com 已提交
36
#define RT_TIMER_THREAD_STACK_SIZE     512
37
#endif /* RT_TIMER_THREAD_STACK_SIZE */
38 39

#ifndef RT_TIMER_THREAD_PRIO
D
dzzxzz@gmail.com 已提交
40
#define RT_TIMER_THREAD_PRIO           0
41
#endif /* RT_TIMER_THREAD_PRIO */
42

B
Bernard Xiong 已提交
43
/* soft timer status */
44
static rt_uint8_t _soft_timer_status = RT_SOFT_TIMER_IDLE;
45
/* soft timer list */
46 47
static rt_list_t _soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
static struct rt_thread _timer_thread;
48
rt_align(RT_ALIGN_SIZE)
49
static rt_uint8_t _timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
50
#endif /* RT_USING_TIMER_SOFT */
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65
#ifndef __on_rt_object_take_hook
    #define __on_rt_object_take_hook(parent)        __ON_HOOK_ARGS(rt_object_take_hook, (parent))
#endif
#ifndef __on_rt_object_put_hook
    #define __on_rt_object_put_hook(parent)         __ON_HOOK_ARGS(rt_object_put_hook, (parent))
#endif
#ifndef __on_rt_timer_enter_hook
    #define __on_rt_timer_enter_hook(t)             __ON_HOOK_ARGS(rt_timer_enter_hook, (t))
#endif
#ifndef __on_rt_timer_exit_hook
    #define __on_rt_timer_exit_hook(t)              __ON_HOOK_ARGS(rt_timer_exit_hook, (t))
#endif

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
D
dzzxzz 已提交
66 67
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
68 69
static void (*rt_timer_enter_hook)(struct rt_timer *timer);
static void (*rt_timer_exit_hook)(struct rt_timer *timer);
70 71 72 73

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

D
dogandog 已提交
75
/**@{*/
76 77

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
78
 * @brief This function will set a hook function on timer,
Thomas_Fly's avatar
Thomas_Fly 已提交
79
 *        which will be invoked when enter timer timeout callback function.
Thomas_Fly's avatar
Thomas_Fly 已提交
80
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
81
 * @param hook is the function point of timer
82
 */
83
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
84
{
85 86 87 88
    rt_timer_enter_hook = hook;
}

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
89
 * @brief This function will set a hook function, which will be
Thomas_Fly's avatar
Thomas_Fly 已提交
90
 *        invoked when exit timer timeout callback function.
Thomas_Fly's avatar
Thomas_Fly 已提交
91
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
92
 * @param hook is the function point of timer
93 94 95 96
 */
void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
{
    rt_timer_exit_hook = hook;
97 98
}

D
dogandog 已提交
99
/**@}*/
100
#endif /* RT_USING_HOOK */
101

Thomas_Fly's avatar
Thomas_Fly 已提交
102 103

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
104
 * @brief [internal] The init funtion of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
105
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
106
 *        The internal called function of rt_timer_init
Thomas_Fly's avatar
Thomas_Fly 已提交
107
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
108
 * @see rt_timer_init
Thomas_Fly's avatar
Thomas_Fly 已提交
109
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
110
 * @param timer is timer object
Thomas_Fly's avatar
Thomas_Fly 已提交
111
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
112
 * @param timeout is the timeout function
Thomas_Fly's avatar
Thomas_Fly 已提交
113
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
114
 * @param parameter is the parameter of timeout function
Thomas_Fly's avatar
Thomas_Fly 已提交
115
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
116
 * @param time is the tick of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
117
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
118
 * @param flag the flag of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
119
 */
120
static void _timer_init(rt_timer_t timer,
mysterywolf's avatar
mysterywolf 已提交
121 122 123 124
                        void (*timeout)(void *parameter),
                        void      *parameter,
                        rt_tick_t  time,
                        rt_uint8_t flag)
125
{
126 127
    int i;

128 129
    /* set flag */
    timer->parent.flag  = flag;
130

131 132
    /* set deactivated */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
133

134 135
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
136

137 138
    timer->timeout_tick = 0;
    timer->init_tick    = time;
139

140
    /* initialize timer list */
141 142 143 144
    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_init(&(timer->row[i]));
    }
145 146
}

Thomas_Fly's avatar
Thomas_Fly 已提交
147
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
148
 * @brief  Find the next emtpy timer ticks
Thomas_Fly's avatar
Thomas_Fly 已提交
149
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
150
 * @param timer_list is the array of time list
Thomas_Fly's avatar
Thomas_Fly 已提交
151
 *
152 153 154 155
 * @param timeout_tick is the next timer's ticks
 *
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
Thomas_Fly's avatar
Thomas_Fly 已提交
156
 */
157
static rt_err_t _timer_list_next_timeout(rt_list_t timer_list[], rt_tick_t *timeout_tick)
158
{
159
    struct rt_timer *timer;
160
    rt_base_t level;
D
dzzxzz@gmail.com 已提交
161

162 163
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
B
Bernard Xiong 已提交
164

165 166 167 168
    if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
    {
        timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                              struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
169 170 171 172 173 174
        *timeout_tick = timer->timeout_tick;

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        return RT_EOK;
175 176 177 178
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);
D
dzzxzz@gmail.com 已提交
179

180
    return -RT_ERROR;
181 182
}

Thomas_Fly's avatar
Thomas_Fly 已提交
183
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
184
 * @brief Remove the timer
Thomas_Fly's avatar
Thomas_Fly 已提交
185
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
186
 * @param timer the point of the timer
Thomas_Fly's avatar
Thomas_Fly 已提交
187
 */
188
rt_inline void _timer_remove(rt_timer_t timer)
189 190 191 192 193 194 195 196 197
{
    int i;

    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_remove(&timer->row[i]);
    }
}

G
Grissiom 已提交
198
#if RT_DEBUG_TIMER
Thomas_Fly's avatar
Thomas_Fly 已提交
199
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
200
 * @brief The number of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
201
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
202 203
 * @param timer the head of timer
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
204
 * @return count of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
205
 */
206
static int _timer_count_height(struct rt_timer *timer)
207 208 209 210 211 212 213 214 215 216
{
    int i, cnt = 0;

    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        if (!rt_list_isempty(&timer->row[i]))
            cnt++;
    }
    return cnt;
}
Thomas_Fly's avatar
Thomas_Fly 已提交
217 218
/**
 * @brief dump the all timer information
Thomas_Fly's avatar
Thomas_Fly 已提交
219
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
220 221
 * @param timer_heads the head of timer
 */
222 223 224 225
void rt_timer_dump(rt_list_t timer_heads[])
{
    rt_list_t *list;

226 227
    for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
         list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
228 229 230 231
         list = list->next)
    {
        struct rt_timer *timer = rt_list_entry(list,
                                               struct rt_timer,
232
                                               row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
233
        rt_kprintf("%d", _timer_count_height(timer));
234 235 236
    }
    rt_kprintf("\n");
}
237
#endif /* RT_DEBUG_TIMER */
238

239 240 241
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
242

D
dogandog 已提交
243
/**@{*/
244 245

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
246 247
 * @brief This function will initialize a timer
 *        normally this function is used to initialize a static timer object.
Thomas_Fly's avatar
Thomas_Fly 已提交
248
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
249
 * @param timer is the point of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
250
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
251
 * @param name is a pointer to the name of the timer
Thomas_Fly's avatar
Thomas_Fly 已提交
252
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
253
 * @param timeout is the callback of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
254
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
255
 * @param parameter is the param of the callback
Thomas_Fly's avatar
Thomas_Fly 已提交
256
 *
mysterywolf's avatar
mysterywolf 已提交
257 258 259
 * @param time is timeout ticks of timer
 *
 *             NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
Thomas_Fly's avatar
Thomas_Fly 已提交
260
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
261
 * @param flag is the flag of timer
mysterywolf's avatar
mysterywolf 已提交
262
 *
263
 */
D
dzzxzz@gmail.com 已提交
264
void rt_timer_init(rt_timer_t  timer,
265
                   const char *name,
D
dzzxzz@gmail.com 已提交
266 267 268 269
                   void (*timeout)(void *parameter),
                   void       *parameter,
                   rt_tick_t   time,
                   rt_uint8_t  flag)
270
{
mysterywolf's avatar
mysterywolf 已提交
271
    /* parameter check */
272
    RT_ASSERT(timer != RT_NULL);
mysterywolf's avatar
mysterywolf 已提交
273
    RT_ASSERT(timeout != RT_NULL);
274
    RT_ASSERT(time < RT_TICK_MAX / 2);
275

276
    /* timer object initialization */
277
    rt_object_init(&(timer->parent), RT_Object_Class_Timer, name);
278

279
    _timer_init(timer, timeout, parameter, time, flag);
280
}
281
RTM_EXPORT(rt_timer_init);
282

283
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
284
 * @brief This function will detach a timer from timer management.
Thomas_Fly's avatar
Thomas_Fly 已提交
285
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
286
 * @param timer is the timer to be detached
Thomas_Fly's avatar
Thomas_Fly 已提交
287
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
288
 * @return the status of detach
289 290 291
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
292
    rt_base_t level;
293

mysterywolf's avatar
mysterywolf 已提交
294
    /* parameter check */
295
    RT_ASSERT(timer != RT_NULL);
296 297
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent));
298

299 300
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
301

302
    _timer_remove(timer);
B
Bernard Xiong 已提交
303 304
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
305

306 307
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
308

309
    rt_object_detach(&(timer->parent));
310

311
    return RT_EOK;
312
}
313
RTM_EXPORT(rt_timer_detach);
314 315 316

#ifdef RT_USING_HEAP
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
317
 * @brief This function will create a timer
318
 *
mysterywolf's avatar
mysterywolf 已提交
319
 * @param name is the name of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
320
 *
mysterywolf's avatar
mysterywolf 已提交
321
 * @param timeout is the timeout function
Thomas_Fly's avatar
Thomas_Fly 已提交
322
 *
mysterywolf's avatar
mysterywolf 已提交
323
 * @param parameter is the parameter of timeout function
Thomas_Fly's avatar
Thomas_Fly 已提交
324
 *
mysterywolf's avatar
mysterywolf 已提交
325
 * @param time is timeout ticks of the timer
Thomas_Fly's avatar
Thomas_Fly 已提交
326
 *
S
Stanley 已提交
327
 *        NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
mysterywolf's avatar
mysterywolf 已提交
328
 *
S
Stanley 已提交
329 330 331 332 333 334 335 336 337 338
 * @param flag is the flag of timer. Timer will invoke the timeout function according to the selected values of flag, if one or more of the following flags is set.
 *
 *          RT_TIMER_FLAG_ONE_SHOT          One shot timing
 *          RT_TIMER_FLAG_PERIODIC          Periodic timing
 *
 *          RT_TIMER_FLAG_HARD_TIMER        Hardware timer
 *          RT_TIMER_FLAG_SOFT_TIMER        Software timer
 *
 *        NOTE:
 *        You can use multiple values with "|" logical operator.  By default, system will use the RT_TIME_FLAG_HARD_TIMER.
339
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
340
 * @return the created timer object
341
 */
D
dzzxzz@gmail.com 已提交
342 343 344 345 346
rt_timer_t rt_timer_create(const char *name,
                           void (*timeout)(void *parameter),
                           void       *parameter,
                           rt_tick_t   time,
                           rt_uint8_t  flag)
347
{
348
    struct rt_timer *timer;
349

mysterywolf's avatar
mysterywolf 已提交
350 351
    /* parameter check */
    RT_ASSERT(timeout != RT_NULL);
352
    RT_ASSERT(time < RT_TICK_MAX / 2);
mysterywolf's avatar
mysterywolf 已提交
353

354 355 356 357 358 359
    /* allocate a object */
    timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
    if (timer == RT_NULL)
    {
        return RT_NULL;
    }
360

361
    _timer_init(timer, timeout, parameter, time, flag);
362

363
    return timer;
364
}
365
RTM_EXPORT(rt_timer_create);
366 367

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
368
 * @brief This function will delete a timer and release timer memory
369
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
370
 * @param timer the timer to be deleted
371
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
372
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
373 374 375
 */
rt_err_t rt_timer_delete(rt_timer_t timer)
{
376
    rt_base_t level;
377

mysterywolf's avatar
mysterywolf 已提交
378
    /* parameter check */
379
    RT_ASSERT(timer != RT_NULL);
380 381
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
382

383 384
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
385

386
    _timer_remove(timer);
B
Bernard Xiong 已提交
387 388
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
389

390 391
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
392

393
    rt_object_delete(&(timer->parent));
394

395
    return RT_EOK;
396
}
397
RTM_EXPORT(rt_timer_delete);
398
#endif /* RT_USING_HEAP */
399 400

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
401
 * @brief This function will start the timer
402
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
403
 * @param timer the timer to be started
404
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
405
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
406 407 408
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
409
    unsigned int row_lvl;
410
    rt_list_t *timer_list;
411
    rt_base_t level;
412
    rt_bool_t need_schedule;
413 414 415
    rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
    unsigned int tst_nr;
    static unsigned int random_nr;
416

mysterywolf's avatar
mysterywolf 已提交
417
    /* parameter check */
418
    RT_ASSERT(timer != RT_NULL);
419
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
420

421 422
    need_schedule = RT_FALSE;

423 424 425
    /* stop timer firstly */
    level = rt_hw_interrupt_disable();
    /* remove timer from list */
426
    _timer_remove(timer);
427 428
    /* change status of timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
429

430
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
431

432
    timer->timeout_tick = rt_tick_get() + timer->init_tick;
433

434
#ifdef RT_USING_TIMER_SOFT
435 436 437
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* insert timer to soft timer list */
438
        timer_list = _soft_timer_list;
439 440
    }
    else
441
#endif /* RT_USING_TIMER_SOFT */
442 443
    {
        /* insert timer to system timer list */
444
        timer_list = _timer_list;
445 446
    }

447 448
    row_head[0]  = &timer_list[0];
    for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
449
    {
450
        for (; row_head[row_lvl] != timer_list[row_lvl].prev;
451
             row_head[row_lvl]  = row_head[row_lvl]->next)
452
        {
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
            struct rt_timer *t;
            rt_list_t *p = row_head[row_lvl]->next;

            /* fix up the entry pointer */
            t = rt_list_entry(p, struct rt_timer, row[row_lvl]);

            /* If we have two timers that timeout at the same time, it's
             * preferred that the timer inserted early get called early.
             * So insert the new timer to the end the the some-timeout timer
             * list.
             */
            if ((t->timeout_tick - timer->timeout_tick) == 0)
            {
                continue;
            }
            else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
            {
                break;
            }
472
        }
473
        if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
474
            row_head[row_lvl + 1] = row_head[row_lvl] + 1;
475
    }
476 477 478 479 480 481 482 483

    /* Interestingly, this super simple timer insert counter works very very
     * well on distributing the list height uniformly. By means of "very very
     * well", I mean it beats the randomness of timer->timeout_tick very easily
     * (actually, the timeout_tick is not random and easy to be attacked). */
    random_nr++;
    tst_nr = random_nr;

484 485
    rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
                         &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
486
    for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
487
    {
488 489 490 491 492 493 494
        if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
            rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
                                 &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
        else
            break;
        /* Shift over the bits we have tested. Works well with 1 bit and 2
         * bits. */
495
        tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
496 497 498 499
    }

    timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

500
#ifdef RT_USING_TIMER_SOFT
501 502 503
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* check whether timer thread is ready */
504
        if ((_soft_timer_status == RT_SOFT_TIMER_IDLE) &&
G
guo 已提交
505
           ((_timer_thread.stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK))
506 507
        {
            /* resume timer thread to check soft timer */
508
            rt_thread_resume(&_timer_thread);
509
            need_schedule = RT_TRUE;
510 511
        }
    }
512
#endif /* RT_USING_TIMER_SOFT */
513

514 515 516 517 518 519 520 521
    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    if (need_schedule)
    {
        rt_schedule();
    }

522
    return RT_EOK;
523
}
524
RTM_EXPORT(rt_timer_start);
525 526

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
527
 * @brief This function will stop the timer
528
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
529
 * @param timer the timer to be stopped
530
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
531
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
532 533 534
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
535
    rt_base_t level;
536

G
guo 已提交
537 538 539 540
    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    /* timer check */
541
    RT_ASSERT(timer != RT_NULL);
542 543
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);

544
    if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
G
guo 已提交
545 546
    {
        rt_hw_interrupt_enable(level);
547
        return -RT_ERROR;
G
guo 已提交
548
    }
549

550
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
551

552
    _timer_remove(timer);
B
Bernard Xiong 已提交
553 554
    /* change status */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
555

556 557
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
558

559
    return RT_EOK;
560
}
561
RTM_EXPORT(rt_timer_stop);
562 563

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
564
 * @brief This function will get or set some options of the timer
565
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
566 567 568
 * @param timer the timer to be get or set
 * @param cmd the control command
 * @param arg the argument
569
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
570
 * @return the statu of control
571
 */
B
bernard 已提交
572
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
573
{
574
    rt_base_t level;
B
Bernard Xiong 已提交
575

mysterywolf's avatar
mysterywolf 已提交
576
    /* parameter check */
577
    RT_ASSERT(timer != RT_NULL);
578
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
579

B
Bernard Xiong 已提交
580
    level = rt_hw_interrupt_disable();
581 582 583 584 585
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(rt_tick_t *)arg = timer->init_tick;
        break;
586

587
    case RT_TIMER_CTRL_SET_TIME:
588
        RT_ASSERT((*(rt_tick_t *)arg) < RT_TICK_MAX / 2);
589 590
        timer->init_tick = *(rt_tick_t *)arg;
        break;
591

592 593 594
    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;
595

596 597 598
    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
        break;
B
Bernard Xiong 已提交
599

600 601 602 603
    case RT_TIMER_CTRL_GET_STATE:
        if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
        {
            /*timer is start and run*/
604
            *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
605 606 607 608
        }
        else
        {
            /*timer is stop*/
609
            *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
610
        }
H
HubretXie 已提交
611 612
        break;

X
xiangxistu 已提交
613 614
    case RT_TIMER_CTRL_GET_REMAIN_TIME:
        *(rt_tick_t *)arg =  timer->timeout_tick;
C
chenchaoqun 已提交
615
        break;
G
guo 已提交
616
    case RT_TIMER_CTRL_GET_FUNC:
617
        arg = (void *)timer->timeout_func;
G
guo 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630
        break;

    case RT_TIMER_CTRL_SET_FUNC:
        timer->timeout_func = (void (*)(void*))arg;
        break;

    case RT_TIMER_CTRL_GET_PARM:
        *(void **)arg = timer->parameter;
        break;

    case RT_TIMER_CTRL_SET_PARM:
        timer->parameter = arg;
        break;
C
chenchaoqun 已提交
631 632 633

    default:
        break;
634
    }
B
Bernard Xiong 已提交
635
    rt_hw_interrupt_enable(level);
636

637
    return RT_EOK;
638
}
639
RTM_EXPORT(rt_timer_control);
640 641

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
642
 * @brief This function will check timer list, if a timeout event happens,
Thomas_Fly's avatar
Thomas_Fly 已提交
643
 *        the corresponding timeout function will be invoked.
644
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
645
 * @note This function shall be invoked in operating system timer interrupt.
646
 */
647
void rt_timer_check(void)
648
{
649 650
    struct rt_timer *t;
    rt_tick_t current_tick;
651
    rt_base_t level;
Nameless-Y's avatar
Nameless-Y 已提交
652 653 654
    rt_list_t list;

    rt_list_init(&list);
655 656 657 658 659 660 661 662

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

    current_tick = rt_tick_get();

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

663
    while (!rt_list_isempty(&_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
664
    {
665
        t = rt_list_entry(_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
666
                          struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
667 668 669 670 671

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

            /* remove timer from timer list firstly */
677
            _timer_remove(t);
B
Bernard Xiong 已提交
678 679 680 681 682 683
            if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
            {
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
            /* add timer to temporary list  */
            rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
684 685 686 687 688 689
            /* call timeout function */
            t->timeout_func(t->parameter);

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

690
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
691 692
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
693 694 695 696 697
            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }
698
            rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
699
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
700
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
701 702 703 704 705 706
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
B
Bernard Xiong 已提交
707
        else break;
708 709 710 711 712 713
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
714 715
}

716
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
717
 * @brief This function will return the next timeout tick in the system.
718
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
719
 * @return the next timeout tick in the system
720 721
 */
rt_tick_t rt_timer_next_timeout_tick(void)
722
{
723 724 725
    rt_tick_t next_timeout = RT_TICK_MAX;
    _timer_list_next_timeout(_timer_list, &next_timeout);
    return next_timeout;
726 727
}

728
#ifdef RT_USING_TIMER_SOFT
729
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
730
 * @brief This function will check software-timer list, if a timeout event happens, the
Thomas_Fly's avatar
Thomas_Fly 已提交
731
 *        corresponding timeout function will be invoked.
732
 */
D
dzzxzz 已提交
733
void rt_soft_timer_check(void)
734
{
735 736
    rt_tick_t current_tick;
    struct rt_timer *t;
737
    rt_base_t level;
Nameless-Y's avatar
Nameless-Y 已提交
738 739 740
    rt_list_t list;

    rt_list_init(&list);
741 742 743

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

B
Bernard Xiong 已提交
744 745
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
746

747
    while (!rt_list_isempty(&_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
748
    {
749
        t = rt_list_entry(_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
750 751 752
                            struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);

        current_tick = rt_tick_get();
753 754 755 756 757

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
D
dzzxzz@gmail.com 已提交
758
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
759
        {
760
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
761 762

            /* remove timer from timer list firstly */
763
            _timer_remove(t);
B
Bernard Xiong 已提交
764 765 766 767 768 769 770
            if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
            {
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
            /* add timer to temporary list  */
            rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));

771
            _soft_timer_status = RT_SOFT_TIMER_BUSY;
B
Bernard Xiong 已提交
772 773
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
774 775 776 777

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

778
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
779 780
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
781 782 783
            /* disable interrupt */
            level = rt_hw_interrupt_disable();

784
            _soft_timer_status = RT_SOFT_TIMER_IDLE;
B
Bernard Xiong 已提交
785 786 787 788 789
            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }
790
            rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
791
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
792
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
793 794 795 796 797 798 799 800
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
        else break; /* not check anymore */
    }
B
Bernard Xiong 已提交
801 802
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
803

804
    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
805 806
}

Thomas_Fly's avatar
Thomas_Fly 已提交
807
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
808
 * @brief System timer thread entry
Thomas_Fly's avatar
Thomas_Fly 已提交
809
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
810
 * @param parameter is the arg of the thread
Thomas_Fly's avatar
Thomas_Fly 已提交
811
 */
812
static void _timer_thread_entry(void *parameter)
813
{
814
    rt_tick_t next_timeout;
B
Bernard Xiong 已提交
815

816 817 818
    while (1)
    {
        /* get the next timeout tick */
819
        if (_timer_list_next_timeout(_soft_timer_list, &next_timeout) != RT_EOK)
820 821
        {
            /* no software timer exist, suspend self. */
G
guo 已提交
822
            rt_thread_suspend_with_flag(rt_thread_self(), RT_UNINTERRUPTIBLE);
823 824 825 826 827 828 829 830 831
            rt_schedule();
        }
        else
        {
            rt_tick_t current_tick;

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

832
            if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
833 834 835 836 837 838 839 840 841 842
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay(next_timeout);
            }
        }

        /* check software timer */
        rt_soft_timer_check();
    }
843
}
844
#endif /* RT_USING_TIMER_SOFT */
845

846 847 848
/**
 * @ingroup SystemInit
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
849
 * @brief This function will initialize system timer
850
 */
D
dzzxzz 已提交
851
void rt_system_timer_init(void)
852
{
dongly's avatar
dongly 已提交
853
    rt_size_t i;
854

855
    for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
856
    {
857
        rt_list_init(_timer_list + i);
858
    }
B
bernard.xiong 已提交
859
}
860

B
bernard.xiong 已提交
861 862 863
/**
 * @ingroup SystemInit
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
864
 * @brief This function will initialize system timer thread
B
bernard.xiong 已提交
865
 */
D
dzzxzz 已提交
866
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
867 868
{
#ifdef RT_USING_TIMER_SOFT
869 870 871
    int i;

    for (i = 0;
872
         i < sizeof(_soft_timer_list) / sizeof(_soft_timer_list[0]);
873 874
         i++)
    {
875
        rt_list_init(_soft_timer_list + i);
876
    }
877

878
    /* start software timer thread */
879
    rt_thread_init(&_timer_thread,
880
                   "timer",
881
                   _timer_thread_entry,
D
dzzxzz@gmail.com 已提交
882
                   RT_NULL,
883 884
                   &_timer_thread_stack[0],
                   sizeof(_timer_thread_stack),
D
dzzxzz@gmail.com 已提交
885 886
                   RT_TIMER_THREAD_PRIO,
                   10);
887

888
    /* startup */
889
    rt_thread_startup(&_timer_thread);
890
#endif /* RT_USING_TIMER_SOFT */
891
}
892

D
dogandog 已提交
893
/**@}*/