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
 */

/*
 * File      : clock.c
10 11 12 13 14
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-05-27     Bernard      add support for same priority thread schedule
15
 * 2006-08-10     Bernard      remove the last rt_schedule in rt_tick_increase
16
 * 2010-03-08     Bernard      remove rt_passed_second
17
 * 2010-05-20     Bernard      fix the tick exceeds the maximum limits
18
 * 2010-07-13     Bernard      fix rt_tick_from_millisecond issue found by kuronca
19
 * 2011-06-26     Bernard      add rt_tick_set function.
20 21
 */

22
#include <rthw.h>
23 24
#include <rtthread.h>

25
static rt_tick_t rt_tick = 0;
26 27 28 29 30 31 32

extern void rt_timer_check(void);

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

/**
 * @addtogroup Clock
 */

D
dogandog 已提交
44
/**@{*/
45 46 47 48 49 50

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

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

65 66 67
    level = rt_hw_interrupt_disable();
    rt_tick = tick;
    rt_hw_interrupt_enable(level);
68 69
}

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

78 79
    /* increase the global tick */
    ++ rt_tick;
80

81 82
    /* check time slice */
    thread = rt_thread_self();
83

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

90 91 92
        /* yield */
        rt_thread_yield();
    }
93

94 95
    /* check timer */
    rt_timer_check();
96 97 98 99 100 101
}

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

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

117
    /* return the calculated tick */
B
bernard 已提交
118
    return tick;
119
}
120
RTM_EXPORT(rt_tick_from_millisecond);
121

D
dogandog 已提交
122
/**@}*/
123