xref: /nrf52832-nimble/rt-thread/src/clock.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2006-03-12     Bernard      first version
9  * 2006-05-27     Bernard      add support for same priority thread schedule
10  * 2006-08-10     Bernard      remove the last rt_schedule in rt_tick_increase
11  * 2010-03-08     Bernard      remove rt_passed_second
12  * 2010-05-20     Bernard      fix the tick exceeds the maximum limits
13  * 2010-07-13     Bernard      fix rt_tick_from_millisecond issue found by kuronca
14  * 2011-06-26     Bernard      add rt_tick_set function.
15  * 2018-11-22     Jesven       add per cpu tick
16  */
17 
18 #include <rthw.h>
19 #include <rtthread.h>
20 
21 #ifdef RT_USING_SMP
22 #define rt_tick rt_cpu_index(0)->tick
23 #else
24 static rt_tick_t rt_tick = 0;
25 #endif
26 
27 extern void rt_timer_check(void);
28 
29 /**
30  * This function will init system tick and set it to zero.
31  * @ingroup SystemInit
32  *
33  * @deprecated since 1.1.0, this function does not need to be invoked
34  * in the system initialization.
35  */
rt_system_tick_init(void)36 void rt_system_tick_init(void)
37 {
38 }
39 
40 /**
41  * @addtogroup Clock
42  */
43 
44 /**@{*/
45 
46 /**
47  * This function will return current tick from operating system startup
48  *
49  * @return current tick
50  */
rt_tick_get(void)51 rt_tick_t rt_tick_get(void)
52 {
53     /* return the global tick */
54     return rt_tick;
55 }
56 RTM_EXPORT(rt_tick_get);
57 
58 /**
59  * This function will set current tick
60  */
rt_tick_set(rt_tick_t tick)61 void rt_tick_set(rt_tick_t tick)
62 {
63     rt_base_t level;
64 
65     level = rt_hw_interrupt_disable();
66     rt_tick = tick;
67     rt_hw_interrupt_enable(level);
68 }
69 
70 /**
71  * This function will notify kernel there is one tick passed. Normally,
72  * this function is invoked by clock ISR.
73  */
rt_tick_increase(void)74 void rt_tick_increase(void)
75 {
76     struct rt_thread *thread;
77 
78     /* increase the global tick */
79 #ifdef RT_USING_SMP
80     rt_cpu_self()->tick ++;
81 #else
82     ++ rt_tick;
83 #endif
84 
85     /* check time slice */
86     thread = rt_thread_self();
87 
88     -- thread->remaining_tick;
89     if (thread->remaining_tick == 0)
90     {
91         /* change to initialized tick */
92         thread->remaining_tick = thread->init_tick;
93 
94         /* yield */
95         rt_thread_yield();
96     }
97 
98     /* check timer */
99     rt_timer_check();
100 }
101 
102 /**
103  * This function will calculate the tick from millisecond.
104  *
105  * @param ms the specified millisecond
106  *           - Negative Number wait forever
107  *           - Zero not wait
108  *           - Max 0x7fffffff
109  *
110  * @return the calculated tick
111  */
rt_tick_from_millisecond(rt_int32_t ms)112 int rt_tick_from_millisecond(rt_int32_t ms)
113 {
114     int tick;
115 
116     if (ms < 0)
117         tick = RT_WAITING_FOREVER;
118     else
119         tick = (RT_TICK_PER_SECOND * ms + 999) / 1000;
120 
121     /* return the calculated tick */
122     return tick;
123 }
124 RTM_EXPORT(rt_tick_from_millisecond);
125 
126 /**@}*/
127 
128