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.c
21  *
22  * @brief	This is the source file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #include "timer.h"
28 /**********************************************************************************************************************
29  *                                         global function implementation                                             *
30  *********************************************************************************************************************/
31 
32 /**
33  * @brief     the specifed timer start working.
34  * @param[in] type - select the timer to start.
35  * @return    none
36  */
timer_start(timer_type_e type)37 void timer_start(timer_type_e type)
38 {
39  	switch(type)
40  	{
41  		case TIMER0:
42  			reg_tmr_ctrl0 |= FLD_TMR0_EN;
43  			break;
44  		case TIMER1:
45  			reg_tmr_ctrl0 |= FLD_TMR1_EN;
46  			break;
47  		default:
48  			break;
49  	}
50 }
51 
52 /**
53  * @brief     the specifed timer stop working.
54  * @param[in] type - select the timer to stop.
55  * @return    none
56  */
timer_stop(timer_type_e type)57 void timer_stop(timer_type_e type)
58 {
59 	switch(type)
60 	{
61 		case TIMER0:
62 			reg_tmr_ctrl0 &= (~FLD_TMR0_EN);
63 			break;
64 		case TIMER1:
65 			reg_tmr_ctrl0 &= (~FLD_TMR1_EN);
66 			break;
67 		default:
68 			break;
69 	}
70 }
71 
72 
73 
74 /**
75  * @brief     set mode, initial tick and capture of timer.
76  * @param[in] type - select the timer to start.
77  * @param[in] mode - select mode for timer.
78  * @param[in] init_tick - initial tick.
79  * @param[in] cap_tick  - tick of capture.
80  * @return    none
81  */
timer_set_mode(timer_type_e type,timer_mode_e mode)82 void timer_set_mode(timer_type_e type, timer_mode_e mode)
83 {
84 	switch(type)
85  	{
86  		case TIMER0:
87  			reg_tmr_sta = FLD_TMR_STA_TMR0; //clear irq status
88  		 	reg_tmr_ctrl0 &= (~FLD_TMR0_MODE);
89  		 	reg_tmr_ctrl0 |= mode;
90  			break;
91  		case TIMER1:
92  			reg_tmr_sta = FLD_TMR_STA_TMR1; //clear irq status
93  			reg_tmr_ctrl0 &= (~FLD_TMR1_MODE);
94  			reg_tmr_ctrl0 |= (mode<<4);
95  			break;
96  		default:
97  			break;
98  	}
99 
100 }
101 
102 /**
103  * @brief     initiate GPIO for gpio trigger and gpio width mode of timer.
104  * @param[in] type - select the timer to start.
105  * @param[in] pin - select pin for timer.
106  * @param[in] pol - select polarity for gpio trigger and gpio width
107  * @return    none
108  */
timer_gpio_init(timer_type_e type,gpio_pin_e pin,gpio_pol_e pol)109 void timer_gpio_init(timer_type_e type, gpio_pin_e pin, gpio_pol_e pol )
110 {
111 	gpio_function_en(pin);
112 	gpio_output_dis(pin); 	//disable output
113 	gpio_input_en(pin);		//enable input
114  	switch(type)
115  	{
116  		case TIMER0:
117  		 	if(pol==POL_FALLING)
118  		 	{
119  		 		gpio_set_up_down_res(pin,GPIO_PIN_PULLUP_10K);
120  		 		gpio_set_gpio2risc0_irq(pin,INTR_LOW_LEVEL);
121  		 		gpio_gpio2risc0_irq_en(pin);
122  		 	}
123  		 	else if(pol==POL_RISING)
124  		 	{
125  		 		gpio_set_up_down_res(pin,GPIO_PIN_PULLDOWN_100K);
126  		 		gpio_set_gpio2risc0_irq(pin,INTR_HIGH_LEVEL);
127  		 		gpio_gpio2risc0_irq_en(pin);
128  		 	}
129  			break;
130 
131  		case TIMER1:
132  		 	if(pol==POL_FALLING)
133  		 	{
134  		 		gpio_set_up_down_res(pin,GPIO_PIN_PULLUP_10K);
135  		 		gpio_set_gpio2risc1_irq(pin,INTR_LOW_LEVEL);
136  		 		gpio_gpio2risc1_irq_en(pin);
137  		 	}
138  		 	else if(pol==POL_RISING)
139  		 	{
140  		 		gpio_set_up_down_res(pin,GPIO_PIN_PULLDOWN_100K);
141  		 		gpio_set_gpio2risc1_irq(pin,INTR_HIGH_LEVEL);
142  		 		gpio_gpio2risc1_irq_en(pin);
143 
144  		 	}
145  			break;
146 
147  		default:
148  			break;
149  	}
150 
151 }
152 
153 
154