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