1 /*
2 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /******************************************************************************
18 * @file dw_timer.c
19 * @brief CSI Source File for timer Driver
20 * @version V1.0
21 * @date 02. June 2017
22 ******************************************************************************/
23 #include "csi_core.h"
24 #include "drv_timer.h"
25 #include "dw_timer.h"
26 #include "soc.h"
27
28 #define ERR_TIMER(errno) (CSI_DRV_ERRNO_TIMER_BASE | errno)
29
30 #define TIMER_NULL_PARAM_CHK(para) \
31 do { \
32 if (para == NULL) { \
33 return ERR_TIMER(EDRV_PARAMETER); \
34 } \
35 } while (0)
36
37 typedef struct {
38 uint32_t base;
39 uint32_t irq;
40 timer_event_cb_t cb_event;
41 uint32_t timeout; ///< the set time (us)
42 uint32_t timeout_flag;
43 void *arg;
44 } dw_timer_priv_t;
45
46 static dw_timer_priv_t timer_instance[CONFIG_TIMER_NUM];
47
48 static const timer_capabilities_t timer_capabilities = {
49 .interrupt_mode = 1 ///< supports Interrupt mode
50 };
51
52 /**
53 \brief Make all the timers in the idle state.
54 \param[in] pointer to timer register base
55 */
timer_deactive_control(dw_timer_reg_t * addr)56 static void timer_deactive_control(dw_timer_reg_t *addr)
57 {
58 /* stop the corresponding timer */
59 addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;
60 /* Disable interrupt. */
61 addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK;
62 }
63
dw_timer_irqhandler(int idx)64 void dw_timer_irqhandler(int idx)
65 {
66 dw_timer_priv_t *timer_priv = &timer_instance[idx];
67 timer_priv->timeout_flag = 1;
68
69 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
70
71 addr->TxEOI;
72
73 if (timer_priv->cb_event) {
74 return timer_priv->cb_event(TIMER_EVENT_TIMEOUT, timer_priv->arg);
75 }
76
77 }
78
target_get_timer_count(void)79 int32_t __attribute__((weak)) target_get_timer_count(void)
80 {
81 return 0;
82 }
83
target_get_timer(uint32_t idx,uint32_t * base,uint32_t * irq)84 int32_t __attribute__((weak)) target_get_timer(uint32_t idx, uint32_t *base, uint32_t *irq)
85 {
86 return NULL;
87 }
88
89 /**
90 \brief get timer instance count.
91 \return timer instance count
92 */
csi_timer_get_instance_count(void)93 int32_t csi_timer_get_instance_count(void)
94 {
95 return target_get_timer_count();
96 }
97
98 /**
99 \brief Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface 2.registers event callback function
100 \param[in] idx instance timer index
101 \param[in] cb_event Pointer to \ref timer_event_cb_t
102 \return pointer to timer instance
103 */
csi_timer_initialize(int32_t idx,timer_event_cb_t cb_event,void * arg)104 timer_handle_t csi_timer_initialize(int32_t idx, timer_event_cb_t cb_event, void *arg)
105 {
106 if (idx < 0 || idx >= CONFIG_TIMER_NUM) {
107 return NULL;
108 }
109
110 uint32_t base = 0u;
111 uint32_t irq = 0u;
112
113 int32_t real_idx = target_get_timer(idx, &base, &irq);
114
115 if (real_idx != idx) {
116 return NULL;
117 }
118
119 dw_timer_priv_t *timer_priv = &timer_instance[idx];
120 timer_priv->base = base;
121 timer_priv->irq = irq;
122 timer_priv->arg = arg;
123
124 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
125 timer_priv->timeout = DW_TIMER_INIT_DEFAULT_VALUE;
126
127 timer_deactive_control(addr);
128 timer_priv->cb_event = cb_event;
129
130 drv_nvic_enable_irq(timer_priv->irq);
131
132 return (timer_handle_t)timer_priv;
133 }
134
135 /**
136 \brief De-initialize TIMER Interface. stops operation and releases the software resources used by the interface
137 \param[in] handle timer handle to operate.
138 \return error code
139 */
csi_timer_uninitialize(timer_handle_t handle)140 int32_t csi_timer_uninitialize(timer_handle_t handle)
141 {
142 TIMER_NULL_PARAM_CHK(handle);
143
144 dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
145 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
146
147 timer_deactive_control(addr);
148 timer_priv->cb_event = NULL;
149
150 drv_nvic_disable_irq(timer_priv->irq);
151 return 0;
152 }
153
154 /**
155 \brief Get driver capabilities.
156 \param[in] handle timer handle to operate.
157 \return \ref timer_capabilities_t
158 */
csi_timer_get_capabilities(timer_handle_t handle)159 timer_capabilities_t csi_timer_get_capabilities(timer_handle_t handle)
160 {
161 return timer_capabilities;
162 }
163
164 /**
165 \brief config timer mode.
166 \param[in] handle timer handle to operate.
167 \param[in] mode \ref timer_mode_e
168 \return error code
169 */
csi_timer_config(timer_handle_t handle,timer_mode_e mode)170 int32_t csi_timer_config(timer_handle_t handle, timer_mode_e mode)
171 {
172 TIMER_NULL_PARAM_CHK(handle);
173
174 dw_timer_priv_t *timer_priv = handle;
175 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
176
177 switch (mode) {
178 case TIMER_MODE_FREE_RUNNING:
179 addr->TxControl &= ~DW_TIMER_TXCONTROL_MODE;
180 break;
181
182 case TIMER_MODE_RELOAD:
183 addr->TxControl |= DW_TIMER_TXCONTROL_MODE;
184 break;
185
186 default:
187 return ERR_TIMER(EDRV_PARAMETER);
188 }
189
190 return 0;
191 }
192
193 /**
194 \brief Set timer.
195 \param[in] instance timer instance to operate.
196 \param[in] timeout the timeout value in microseconds(us).
197 \return error code
198 */
csi_timer_set_timeout(timer_handle_t handle,uint32_t timeout)199 int32_t csi_timer_set_timeout(timer_handle_t handle, uint32_t timeout)
200 {
201 TIMER_NULL_PARAM_CHK(handle);
202
203 dw_timer_priv_t *timer_priv = handle;
204 timer_priv->timeout = timeout;
205 return 0;
206 }
207
208 /**
209 \brief Start timer.
210 \param[in] handle timer handle to operate.
211 \return error code
212 */
csi_timer_start(timer_handle_t handle,uint32_t apbfreq)213 int32_t csi_timer_start(timer_handle_t handle, uint32_t apbfreq)
214 {
215 TIMER_NULL_PARAM_CHK(handle);
216
217 dw_timer_priv_t *timer_priv = handle;
218
219 timer_priv->timeout_flag = 0;
220
221 uint32_t min_us = apbfreq / 1000000;
222
223 if ((timer_priv->timeout < min_us) || (timer_priv->timeout > 0xffffffff / min_us)) {
224 return ERR_TIMER(EDRV_PARAMETER);
225 }
226
227 uint32_t load = (uint32_t)(timer_priv->timeout * min_us);
228
229 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
230
231 addr->TxLoadCount = load; /* load time(us) */
232 addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
233 addr->TxControl |= DW_TIMER_TXCONTROL_ENABLE; /* enable the corresponding timer */
234 addr->TxControl &= ~DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
235
236 return 0;
237 }
238
239 /**
240 \brief Stop timer.
241 \param[in] handle timer handle to operate.
242 \return error code
243 */
csi_timer_stop(timer_handle_t handle)244 int32_t csi_timer_stop(timer_handle_t handle)
245 {
246 TIMER_NULL_PARAM_CHK(handle);
247
248 dw_timer_priv_t *timer_priv = handle;
249 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
250
251 addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK; /* enable interrupt */
252 addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* disable the timer */
253
254 return 0;
255 }
256
257 /**
258 \brief suspend timer.
259 \param[in] instance timer instance to operate.
260 \return error code
261 */
csi_timer_suspend(timer_handle_t handle)262 int32_t csi_timer_suspend(timer_handle_t handle)
263 {
264 TIMER_NULL_PARAM_CHK(handle);
265
266 return ERR_TIMER(EDRV_UNSUPPORTED);
267 }
268
269 /**
270 \brief resume timer.
271 \param[in] handle timer handle to operate.
272 \return error code
273 */
csi_timer_resume(timer_handle_t handle)274 int32_t csi_timer_resume(timer_handle_t handle)
275 {
276 TIMER_NULL_PARAM_CHK(handle);
277
278 dw_timer_priv_t *timer_priv = handle;
279 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
280
281 addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE; /* stop the corresponding timer */
282 addr->TxControl &= DW_TIMER_TXCONTROL_ENABLE; /* restart the corresponding timer */
283
284 return 0;
285 }
286
287 /**
288 \brief get timer current value
289 \param[in] handle timer handle to operate.
290 \param[in] value timer current value
291 \return error code
292 */
csi_timer_get_current_value(timer_handle_t handle,uint32_t * value)293 int32_t csi_timer_get_current_value(timer_handle_t handle, uint32_t *value)
294 {
295 TIMER_NULL_PARAM_CHK(handle);
296
297 dw_timer_priv_t *timer_priv = handle;
298 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
299
300 *value = addr->TxCurrentValue;
301 return 0;
302 }
303
304 /**
305 \brief Get TIMER status.
306 \param[in] handle timer handle to operate.
307 \return TIMER status \ref timer_status_t
308 */
csi_timer_get_status(timer_handle_t handle)309 timer_status_t csi_timer_get_status(timer_handle_t handle)
310 {
311 timer_status_t timer_status = {0};
312
313 if (handle == NULL) {
314 return timer_status;
315 }
316
317 dw_timer_priv_t *timer_priv = handle;
318 dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
319
320 if (addr->TxControl & DW_TIMER_TXCONTROL_ENABLE) {
321 timer_status.active = 1;
322 }
323
324 if (timer_priv->timeout_flag == 1) {
325 timer_status.timeout = 1;
326 }
327
328 return timer_status;
329 }
330
331