clock.c 2.0 KB
Newer Older
1 2 3
/*
 * File      : clock.c
 * This file is part of RT-Thread RTOS
B
bernard.xiong 已提交
4
 * COPYRIGHT (C) 2006 - 2009, 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 18 19 20 21 22 23
 */

#include <rtthread.h>

static rt_tick_t rt_tick;

extern void rt_timer_check(void);
24
extern void rt_timer_switch(void);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/**
 * This function will init system tick and set it to zero.
 * @ingroup SystemInit
 *
 */
void rt_system_tick_init()
{
	rt_tick = 0;
}

/**
 * @addtogroup Clock
 */

/*@{*/

/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
rt_tick_t rt_tick_get()
{
	/* return the global tick */
	return rt_tick;
}

/**
B
bernard.xiong 已提交
54
 * This function will notify kernel there is one tick passed. Normally,
55 56 57 58 59 60 61
 * this function is invoked by clock ISR.
 */
void rt_tick_increase()
{
	struct rt_thread* thread;

	/* increase the global tick */
62 63 64 65
	if (rt_tick == RT_TICK_MAX)
	{
		/* switch to long timer list */
		rt_timer_switch();
66

67 68 69
		rt_tick = 0;
	}
	else ++ rt_tick;
70 71 72 73 74 75 76 77 78 79 80 81 82

	/* 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 */
	    rt_thread_yield();
	}
83 84 85

	/* check timer  */
	rt_timer_check();
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

/**
 * 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 */
	return RT_TICK_PER_SECOND * (ms / 1000);
}

/*@}*/