• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @defgroup los_tick
34  * @ingroup kernel
35  */
36 
37 #ifndef _LOS_TICK_H
38 #define _LOS_TICK_H
39 
40 #include "los_error.h"
41 #include "los_timer.h"
42 
43 #ifdef __cplusplus
44 #if __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
47 #endif /* __cplusplus */
48 
49 /**
50  * @ingroup los_tick
51  * Tick error code: The Tick configuration is incorrect.
52  *
53  * Value: 0x02000400
54  *
55  * Solution: Change values of the OS_SYS_CLOCK and LOSCFG_BASE_CORE_TICK_PER_SECOND
56  * system time configuration modules in Los_config.h.
57  */
58 #define LOS_ERRNO_TICK_CFG_INVALID       LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x00)
59 
60 /**
61  * @ingroup los_tick
62  * Tick error code: The system tick timer uninitialized.
63  *
64  * Value: 0x02000401
65  *
66  * Solution: None.
67  */
68 #define LOS_ERRNO_TICK_NO_HWTIMER        LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x01)
69 
70 /**
71  * @ingroup los_tick
72  * Tick error code: The number of Ticks is too small.
73  *
74  * Value: 0x02000402
75  *
76  * Solution: Change values of the OS_SYS_CLOCK and LOSCFG_BASE_CORE_TICK_PER_SECOND
77  * system time configuration modules according to the SysTick_Config function.
78  */
79 #define LOS_ERRNO_TICK_PER_SEC_TOO_SMALL LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x02)
80 
81 /**
82  *  @ingroup  los_tick
83  *  @brief: System timer cycles get function.
84  *
85  *  @par Description:
86  *  This API is used to get system timer cycles.
87  *
88  * @attention:
89  * <ul><li>None.</li></ul>
90  *
91  * @param: None.
92  *
93  * @retval: current system cycles.
94  *
95  * @par Dependency:
96  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
97  * @see None.
98  *
99  * */
100 extern UINT64 LOS_SysCycleGet(VOID);
101 
102 /**
103  * @ingroup los_tick
104  * Number of milliseconds in one second.
105  */
106 #define OS_SYS_MS_PER_SECOND   1000
107 
108 /**
109  * @ingroup los_tick
110  * Ticks per second
111  */
112 extern UINT32    g_ticksPerSec;
113 
114 /**
115  * @ingroup los_tick
116  * Cycles per Second
117  */
118 extern UINT32    g_uwCyclePerSec;
119 
120 /**
121  * @ingroup los_tick
122  * Cycles per Tick
123  */
124 extern UINT32    g_cyclesPerTick;
125 
126 /**
127  * @ingroup los_tick
128  * System Clock
129  */
130 extern UINT32    g_sysClock;
131 
132 /**
133  * @ingroup los_tick
134  * Number of microseconds in one second.
135  */
136 #define OS_SYS_US_PER_SECOND   1000000
137 
138 #define OS_SYS_NS_PER_SECOND   1000000000
139 
140 #define OS_SYS_NS_PER_MS       1000000
141 
142 #define OS_SYS_NS_PER_US       1000
143 
144 #define OS_CYCLE_PER_TICK      (g_sysClock / LOSCFG_BASE_CORE_TICK_PER_SECOND)
145 
146 #define OS_NS_PER_CYCLE        (OS_SYS_NS_PER_SECOND / g_sysClock)
147 
148 #define OS_MS_PER_TICK         (OS_SYS_MS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
149 
150 #define OS_US_PER_TICK         (OS_SYS_US_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
151 
152 #define OS_NS_PER_TICK         (OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
153 
154 #define OS_SYS_CYCLE_TO_NS(cycle, freq)  ((cycle) / (freq)) * OS_SYS_NS_PER_SECOND + \
155     ((cycle) % (freq) * OS_SYS_NS_PER_SECOND / (freq))
156 
157 #define OS_SYS_NS_TO_CYCLE(time, freq) (((time) / OS_SYS_NS_PER_SECOND) * (freq) +     \
158     ((time) % OS_SYS_NS_PER_SECOND) * (freq) / OS_SYS_NS_PER_SECOND)
159 
160 #define OS_SYS_TICK_TO_CYCLE(ticks) (((UINT64)(ticks) * g_sysClock) / LOSCFG_BASE_CORE_TICK_PER_SECOND)
161 
162 #define OS_SYS_CYCLE_TO_TICK(cycle) ((((UINT64)(cycle)) * LOSCFG_BASE_CORE_TICK_PER_SECOND) / g_sysClock)
163 
164 /**
165  * @ingroup los_tick
166  * System time basic function error code: Null pointer.
167  *
168  * Value: 0x02000010
169  *
170  * Solution: Check whether the input parameter is null.
171  */
172 #define LOS_ERRNO_SYS_PTR_NULL                 LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x10)
173 
174 /**
175  * @ingroup los_tick
176  * System time basic function error code: Invalid system clock configuration.
177  *
178  * Value: 0x02000011
179  *
180  * Solution: Configure a valid system clock in los_config.h.
181  */
182 #define LOS_ERRNO_SYS_CLOCK_INVALID            LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x11)
183 
184 /**
185  * @ingroup los_tick
186  * System time basic function error code: This error code is not in use temporarily.
187  *
188  * Value: 0x02000012
189  *
190  * Solution: None.
191  */
192 #define LOS_ERRNO_SYS_MAXNUMOFCORES_IS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x12)
193 
194 /**
195  * @ingroup los_tick
196  * System time error code: This error code is not in use temporarily.
197  *
198  * Value: 0x02000013
199  *
200  * Solution: None.
201  */
202 #define LOS_ERRNO_SYS_PERIERRCOREID_IS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x13)
203 
204 /**
205  * @ingroup los_tick
206  * System time error code: This error code is not in use temporarily.
207  *
208  * Value: 0x02000014
209  *
210  * Solution: None.
211  */
212 #define LOS_ERRNO_SYS_HOOK_IS_FULL             LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x14)
213 
214 /**
215  * @ingroup los_tick
216  * System time error code: The Tick Timer must be registered before kernel initialization.
217  *
218  * Value: 0x02000015
219  *
220  * Solution: None.
221  */
222 #define LOS_ERRNO_SYS_TIMER_IS_RUNNING         LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x15)
223 
224 /**
225  * @ingroup los_tick
226  * System time error code: The tick timer method is NULL.
227  *
228  * Value: 0x02000016
229  *
230  * Solution: None.
231  */
232 #define LOS_ERRNO_SYS_HOOK_IS_NULL             LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x16)
233 
234 /**
235  * @ingroup los_tick
236  * System time error code: The tick timer addr fault.
237  *
238  * Value: 0x02000017
239  *
240  * Solution: None.
241  */
242 #define LOS_ERRNO_SYS_TIMER_ADDR_FAULT          LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x16)
243 
244 /**
245  * @ingroup los_tick
246  * system time structure.
247  */
248 typedef struct TagSysTime {
249     UINT16  uwYear;    /**< value 1970 ~ 2038 or 1970 ~ 2100 */
250     UINT8   ucMonth;   /**< value 1 - 12 */
251     UINT8   ucDay;     /**< value 1 - 31 */
252     UINT8   ucHour;    /**< value 0 - 23 */
253     UINT8   ucMinute;  /**< value 0 - 59 */
254     UINT8   ucSecond;  /**< value 0 - 59 */
255     UINT8   ucWeek;    /**< value 0 - 6  */
256 } SYS_TIME_S;
257 
258 UINT64 OsTickTimerReload(UINT64 period);
259 
260 #if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
261 VOID OsTickTimerBaseReset(UINT64 currTime);
262 #endif
263 
264 UINT32 OsTickTimerInit(VOID);
265 
266 VOID OsTickSysTimerStartTimeSet(UINT64 currTime);
267 
268 /**
269  * @ingroup los_tick
270  * @brief Obtain the number of Ticks.
271  *
272  * @par Description:
273  * This API is used to obtain the number of Ticks.
274  * @attention
275  * <ul>
276  * <li>None</li>
277  * </ul>
278  *
279  * @param  None
280  *
281  * @retval UINT64 The number of Ticks.
282  * @par Dependency:
283  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
284  * @see None
285  */
286 extern UINT64 LOS_TickCountGet(VOID);
287 
288 /**
289  * @ingroup los_tick
290  * @brief Obtain the number of cycles in one second.
291  *
292  * @par Description:
293  * This API is used to obtain the number of cycles in one second.
294  * @attention
295  * <ul>
296  * <li>None</li>
297  * </ul>
298  *
299  * @param  None
300  *
301  * @retval UINT32 Number of cycles obtained in one second.
302  * @par Dependency:
303  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
304  * @see None
305  */
306 extern UINT32 LOS_CyclePerTickGet(VOID);
307 
308 /**
309  * @ingroup los_tick
310  * @brief Convert Ticks to milliseconds.
311  *
312  * @par Description:
313  * This API is used to convert Ticks to milliseconds.
314  * @attention
315  * <ul>
316  * <li>The number of milliseconds obtained through the conversion is 32-bit.</li>
317  * </ul>
318  *
319  * @param  ticks  [IN] Number of Ticks. The value range is (0,OS_SYS_CLOCK).
320  *
321  * @retval UINT32 Number of milliseconds obtained through the conversion. Ticks are successfully converted to
322  * milliseconds.
323  * @par  Dependency:
324  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
325  * @see LOS_MS2Tick
326  */
327 extern UINT32 LOS_Tick2MS(UINT32 ticks);
328 
329 /**
330  * @ingroup los_tick
331  * @brief Convert milliseconds to Ticks.
332  *
333  * @par Description:
334  * This API is used to convert milliseconds to Ticks.
335  * @attention
336  * <ul>
337  * <li>If the parameter passed in is equal to 0xFFFFFFFF, the retval is 0xFFFFFFFF. Pay attention to the value to be
338  * converted because data possibly overflows.</li>
339  * </ul>
340  *
341  * @param  millisec  [IN] Number of milliseconds.
342  *
343  * @retval UINT32 Number of Ticks obtained through the conversion.
344  * @par Dependency:
345  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
346  * @see LOS_Tick2MS
347  */
348 extern UINT32 LOS_MS2Tick(UINT32 millisec);
349 
350 /**
351  * @ingroup los_tick
352  * @brief Re-initializes the system tick timer.
353  *
354  * @par Description:
355  * This API is used to re-initialize the system Tick timer.
356  * @attention
357  *
358  * @param timer        [IN] Specify the tick timer.
359  * @param tickHandler  [IN] Tick Interrupts the execution of the hook function.
360  *
361  * @retval LOS_OK or Error code.
362  * @par Dependency:
363  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
364  * @see
365  */
366 extern UINT32 LOS_TickTimerRegister(const ArchTickTimer *timer, const HWI_PROC_FUNC tickHandler);
367 
368 /* *
369  * @ingroup  los_task
370  * @brief: cpu delay.
371  *
372  * @par Description:
373  * This API is used to cpu delay, no task switching.
374  *
375  * @attention:
376  * <ul><li>None.</li></ul>
377  *
378  * @param  UINT64  [IN] delay times, microseconds.
379  *
380  * @retval: None.
381  * @par Dependency:
382  * <ul><li>los_task.h: the header file that contains the API declaration.</li></ul>
383  * @see None.
384  */
385 extern VOID LOS_UDelay(UINT64 microseconds);
386 
387 /* *
388  * @ingroup  los_task
389  * @brief: cpu delay.
390  *
391  * @par Description:
392  * This API is used to cpu delay, no task switching.
393  *
394  * @attention:
395  * <ul><li>None.</li></ul>
396  *
397  * @param  UINT32  [IN] delay times, millisecond.
398  *
399  * @retval: None.
400  * @par Dependency:
401  * <ul><li>los_task.h: the header file that contains the API declaration.</li></ul>
402  * @see None.
403  */
404 extern VOID LOS_MDelay(UINT32 millisec);
405 
406 /* *
407  * @ingroup  los_task
408  * @brief: cpu nanosecond get.
409  *
410  * @par Description:
411  * This API is used to get the current number of nanoseconds.
412  *
413  * @attention:
414  * <ul><li>None.</li></ul>
415  *
416  * @param  none.
417  *
418  * @retval: None.
419  * @par Dependency:
420  * <ul><li>los_task.h: the header file that contains the API declaration.</li></ul>
421  * @see None.
422  */
423 extern UINT64 LOS_CurrNanosec(VOID);
424 
425 /**
426  * @ingroup  los_tick
427  * @brief Handle the system tick timeout.
428  *
429  * @par Description:
430  * This API is called when the system tick timeout and triggers the interrupt.
431  *
432  * @attention
433  * <ul>
434  * <li>None.</li>
435  * </ul>
436  *
437  * @param none.
438  *
439  * @retval None.
440  * @par Dependency:
441  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
442  * @see None.
443  */
444 extern VOID OsTickHandler(VOID);
445 
446 /**
447  * @ingroup los_tick
448  * Define the CPU Tick structure.
449  */
450 typedef struct TagCpuTick {
451     UINT32 cntHi; /* < Upper 32 bits of the tick value */
452     UINT32 cntLo; /* < Lower 32 bits of the tick value */
453 } CpuTick;
454 
455 /**
456  * @ingroup los_tick
457  * Number of operable bits of a 32-bit operand
458  */
459 #define OS_SYS_MV_32_BIT       32
460 
461 /**
462  * @ingroup los_tick
463  * Number of milliseconds in one second.
464  */
465 #define OS_SYS_MS_PER_SECOND   1000
466 
467 /**
468  * @ingroup los_tick
469  * Number of microseconds in one second.
470  */
471 #define OS_SYS_US_PER_SECOND   1000000
472 
473 #define OS_SYS_US_PER_MS       1000
474 
475 /**
476  * @ingroup los_tick
477  * The maximum length of name.
478  */
479 #define OS_SYS_APPVER_NAME_MAX 64
480 
481 /**
482  * @ingroup los_tick
483  * The magic word.
484  */
485 #define OS_SYS_MAGIC_WORD      0xAAAAAAAA
486 
487 /**
488  * @ingroup los_tick
489  * The initialization value of stack space.
490  */
491 #define OS_SYS_EMPTY_STACK     0xCACACACA
492 
493 /**
494  * @ingroup los_tick
495  * @brief Convert cycles to milliseconds.
496  *
497  * @par Description:
498  * This API is used to convert cycles to milliseconds.
499  * @attention
500  * <ul>
501  * <li>None.</li>
502  * </ul>
503  *
504  * @param  cpuTick  [IN]  Number of CPU cycles.
505  * @param  msHi     [OUT] Upper 32 bits of the number of milliseconds.
506  * @param  msLo     [OUT] Lower 32 bits of the number of milliseconds.
507  *
508  * @retval #LOS_ERRNO_SYS_PTR_NULL    0x02000011: Invalid parameter.
509  * @retval #LOS_OK                   0:  Cycles are successfully converted to microseconds.
510  * @par Dependency:
511  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
512  * @see None.
513  */
514 extern UINT32 OsCpuTick2MS(CpuTick *cpuTick, UINT32 *msHi, UINT32 *msLo);
515 
516 /**
517  * @ingroup los_tick
518  * @brief Convert cycles to microseconds.
519  *
520  * @par Description:
521  * This API is used to convert cycles to microseconds.
522  * @attention
523  * <ul>
524  * <li>None.</li>
525  * </ul>
526  *
527  * @param  cpuTick  [IN]  Number of CPU cycles.
528  * @param  usHi     [OUT] Upper 32 bits of the number of microseconds.
529  * @param  usLo     [OUT] Lower 32 bits of the number of microseconds.
530  *
531  * @retval #LOS_ERRNO_SYS_PTR_NULL    0x02000011: Invalid parameter.
532  * @retval #LOS_OK                   0: Cycles are successfully converted to microseconds.
533  * @par Dependency:
534  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
535  * @see None.
536  */
537 extern UINT32 OsCpuTick2US(CpuTick *cpuTick, UINT32 *usHi, UINT32 *usLo);
538 
539 /**
540  * @ingroup los_tick
541  * @brief Convert cycles to milliseconds.
542  *
543  * @par Description:
544  * This API is used to convert cycles to milliseconds.
545  * @attention
546  * <ul>
547  * <li>None.</li>
548  * </ul>
549  *
550  * @param  cycle     [IN] Number of cycles.
551  *
552  * @retval Number of milliseconds obtained through the conversion.    Cycles are successfully converted to milliseconds.
553  * @par Dependency:
554  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
555  * @see None.
556  */
OsCycle2MS(UINT64 cycle)557 STATIC_INLINE UINT64 OsCycle2MS(UINT64 cycle)
558 {
559     return (UINT64)((cycle / (g_sysClock / OS_SYS_MS_PER_SECOND)));
560 }
561 
562 /**
563  * @ingroup los_tick
564  * @brief Convert cycles to microseconds.
565  *
566  * @par Description:
567  * This API is used to convert cycles to microseconds.
568  * @attention
569  * <ul>
570  * <li>None.</li>
571  * </ul>
572  *
573  * @param  cycle     [IN] Number of cycles.
574  *
575  * @retval Number of microseconds obtained through the conversion. Cycles are successfully converted to microseconds.
576  * @par Dependency:
577  * <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
578  * @see None.
579  */
OsCycle2US(UINT64 cycle)580 STATIC_INLINE UINT64 OsCycle2US(UINT64 cycle)
581 {
582     UINT64 tmp = g_sysClock / OS_SYS_US_PER_SECOND;
583     if (tmp == 0) {
584         return 0;
585     }
586     return (UINT64)(cycle / tmp);
587 }
588 
589 
590 #ifdef __cplusplus
591 #if __cplusplus
592 }
593 #endif /* __cplusplus */
594 #endif /* __cplusplus */
595 
596 #endif /* _LOS_TICK_H */
597