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	timer.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 /**	@page TIMER
28  *
29  *	Introduction
30  *	===============
31  *	B91 supports two timers: Timer0~ Timer1. The two timers all support four modes:
32  *		- Mode 0 (System Clock Mode),
33  *		- Mode 1 (GPIO Trigger Mode),
34  *		- Mode 2 (GPIO Pulse Width Mode),
35  *		- Mode 3 (Tick Mode),
36  *
37  *	Timer 1 can also be configured as "watchdog" to monitor firmware running.
38  *
39  *	API Reference
40  *	===============
41  *	Header File: timer.h
42  */
43 #ifndef TIMER_H_
44 #define TIMER_H_
45 
46 #include "analog.h"
47 #include "gpio.h"
48 #include "reg_include/register_b91.h"
49 
50 
51 /**********************************************************************************************************************
52  *                                         global constants                                                           *
53  *********************************************************************************************************************/
54 
55 
56 /**********************************************************************************************************************
57  *                                         global data type                                                           *
58  *********************************************************************************************************************/
59 /**
60  * @brief   Type of Timer
61  */
62 typedef enum{
63 	TIMER0		=0,
64 	TIMER1		=1,
65 }timer_type_e;
66 
67 
68 /**
69  * @brief   Mode of Timer
70  */
71 typedef enum{
72 	TIMER_MODE_SYSCLK		=0,
73 	TIMER_MODE_GPIO_TRIGGER	=1,
74 	TIMER_MODE_GPIO_WIDTH	=2,
75 	TIMER_MODE_TICK			=3,
76 }timer_mode_e;
77 
78 typedef enum{
79 	TMR_STA_TMR0   =		BIT(0),
80     TMR_STA_TMR1   =		BIT(1),
81 }time_irq_e;
82 
83 /**********************************************************************************************************************
84  *                                      global function prototype                                                     *
85  *********************************************************************************************************************/
86 
87 /*
88  * @brief    This function refer to get timer irq status.
89  * @param[in] status - variable of enum to select the timer interrupt source.
90  * @return   the status of timer0/timer1.
91  */
timer_get_irq_status(time_irq_e status)92 static inline unsigned char timer_get_irq_status(time_irq_e status)
93 {
94     return  reg_tmr_sta&status ;
95 }
96 
97 /*
98  * @brief     This function refer to clr timer0 irq status.
99  * @param[in] status - variable of enum to select the timerinterrupt source.
100  * @return    none
101  */
timer_clr_irq_status(time_irq_e status)102 static inline void timer_clr_irq_status(time_irq_e status)
103 {
104 		reg_tmr_sta= status;
105 }
106 
107 
108 /*
109  * @brief   This function refer to get timer0 tick.
110  * @return  none
111  */
timer0_get_gpio_width(void)112 static inline  unsigned int timer0_get_gpio_width(void)
113 {
114 	 return reg_tmr0_tick;
115 
116 }
117 
118 
119 /*
120  * @brief   This function refer to get timer1 tick.
121  * @return  none
122  */
timer1_get_gpio_width(void)123 static inline unsigned int timer1_get_gpio_width(void)
124 {
125 	return reg_tmr1_tick;
126 
127 }
128 
129 
130 /*
131  * @brief   This function refer to set timer0 tick .
132  * @param[in] tick - the tick of timer0
133  * @return  none
134  */
timer0_set_tick(unsigned int tick)135 static inline void timer0_set_tick(unsigned int tick)
136 {
137 	reg_tmr0_tick = tick;
138 }
139 
140 /*
141  * @brief   This function refer to get timer0 tick.
142  * @return  none
143  */
timer0_get_tick(void)144 static inline unsigned int timer0_get_tick(void)
145 {
146 	return reg_tmr0_tick ;
147 }
148 
149 
150 /*
151  * @brief   This function refer to set timer1 tick.
152  * @param[in] tick - the tick of timer1
153  * @return  none
154  */
timer1_set_tick(unsigned int tick)155 static inline void timer1_set_tick(unsigned int tick)
156 {
157 	reg_tmr1_tick = tick;
158 }
159 
160 /*
161  * @brief   This function refer to get timer1 tick.
162  * @return  none
163  */
timer1_get_tick(void)164 static inline unsigned int timer1_get_tick(void)
165 {
166 	return reg_tmr1_tick;
167 }
168 
169 /*
170  * @brief     This function set to initial tick for timr0/timer1.
171  * @param[in] type - timer0/timer1.
172  * @param[in] init_tick - initial tick value.
173  * @return    none
174  */
timer_set_init_tick(timer_type_e type,unsigned int init_tick)175 static inline void timer_set_init_tick(timer_type_e type, unsigned int init_tick)
176 {
177 	reg_tmr_tick(type) = init_tick;
178 }
179 /*
180  * @brief     This function set to capture tick for timr0/timer1.
181  * @param[in] type - timer0/timer1.
182  * @param[in] cap_tick - initial tick value.
183  * @return    none
184  */
timer_set_cap_tick(timer_type_e type,unsigned int cap_tick)185 static inline void timer_set_cap_tick(timer_type_e type, unsigned int cap_tick)
186 {
187 	reg_tmr_capt(type) = cap_tick;
188 }
189 
190 
191 
192 /**
193  * @brief     the specifed timer start working.
194  * @param[in] type - select the timer to start.
195  * @return    none
196  */
197 void timer_start(timer_type_e type);
198 
199 /**
200  * @brief     set mode, initial tick and capture of timer.
201  * @param[in] type - select the timer to start.
202  * @param[in] mode - select mode for timer.
203  * @return    none
204  */
205 void timer_set_mode(timer_type_e type, timer_mode_e mode);
206 
207 /**
208  * @brief     initiate GPIO for gpio trigger and gpio width mode of timer.
209  * @param[in] type - select the timer to start.
210  * @param[in] pin - select pin for timer.
211  * @param[in] pol - select polarity for gpio trigger and gpio width
212  * @return    none
213  */
214 void timer_gpio_init(timer_type_e type, gpio_pin_e pin, gpio_pol_e pol );
215 
216 
217 
218 /**
219  * @brief     the specifed timer stop working.
220  * @param[in] type - select the timer to stop.
221  * @return    none
222  */
223 void timer_stop(timer_type_e type);
224 
225 
226 
227 #endif /* TIMER_H_ */
228