clock.c 3.0 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
mysterywolf's avatar
mysterywolf 已提交
16
 * 2020-12-29     Meco Man     add function rt_hw_1ms_tick_get()
17 18
 */

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

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

/**
 * @addtogroup Clock
 */

D
dogandog 已提交
32
/**@{*/
33 34 35 36 37 38

/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
D
dzzxzz 已提交
39
rt_tick_t rt_tick_get(void)
40
{
41 42
    /* return the global tick */
    return rt_tick;
43
}
44
RTM_EXPORT(rt_tick_get);
45

46 47 48 49 50
/**
 * This function will set current tick
 */
void rt_tick_set(rt_tick_t tick)
{
51
    rt_base_t level;
52

53 54 55
    level = rt_hw_interrupt_disable();
    rt_tick = tick;
    rt_hw_interrupt_enable(level);
56 57
}

58
/**
B
bernard.xiong 已提交
59
 * This function will notify kernel there is one tick passed. Normally,
60 61
 * this function is invoked by clock ISR.
 */
D
dzzxzz 已提交
62
void rt_tick_increase(void)
63
{
64
    struct rt_thread *thread;
65

66
    /* increase the global tick */
S
shaojinchun 已提交
67 68 69
#ifdef RT_USING_SMP
    rt_cpu_self()->tick ++;
#else
70
    ++ rt_tick;
S
shaojinchun 已提交
71
#endif
72

73 74
    /* check time slice */
    thread = rt_thread_self();
75

76 77 78 79 80
    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;
81

82
        thread->stat |= RT_THREAD_STAT_YIELD;
83

84
        rt_schedule();
85
    }
86

87 88
    /* check timer */
    rt_timer_check();
89 90 91 92 93 94
}

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

    if (ms < 0)
E
Ernest Chen 已提交
106 107
    {
        tick = (rt_tick_t)RT_WAITING_FOREVER;
108
    }
B
bernard 已提交
109
    else
110 111
    {
        tick = RT_TICK_PER_SECOND * (ms / 1000);
E
Ernest Chen 已提交
112
        tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
113 114
    }
    
115
    /* return the calculated tick */
B
bernard 已提交
116
    return tick;
117
}
118
RTM_EXPORT(rt_tick_from_millisecond);
119

mysterywolf's avatar
mysterywolf 已提交
120
/**
mysterywolf's avatar
update  
mysterywolf 已提交
121
 * This function will provide the passed millisecond from boot.
mysterywolf's avatar
mysterywolf 已提交
122
 *
mysterywolf's avatar
update  
mysterywolf 已提交
123
 * @return passed millisecond from boot
mysterywolf's avatar
mysterywolf 已提交
124
 */
mysterywolf's avatar
update  
mysterywolf 已提交
125
RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
mysterywolf's avatar
mysterywolf 已提交
126
{
mysterywolf's avatar
update  
mysterywolf 已提交
127 128
#if 1000 % RT_TICK_PER_SECOND == 0u
    return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
mysterywolf's avatar
mysterywolf 已提交
129 130 131 132 133 134 135
#else
    #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
    please redefine this function in another file by using a high-precision hard-timer."
    return 0;
#endif
}

D
dogandog 已提交
136
/**@}*/
137