• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: OS Abstract Layer.
15  */
16 
17 /**
18  * @defgroup osal_timer osal_timer
19  */
20 #ifndef __OSAL_TIMER_H__
21 #define __OSAL_TIMER_H__
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 typedef struct {
30     void *timer;
31     void (*handler)(unsigned long);
32     unsigned long data;    // data for handler
33     unsigned int interval; // timer timing duration, unit: ms.
34 } osal_timer;
35 
36 typedef struct {
37     long tv_sec;
38     long tv_usec;
39 } osal_timeval;
40 
41 typedef struct {
42     int tm_sec;
43     int tm_min;
44     int tm_hour;
45     int tm_mday;
46     int tm_mon;
47     int tm_year;
48     int tm_wday;
49     int tm_yday;
50     int tm_isdst;
51 } osal_rtc_time;
52 
53 /**
54  * @ingroup osal_timer
55  * @brief Initialize the timer.
56  *
57  * @par Description:
58  * Initialize the timer.
59  *
60  * @param timer [in/out] The timer to be initialized.
61  * Assign values to timer->handler and timer->data before calling this interface,
62  * because you will not be able to change them after that.
63  *
64  * @attention
65  * When mod exit, must call osal_timer_destroy to free the timer, otherwise will lead to memory leak;
66  *
67  * @return OSAL_SUCCESS/OSAL_FAILURE
68  *
69  * @par Support System:
70  * linux liteos freertos.
71  */
72 int osal_timer_init(osal_timer *timer);
73 
74 /**
75  * @ingroup osal_timer
76  * @brief start a timer.
77  *
78  * @par Description:
79  * start a timer.
80  *
81  * @attention
82  * The kernel will do a ->function(timer) callback from the timer interrupt at the ->expires point in the future.
83  * The timer's ->expires, ->function fields must be set prior calling this function.
84  * Timers with an ->expires field in the past will be executed in the next timer tick.
85  *
86  * @param timer [in] the timer to be start.
87  *
88  * @return OSAL_SUCCESS/OSAL_FAILURE
89  *
90  * @par Support System:
91  * linux liteos freertos.
92  */
93 int osal_timer_start(osal_timer *timer);
94 
95 /**
96  * @ingroup osal_timer
97  * @brief modify a timer's timeout.
98  *
99  * @par Description:
100  * modify a timer's timeout. This API is a more efficient way to update the expire field of an active timer
101  * (if the timer is inactive it will be activated).
102  *
103  * @param timer [in] the timer to be modified.
104  * @param interval [in] new timeout, unit: ms.
105  *
106  * @return OSAL_SUCCESS/OSAL_FAILURE
107  *
108  * @par Support System:
109  * linux liteos freertos.
110  */
111 int osal_timer_mod(osal_timer *timer, unsigned int interval);
112 
113 /**
114  * @ingroup osal_timer
115  * @brief Start a timer on a particular CPU.
116  *
117  * @par Description:
118  * Start a timer on a particular CPU.
119  *
120  * @param timer [in] the timer to be added.
121  * @param delay [in] delay time.
122  * @param cpu [in] the CPU to start it on.
123  *
124  * @return OSAL_SUCCESS/OSAL_FAILURE
125  *
126  * @par Support System:
127  * linux.
128  */
129 int osal_timer_start_on(osal_timer *timer, unsigned long delay, int cpu);
130 
131 /**
132  * @ingroup osal_timer
133  * @brief Deactivate a timer.
134  *
135  * @par Description:
136  * Deactivate a timer. This works on both active and inactive timers.
137  *
138  * @param timer [in] the timer to be deactivated.
139  *
140  * @retval 1 stop success, timer is pengding. (Only Linux and LiteOS are supported return 1)
141  * @retval OSAL_SUCCESS stop success, timer is already stoped.
142  * @retval OSAL_FAILURE
143  *
144  * @par Support System:
145  * linux liteos freertos.
146  */
147 int osal_timer_stop(osal_timer *timer);
148 
149 /**
150  * @ingroup osal_timer
151  * @brief destroy the timer.
152  *
153  * @par Description:
154  * destroy the timer.
155  *
156  * @param timer [in] The timer to be destroyed.
157  *
158  * @attention
159  * When mod exit, this function must be called to free the timer, otherwise will lead to memory leak;
160  *
161  * @return OSAL_SUCCESS/OSAL_FAILURE
162  *
163  * @par Support System:
164  * linux liteos freertos.
165  */
166 int osal_timer_destroy(osal_timer *timer);
167 
168 /**
169  * @ingroup osal_timer
170  * @brief get_private_data by timer.
171  *
172  * @par Description:
173  * The parameters of the timer callback function cannot be directly used.
174  * You need to invoke this interface in the callback function to obtain the parameters that can be used.
175  *
176  * @param sys_data [in] Parameters passed to the callback function.
177  *
178  * @return Returns the parameters that can be used directly.
179  *
180  * @par Support System:
181  * linux liteos freertos.
182  */
183 unsigned long osal_timer_get_private_data(const void *sys_data);
184 
185 /**
186  * @ingroup osal_timer
187  * @brief deactivate a timer and wait for the handler to finish.
188  *
189  * @par Description:
190  * This function only differs from del_timer() on SMP,
191  * besides deactivating the timer it also makes sure the handler has finished executing on other CPUs.
192  *
193  * @attention
194  * Synchronization rules: Callers must prevent restarting of the timer, otherwise this function is meaningless.
195  * It must not be called from interrupt contexts unless the timer is an irqsafe one.
196  * The caller must not hold locks which would prevent completion of the timer's handler.
197  * The timer's handler must not call add_timer_on().
198  * Upon exit the timer is not queued and the handler is not running on any CPU.
199  *
200  * @return OSAL_SUCCESS/OSAL_FAILURE
201  *
202  * @par Support System:
203  * linux.
204  */
205 int osal_timer_destroy_sync(osal_timer *timer);
206 
207 /**
208  * @ingroup osal_timer
209  * @brief Obtain system time in nanoseconds.
210  *
211  * @par Description:
212  * Obtain system time in nanoseconds.
213  *
214  * @return returns current time in nanoseconds.
215  *
216  * @par Support System:
217  * linux liteos.
218  */
219 unsigned long long osal_sched_clock(void);
220 
221 /**
222  * @ingroup osal_timer
223  * @brief Obtain the number of Ticks(in liteos) or jiffies(in linux).
224  *
225  * @par Description:
226  * Obtain the number of Ticks(in liteos) or jiffies(in linux).
227  *
228  * @return Return the number of Ticks(in liteos) or jiffies(in linux).
229  *
230  * @par Support System:
231  * linux liteos freertos.
232  */
233 unsigned long long osal_get_jiffies(void);
234 
235 /**
236  * @ingroup osal_timer
237  * @brief Convert milliseconds to Ticks/jiffies.
238  *
239  * @par Description:
240  * Convert milliseconds to Ticks/jiffies.
241  *
242  * @param m [in] The time to be converted. Unit: ms.
243  * @return Ticks/jiffies after conversion
244  *
245  * @par Support System:
246  * linux liteos freertos.
247  */
248 unsigned long osal_msecs_to_jiffies(const unsigned int m);
249 
250 /**
251  * @ingroup osal_timer
252  * @brief Convert Ticks/jiffies to milliseconds.
253  *
254  * @par Description:
255  * Convert Ticks/jiffies to milliseconds.
256  *
257  * @return Milliseconds obtained through the conversion,
258  * if value > 0xFFFFFFFF, the value will be 0xFFFFFFFF.
259  *
260  * @param n [in] Number of Ticks/jiffies be converted.
261  *
262  * @par Support System:
263  * linux liteos freertos.
264  */
265 unsigned int osal_jiffies_to_msecs(const unsigned int n);
266 
267 /**
268  * @ingroup osal_timer
269  * @brief Obtain the number of cycles in one tick.
270  *
271  * @par Description:
272  * This API is used to obtain the number of cycles in one tick.
273  *
274  * @return Number of cycles in one tick.
275  *
276  * @par Support System:
277  * liteos.
278  */
279 unsigned int osal_get_cycle_per_tick(void);
280 
281 /**
282  * @ingroup osal_timer
283  * @brief Obtaining the Current System Kernel Time.
284  *
285  * @par Description:
286  * Obtaining the Current System Kernel Time.
287  *
288  * @param tv [out] Obtained Current System Kernel Time.
289  *
290  * @par Support System:
291  * linux liteos freertos.
292  */
293 void osal_gettimeofday(osal_timeval *tv);
294 
295 
296 /* Return values for the timer callback function */
297 typedef enum {
298     OSAL_HRTIMER_NORESTART, /* < The timer will not be restarted. */
299     OSAL_HRTIMER_RESTART    /* < The timer must be restarted. */
300 } osal_hrtimer_restart;
301 
302 /* hrtimer struct */
303 typedef struct osal_hrtimer {
304     void *timer;
305     osal_hrtimer_restart (*handler)(void *timer);
306     unsigned long interval; /* Unit ms */
307 } osal_hrtimer;
308 
309 /**
310  * @ingroup osal_timer
311  * @brief Create a high-resolution timer.
312  *
313  * @par Description:
314  * This API is used to create a high-resolution timer node and initialize timer parameters.
315  *
316  * @param hrtimer [in/out] The hrtimer to be initialized.
317  * Assign values to hrtimer->handler and hrtimer->interval before calling this interface,
318  * because you will not be able to change them after that.
319  *
320  * @attention
321  * When mod exit, must call osal_hrtimer_destroy to free the timer, otherwise will lead to memory leak;
322  *
323  * @return OSAL_SUCCESS/OSAL_FAILURE
324  *
325  * @par Support System:
326  * liteos.
327  */
328 int osal_hrtimer_create(osal_hrtimer *hrtimer);
329 
330 /**
331  * @ingroup osal_timer
332  * @brief Start a high-resolution timer.
333  *
334  * @par Description:
335  * This API is used to add a high-resolution timer node to the global linked list and start timing.
336  *
337  * @retval    -1       The high-resolution timer fails to be started.
338  * @retval    0        The high-resolution timer is successfully started.
339  * @retval    1        The high-resolution timer node is already in the linked list.
340  *
341  * @par Support System:
342  * liteos.
343  */
344 int osal_hrtimer_start(osal_hrtimer *hrtimer);
345 
346 /**
347  * @ingroup osal_timer
348  * @brief Delete an existing high-resolution timer.
349  *
350  * @par Description:
351  * Delete an existing high-resolution timer.
352  *
353  * @attention
354  * If the pointer to the high-resolution timer is null or the timer node does not exist,
355  * the high-resolution timer fails to be deleted.
356  *
357  * @return OSAL_SUCCESS/OSAL_FAILURE
358  *
359  * @par Support System:
360  * liteos.
361  */
362 int osal_hrtimer_destroy(osal_hrtimer *hrtimer);
363 
364 #ifdef __cplusplus
365 #if __cplusplus
366 }
367 #endif
368 #endif
369 #endif /* __OSAL_TIMER_H__ */
370