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