rt-thread-official/src/clock.c

142 lines
3.1 KiB
C
Raw Normal View History

/*
2021-03-08 11:25:38 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2013-06-24 17:06:09 +08:00
*
* SPDX-License-Identifier: Apache-2.0
2013-06-24 17:06:09 +08:00
*
* Change Logs:
* Date Author Notes
* 2006-03-12 Bernard first version
* 2006-05-27 Bernard add support for same priority thread schedule
* 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
* 2010-03-08 Bernard remove rt_passed_second
* 2010-05-20 Bernard fix the tick exceeds the maximum limits
* 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
* 2011-06-26 Bernard add rt_tick_set function.
2018-11-22 14:40:43 +08:00
* 2018-11-22 Jesven add per cpu tick
2020-12-30 16:33:30 +08:00
* 2020-12-29 Meco Man add function rt_tick_get_millisecond()
*/
#include <rthw.h>
#include <rtthread.h>
2018-11-22 14:40:43 +08:00
#ifdef RT_USING_SMP
#define rt_tick rt_cpu_index(0)->tick
#else
static rt_tick_t rt_tick = 0;
2018-11-22 14:40:43 +08:00
#endif
/**
* @addtogroup Clock
*/
/**@{*/
/**
* This function will return current tick from operating system startup
*
* @return current tick
*/
rt_tick_t rt_tick_get(void)
{
/* return the global tick */
return rt_tick;
}
RTM_EXPORT(rt_tick_get);
/**
* This function will set current tick
*/
void rt_tick_set(rt_tick_t tick)
{
rt_base_t level;
level = rt_hw_interrupt_disable();
rt_tick = tick;
rt_hw_interrupt_enable(level);
}
/**
* This function will notify kernel there is one tick passed. Normally,
* this function is invoked by clock ISR.
*/
void rt_tick_increase(void)
{
struct rt_thread *thread;
/* increase the global tick */
2018-11-22 14:40:43 +08:00
#ifdef RT_USING_SMP
rt_cpu_self()->tick ++;
#else
++ rt_tick;
2018-11-22 14:40:43 +08:00
#endif
/* check time slice */
thread = rt_thread_self();
-- thread->remaining_tick;
if (thread->remaining_tick == 0)
{
rt_base_t level;
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;
level = rt_hw_interrupt_disable();
thread->stat |= RT_THREAD_STAT_YIELD;
rt_hw_interrupt_enable(level);
rt_schedule();
}
/* check timer */
rt_timer_check();
}
/**
* This function will calculate the tick from millisecond.
*
* @param ms the specified millisecond
* - Negative Number wait forever
* - Zero not wait
* - Max 0x7fffffff
*
* @return the calculated tick
*/
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
{
rt_tick_t tick;
if (ms < 0)
2019-03-26 17:53:57 +08:00
{
tick = (rt_tick_t)RT_WAITING_FOREVER;
}
else
{
tick = RT_TICK_PER_SECOND * (ms / 1000);
2019-03-26 17:53:57 +08:00
tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
}
2021-03-08 11:25:38 +08:00
/* return the calculated tick */
return tick;
}
RTM_EXPORT(rt_tick_from_millisecond);
2020-12-29 00:08:24 +08:00
/**
2020-12-30 16:32:20 +08:00
* This function will provide the passed millisecond from boot.
2020-12-29 00:08:24 +08:00
*
2020-12-30 16:32:20 +08:00
* @return passed millisecond from boot
2020-12-29 00:08:24 +08:00
*/
2020-12-30 16:32:20 +08:00
RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
2020-12-29 00:08:24 +08:00
{
2020-12-30 16:32:20 +08:00
#if 1000 % RT_TICK_PER_SECOND == 0u
return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
2020-12-29 00:08:24 +08:00
#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
}
/**@}*/