2009-07-03 06:48:23 +08:00
|
|
|
/*
|
|
|
|
* File : clock.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
2012-03-17 14:43:49 +08:00
|
|
|
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
2009-12-25 20:18:53 +08:00
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2006-03-12 Bernard first version
|
|
|
|
* 2006-05-27 Bernard add support for same priority thread schedule
|
2010-05-23 07:26:08 +08:00
|
|
|
* 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
|
2010-03-09 07:09:40 +08:00
|
|
|
* 2010-03-08 Bernard remove rt_passed_second
|
2010-05-23 07:26:08 +08:00
|
|
|
* 2010-05-20 Bernard fix the tick exceeds the maximum limits
|
2010-07-13 15:36:37 +08:00
|
|
|
* 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
|
2011-06-29 22:45:35 +08:00
|
|
|
* 2011-06-26 Bernard add rt_tick_set function.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
2011-06-29 22:45:35 +08:00
|
|
|
#include <rthw.h>
|
2009-07-03 06:48:23 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
|
2012-01-31 20:17:47 +08:00
|
|
|
static rt_tick_t rt_tick = 0;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
extern void rt_timer_check(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will init system tick and set it to zero.
|
|
|
|
* @ingroup SystemInit
|
|
|
|
*
|
2012-01-31 20:17:47 +08:00
|
|
|
* @deprecated since 1.1.0, this function does not need to be invoked
|
|
|
|
* in the system initialization.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_system_tick_init(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Clock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*@{*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will return current tick from operating system startup
|
|
|
|
*
|
|
|
|
* @return current tick
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
rt_tick_t rt_tick_get(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-20 15:34:48 +08:00
|
|
|
/* return the global tick */
|
|
|
|
return rt_tick;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_tick_get);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2011-06-29 22:45:35 +08:00
|
|
|
/**
|
|
|
|
* This function will set current tick
|
|
|
|
*/
|
|
|
|
void rt_tick_set(rt_tick_t tick)
|
|
|
|
{
|
2012-12-20 15:34:48 +08:00
|
|
|
rt_base_t level;
|
2011-06-29 22:45:35 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
rt_tick = tick;
|
|
|
|
rt_hw_interrupt_enable(level);
|
2011-06-29 22:45:35 +08:00
|
|
|
}
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/**
|
2009-12-25 20:18:53 +08:00
|
|
|
* This function will notify kernel there is one tick passed. Normally,
|
2009-07-03 06:48:23 +08:00
|
|
|
* this function is invoked by clock ISR.
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_tick_increase(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-20 15:34:48 +08:00
|
|
|
struct rt_thread *thread;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* increase the global tick */
|
|
|
|
++ rt_tick;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* check time slice */
|
|
|
|
thread = rt_thread_self();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
-- thread->remaining_tick;
|
|
|
|
if (thread->remaining_tick == 0)
|
|
|
|
{
|
|
|
|
/* change to initialized tick */
|
|
|
|
thread->remaining_tick = thread->init_tick;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* yield */
|
|
|
|
rt_thread_yield();
|
|
|
|
}
|
2010-05-23 07:26:08 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* check timer */
|
|
|
|
rt_timer_check();
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2012-12-20 15:34:48 +08:00
|
|
|
/* return the calculated tick */
|
|
|
|
return (RT_TICK_PER_SECOND * ms + 999) / 1000;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_tick_from_millisecond);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/*@}*/
|
|
|
|
|