2009-07-03 06:48:23 +08:00
|
|
|
/*
|
2021-03-08 11:25:38 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
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.
|
2018-11-22 14:40:43 +08:00
|
|
|
* 2018-11-22 Jesven add per cpu tick
|
2021-06-02 15:30:32 +08:00
|
|
|
* 2020-12-29 Meco Man implement rt_tick_get_millisecond()
|
|
|
|
* 2021-06-01 Meco Man add critical section projection for rt_tick_increase()
|
2023-10-17 09:26:42 +08:00
|
|
|
* 2023-10-16 RiceChen fix: only the main core detection rt_timer_check(), in SMP mode
|
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>
|
|
|
|
|
2018-11-22 14:40:43 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
#define rt_tick rt_cpu_index(0)->tick
|
|
|
|
#else
|
2021-06-03 20:09:26 +08:00
|
|
|
static volatile rt_tick_t rt_tick = 0;
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_SMP */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2022-02-13 14:50:51 +08:00
|
|
|
#ifndef __on_rt_tick_hook
|
|
|
|
#define __on_rt_tick_hook() __ON_HOOK_ARGS(rt_tick_hook, ())
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
|
|
|
|
static void (*rt_tick_hook)(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Hook
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**@{*/
|
|
|
|
|
|
|
|
/**
|
2023-04-25 12:36:39 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when tick increase
|
2022-02-13 14:50:51 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param hook the hook function
|
|
|
|
*/
|
|
|
|
void rt_tick_sethook(void (*hook)(void))
|
|
|
|
{
|
|
|
|
rt_tick_hook = hook;
|
|
|
|
}
|
|
|
|
/**@}*/
|
|
|
|
#endif /* RT_USING_HOOK */
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup Clock
|
|
|
|
*/
|
|
|
|
|
2016-08-19 10:05:00 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-18 16:23:50 +08:00
|
|
|
* @brief This function will return current tick from operating system startup.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-18 16:23:50 +08:00
|
|
|
* @return Return current tick.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
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
|
|
|
/**
|
2021-09-18 16:23:50 +08:00
|
|
|
* @brief This function will set current tick.
|
2021-09-10 16:31:29 +08:00
|
|
|
*
|
2021-09-10 15:50:15 +08:00
|
|
|
* @param tick is the value that you will set.
|
2011-06-29 22:45:35 +08:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2021-09-10 15:50:15 +08:00
|
|
|
* @brief This function will notify kernel there is one tick passed.
|
|
|
|
* Normally, this function is invoked by clock ISR.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
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;
|
2021-06-01 11:42:47 +08:00
|
|
|
rt_base_t level;
|
|
|
|
|
2022-02-13 14:50:51 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
|
|
|
|
|
2021-06-01 11:42:47 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* increase the global tick */
|
2018-11-22 14:40:43 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
rt_cpu_self()->tick ++;
|
|
|
|
#else
|
2012-12-20 15:34:48 +08:00
|
|
|
++ rt_tick;
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_SMP */
|
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;
|
2019-11-05 08:41:38 +08:00
|
|
|
thread->stat |= RT_THREAD_STAT_YIELD;
|
2019-10-11 09:13:40 +08:00
|
|
|
|
2021-06-01 11:42:47 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2020-07-24 21:13:56 +08:00
|
|
|
rt_schedule();
|
2012-12-20 15:34:48 +08:00
|
|
|
}
|
2021-06-01 11:42:47 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2010-05-23 07:26:08 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* check timer */
|
2023-10-17 09:26:42 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
if (rt_hw_cpu_id() != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-20 15:34:48 +08:00
|
|
|
rt_timer_check();
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:50:15 +08:00
|
|
|
* @brief This function will calculate the tick from millisecond.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-18 16:23:50 +08:00
|
|
|
* @param ms is the specified millisecond.
|
2021-09-10 15:50:15 +08:00
|
|
|
* - Negative Number wait forever
|
|
|
|
* - Zero not wait
|
|
|
|
* - Max 0x7fffffff
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-18 16:23:50 +08:00
|
|
|
* @return Return the calculated tick.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2019-03-20 14:54:17 +08:00
|
|
|
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2019-03-20 14:54:17 +08:00
|
|
|
rt_tick_t tick;
|
2017-10-15 22:31:53 +08:00
|
|
|
|
|
|
|
if (ms < 0)
|
2019-03-26 17:53:57 +08:00
|
|
|
{
|
|
|
|
tick = (rt_tick_t)RT_WAITING_FOREVER;
|
2019-03-20 14:54:17 +08:00
|
|
|
}
|
2017-10-15 22:31:53 +08:00
|
|
|
else
|
2019-03-20 14:54:17 +08:00
|
|
|
{
|
|
|
|
tick = RT_TICK_PER_SECOND * (ms / 1000);
|
2019-03-26 17:53:57 +08:00
|
|
|
tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
|
2019-03-20 14:54:17 +08:00
|
|
|
}
|
2021-03-08 11:25:38 +08:00
|
|
|
|
2012-12-20 15:34:48 +08:00
|
|
|
/* return the calculated tick */
|
2017-10-15 22:31:53 +08:00
|
|
|
return tick;
|
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
|
|
|
|
2020-12-29 00:08:24 +08:00
|
|
|
/**
|
2021-09-10 15:50:15 +08:00
|
|
|
* @brief This function will return the passed millisecond from boot.
|
2021-09-10 16:31:29 +08:00
|
|
|
*
|
2021-09-11 18:29:37 +08:00
|
|
|
* @note if the value of RT_TICK_PER_SECOND is lower than 1000 or
|
2021-09-10 15:50:15 +08:00
|
|
|
* is not an integral multiple of 1000, this function will not
|
|
|
|
* provide the correct 1ms-based tick.
|
2021-09-10 16:31:29 +08:00
|
|
|
*
|
2021-09-18 16:23:50 +08:00
|
|
|
* @return Return passed millisecond from boot.
|
2020-12-29 00:08:24 +08:00
|
|
|
*/
|
2022-12-12 02:12:03 +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;
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* 1000 % RT_TICK_PER_SECOND == 0u */
|
2020-12-29 00:08:24 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:05:00 +08:00
|
|
|
/**@}*/
|