-
Notifications
You must be signed in to change notification settings - Fork 6
/
qei_pic24f_dspic33f.c
310 lines (276 loc) · 5.68 KB
/
qei_pic24f_dspic33f.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
/**
* @file qei_pic24f_dspic33f.c
* @author Sebastien CAUX (sebcaux)
* @copyright Robotips 2016-2017
* @copyright UniSwarm 2018-2023
*
* @date April 18, 2016, 22:33 PM
*
* @brief Quadrature Encoder Interface support driver for dsPIC33FJ,
* PIC24F, PIC24FJ and PIC24HJ
*
* Implementation based on Microchip document DS70208C :
* http://ww1.microchip.com/downloads/en/DeviceDoc/70208C.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:
QEI1CON = 0b0000011100000000;
break;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
QEI2CON = 0b0000011100000000;
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.QEIM = 0b000;
break;
# endif
# if (QEI_COUNT >= 2) && !defined(QEI2_DISABLE)
case QEI2_ID:
QEI2CONbits.QEIM = 0b000;
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)
{
// TODO implement me
return 0;
}
/**
* 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
}
#else
return -1;
#endif
}
int qei_setHomeValue(rt_dev_t device, qei_type home)
{
// TODO implement me
return 0;
}