-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqei_pic32mk.c
401 lines (368 loc) · 7.92 KB
/
qei_pic32mk.c
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* @file qei_pic32mk.c
* @author Sebastien CAUX (sebcaux)
* @copyright Robotips 2017
* @copyright UniSwarm 2018-2023
*
* @date June 19, 2017, 21:40 PM
*
* @brief Quadrature Encoder Interface support driver for PIC32MK
*
* Implementation based on Microchip document DS60001346A :
* http://ww1.microchip.com/downloads/en/DeviceDoc/60001346A.pdf
*/
#include "qei.h"
#include <archi.h>
#if !defined(QEI_COUNT) || QEI_COUNT == 0
# warning "No qei on the current device or unknow device"
#endif
#ifdef UDEVKIT_HAVE_CONFIG
# include "udevkit_config.h"
#endif
enum
{
QEI_FLAG_UNUSED = 0x00
};
typedef struct
{
union
{
struct
{
unsigned used : 1;
unsigned enabled : 1;
unsigned : 6;
};
uint8_t val;
};
} qei_status;
struct qei_dev
{
qei_status flags;
};
static struct qei_dev _qeis[] = {
#if QEI_COUNT >= 1
{.flags = {{.val = QEI_FLAG_UNUSED}}},
#endif
};
/**
* @brief Gives a free QEI device number and open it
* @return QEI device number
*/
rt_dev_t qei_getFreeDevice(void)
{
#if QEI_COUNT >= 1
uint8_t qei_id;
for (qei_id = 0; qei_id < QEI_COUNT; qei_id++)
{
if (_qeis[qei_id].flags.used == 0)
{
break;
}
}
if (qei_id == QEI_COUNT)
{
return NULLDEV;
}
rt_dev_t device = MKDEV(DEV_CLASS_QEI, qei_id);
qei_open(device);
return device;
#else
return NULLDEV;
#endif
}
/**
* @brief Open a QEI
* @param device QEI device number
* @return 0 if ok, -1 in case of error
*/
int qei_open(rt_dev_t device)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei >= QEI_COUNT)
{
return -1;
}
if (_qeis[qei].flags.used == 1)
{
return -1;
}
_qeis[qei].flags.used = 1;
return 0;
#else
return -1;
#endif
}
/**
* @brief Close a QEI
* @param device QEI device number
* @return 0 if ok, -1 in case of error
*/
int qei_close(rt_dev_t device)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei >= QEI_COUNT)
{
return -1;
}
qei_disable(device);
_qeis[qei].flags.used = 0;
return 0;
#else
return -1;
#endif
}
/**
* @brief QEI sdk state
* @param device qei device number
* @return true if qei was openned by qei_open function
*/
bool qei_isOpened(rt_dev_t device)
{
uint8_t qei = MINOR(device);
if (qei >= QEI_COUNT)
{
return false;
}
return (_qeis[qei].flags.used == 1);
}
/**
* @brief Enable the specified QEI device
* @param device QEI device number
* @return 0 if ok, -1 in case of error
*/
int qei_enable(rt_dev_t device)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei > QEI_COUNT)
{
return -1;
}
_qeis[qei].flags.enabled = 1;
switch (qei)
{
# if (QEI_COUNT >= 1) && !defined(QEI1_DISABLE)
case QEI1_ID:
QEI1CONbits.QEIEN = 1;
break;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
QEI2CONbits.QEIEN = 1;
break;
# endif
# if (QEI_COUNT >= 3) && !defined(QEI3_DISABLE)
case QEI3_ID:
QEI3CONbits.QEIEN = 1;
break;
# endif
# if (QEI_COUNT >= 4) && !defined(QEI4_DISABLE)
case QEI4_ID:
QEI4CONbits.QEIEN = 1;
break;
# endif
# if (QEI_COUNT >= 5) && !defined(QEI5_DISABLE)
case QEI5_ID:
QEI5CONbits.QEIEN = 1;
break;
# endif
# if (QEI_COUNT >= 6) && !defined(QEI6_DISABLE)
case QEI6_ID:
QEI6CONbits.QEIEN = 1;
break;
# endif
}
return 0;
#else
return -1;
#endif
}
/**
* @brief Disable the specified QEI device
* @param device QEI device number
* @return 0 if ok, -1 in case of error
*/
int qei_disable(rt_dev_t device)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei > QEI_COUNT)
{
return -1;
}
_qeis[qei].flags.enabled = 0;
switch (qei)
{
# if (QEI_COUNT >= 1) && !defined(QEI1_DISABLE)
case QEI1_ID:
QEI1CONbits.QEIEN = 0;
break;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
QEI2CONbits.QEIEN = 0;
break;
# endif
# if (QEI_COUNT >= 3) && !defined(QEI3_DISABLE)
case QEI3_ID:
QEI3CONbits.QEIEN = 0;
break;
# endif
# if (QEI_COUNT >= 4) && !defined(QEI4_DISABLE)
case QEI4_ID:
QEI4CONbits.QEIEN = 0;
break;
# endif
# if (QEI_COUNT >= 5) && !defined(QEI5_DISABLE)
case QEI5_ID:
QEI5CONbits.QEIEN = 0;
break;
# endif
# if (QEI_COUNT >= 6) && !defined(QEI6_DISABLE)
case QEI6_ID:
QEI6CONbits.QEIEN = 0;
break;
# endif
}
return 0;
#else
return -1;
#endif
}
/**
* @brief QEI sdk enabled state
* @param device qei device number
* @return true if qei was enabled by qei_enable function
*/
bool qei_isEnabled(rt_dev_t device)
{
uint8_t qei = MINOR(device);
if (qei >= QEI_COUNT)
{
return false;
}
return (_qeis[qei].flags.enabled == 1);
}
/**
* Configures the specified QEI device with bits in config
* @param device QEI device number
* @param config config bit
* @return 0 if ok, -1 in case of error
*/
int qei_setConfig(rt_dev_t device, uint16_t config)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei > QEI_COUNT)
{
return -1;
}
switch (qei)
{
# if (QEI_COUNT >= 1) && !defined(QEI1_DISABLE)
case QEI1_ID:
// TODO implement me
break;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
break;
# endif
# if (QEI_COUNT >= 3) && !defined(QEI3_DISABLE)
case QEI3_ID:
break;
# endif
# if (QEI_COUNT >= 4) && !defined(QEI4_DISABLE)
case QEI4_ID:
break;
# endif
# if (QEI_COUNT >= 5) && !defined(QEI5_DISABLE)
case QEI5_ID:
break;
# endif
# if (QEI_COUNT >= 6) && !defined(QEI6_DISABLE)
case QEI6_ID:
break;
# endif
}
return 0;
#else
return -1;
#endif
}
/**
* Configure the hardware input filter on QEIx A, B and I
* @param device QEI device number
* @param divider Clock divider
* @return 0 if ok, -1 in case of error
*/
int qei_setInputFilterConfig(rt_dev_t device, uint16_t divider)
{
// TODO implement me
return 0;
}
/**
* Set QEI position counter to work in modulo mode
* @param device QEI device number
* @param minimum The position counter minimum value. will loop to the maximum value
* @param maximum The position counter maximum value. will loop to the minimum value
* @return 0 if ok, -1 in case of error
*/
int qei_setModuloCountMode(rt_dev_t device, int32_t minimum, int32_t maximum)
{
// TODO implement me
return 0;
}
/**
* Returns the actual position of the specified QEI
* @param device QEI device number
* @return position
*/
qei_type qei_value(rt_dev_t device)
{
#if QEI_COUNT >= 1
uint8_t qei = MINOR(device);
if (qei > QEI_COUNT)
{
return -1;
}
switch (qei)
{
# if (QEI_COUNT >= 1) && !defined(QEI1_DISABLE)
case QEI1_ID:
return POS1CNT;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
return POS2CNT;
# endif
# if (QEI_COUNT >= 3) && !defined(QEI3_DISABLE)
case QEI3_ID:
return POS3CNT;
# endif
# if (QEI_COUNT >= 4) && !defined(QEI4_DISABLE)
case QEI4_ID:
return POS4CNT;
# endif
# if (QEI_COUNT >= 5) && !defined(QEI5_DISABLE)
case QEI5_ID:
return POS5CNT;
# endif
# if (QEI_COUNT >= 6) && !defined(QEI6_DISABLE)
case QEI6_ID:
return POS6CNT;
# endif
}
return 0;
#else
return -1;
#endif
}
int qei_setHomeValue(rt_dev_t device, qei_type home)
{
// TODO implement me
return 0;
}