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