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