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 /** @page STIMER
19 *
20 * Introduction
21 * ===============
22 * TLSRB91 stimer use 16M clock count, have stimer irq.
23 *
24 * API Reference
25 * ===============
26 * Header File: uart.h
27 */
28 #ifndef STIMER_H_
29 #define STIMER_H_
30 #include "compiler.h"
31 #include "reg_include/stimer_reg.h"
32
33 /**********************************************************************************************************************
34 * global constants *
35 *********************************************************************************************************************/
36
37 /**********************************************************************************************************************
38 * global macro *
39 *********************************************************************************************************************/
40
41 /**********************************************************************************************************************
42 * global data type *
43 *********************************************************************************************************************/
44 /**********************************************************************************************************************
45 * global variable declaration *
46 *********************************************************************************************************************/
47
48 /**********************************************************************************************************************
49 * global function prototype *
50 *********************************************************************************************************************/
51 /**
52 * @brief define system clock tick per us/ms/s.
53 */
54 enum {
55 SYSTEM_TIMER_TICK_1US = 16,
56 SYSTEM_TIMER_TICK_1MS = 16000,
57 SYSTEM_TIMER_TICK_1S = 16000000,
58
59 SYSTEM_TIMER_TICK_625US = 10000, // 625*16
60 SYSTEM_TIMER_TICK_1250US = 20000, // 1250*16
61 };
62
63 /**
64 * @brief This function servers to set stimer irq mask.
65 * @param[in] mask - the irq mask.
66 * @return none.
67 */
stimer_set_irq_mask(stimer_irq_e mask)68 static inline void stimer_set_irq_mask(stimer_irq_e mask)
69 {
70 reg_system_irq_mask |= mask;
71 }
72
73 /**
74 * @brief This function servers to clear stimer irq mask.
75 * @param[in] mask - the irq mask.
76 * @return none.
77 */
stimer_clr_irq_mask(stimer_irq_e mask)78 static inline void stimer_clr_irq_mask(stimer_irq_e mask)
79 {
80 reg_system_irq_mask &= (~mask);
81 }
82
83 /**
84 * @brief This function servers to clear stimer irq status.
85 * @param[in] status - the irq status.
86 * @return none.
87 */
stimer_clr_irq_status(stimer_irq_e status)88 static inline void stimer_clr_irq_status(stimer_irq_e status)
89 {
90 reg_system_cal_irq = (status);
91 }
92
93 /**
94 * @brief This function servers to get stimer irq status.
95 * @param[in] status - the irq status.
96 * @return none.
97 */
stimer_get_irq_status(stimer_irq_e status)98 static inline unsigned char stimer_get_irq_status(stimer_irq_e status)
99 {
100 return (reg_system_cal_irq & status);
101 }
102
103 /**
104 * @brief This function servers to set tick irq capture.
105 * @param[in] tick - the value of irq tick.
106 * @return none.
107 */
stimer_set_irq_capture(unsigned int tick)108 static inline void stimer_set_irq_capture(unsigned int tick)
109 {
110 reg_system_irq_level = (tick);
111 }
112
113 /**
114 * @brief This function servers to set stimer tick.
115 * @param[in] tick - the value of tick.
116 * @return none.
117 */
stimer_set_tick(unsigned int tick)118 static inline void stimer_set_tick(unsigned int tick)
119 {
120 reg_system_tick = (tick);
121 }
122
123 /**
124 * @brief This function servers to enable stimer.
125 * @return none.
126 */
stimer_enable(void)127 static inline void stimer_enable(void)
128 {
129 reg_system_ctrl |= FLD_SYSTEM_TIMER_EN;
130 }
131
132 /**
133 * @brief This function servers to disable stimer.
134 * @return none.
135 */
stimer_disable(void)136 static inline void stimer_disable(void)
137 {
138 reg_system_ctrl &= ~(FLD_SYSTEM_TIMER_EN);
139 }
140
141 /*
142 * @brief This function performs to get system timer tick.
143 * @return system timer tick value.
144 **/
stimer_get_tick(void)145 static inline unsigned int stimer_get_tick(void)
146 {
147 return reg_system_tick;
148 }
149
150 /**
151 * @brief This function serves to set timeout by us.
152 * @param[in] ref - reference tick of system timer .
153 * @param[in] us - count by us.
154 * @return true - timeout, false - not timeout
155 */
clock_time_exceed(unsigned int ref,unsigned int us)156 static inline _Bool clock_time_exceed(unsigned int ref, unsigned int us)
157 {
158 return ((unsigned int)(stimer_get_tick() - ref) > us * SYSTEM_TIMER_TICK_1US);
159 }
160 /**
161 * @brief This function performs to set delay time by us.
162 * @param[in] microsec - need to delay.
163 * @return none
164 */
165 _attribute_ram_code_sec_noinline_ void delay_us(unsigned int microsec);
166
167 /**
168 * @brief This function performs to set delay time by ms.
169 * @param[in] millisec - need to delay.
170 * @return none
171 */
172 _attribute_ram_code_sec_noinline_ void delay_ms(unsigned int millisec);
173
174 #endif /* STIMER_H_ */
175