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
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-1-28 Wayne First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_TIMER_CAPTURE)
#include <rtdevice.h>
#include "NuMicro.h"
#include <drv_sys.h>
/* Private define ---------------------------------------------------------------*/
/* Timer prescale setting. Since it will affect the pulse width of measure, it is recommended to set to 2. */
#define PSC_DIV (2)
#define NU_TCAP_DEVICE(etimer) (nu_capture_t*)(etimer)
enum
{
TCAP_START = -1,
#if defined(BSP_USING_TIMER0_CAPTURE)
TCAP0_IDX,
#endif
#if defined(BSP_USING_TIMER1_CAPTURE)
TCAP1_IDX,
#endif
#if defined(BSP_USING_TIMER2_CAPTURE)
TCAP2_IDX,
#endif
#if defined(BSP_USING_TIMER3_CAPTURE)
TCAP3_IDX,
#endif
#if defined(BSP_USING_TIMER4_CAPTURE)
TCAP4_IDX,
#endif
/* BSP_USING_TIMER5 is reserved for Systick usage. */
TCAP_CNT
};
/* Private typedef --------------------------------------------------------------*/
typedef struct _timer
{
struct rt_inputcapture_device parent;
char *name;
uint32_t idx;
IRQn_Type irqn;
E_SYS_IPRST rstidx;
E_SYS_IPCLK clkidx;
uint32_t u32CurrentCnt;
} 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);
/* Public functions -------------------------------------------------------------*/
/* Private variables ------------------------------------------------------------*/
static nu_capture_t nu_etcap_arr [] =
{
#if defined(BSP_USING_TIMER0_CAPTURE)
{
.name = "timer0i0",
.idx = 0,
.irqn = IRQ_TIMER0,
.rstidx = TIMER0RST,
.clkidx = TIMER0CKEN,
},
#endif
#if defined(BSP_USING_TIMER1_CAPTURE)
{
.name = "timer1i0",
.idx = 1,
.irqn = IRQ_TIMER1,
.rstidx = TIMER1RST,
.clkidx = TIMER1CKEN,
},
#endif
#if defined(BSP_USING_TIMER2_CAPTURE)
{
.name = "timer2i0",
.idx = 2,
.irqn = IRQ_TIMER2,
.rstidx = TIMER2RST,
.clkidx = TIMER2CKEN,
},
#endif
#if defined(BSP_USING_TIMER3_CAPTURE)
{
.name = "timer3i0",
.idx = 3,
.irqn = IRQ_TIMER3,
.rstidx = TIMER3RST,
.clkidx = TIMER3CKEN,
},
#endif
#if defined(BSP_USING_TIMER4_CAPTURE)
{
.name = "timer4i0",
.idx = 4,
.irqn = IRQ_TIMER4,
.rstidx = TIMER4RST,
.clkidx = TIMER4CKEN,
},
#endif
/* BSP_USING_TIMER5 is reserved for Systick usage. */
};
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 ------------------------------------------------------------*/
static void nu_tcap_isr(int vector, void *param)
{
nu_capture_t *psNuTCap = NU_TCAP_DEVICE(param);
RT_ASSERT(psNuTCap != RT_NULL);
ETIMER_ClearCaptureIntFlag(psNuTCap->idx);
/* Report caputring data and level. */
psNuTCap->u32CurrentCnt = ETIMER_GetCaptureData(psNuTCap->idx);
rt_hw_inputcapture_isr(&psNuTCap->parent, ETIMER_GetCaptureFallingEdgeFlag(psNuTCap->idx));
}
static rt_err_t nu_capture_get_pulsewidth(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us)
{
nu_capture_t *psNuTCap = NU_TCAP_DEVICE(inputcapture);
RT_ASSERT(inputcapture != RT_NULL);
RT_ASSERT(pulsewidth_us != RT_NULL);
*pulsewidth_us = psNuTCap->u32CurrentCnt / PSC_DIV;
return RT_EOK;
}
static rt_err_t nu_capture_init(struct rt_inputcapture_device *inputcapture)
{
nu_capture_t *psNuTCap = NU_TCAP_DEVICE(inputcapture);
RT_ASSERT(inputcapture != RT_NULL);
nu_sys_ipclk_enable(psNuTCap->clkidx);
nu_sys_ip_reset(psNuTCap->rstidx);
return RT_EOK;
}
static uint8_t cal_time_prescale(nu_capture_t *psNuTCap)
{
uint32_t u32Clk = ETIMER_GetModuleClock(psNuTCap->idx);
/* 1 tick = 1/PSC_DIV us */
return (u32Clk / 1000000 / PSC_DIV) - 1;
}
static rt_err_t nu_capture_open(struct rt_inputcapture_device *inputcapture)
{
nu_capture_t *psNuTCap = NU_TCAP_DEVICE(inputcapture);
RT_ASSERT(inputcapture != RT_NULL);
/* Enable Timer Interrupt */
rt_hw_interrupt_umask(psNuTCap->irqn);
/* Clear counter before openning. */
ETIMER_ClearCounter(psNuTCap->idx);
ETIMER_Open(psNuTCap->idx, ETIMER_CONTINUOUS_MODE, 1);
ETIMER_SET_PRESCALE_VALUE(psNuTCap->idx, cal_time_prescale(psNuTCap));
ETIMER_SET_CMP_VALUE(psNuTCap->idx, 0xFFFFFF);
ETIMER_EnableCapture(psNuTCap->idx, ETIMER_CAPTURE_COUNTER_RESET_MODE, ETIMER_CAPTURE_RISING_THEN_FALLING_EDGE);
ETIMER_EnableCaptureInt(psNuTCap->idx);
ETIMER_Start(psNuTCap->idx);
return RT_EOK;
}
static rt_err_t nu_capture_close(struct rt_inputcapture_device *inputcapture)
{
nu_capture_t *psNuTCap = NU_TCAP_DEVICE(inputcapture);
RT_ASSERT(inputcapture != RT_NULL);
ETIMER_Stop(psNuTCap->idx);
ETIMER_DisableCaptureInt(psNuTCap->idx);
ETIMER_Close(psNuTCap->idx);
rt_hw_interrupt_mask(psNuTCap->irqn);
return RT_EOK;
}
/* Init and register timer capture */
static int nu_timer_capture_device_init(void)
{
int i;
for (i = (TCAP_START + 1); i < TCAP_CNT; i++)
{
/* Register operations */
nu_etcap_arr[i].parent.ops = &nu_capture_ops;
/* Install ISR */
rt_hw_interrupt_install(nu_etcap_arr[i].irqn, nu_tcap_isr, &nu_etcap_arr[i], nu_etcap_arr[i].name);
/* Register inputcapture device */
rt_device_inputcapture_register(&nu_etcap_arr[i].parent, nu_etcap_arr[i].name, &nu_etcap_arr[i]);
}
return 0;
}
INIT_DEVICE_EXPORT(nu_timer_capture_device_init);
#endif //#if defined(BSP_USING_TIMER_CAPTURE)
|