clock.c 3.2 KB
Newer Older
1 2 3
/*
 * File      : clock.c
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5
 *
B
Bernard Xiong 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-05-27     Bernard      add support for same priority thread schedule
24
 * 2006-08-10     Bernard      remove the last rt_schedule in rt_tick_increase
25
 * 2010-03-08     Bernard      remove rt_passed_second
26
 * 2010-05-20     Bernard      fix the tick exceeds the maximum limits
27
 * 2010-07-13     Bernard      fix rt_tick_from_millisecond issue found by kuronca
28
 * 2011-06-26     Bernard      add rt_tick_set function.
29 30
 */

31
#include <rthw.h>
32 33
#include <rtthread.h>

34
static rt_tick_t rt_tick = 0;
35 36 37 38 39 40 41

extern void rt_timer_check(void);

/**
 * This function will init system tick and set it to zero.
 * @ingroup SystemInit
 *
42 43
 * @deprecated since 1.1.0, this function does not need to be invoked
 * in the system initialization.
44
 */
D
dzzxzz 已提交
45
void rt_system_tick_init(void)
46 47 48 49 50 51 52
{
}

/**
 * @addtogroup Clock
 */

D
dogandog 已提交
53
/**@{*/
54 55 56 57 58 59

/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
D
dzzxzz 已提交
60
rt_tick_t rt_tick_get(void)
61
{
62 63
    /* return the global tick */
    return rt_tick;
64
}
65
RTM_EXPORT(rt_tick_get);
66

67 68 69 70 71
/**
 * This function will set current tick
 */
void rt_tick_set(rt_tick_t tick)
{
72
    rt_base_t level;
73

74 75 76
    level = rt_hw_interrupt_disable();
    rt_tick = tick;
    rt_hw_interrupt_enable(level);
77 78
}

79
/**
B
bernard.xiong 已提交
80
 * This function will notify kernel there is one tick passed. Normally,
81 82
 * this function is invoked by clock ISR.
 */
D
dzzxzz 已提交
83
void rt_tick_increase(void)
84
{
85
    struct rt_thread *thread;
86

87 88
    /* increase the global tick */
    ++ rt_tick;
89

90 91
    /* check time slice */
    thread = rt_thread_self();
92

93 94 95 96 97
    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;
98

99 100 101
        /* yield */
        rt_thread_yield();
    }
102

103 104
    /* check timer */
    rt_timer_check();
105 106 107 108 109 110
}

/**
 * This function will calculate the tick from millisecond.
 *
 * @param ms the specified millisecond
B
bernard 已提交
111 112 113
 *           - Negative Number wait forever
 *           - Zero not wait
 *           - Max 0x7fffffff
114 115 116
 *
 * @return the calculated tick
 */
B
bernard 已提交
117
int rt_tick_from_millisecond(rt_int32_t ms)
118
{
B
bernard 已提交
119 120 121 122 123 124 125
    int tick;

    if (ms < 0)
        tick = RT_WAITING_FOREVER;
    else
        tick = (RT_TICK_PER_SECOND * ms + 999) / 1000;

126
    /* return the calculated tick */
B
bernard 已提交
127
    return tick;
128
}
129
RTM_EXPORT(rt_tick_from_millisecond);
130

D
dogandog 已提交
131
/**@}*/
132