1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef __OAL_LINUX_TIMER_H__
20 #define __OAL_LINUX_TIMER_H__
21
22 /* ****************************************************************************
23 1 其他头文件包含
24 **************************************************************************** */
25 #include <linux/timer.h>
26 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
27 #include <los_swtmr.h>
28 #include "oal_time.h"
29 #endif
30
31 #ifdef __cplusplus
32 #if __cplusplus
33 extern "C" {
34 #endif
35 #endif
36
37 /* ****************************************************************************
38 2 STRUCT定义
39 **************************************************************************** */
40 typedef struct timer_list oal_timer_list_stru;
41
42 /* ****************************************************************************
43 3 枚举定义
44 **************************************************************************** */
45 /* ****************************************************************************
46 4 全局变量声明
47 **************************************************************************** */
48 /* ****************************************************************************
49 5 消息头定义
50 **************************************************************************** */
51 /* ****************************************************************************
52 6 消息定义
53 **************************************************************************** */
54 /* ****************************************************************************
55 7 宏定义
56 **************************************************************************** */
57 /* ****************************************************************************
58 8 UNION定义
59 **************************************************************************** */
60 /* ****************************************************************************
61 9 OTHERS定义
62 **************************************************************************** */
63 #ifdef _PRE_HDF_LINUX
64 typedef void (*oal_timer_func)(struct timer_list *);
65 #else
66 typedef void (*oal_timer_func)(unsigned long);
67 #endif
68
69 /* ****************************************************************************
70 10 函数声明
71 **************************************************************************** */
72 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
73
74 /* ****************************************************************************
75 功能描述 : 初始化定时器
76 输入参数 : pst_timer: 定时器结构体指针
77 输出参数 : 无
78 返 回 值 :
79 **************************************************************************** */
oal_timer_init(oal_timer_list_stru * pst_timer,unsigned long ul_delay,oal_timer_func p_func,unsigned long ui_arg)80 static inline hi_void oal_timer_init(oal_timer_list_stru *pst_timer, unsigned long ul_delay, oal_timer_func p_func,
81 unsigned long ui_arg)
82 {
83 #ifdef _PRE_HDF_LINUX
84 timer_setup(pst_timer, p_func, 0);
85 hi_unref_param(ui_arg);
86 #else
87 init_timer(pst_timer);
88 pst_timer->function = p_func;
89 pst_timer->data = ui_arg;
90 #endif
91 pst_timer->expires = jiffies + msecs_to_jiffies(ul_delay);
92 }
93
94 /* ****************************************************************************
95 功能描述 : 删除定时器
96 输入参数 : pst_timer: 定时器结构体指针
97 输出参数 : 无
98 返 回 值 :
99 **************************************************************************** */
oal_timer_delete(oal_timer_list_stru * pst_timer)100 static inline hi_s32 oal_timer_delete(oal_timer_list_stru *pst_timer)
101 {
102 return del_timer(pst_timer);
103 }
104
105 /* ****************************************************************************
106 功能描述 : 同步删除定时器,用于多核
107 输入参数 : pst_timer: 定时器结构体指针
108 输出参数 : 无
109 返 回 值 :
110 **************************************************************************** */
oal_timer_delete_sync(oal_timer_list_stru * pst_timer)111 static inline hi_s32 oal_timer_delete_sync(oal_timer_list_stru *pst_timer)
112 {
113 return del_timer_sync(pst_timer);
114 }
115
116 /* ****************************************************************************
117 功能描述 : 激活定时器
118 输入参数 : pst_timer: 定时器结构体指针
119 输出参数 : 无
120 返 回 值 :
121 **************************************************************************** */
oal_timer_add(oal_timer_list_stru * pst_timer)122 static inline hi_void oal_timer_add(oal_timer_list_stru *pst_timer)
123 {
124 add_timer(pst_timer);
125 }
126
127 /* ****************************************************************************
128 功能描述 : 重启定时器
129 输入参数 : pst_timer: 结构体指针
130 ui_expires: 重启的溢出事件
131 输出参数 : 无
132 返 回 值 :
133 **************************************************************************** */
oal_timer_start(oal_timer_list_stru * pst_timer,unsigned long ui_delay)134 static inline hi_s32 oal_timer_start(oal_timer_list_stru *pst_timer, unsigned long ui_delay)
135 {
136 return mod_timer(pst_timer, (jiffies + msecs_to_jiffies(ui_delay)));
137 }
138
139 /* ****************************************************************************
140 功能描述 : 指定cpu,重启定时器,调用时timer要处于非激活状态否者会死机
141 输入参数 : pst_timer: 结构体指针
142 ui_expires: 重启的溢出事件
143 输出参数 : 无
144 返 回 值 :
145 **************************************************************************** */
oal_timer_start_on(oal_timer_list_stru * pst_timer,unsigned long ui_delay,hi_s32 cpu)146 static inline hi_void oal_timer_start_on(oal_timer_list_stru *pst_timer, unsigned long ui_delay, hi_s32 cpu)
147 {
148 pst_timer->expires = jiffies + msecs_to_jiffies(ui_delay);
149 add_timer_on(pst_timer, cpu);
150 }
151 #endif
152
153 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
154 /* ****************************************************************************
155 功能描述 : 初始化定时器
156 输入参数 : pst_timer: 定时器结构体指针
157 输出参数 : 无
158 返 回 值 :
159 **************************************************************************** */
oal_timer_init(oal_timer_list_stru * pst_timer,hi_u32 ul_delay,oal_timer_func p_func,hi_u64 ui_arg)160 static inline hi_void oal_timer_init(oal_timer_list_stru *pst_timer, hi_u32 ul_delay, oal_timer_func p_func,
161 hi_u64 ui_arg)
162 {
163 init_timer(pst_timer);
164 pst_timer->expires = OAL_MSECS_TO_JIFFIES(ul_delay);
165 pst_timer->function = p_func;
166 pst_timer->data = ui_arg;
167 }
168
169 /* ****************************************************************************
170 功能描述 : 删除定时器
171 输入参数 : pst_timer: 定时器结构体指针
172 输出参数 : 无
173 返 回 值 :
174 **************************************************************************** */
oal_timer_delete(oal_timer_list_stru * pst_timer)175 static inline hi_s32 oal_timer_delete(oal_timer_list_stru *pst_timer)
176 {
177 return del_timer(pst_timer);
178 }
179
180 /* ****************************************************************************
181 功能描述 : 同步删除定时器,用于多核
182 输入参数 : pst_timer: 定时器结构体指针
183 输出参数 : 无
184 返 回 值 :
185 **************************************************************************** */
oal_timer_delete_sync(oal_timer_list_stru * pst_timer)186 static inline hi_s32 oal_timer_delete_sync(oal_timer_list_stru *pst_timer)
187 {
188 return del_timer_sync(pst_timer);
189 }
190
191 /* ****************************************************************************
192 功能描述 : 激活定时器
193 输入参数 : pst_timer: 定时器结构体指针
194 输出参数 : 无
195 返 回 值 :
196 **************************************************************************** */
oal_timer_add(oal_timer_list_stru * pst_timer)197 static inline hi_void oal_timer_add(oal_timer_list_stru *pst_timer)
198 {
199 add_timer(pst_timer);
200 }
201
202 /* ****************************************************************************
203 功能描述 : 重启定时器
204 输入参数 : pst_timer: 结构体指针
205 ui_expires: 重启的溢出事件
206 输出参数 : 无
207 返 回 值 :
208 **************************************************************************** */
oal_timer_start(oal_timer_list_stru * pst_timer,hi_u64 ui_delay)209 static inline hi_s32 oal_timer_start(oal_timer_list_stru *pst_timer, hi_u64 ui_delay)
210 {
211 if (pst_timer->flag == TIMER_UNVALID) {
212 pst_timer->expires = OAL_MSECS_TO_JIFFIES((hi_u32)ui_delay);
213 add_timer(pst_timer);
214 return 0;
215 } else {
216 return mod_timer(pst_timer, OAL_MSECS_TO_JIFFIES((hi_u32)ui_delay));
217 }
218 }
219
220 /* ****************************************************************************
221 功能描述 : 指定cpu,重启定时器,调用时timer要处于非激活状态否者会死机
222 输入参数 : pst_timer: 结构体指针
223 ui_expires: 重启的溢出事件
224 输出参数 : 无
225 返 回 值 :
226 **************************************************************************** */
oal_timer_start_on(oal_timer_list_stru * pst_timer,hi_u64 ui_delay,hi_s32 cpu)227 static inline hi_void oal_timer_start_on(oal_timer_list_stru *pst_timer, hi_u64 ui_delay, hi_s32 cpu)
228 {
229 hi_unref_param(cpu);
230 pst_timer->expires = OAL_MSECS_TO_JIFFIES(ui_delay);
231 add_timer(pst_timer);
232 }
233 #endif
234
235 #ifdef __cplusplus
236 #if __cplusplus
237 }
238 #endif
239 #endif
240
241 #endif /* end of oal_timer.h */
242