clock.c 2.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
Bernard Xiong 已提交
5
 *
6 7 8 9
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-05-27     Bernard      add support for same priority thread schedule
10
 * 2006-08-10     Bernard      remove the last rt_schedule in rt_tick_increase
11
 * 2010-03-08     Bernard      remove rt_passed_second
12
 * 2010-05-20     Bernard      fix the tick exceeds the maximum limits
13
 * 2010-07-13     Bernard      fix rt_tick_from_millisecond issue found by kuronca
14
 * 2011-06-26     Bernard      add rt_tick_set function.
15 16
 */

17
#include <rthw.h>
18 19
#include <rtthread.h>

20
static rt_tick_t rt_tick = 0;
21 22 23 24 25 26 27

extern void rt_timer_check(void);

/**
 * This function will init system tick and set it to zero.
 * @ingroup SystemInit
 *
28 29
 * @deprecated since 1.1.0, this function does not need to be invoked
 * in the system initialization.
30
 */
D
dzzxzz 已提交
31
void rt_system_tick_init(void)
32 33 34 35 36 37 38
{
}

/**
 * @addtogroup Clock
 */

D
dogandog 已提交
39
/**@{*/
40 41 42 43 44 45

/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
D
dzzxzz 已提交
46
rt_tick_t rt_tick_get(void)
47
{
48 49
    /* return the global tick */
    return rt_tick;
50
}
51
RTM_EXPORT(rt_tick_get);
52

53 54 55 56 57
/**
 * This function will set current tick
 */
void rt_tick_set(rt_tick_t tick)
{
58
    rt_base_t level;
59

60 61 62
    level = rt_hw_interrupt_disable();
    rt_tick = tick;
    rt_hw_interrupt_enable(level);
63 64
}

65
/**
B
bernard.xiong 已提交
66
 * This function will notify kernel there is one tick passed. Normally,
67 68
 * this function is invoked by clock ISR.
 */
D
dzzxzz 已提交
69
void rt_tick_increase(void)
70
{
71
    struct rt_thread *thread;
72

73 74
    /* increase the global tick */
    ++ rt_tick;
75

76 77
    /* check time slice */
    thread = rt_thread_self();
78

79 80 81 82 83
    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;
84

85 86 87
        /* yield */
        rt_thread_yield();
    }
88

89 90
    /* check timer */
    rt_timer_check();
91 92 93 94 95 96
}

/**
 * This function will calculate the tick from millisecond.
 *
 * @param ms the specified millisecond
B
bernard 已提交
97 98 99
 *           - Negative Number wait forever
 *           - Zero not wait
 *           - Max 0x7fffffff
100 101 102
 *
 * @return the calculated tick
 */
B
bernard 已提交
103
int rt_tick_from_millisecond(rt_int32_t ms)
104
{
B
bernard 已提交
105 106 107 108 109 110 111
    int tick;

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

112
    /* return the calculated tick */
B
bernard 已提交
113
    return tick;
114
}
115
RTM_EXPORT(rt_tick_from_millisecond);
116

D
dogandog 已提交
117
/**@}*/
118