summaryrefslogtreecommitdiff
path: root/src/clock.c
diff options
context:
space:
mode:
authorguo <[email protected]>2021-09-28 10:36:54 +0800
committerGitHub <[email protected]>2021-09-28 10:36:54 +0800
commit55a302cd092758a44b589d65470a69d9e8ae4ced (patch)
treee60fc53994c426007e9eb4877e5bd123f124ef39 /src/clock.c
parent00727d6b6db92dc1cf98f028f26fd6168594b905 (diff)
parent00d42ef73cff61ecda7ff006b077a73e1a4ff669 (diff)
Merge branch 'master' into usbh_construct
Diffstat (limited to 'src/clock.c')
-rw-r--r--src/clock.c56
1 files changed, 35 insertions, 21 deletions
diff --git a/src/clock.c b/src/clock.c
index 66923e5c00..21be3a4203 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -13,7 +13,8 @@
* 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 Jesven add per cpu tick
- * 2020-12-29 Meco Man add function rt_tick_get_millisecond()
+ * 2020-12-29 Meco Man implement rt_tick_get_millisecond()
+ * 2021-06-01 Meco Man add critical section projection for rt_tick_increase()
*/
#include <rthw.h>
@@ -22,8 +23,8 @@
#ifdef RT_USING_SMP
#define rt_tick rt_cpu_index(0)->tick
#else
-static rt_tick_t rt_tick = 0;
-#endif
+static volatile rt_tick_t rt_tick = 0;
+#endif /* RT_USING_SMP */
/**
* @addtogroup Clock
@@ -32,9 +33,9 @@ static rt_tick_t rt_tick = 0;
/**@{*/
/**
- * This function will return current tick from operating system startup
+ * @brief This function will return current tick from operating system startup.
*
- * @return current tick
+ * @return Return current tick.
*/
rt_tick_t rt_tick_get(void)
{
@@ -44,7 +45,9 @@ rt_tick_t rt_tick_get(void)
RTM_EXPORT(rt_tick_get);
/**
- * This function will set current tick
+ * @brief This function will set current tick.
+ *
+ * @param tick is the value that you will set.
*/
void rt_tick_set(rt_tick_t tick)
{
@@ -56,19 +59,22 @@ void rt_tick_set(rt_tick_t tick)
}
/**
- * This function will notify kernel there is one tick passed. Normally,
- * this function is invoked by clock ISR.
+ * @brief 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;
+ rt_base_t level;
+
+ level = rt_hw_interrupt_disable();
/* increase the global tick */
#ifdef RT_USING_SMP
rt_cpu_self()->tick ++;
#else
++ rt_tick;
-#endif
+#endif /* RT_USING_SMP */
/* check time slice */
thread = rt_thread_self();
@@ -78,25 +84,29 @@ void rt_tick_increase(void)
{
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;
-
thread->stat |= RT_THREAD_STAT_YIELD;
+ rt_hw_interrupt_enable(level);
rt_schedule();
}
+ else
+ {
+ rt_hw_interrupt_enable(level);
+ }
/* check timer */
rt_timer_check();
}
/**
- * This function will calculate the tick from millisecond.
+ * @brief This function will calculate the tick from millisecond.
*
- * @param ms the specified millisecond
- * - Negative Number wait forever
- * - Zero not wait
- * - Max 0x7fffffff
+ * @param ms is the specified millisecond.
+ * - Negative Number wait forever
+ * - Zero not wait
+ * - Max 0x7fffffff
*
- * @return the calculated tick
+ * @return Return the calculated tick.
*/
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
{
@@ -111,16 +121,20 @@ rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
tick = RT_TICK_PER_SECOND * (ms / 1000);
tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
}
-
+
/* return the calculated tick */
return tick;
}
RTM_EXPORT(rt_tick_from_millisecond);
/**
- * This function will provide the passed millisecond from boot.
+ * @brief This function will return the passed millisecond from boot.
+ *
+ * @note if the value of RT_TICK_PER_SECOND is lower than 1000 or
+ * is not an integral multiple of 1000, this function will not
+ * provide the correct 1ms-based tick.
*
- * @return passed millisecond from boot
+ * @return Return passed millisecond from boot.
*/
RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
{
@@ -130,7 +144,7 @@ RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
#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
+#endif /* 1000 % RT_TICK_PER_SECOND == 0u */
}
/**@}*/