• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #ifndef BLT_SOFT_TIMER_H_
19 #define BLT_SOFT_TIMER_H_
20 
21 // user define
22 #ifndef BLT_SOFTWARE_TIMER_ENABLE
23 #define BLT_SOFTWARE_TIMER_ENABLE 0  // enable or disable
24 #endif
25 
26 #define MAX_TIMER_NUM 4  // timer max number
27 
28 #define MAINLOOP_ENTRY 0
29 #define CALLBACK_ENTRY 1
30 
31 // if t1 < t2  return 1
32 #define TIME_COMPARE_SMALL(t1, t2) ((u32)((t2) - (t1)) < BIT(30))
33 
34 // if t1 > t2 return 1
35 #define TIME_COMPARE_BIG(t1, t2) ((u32)((t1) - (t2)) < BIT(30))
36 
37 #define BLT_TIMER_SAFE_MARGIN_PRE  (SYSTEM_TIMER_TICK_1US << 7)  // 128 us
38 #define BLT_TIMER_SAFE_MARGIN_POST (SYSTEM_TIMER_TICK_1S << 2)   // 4S
39 
40 /**
41  * @brief		This function is used to check the current time is what the timer expects or not
42  * @param[in]	t - the time is expired for setting
43  * @param[in]   now - Current system clock time
44  * @return		0 - The current time isn't what the timer expects
45  * 				1 - The current time is what the timer expects
46  */
blt_is_timer_expired(u32 t,u32 now)47 static int inline blt_is_timer_expired(u32 t, u32 now)
48 {
49     return ((u32)(now + BLT_TIMER_SAFE_MARGIN_PRE - t) < BLT_TIMER_SAFE_MARGIN_POST);
50 }
51 
52 /**
53  * @brief	callback function for software timer task
54  */
55 typedef int (*blt_timer_callback_t)(void);
56 
57 typedef struct blt_time_event_t {
58     blt_timer_callback_t cb;
59     u32 t;
60     u32 interval;
61 } blt_time_event_t;
62 
63 // timer table managemnt
64 typedef struct blt_soft_timer_t {
65     blt_time_event_t timer[MAX_TIMER_NUM];  // timer0 - timer3
66     u8 currentNum;                          // total valid timer num
67 } blt_soft_timer_t;
68 
69 //////////////////////// USER  INTERFACE ///////////////////////////////////
70 // return 0 means Fail, others OK
71 /**
72  * @brief		This function is used to add new software timer task
73  * @param[in]	func - callback function for software timer task
74  * @param[in]	interval_us - the interval for software timer task
75  * @return      0 - timer task is full, add fail
76  * 				1 - create successfully
77  */
78 int blt_soft_timer_add(blt_timer_callback_t func, u32 interval_us);
79 
80 /**
81  * @brief		This function is used to delete timer tasks
82  * @param[in]	func - callback function for software timer task
83  * @return      0 - delete fail
84  * 				1 - delete successfully
85  */
86 int blt_soft_timer_delete(blt_timer_callback_t func);
87 
88 //////////////////////// SOFT TIMER MANAGEMENT  INTERFACE ///////////////////////////////////
89 
90 /**
91  * @brief		This function is used to register the call back for pm_appWakeupLowPowerCb
92  * @param[in]	none
93  * @return      none
94  */
95 void blt_soft_timer_init(void);
96 
97 /**
98  * @brief		This function is used to manage software timer tasks
99  * @param[in]	type - the type for trigger
100  * @return      none
101  */
102 void blt_soft_timer_process(int type);
103 
104 /**
105  * @brief		Timer tasks are originally ordered. When deleting, it will
106  * 				be overwritten forward, so the order will not be destroyed
107  * 				and there is no need to reorder
108  * @param[in]	index - the index for some software timer task
109  * @return      0 - delete fail
110  * 				other - delete successfully
111  */
112 int blt_soft_timer_delete_by_index(u8 index);
113 
114 /**
115  * @brief		This function is used to check the current time is what the timer expects or not
116  * @param[in]	e - callback function for software timer task
117  * @return		none
118  */
119 int is_timer_expired(blt_timer_callback_t *e);
120 
121 #endif /* BLT_SOFT_TIMER_H_ */
122