1 /*
2  * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _KERNEL_OS_RTTHREAD_OS_TIME_H_
31 #define _KERNEL_OS_RTTHREAD_OS_TIME_H_
32 
33 #include "_os_common.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* Parameters used to convert the time values */
40 #define OS_MSEC_PER_SEC     1000U       /* milliseconds per second */
41 #define OS_USEC_PER_MSEC    1000U       /* microseconds per millisecond */
42 #define OS_USEC_PER_SEC     1000000U    /* microseconds per second */
43 
44 /* system clock's frequency, OS ticks per second */
45 #define OS_HZ               (RT_TICK_PER_SECOND)
46 
47 /* microseconds per OS tick (1000000 / OS_HZ) */
48 #define OS_TICK             (OS_USEC_PER_SEC / OS_HZ)
49 
50 /** @brief Get the number of ticks since OS start */
51 /* Due to portTICK_TYPE_IS_ATOMIC is 1, calling xTaskGetTickCount() in ISR is
52  * safe also.
53  */
54 #define OS_GetTicks()       ((OS_Time_t)rt_tick_get())
55 
56 /** @brief Get the number of seconds since OS start */
57 #define OS_GetTime()        (OS_GetTicks() / OS_HZ)
58 
59 /**
60  * @brief Macros used to convert various time units to each other
61  *     - Secs stand for seconds
62  *     - MSecs stand for milliseconds
63  *     - Ticks stand for OS ticks
64  *     - Jiffies stand for OS jiffies, which is a synonym for OS ticks
65  */
66 /* there will be return 0 ticks ,when OS_HZ less than 1000 */
67 /*
68  *#define OS_SecsToTicks(sec)     ((OS_Time_t)(sec) * OS_HZ)
69  *#define OS_MSecsToTicks(msec)   ((OS_Time_t)(msec) * (OS_USEC_PER_MSEC / OS_TICK))
70 */
71 #define OS_SecsToTicks(sec)     ((OS_Time_t)(sec) * OS_HZ)
72 #define OS_MSecsToTicks(msec)   ((OS_Time_t)(msec) * OS_USEC_PER_MSEC / OS_TICK)
73 /*
74  * there will be warned :  division by zero,so change the definition
75  *#define OS_TicksToMSecs(t)      ((uint32_t)(t) / (OS_USEC_PER_MSEC / OS_TICK))
76  *#define OS_TicksToSecs(t)       ((uint32_t)(t) / (OS_USEC_PER_SEC / OS_TICK))
77 */
78 #define OS_TicksToMSecs(t)      ((uint32_t)(t) * OS_TICK / OS_USEC_PER_MSEC)
79 #define OS_TicksToSecs(t)       ((uint32_t)(t) * OS_TICK / OS_USEC_PER_SEC)
80 
81 #define OS_GetJiffies()         OS_GetTicks()
82 #define OS_SecsToJiffies(sec)   OS_SecsToTicks(sec)
83 #define OS_MSecsToJiffies(msec) OS_MSecsToTicks(msec)
84 #define OS_JiffiesToMSecs(j)    OS_TicksToMSecs(j)
85 #define OS_JiffiesToSecs(j)     OS_TicksToSecs(j)
86 
87 /**
88  * @brief Macros used to sleep for the given time (milliseconds or seconds)
89  */
90 #define OS_MSleep(msec)         rt_thread_delay((rt_tick_t)OS_MSecsToTicks(msec))
91 #define OS_Sleep(sec)           OS_MSleep((sec) * OS_MSEC_PER_SEC)
92 #define OS_SSleep(sec)          OS_Sleep(sec)
93 
94 /**
95  * @brief Macros used to compare time values
96  *
97  *  These inlines deal with timer wrapping correctly. You are
98  *  strongly encouraged to use them
99  *  1. Because people otherwise forget
100  *  2. Because if the timer wrap changes in future you won't have to
101  *     alter your code.
102  *
103  * OS_TimeAfter(a,b) returns true if the time a is after time b.
104  *
105  * Do this with "<0" and ">=0" to only test the sign of the result. A
106  * good compiler would generate better code (and a really good compiler
107  * wouldn't care). Gcc is currently neither.
108  */
109 #define OS_TimeAfter(a, b)              ((int32_t)(b) - (int32_t)(a) < 0)
110 #define OS_TimeBefore(a, b)             OS_TimeAfter(b, a)
111 #define OS_TimeAfterEqual(a, b)         ((int32_t)(a) - (int32_t)(b) >= 0)
112 #define OS_TimeBeforeEqual(a, b)        OS_TimeAfterEqual(b, a)
113 
114 /** @brief Macros used to generate fake random 32-bit value */
115 /* The fake random 32-bit value is generated by combining OS ticks and
116  * the value of rand().
117  */
118 int rand(void);
119 #define OS_Rand32() ((uint32_t)((rand() & 0xffffff) | (OS_GetTicks() << 24)))
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif /* _KERNEL_OS_RTTHREAD_OS_TIME_H_ */
126