clock.c 2.7 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.
S
shaojinchun 已提交
15
 * 2018-11-22     Jesven       add per cpu tick
16 17
 */

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

S
shaojinchun 已提交
21 22 23
#ifdef RT_USING_SMP
#define rt_tick rt_cpu_index(0)->tick
#else
24
static rt_tick_t rt_tick = 0;
S
shaojinchun 已提交
25
#endif
26 27 28 29 30

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

/**
 * @addtogroup Clock
 */

D
dogandog 已提交
42
/**@{*/
43 44 45 46 47 48

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

56 57 58 59 60
/**
 * This function will set current tick
 */
void rt_tick_set(rt_tick_t tick)
{
61
    rt_base_t level;
62

63 64 65
    level = rt_hw_interrupt_disable();
    rt_tick = tick;
    rt_hw_interrupt_enable(level);
66 67
}

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

76
    /* increase the global tick */
S
shaojinchun 已提交
77 78 79
#ifdef RT_USING_SMP
    rt_cpu_self()->tick ++;
#else
80
    ++ rt_tick;
S
shaojinchun 已提交
81
#endif
82

83 84
    /* check time slice */
    thread = rt_thread_self();
85

86 87 88 89 90
    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;
91

92 93 94
        /* yield */
        rt_thread_yield();
    }
95

96 97
    /* check timer */
    rt_timer_check();
98 99 100 101 102 103
}

/**
 * This function will calculate the tick from millisecond.
 *
 * @param ms the specified millisecond
B
bernard 已提交
104 105 106
 *           - Negative Number wait forever
 *           - Zero not wait
 *           - Max 0x7fffffff
107 108 109
 *
 * @return the calculated tick
 */
110
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
111
{
112
    rt_tick_t tick;
B
bernard 已提交
113 114

    if (ms < 0)
115
    {    
B
bernard 已提交
116
        tick = RT_WAITING_FOREVER;
117
    }
B
bernard 已提交
118
    else
119 120 121 122 123
    {
        tick = RT_TICK_PER_SECOND * (ms / 1000);
        tick += (RT_TICK_PER_SECOND * (ms%1000) + 999) / 1000;
    }
    
124
    /* return the calculated tick */
B
bernard 已提交
125
    return tick;
126
}
127
RTM_EXPORT(rt_tick_from_millisecond);
128

D
dogandog 已提交
129
/**@}*/
130