clock.c 2.4 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 6 7
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
B
bernard.xiong 已提交
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-05-27     Bernard      add support for same priority thread schedule
14
 * 2006-08-10     Bernard      remove the last rt_schedule in rt_tick_increase
15
 * 2010-03-08     Bernard      remove rt_passed_second
16
 * 2010-05-20     Bernard      fix the tick exceeds the maximum limits
17
 * 2010-07-13     Bernard      fix rt_tick_from_millisecond issue found by kuronca
18
 * 2011-06-26     Bernard      add rt_tick_set function.
19 20
 */

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

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

extern void rt_timer_check(void);

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

/**
 * @addtogroup Clock
 */

/*@{*/

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

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

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

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

	/* increase the global tick */
78
	++ rt_tick;
79 80 81 82 83 84 85 86 87 88 89

	/* check time slice */
	thread = rt_thread_self();

	-- thread->remaining_tick;
	if (thread->remaining_tick == 0)
	{
		/* change to initialized tick */
		thread->remaining_tick = thread->init_tick;

		/* yield */
D
dzzxzz 已提交
90
		rt_thread_yield();
91
	}
92

D
dzzxzz 已提交
93
	/* check timer */
94
	rt_timer_check();
95 96 97 98 99 100 101 102 103 104 105 106
}

/**
 * This function will calculate the tick from millisecond.
 *
 * @param ms the specified millisecond
 *
 * @return the calculated tick
 */
rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
{
	/* return the calculated tick */
D
dzzxzz 已提交
107
	return (RT_TICK_PER_SECOND * ms + 999) / 1000;
108
}
109
RTM_EXPORT(rt_tick_from_millisecond);
110 111 112

/*@}*/