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
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-4-20 FYChou First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_QEI)
#include <rtdevice.h>
#include "NuMicro.h"
/* Private define ---------------------------------------------------------------*/
/* Private typedef --------------------------------------------------------------*/
struct nu_qei
{
struct rt_pulse_encoder_device dev;
char *name;
QEI_T *qei_base;
rt_uint32_t max_cntval;
rt_uint32_t cmp_val;
IRQn_Type IRQn;
rt_uint8_t qei_type;
rt_uint8_t qei_flag;
};
typedef struct nu_qei *nu_qei_t;
/* Private functions ------------------------------------------------------------*/
static rt_uint32_t nu_qei_type(struct rt_pulse_encoder_device *pulse_encoder);
static rt_err_t nu_qei_init(struct rt_pulse_encoder_device *pulse_encoder);
static rt_int32_t nu_qei_get_count(struct rt_pulse_encoder_device *pulse_encoder);
static rt_err_t nu_qei_clear_count(struct rt_pulse_encoder_device *pulse_encoder);
static rt_err_t nu_qei_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args);
/* Public functions -------------------------------------------------------------*/
rt_int32_t nu_qei_get_maxval(rt_device_t *pulse_encoder);
rt_int32_t nu_qei_get_cmpval(rt_device_t *pulse_encoder);
rt_int32_t nu_qei_get_type(rt_device_t *pulse_encoder);
void nu_qei_set_maxval_type(rt_device_t *pulse_encoder, rt_uint32_t u32val, rt_uint8_t u8type);
void nu_qei_set_cmpval(rt_device_t *pulse_encoder, rt_uint32_t u32val);
/* Private variables ------------------------------------------------------------*/
static struct nu_qei nu_qei_arr [] =
{
#if defined(BSP_USING_QEI0)
{
.name = "qei0",
.qei_base = QEI0,
.max_cntval = 1000,
.cmp_val = 100,
.IRQn = QEI0_IRQn,
.qei_type = AB_PHASE_PULSE_ENCODER,
.qei_flag = 0
},
#endif
#if defined(BSP_USING_QEI1)
{
.name = "qei1",
.qei_base = QEI1,
.max_cntval = 1000,
.cmp_val = 100,
.IRQn = QEI1_IRQn,
.qei_type = AB_PHASE_PULSE_ENCODER,
.qei_flag = 0
},
#endif
{0}
};
static const struct rt_pulse_encoder_ops nu_qei_ops =
{
.init = nu_qei_init,
.get_count = nu_qei_get_count,
.clear_count = nu_qei_clear_count,
.control = nu_qei_control,
};
typedef struct rt_pulse_encoder_ops *nu_qei_ops_t;
/* Public variables -------------------------------------------------------------*/
static rt_uint32_t nu_qei_type(struct rt_pulse_encoder_device *pulse_encoder)
{
rt_uint32_t u32type;
switch (pulse_encoder->type)
{
case SINGLE_PHASE_PULSE_ENCODER:
u32type = (((nu_qei_t)pulse_encoder)->cmp_val) ? QEI_CTL_X2_COMPARE_COUNTING_MODE : QEI_CTL_X2_FREE_COUNTING_MODE;
break;
case UNKNOWN_PULSE_ENCODER_TYPE:
case AB_PHASE_PULSE_ENCODER:
default:
u32type = (((nu_qei_t)pulse_encoder)->cmp_val) ? QEI_CTL_X4_COMPARE_COUNTING_MODE : QEI_CTL_X4_FREE_COUNTING_MODE;
break;
}
return u32type;
}
static rt_err_t nu_qei_init(struct rt_pulse_encoder_device *pulse_encoder)
{
rt_uint32_t u32type;
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t)pulse_encoder)->qei_base;
/* enable noise filter */
QEI_ENABLE_NOISE_FILTER(qui_base, QEI_CTL_NFCLKSEL_DIV2);
/* set qei mode */
u32type = nu_qei_type(pulse_encoder);
/* set compare value and interrupt */
if (((nu_qei_t)pulse_encoder)->cmp_val)
{
QEI_SET_CNT_CMP(qui_base, ((nu_qei_t)pulse_encoder)->cmp_val);
QEI_EnableInt(qui_base, QEI_CTL_CMPIEN_Msk);
QEI_ENABLE_CNT_CMP(qui_base);
NVIC_EnableIRQ(((nu_qei_t)pulse_encoder)->IRQn);
}
QEI_Open(qui_base, u32type, ((nu_qei_t)pulse_encoder)->max_cntval);
return RT_EOK;
}
static rt_int32_t nu_qei_get_count(struct rt_pulse_encoder_device *pulse_encoder)
{
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t)pulse_encoder)->qei_base;
return (rt_int32_t)QEI_GET_CNT_VALUE(qui_base);
}
static rt_err_t nu_qei_clear_count(struct rt_pulse_encoder_device *pulse_encoder)
{
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t)pulse_encoder)->qei_base;
QEI_Stop(qui_base);
QEI_SET_CNT_VALUE(qui_base, 0);
QEI_Start(qui_base);
return RT_EOK;
}
static rt_err_t nu_qei_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args)
{
rt_err_t result;
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t)pulse_encoder)->qei_base;
RT_ASSERT(cmd != RT_NULL);
result = RT_EOK;
switch (cmd)
{
case PULSE_ENCODER_CMD_ENABLE:
QEI_Start(qui_base);
if (((nu_qei_t)pulse_encoder)->cmp_val)
{
QEI_EnableInt(qui_base, QEI_CTL_CMPIEN_Msk);
QEI_ENABLE_CNT_CMP(qui_base);
NVIC_EnableIRQ(((nu_qei_t)pulse_encoder)->IRQn);
}
break;
case PULSE_ENCODER_CMD_DISABLE:
if (((nu_qei_t)pulse_encoder)->cmp_val)
{
QEI_DisableInt(qui_base, QEI_CTL_CMPIEN_Msk);
QEI_DISABLE_CNT_CMP(qui_base);
NVIC_DisableIRQ(((nu_qei_t)pulse_encoder)->IRQn);
}
QEI_Stop(qui_base);
break;
default:
result = -RT_ENOSYS;
break;
}
return result;
}
#if defined(BSP_USING_QEI0)
/* QEI0 interrupt entry */
void QEI0_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (QEI_GET_INT_FLAG(nu_qei_arr[0].qei_base, QEI_STATUS_CMPF_Msk))
{
nu_qei_arr[0].qei_flag = 1;
QEI_CLR_INT_FLAG(nu_qei_arr[0].qei_base, QEI_STATUS_CMPF_Msk);
rt_kprintf("QEI0 CMP flag rising\n") ;
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(BSP_USING_QEI1)
/* QEI1 interrupt entry */
void QEI1_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (QEI_GET_INT_FLAG(nu_qei_arr[1].qei_base, QEI_STATUS_CMPF_Msk))
{
nu_qei_arr[1].qei_flag = 1;
QEI_CLR_INT_FLAG(nu_qei_arr[1].qei_base, QEI_STATUS_CMPF_Msk);
rt_kprintf("QEI1 CMP flag rising\n") ;
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
rt_int32_t nu_qei_get_maxval(rt_device_t *pulse_encoder)
{
return ((nu_qei_t) * pulse_encoder)->max_cntval;
}
rt_int32_t nu_qei_get_cmpval(rt_device_t *pulse_encoder)
{
return ((nu_qei_t) * pulse_encoder)->cmp_val;
}
rt_int32_t nu_qei_get_type(rt_device_t *pulse_encoder)
{
return ((nu_qei_t) * pulse_encoder)->qei_type;
}
void nu_qei_set_maxval_type(rt_device_t *pulse_encoder, rt_uint32_t u32val, rt_uint8_t u8type)
{
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t) * pulse_encoder)->qei_base;
RT_ASSERT(u8type <= AB_PHASE_PULSE_ENCODER);
((nu_qei_t)*pulse_encoder)->qei_type = u8type;
((nu_qei_t)*pulse_encoder)->dev.type = (enum rt_pulse_encoder_type)u8type;
((nu_qei_t)*pulse_encoder)->max_cntval = u32val;
QEI_Open(qui_base, nu_qei_type(&(((nu_qei_t)*pulse_encoder)->dev)), u32val);
}
void nu_qei_set_cmpval(rt_device_t *pulse_encoder, rt_uint32_t u32val)
{
QEI_T *qui_base;
RT_ASSERT(pulse_encoder != RT_NULL);
qui_base = ((nu_qei_t) * pulse_encoder)->qei_base;
((nu_qei_t)*pulse_encoder)->cmp_val = u32val;
QEI_SET_CNT_CMP(qui_base, ((nu_qei_t)*pulse_encoder)->cmp_val);
if (((nu_qei_t)*pulse_encoder)->cmp_val)
{
QEI_EnableInt(qui_base, QEI_CTL_CMPIEN_Msk);
QEI_ENABLE_CNT_CMP(qui_base);
NVIC_EnableIRQ(((nu_qei_t)*pulse_encoder)->IRQn);
}
else
{
QEI_DisableInt(qui_base, QEI_CTL_CMPIEN_Msk);
QEI_DISABLE_CNT_CMP(qui_base);
NVIC_DisableIRQ(((nu_qei_t)*pulse_encoder)->IRQn);
}
}
int rt_hw_qei_init(void)
{
int result = -RT_ERROR;
int nu_sel = 0;
while (nu_qei_arr[nu_sel].name != 0)
{
nu_qei_arr[nu_sel].dev.type = (enum rt_pulse_encoder_type)nu_qei_arr[nu_sel].qei_type;
nu_qei_arr[nu_sel].dev.ops = &nu_qei_ops;
result = rt_device_pulse_encoder_register((void *)&nu_qei_arr[nu_sel], nu_qei_arr[nu_sel].name, &(nu_qei_arr[nu_sel].qei_flag));
RT_ASSERT(result == RT_EOK);
nu_sel++;
}
return result;
}
INIT_APP_EXPORT(rt_hw_qei_init);
#endif /* BSP_USING_QEI */
|