• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #pragma once
17 
18 #include <common/bk_include.h>
19 #include <driver/timer_types.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* @brief Overview about this API header
26  *
27  */
28 
29 /**
30  * @brief Timer API
31  * @defgroup bk_api_timer timer API group
32  * @{
33  */
34 
35 /**
36  * @brief     Init the timer driver
37  *
38  * This API init the resoure common to all timer channels:
39  *   - Init timer driver control memory
40  *
41  * This API should be called before any other timer APIs.
42  *
43  * @return
44  *    - BK_OK: succeed
45  *    - others: other errors.
46  */
47 bk_err_t bk_timer_driver_init(void);
48 
49 /**
50  * @brief     Deinit the timer driver
51  *
52  * This API free all resource related to timer and power down all timer channels.
53  *
54  * @return
55  *    - BK_OK: succeed
56  *    - others: other errors.
57  */
58 bk_err_t bk_timer_driver_deinit(void);
59 
60 /**
61  * @brief     Start the timer
62  *
63  * @attention 1. timer2 has been used for time cali
64  * @attention 2. timer3 has been used for clock, but bk7271 use timer1
65  * @attention 3. bk7271 has used timer0(10ms)
66  *
67  * @param timer_id the timer ID to be started
68  * @param time_ms time delay value of the timer
69  * @param callback the timer call back function
70  *
71  * @return
72  *    - BK_OK: succeed
73  *    - BK_ERR_TIMER_NOT_INIT: timer driver not init
74  *    - BK_ERR_TIMER_ID: timer id is invalid
75  *    - BK_ERR_TIMER_IS_RUNNING: timer id is running
76  *    - others: other errors.
77  */
78 bk_err_t bk_timer_start(timer_id_t timer_id, uint32_t time_ms, timer_isr_t callback);
79 
80 /**
81  * @brief     Start the timer without set callback
82  *
83  * @param timer_id the timer ID to be started
84  * @param timer_ms time delay value of the timer
85  *
86  * @return
87  *    - BK_OK: succeed
88  *    - others: others errors
89  */
90 bk_err_t bk_timer_start_without_callback(timer_id_t timer_id, uint32_t time_ms);
91 
92 /**
93  * @brief     Stop the timer
94  *
95  * @param timer_id the timer ID to be stoped
96  *
97  * @return
98  *    - BK_OK: succeed
99  *    - others: other errors.
100  */
101 bk_err_t bk_timer_stop(timer_id_t timer_id);
102 
103 /**
104  * @brief     Get the current counter value
105  *
106  * @attention 1. The caller need to make sure the parameter timer_id is correct!
107  *
108  * @param timer_id the timer id
109  *
110  * @return counter value
111  */
112 uint32_t bk_timer_get_cnt(timer_id_t timer_id);
113 
114 /**
115  * @brief     Enable the timer
116  *
117  * @param timer_id the timer ID
118  *
119  * @return
120  *    - BK_OK: succeed
121  *    - others: other errors.
122  */
123 bk_err_t bk_timer_enable(timer_id_t timer_id);
124 
125 /**
126  * @brief     Disable the timer
127  *
128  * @param timer_id the timer ID
129  *
130  * @return
131  *    - BK_OK: succeed
132  *    - others: other errors.
133  */
134 bk_err_t bk_timer_disable(timer_id_t timer_id);
135 
136 /**
137  * @brief     Get the counter value set by bk_timer_start()
138  *
139  * @param timer_id the timer id
140  *
141  * @return counter value set by bk_timer_start()
142  */
143 uint32_t bk_timer_get_period(timer_id_t timer_id);
144 
145 /**
146  * @brief     Get the timer enable status
147  *
148  * @param timer_id the timer id
149  *
150  * @return timer enable status
151  */
152 uint32_t bk_timer_get_enable_status(void);
153 
154 /**
155  * @brief     Get whether the timer interrupt triggered
156  *
157  * @param timer_id the timer id
158  *
159  * @return the timer interrupt status
160  */
161 bool bk_timer_is_interrupt_triggered(timer_id_t timer_id);
162 
163 /**
164  * @brief     Get the timer count value in ms or us
165  *
166  * @param timer_id the timer id
167  * @param div the divider of clock frequency
168  * @param last_count last count of the timer
169  * @param timer_value_unit_t the time unit, ms or us
170  * @return the timer value in micro seconds
171  */
172 uint64_t bk_timer_get_time(timer_id_t timer_id, uint32_t div, uint32_t last_count, timer_value_unit_t unit_type);
173 
174 
175 /**
176  * @brief     This function set the delay based on the current running counter, which is different from bk_timer_start.
177  *
178  * @param timer_id the timer id
179  * @param time_us time delay value of the timer by us
180  * @param callback the timer delay call back function
181  * @return
182  *    - BK_OK: succeed
183  *    - others: other errors.
184  */
185 bk_err_t bk_timer_delay_with_callback(timer_id_t timer_id, uint64_t time_us, timer_isr_t callback);
186 
187 /**
188  * @brief     Cancel the timer delay task, reset timer and set ISR to NULL, it will not disable the timer, which is different from bk_timer_stop.
189  *
190  * @param timer_id the timer id
191  * @return
192  *    - BK_OK: succeed
193  *    - others: other errors.
194  */
195 bk_err_t bk_timer_cancel(timer_id_t timer_id);
196 
197 /**
198  * @}
199  */
200 
201 #ifdef __cplusplus
202 }
203 #endif
204