1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-5-21 Philo First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_TIMER_CAPTURE)
#if defined(BSP_USING_TIMER0_CAPTURE)|| \
defined(BSP_USING_TIMER1_CAPTURE)|| \
defined(BSP_USING_TIMER2_CAPTURE)|| \
defined(BSP_USING_TIMER3_CAPTURE)
#include <rtdevice.h>
#include "NuMicro.h"
/* Private typedef --------------------------------------------------------------*/
typedef struct _timer
{
struct rt_inputcapture_device parent;
TIMER_T *timer;
uint8_t u8Channel;
IRQn_Type irq;
uint32_t u32CurrentCnt;
rt_bool_t input_data_level;
rt_bool_t first_edge;
} nu_capture_t;
/* Private functions ------------------------------------------------------------*/
static rt_err_t nu_capture_init(struct rt_inputcapture_device *inputcapture);
static rt_err_t nu_capture_open(struct rt_inputcapture_device *inputcapture);
static rt_err_t nu_capture_close(struct rt_inputcapture_device *inputcapture);
static rt_err_t nu_capture_get_pulsewidth(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us);
/* Private define ---------------------------------------------------------------*/
#define TIMER_CHANNEL_NUM (4)
#define TIMER0_POS (0)
#define TIMER1_POS (1)
#define TIMER2_POS (2)
#define TIMER3_POS (3)
/* Timer prescale setting. Since it will affect the pulse width of measure, it is recommended to set to 2. */
#define PSC_DIV (2)
/* Public functions -------------------------------------------------------------*/
/* Private variables ------------------------------------------------------------*/
static const char *nu_timer_device_name[TIMER_CHANNEL_NUM] = { "timer0i0", "timer1i0", "timer2i0", "timer3i0"};
static const IRQn_Type nu_timer_irq[TIMER_CHANNEL_NUM] = { TMR0_IRQn, TMR1_IRQn, TMR2_IRQn, TMR3_IRQn};
static TIMER_T *nu_timer_base[TIMER_CHANNEL_NUM] = { TIMER0, TIMER1, TIMER2, TIMER3};
static nu_capture_t *nu_timer_capture[TIMER_CHANNEL_NUM] = {0};
static struct rt_inputcapture_ops nu_capture_ops =
{
.init = nu_capture_init,
.open = nu_capture_open,
.close = nu_capture_close,
.get_pulsewidth = nu_capture_get_pulsewidth,
};
/* Functions define ------------------------------------------------------------*/
void timer_interrupt_handler(nu_capture_t *nu_timer_capture)
{
TIMER_ClearCaptureIntFlag(nu_timer_capture->timer);
/* First event is rising edge */
if (nu_timer_capture->first_edge == RT_TRUE)
{
nu_timer_capture->first_edge = RT_FALSE;
nu_timer_capture->input_data_level = RT_FALSE;
}
else
{
nu_timer_capture->input_data_level = !nu_timer_capture->input_data_level;
nu_timer_capture->u32CurrentCnt = TIMER_GetCaptureData(nu_timer_capture->timer);
rt_hw_inputcapture_isr(&nu_timer_capture->parent, nu_timer_capture->input_data_level);
}
}
#if defined(BSP_USING_TIMER0_CAPTURE)
void TMR0_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
timer_interrupt_handler(nu_timer_capture[0]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif //defined(BSP_USING_TIMER0_CAPTURE)
#if defined(BSP_USING_TIMER1_CAPTURE)
void TMR1_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
timer_interrupt_handler(nu_timer_capture[1]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif //defined(BSP_USING_TIMER1_CAPTURE)
#if defined(BSP_USING_TIMER2_CAPTURE)
void TMR2_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
timer_interrupt_handler(nu_timer_capture[2]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif //defined(BSP_USING_TIMER2_CAPTURE)
#if defined(BSP_USING_TIMER3_CAPTURE)
void TMR3_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
timer_interrupt_handler(nu_timer_capture[3]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif //defined(BSP_USING_TIMER3_CAPTURE)
static rt_err_t nu_capture_get_pulsewidth(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us)
{
rt_err_t ret = RT_EOK;
nu_capture_t *nu_capture;
nu_capture = (nu_capture_t *)inputcapture;
*pulsewidth_us = nu_capture->u32CurrentCnt / PSC_DIV;
return -(ret);
}
static rt_err_t nu_timer_init(nu_capture_t *nu_capture)
{
SYS_UnlockReg();
#if defined(BSP_USING_TIMER0_CAPTURE)
if (nu_capture->timer == TIMER0)
{
/* Enable TIMER0 clock */
CLK_EnableModuleClock(TMR0_MODULE);
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);
goto exit_nu_timer_init;
}
#endif
#if defined(BSP_USING_TIMER1_CAPTURE)
if (nu_capture->timer == TIMER1)
{
/* Enable TIMER1 clock */
CLK_EnableModuleClock(TMR1_MODULE);
CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_PCLK0, 0);
goto exit_nu_timer_init;
}
#endif
#if defined(BSP_USING_TIMER2_CAPTURE)
if (nu_capture->timer == TIMER2)
{
/* Enable TIMER2 clock */
CLK_EnableModuleClock(TMR2_MODULE);
CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_PCLK1, 0);
goto exit_nu_timer_init;
}
#endif
#if defined(BSP_USING_TIMER3_CAPTURE)
if (nu_capture->timer == TIMER3)
{
/* Enable TIMER3 clock */
CLK_EnableModuleClock(TMR3_MODULE);
CLK_SetModuleClock(TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_PCLK1, 0);
goto exit_nu_timer_init;
}
#endif
SYS_LockReg();
return -(RT_ERROR);
exit_nu_timer_init:
SYS_LockReg();
return RT_EOK;
}
static rt_err_t nu_capture_init(struct rt_inputcapture_device *inputcapture)
{
rt_err_t ret = RT_EOK;
nu_capture_t *nu_capture;
RT_ASSERT(inputcapture != RT_NULL);
nu_capture = (nu_capture_t *) inputcapture;
if (nu_timer_init(nu_capture) != RT_EOK)
{
rt_kprintf("Failed to initialize TIMER.\n");
ret = -RT_ERROR;
}
return -(ret);
}
static uint8_t cal_time_prescale(nu_capture_t *nu_capture)
{
uint32_t u32Clk = TIMER_GetModuleClock(nu_capture->timer);
/* 1 tick = 1/PSC_DIV us */
return (u32Clk / 1000000 / PSC_DIV) - 1;
}
static rt_err_t nu_capture_open(struct rt_inputcapture_device *inputcapture)
{
rt_err_t ret = RT_EOK;
nu_capture_t *nu_capture;
RT_ASSERT(inputcapture != RT_NULL);
nu_capture = (nu_capture_t *) inputcapture;
nu_capture->first_edge = RT_TRUE;
/* Enable Timer NVIC */
NVIC_EnableIRQ(nu_capture->irq);
/* Reset counter before openning. */
TIMER_ResetCounter(nu_capture->timer);
TIMER_Open(nu_capture->timer, TIMER_CONTINUOUS_MODE, 1);
TIMER_SET_PRESCALE_VALUE(nu_capture->timer, cal_time_prescale(nu_capture));
TIMER_SET_CMP_VALUE(nu_capture->timer, 0xFFFFFF);
TIMER_EnableCapture(nu_capture->timer, TIMER_CAPTURE_COUNTER_RESET_MODE, TIMER_CAPTURE_EVENT_RISING_FALLING);
TIMER_EnableInt(nu_capture->timer);
TIMER_EnableCaptureInt(nu_capture->timer);
TIMER_Start(nu_capture->timer);
return ret;
}
static rt_err_t nu_capture_close(struct rt_inputcapture_device *inputcapture)
{
rt_err_t ret = RT_EOK;
nu_capture_t *nu_capture;
RT_ASSERT(inputcapture != RT_NULL);
nu_capture = (nu_capture_t *) inputcapture;
TIMER_Stop(nu_capture->timer);
TIMER_DisableCaptureInt(nu_capture->timer);
TIMER_DisableInt(nu_capture->timer);
TIMER_Close(nu_capture->timer);
NVIC_DisableIRQ(nu_capture->irq);
return ret;
}
/* Init and register timer capture */
static int nu_timer_capture_device_init(void)
{
uint8_t TIMER_MSK = 0;
#if defined(BSP_USING_TIMER0_CAPTURE)
TIMER_MSK |= (0x1 << 0);
#endif
#if defined(BSP_USING_TIMER1_CAPTURE)
TIMER_MSK |= (0x1 << 1);
#endif
#if defined(BSP_USING_TIMER2_CAPTURE)
TIMER_MSK |= (0x1 << 2);
#endif
#if defined(BSP_USING_TIMER3_CAPTURE)
TIMER_MSK |= (0x1 << 3);
#endif
for (int i = 0; i < TIMER_CHANNEL_NUM; i++)
{
if (TIMER_MSK & (0x1 << i))
{
nu_timer_capture[i] = (nu_capture_t *)rt_malloc(sizeof(nu_capture_t));
nu_timer_capture[i]->timer = nu_timer_base[i];
nu_timer_capture[i]->u8Channel = i;
nu_timer_capture[i]->irq = nu_timer_irq[i];
nu_timer_capture[i]->u32CurrentCnt = 0;
nu_timer_capture[i]->parent.ops = &nu_capture_ops;
nu_timer_capture[i]->first_edge = RT_TRUE;
/* register inputcapture device */
rt_device_inputcapture_register(&nu_timer_capture[i]->parent, nu_timer_device_name[i], &nu_timer_capture[i]);
}
}
return 0;
}
INIT_DEVICE_EXPORT(nu_timer_capture_device_init);
#endif //#if defined(BSP_USING_TIMER*_CAPTURE)
#endif //#if defined(BSP_USING_TIMER_CAPTURE)
|