-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax12.c
373 lines (302 loc) · 10.3 KB
/
ax12.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
/*
* Template dsPIC33F
* Compiler : Microchip xC16
* µC : 33FJ64MC804
* Juillet 2012
* ____________ _ _
* |___ /| ___ \ | | | |
* / / | |_/ /___ | |__ ___ | |_
* / / | // _ \| '_ \ / _ \| __|
* / / | |\ \ (_) | |_) | (_) | |_
* /_/ |_| \_\___/|____/ \___/'\__|
* 7robot.fr
*/
#include <uart.h>
#include <p33Fxxxx.h> /* Includes device header file */
#include <stdint.h> /* Includes uint16_t definition */
#include <stdbool.h>
#include <delay.h>
#include "main.h"
/******************************************************************************
* Global Variables
******************************************************************************/
byte checksumAX;
volatile int posAX = -5;
volatile int Delay_TimeOut_AX12 = 0;
void Init_IT_AX12 (void)
{
TRISBbits.TRISB7=1; //Pin RB7 en entrée pour les AX12
_ODCB7 = 1; // Open drain sur la pin RB7(pour les AX12)
OpenUART2(UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_FLOW
& UART_UEN_00 & UART_DIS_WAKE & UART_DIS_LOOPBACK
& UART_DIS_ABAUD & UART_UXRX_IDLE_ONE
& UART_BRGH_SIXTEEN
& UART_NO_PAR_8BIT & UART_1STOPBIT,
UART_INT_TX_BUF_EMPTY & UART_IrDA_POL_INV_ZERO
& UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_TX_BUF_NOT_FUL & UART_INT_RX_CHAR
& UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR,
BRGVALAX12);
ConfigIntUART2(UART_RX_INT_PR5 & UART_RX_INT_EN
& UART_TX_INT_PR5 & UART_TX_INT_DIS);
TRIS_PIN_REMAPABLE_AX12 = 1; // pin AX12 en IN pour remapable IN
OPEN_DRAIN_PIN_REMAPABLE_AX12 = 1;
}
/*************************************************
* TX et RX Interrupt *
*************************************************/
void __attribute__((__interrupt__, no_auto_psv)) _U2RXInterrupt(void){
InterruptAX(); // RX des AX12
_U2RXIF = 0; // On baisse le FLAG
}
void __attribute__((__interrupt__, no_auto_psv)) _U2TXInterrupt(void){
_U2TXIF = 0; // clear TX interrupt flag
}
/*
* Public global variables, which have to be declared volatile.
*/
volatile int responseReadyAX = 0;
responseAXtype responseAX;
/******************************************************************************
* Wiring dependent functions, that you should customize
******************************************************************************/
// du coup, le bit IOLOCK est à 0 au reset du pic,
// donc on va pas y toucher, ça permet de tout faire marcher facile....
void SetTX() {
//__builtin_write_OSCCONL(0x46);
//__builtin_write_OSCCONL(0x57);
//__builtin_write_OSCCONL(OSCCON & 0xBF);
_U2RXR = 31; // disable RX
PIN_REMAPABLE_AX12_OUT = 0b00101; // RP10 = U2TX (p.167) TX => RP10
//__builtin_write_OSCCONL(OSCCON | 0x40);
}
void SetRX() {
//__builtin_write_OSCCONL(0x46);
//__builtin_write_OSCCONL(0x57);
//__builtin_write_OSCCONL(OSCCON & 0xBF); // IOLOCK = 0
_U2RXR = PIN_REMAPABLE_AX12_IN; // RP10 = U2RX (p.165) RX <= RP10
PIN_REMAPABLE_AX12_OUT = 0; // disable TX
// __builtin_write_OSCCONL(OSCCON | 0x40);
}
/******************************************************************************
* Functions to read and write command and return packets
******************************************************************************/
void PushUART(byte b) {
while (U2STAbits.UTXBF); // UART2 TX Buffer Full
WriteUART2(b);
checksumAX += b;
}
/*
* Write the first bytes of a command packet, assuming a <len> parameters will
* follow.
*/
void PushHeaderAX(byte id, byte len, byte inst) {
SetTX();
PushUART(0xFF);
PushUART(0xFF);
checksumAX = 0; // The first two bytes don't count.
PushUART(id);
PushUART(len + 2); // Bytes to go : instruction + buffer (len) + checksum.
PushUART(inst);
}
/* Write a buffer of given length to the body of a command packet. */
void PushBufferAX(byte len, byte* buf) {
byte i;
for (i = 0; i < len; i++) {
PushUART(buf[i]);
}
}
/* Finish a command packet by sending the checksum. */
void PushFooterAX() {
PushUART(~checksumAX);
while (BusyUART2()); // UART1 Transmit Shift Register Empty
SetRX();
}
/**/
void InterruptAX() {
while(DataRdyUART2()) {
byte b = ReadUART2();
if(posAX == -5 && b == 0xFF)
posAX = -4;
else if(posAX == -4 && b == 0xFF) {
posAX = -3;
checksumAX = 0;
responseAX.len = 1;
}
else if(posAX == -3) {
posAX = -2;
responseAX.id = b;
}
else if(posAX == -2 && b < 2 + 4 /*taille de ax.parameters*/) {
posAX = -1;
checksumAX = responseAX.id + b;
responseAX.len = b - 2;
}
else if(posAX == -1) {
posAX = 0;
responseAX.error = *((errorAX*)&b);
}
else if(0 <= posAX && posAX < responseAX.len) {
((byte*)&responseAX.params)[posAX++] = b;
checksumAX += b;
}
else if(posAX == responseAX.len && (b & checksumAX) == 0) {
responseReadyAX = 1;
posAX = -5;
}
else
posAX = -5; // Erreur.
}
}
/******************************************************************************
* Instructions Implementation
******************************************************************************/
void PingAX(byte id) {
PushHeaderAX(id, 2, AX_INST_PING);
PushFooterAX();
}
void ReadAX(byte id, byte address, byte len) {
PushHeaderAX(id, 2, AX_INST_READ_DATA);
PushUART(address);
PushUART(len);
PushFooterAX();
}
void WriteAX(byte id, byte address, byte len, byte* buf) {
PushHeaderAX(id, 1 + len, AX_INST_WRITE_DATA);
PushUART(address);
PushBufferAX(len, buf);
PushFooterAX();
}
void RegWriteAX(byte id, byte address, byte len, byte* buf) {
PushHeaderAX(id, 1 + len, AX_INST_REG_WRITE);
PushUART(address);
PushBufferAX(len, buf);
PushFooterAX();
}
void ActionAX(byte id) {
PushHeaderAX(id, 0, AX_INST_ACTION);
PushFooterAX();
}
void ResetAX(byte id) {
PushHeaderAX(id, 0, AX_INST_RESET);
PushFooterAX();
}
/******************************************************************************
* Convenience Functions
******************************************************************************/
byte RegisterLenAX(byte address) {
switch (address) {
case 2: case 3: case 4: case 5: case 11: case 12: case 13: case 16:
case 17: case 18: case 19: case 24: case 25: case 26: case 27: case 28:
case 29: case 42: case 43: case 44: case 46: case 47:
return 1;
break;
case 0: case 6: case 8: case 14: case 20: case 22: case 30: case 32:
case 34: case 36: case 38: case 40: case 48:
return 2;
break;
}
return 0; // Unexpected.
}
// old version :
/* Write a value to a registry, guessing its width.
void PutAX(byte id, byte address, int value) {
responseReadyAX = 0;
WriteAX(id, address, RegisterLenAX(address), (byte*)&value);
}
* */
/* Read a value from a registry, guessing its width. */
char GetAX(byte id, byte address) {
#ifdef TEST_RECEPTION_AX12
char i = 0;
char OK = 0;
while (i < 10 && !OK) {
OK = GetAX_Check(id, address);
i++;
}
return (OK == 0);
#else
responseReadyAX = 0;
ReadAX(id, address, RegisterLenAX(address));
return 0;
#endif
}
char PutAX(byte id, byte address, int value) {
#ifdef DOUBLE_COMMANDE_AX12
WriteAX(id, address, RegisterLenAX(address), (byte*)&value);
__delay_ms(7);
WriteAX(id, address, RegisterLenAX(address), (byte*)&value);
return 0;
#else
#ifdef TEST_RECEPTION_AX12
char i = 0;
char OK = 0;
while (i < 10 && !OK) {
OK = PutAX_Check(id, address, value);
i++;
}
return (OK == 0);
#else
responseReadyAX = 0;
WriteAX(id, address, RegisterLenAX(address), (byte*)&value);
return 0;
#endif
#endif
}
// Write a value to a registry, guessing its width.
// écoute ensuite la réponse, timeout à 10ms
// si réponse pas bonne, on retente
char PutAX_Check(byte id, byte address, int value) {
char Reponse_Ok = 1;
responseReadyAX = 0; // reset la reception
posAX = -5; // reset BIS
WriteAX(id, address, RegisterLenAX(address), (byte*)&value );
Delay_TimeOut_AX12 = 10;
// tant que le timer 10ms a pas déclenché, et que l'on a pas reçu la réponse de l'AX
while (Delay_TimeOut_AX12 && !responseReadyAX);
if (responseReadyAX) { // si on a eu une réponse, on l'analyse
if (responseAX.id != id) {
Reponse_Ok = 0;
}else if ( responseAX.error.input_voltage || responseAX.error.angle_limit ||
responseAX.error.overheating || responseAX.error.range ||
responseAX.error.cheksum || responseAX.error.overload ||
responseAX.error.instruction ) {
Reponse_Ok = 0;
}
} else { // si pas de réponse
Reponse_Ok = 0;
}
return Reponse_Ok;
}
char GetAX_Check (byte id, byte address)
{
char Reponse_Ok = 1;
responseReadyAX = 0; // reset la reception
posAX = -5; // reset BIS
ReadAX(id, address, RegisterLenAX(address));
Delay_TimeOut_AX12 = 10;
// tant que le timer 10ms a pas d?clench?, et que l'on a pas re?u la r?ponse de l'AX
while (Delay_TimeOut_AX12 && !responseReadyAX);
if (responseReadyAX) { // si on a eu une r?ponse, on l'analyse
if (responseAX.id != id) {
Reponse_Ok = 0;
}else if ( responseAX.error.input_voltage || responseAX.error.angle_limit ||
responseAX.error.overheating || responseAX.error.range ||
responseAX.error.cheksum || responseAX.error.overload ||
responseAX.error.instruction ) {
Reponse_Ok = 0;
}
} else { // si pas de r?ponse
Reponse_Ok = 0;
}
return Reponse_Ok;
}
int GetAX_Pos (byte id)
{
char OK = 0;
OK = GetAX(id, AX_PRESENT_POSITION);
if (OK) {
return responseAX.params[0] + 256*responseAX.params[1];
} else {
return -1;
}
}