forked from collin80/ESP32RET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gvret_comm.cpp
489 lines (478 loc) · 17.6 KB
/
gvret_comm.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
Implements handling of the GVRET comm protocol, both sending and receiving
*/
#include "gvret_comm.h"
#include "SerialConsole.h"
#include "config.h"
#include "can_manager.h"
GVRET_Comm_Handler::GVRET_Comm_Handler()
{
step = 0;
state = IDLE;
}
void GVRET_Comm_Handler::processIncomingByte(uint8_t in_byte)
{
uint32_t busSpeed = 0;
uint32_t now = micros();
uint8_t temp8;
uint16_t temp16;
switch (state) {
case IDLE:
if(in_byte == 0xF1)
{
state = GET_COMMAND;
}
else if(in_byte == 0xE7)
{
settings.useBinarySerialComm = true;
SysSettings.lawicelMode = false;
//setPromiscuousMode(); //going into binary comm will set promisc. mode too.
}
else
{
console.rcvCharacter((uint8_t) in_byte);
}
break;
case GET_COMMAND:
switch(in_byte)
{
case PROTO_BUILD_CAN_FRAME:
state = BUILD_CAN_FRAME;
buff[0] = 0xF1;
step = 0;
break;
case PROTO_TIME_SYNC:
state = TIME_SYNC;
step = 0;
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 1; //time sync
transmitBuffer[transmitBufferLength++] = (uint8_t) (now & 0xFF);
transmitBuffer[transmitBufferLength++] = (uint8_t) (now >> 8);
transmitBuffer[transmitBufferLength++] = (uint8_t) (now >> 16);
transmitBuffer[transmitBufferLength++] = (uint8_t) (now >> 24);
break;
case PROTO_DIG_INPUTS:
//immediately return the data for digital inputs
temp8 = 0; //getDigital(0) + (getDigital(1) << 1) + (getDigital(2) << 2) + (getDigital(3) << 3) + (getDigital(4) << 4) + (getDigital(5) << 5);
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 2; //digital inputs
transmitBuffer[transmitBufferLength++] = temp8;
temp8 = checksumCalc(buff, 2);
transmitBuffer[transmitBufferLength++] = temp8;
state = IDLE;
break;
case PROTO_ANA_INPUTS:
//immediately return data on analog inputs
temp16 = 0;// getAnalog(0); // Analogue input 1
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 3;
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(1); // Analogue input 2
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(2); // Analogue input 3
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(3); // Analogue input 4
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(4); // Analogue input 5
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(5); // Analogue input 6
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp16 = 0;//getAnalog(6); // Vehicle Volts
transmitBuffer[transmitBufferLength++] = temp16 & 0xFF;
transmitBuffer[transmitBufferLength++] = uint8_t(temp16 >> 8);
temp8 = checksumCalc(buff, 9);
transmitBuffer[transmitBufferLength++] = temp8;
state = IDLE;
break;
case PROTO_SET_DIG_OUT:
state = SET_DIG_OUTPUTS;
buff[0] = 0xF1;
break;
case PROTO_SETUP_CANBUS:
state = SETUP_CANBUS;
step = 0;
buff[0] = 0xF1;
break;
case PROTO_GET_CANBUS_PARAMS:
//immediately return data on canbus params
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 6;
transmitBuffer[transmitBufferLength++] = settings.CAN0_Enabled + ((unsigned char) settings.CAN0ListenOnly << 4);
transmitBuffer[transmitBufferLength++] = settings.CAN0Speed;
transmitBuffer[transmitBufferLength++] = settings.CAN0Speed >> 8;
transmitBuffer[transmitBufferLength++] = settings.CAN0Speed >> 16;
transmitBuffer[transmitBufferLength++] = settings.CAN0Speed >> 24;
transmitBuffer[transmitBufferLength++] = 0;
transmitBuffer[transmitBufferLength++] = settings.CAN1Speed;
transmitBuffer[transmitBufferLength++] = settings.CAN1Speed >> 8;
transmitBuffer[transmitBufferLength++] = settings.CAN1Speed >> 16;
transmitBuffer[transmitBufferLength++] = settings.CAN1Speed >> 24;
state = IDLE;
break;
case PROTO_GET_DEV_INFO:
//immediately return device information
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 7;
transmitBuffer[transmitBufferLength++] = CFG_BUILD_NUM & 0xFF;
transmitBuffer[transmitBufferLength++] = (CFG_BUILD_NUM >> 8);
transmitBuffer[transmitBufferLength++] = 0x20;
transmitBuffer[transmitBufferLength++] = 0;
transmitBuffer[transmitBufferLength++] = 0;
transmitBuffer[transmitBufferLength++] = 0; //was single wire mode. Should be rethought for this board.
state = IDLE;
break;
case PROTO_SET_SW_MODE:
buff[0] = 0xF1;
state = SET_SINGLEWIRE_MODE;
step = 0;
break;
case PROTO_KEEPALIVE:
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 0x09;
transmitBuffer[transmitBufferLength++] = 0xDE;
transmitBuffer[transmitBufferLength++] = 0xAD;
state = IDLE;
break;
case PROTO_SET_SYSTYPE:
buff[0] = 0xF1;
state = SET_SYSTYPE;
step = 0;
break;
case PROTO_ECHO_CAN_FRAME:
state = ECHO_CAN_FRAME;
buff[0] = 0xF1;
step = 0;
break;
case PROTO_GET_NUMBUSES:
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 12;
transmitBuffer[transmitBufferLength++] = SysSettings.numBuses;
state = IDLE;
break;
case PROTO_GET_EXT_BUSES:
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 13;
for (int u = 2; u < 17; u++) transmitBuffer[transmitBufferLength++] = 0;
step = 0;
state = IDLE;
break;
case PROTO_SET_EXT_BUSES:
state = SETUP_EXT_BUSES;
step = 0;
buff[0] = 0xF1;
break;
}
break;
case BUILD_CAN_FRAME:
buff[1 + step] = in_byte;
switch(step)
{
case 0:
build_out_frame.id = in_byte;
break;
case 1:
build_out_frame.id |= in_byte << 8;
break;
case 2:
build_out_frame.id |= in_byte << 16;
break;
case 3:
build_out_frame.id |= in_byte << 24;
if(build_out_frame.id & 1 << 31)
{
build_out_frame.id &= 0x7FFFFFFF;
build_out_frame.extended = true;
} else build_out_frame.extended = false;
break;
case 4:
out_bus = in_byte & 3;
break;
case 5:
build_out_frame.length = in_byte & 0xF;
if(build_out_frame.length > 8)
{
build_out_frame.length = 8;
}
break;
default:
if(step < build_out_frame.length + 6)
{
build_out_frame.data.uint8[step - 6] = in_byte;
}
else
{
state = IDLE;
//this would be the checksum byte. Compute and compare.
//temp8 = checksumCalc(buff, step);
build_out_frame.rtr = 0;
if (out_bus == 0) canManager.sendFrame(&CAN0, build_out_frame);
if (out_bus == 1) canManager.sendFrame(&CAN1, build_out_frame);
}
break;
}
step++;
break;
case TIME_SYNC:
state = IDLE;
break;
case GET_DIG_INPUTS:
// nothing to do
break;
case GET_ANALOG_INPUTS:
// nothing to do
break;
case SET_DIG_OUTPUTS: //todo: validate the XOR byte
buff[1] = in_byte;
//temp8 = checksumCalc(buff, 2);
for(int c = 0; c < 8; c++){
if(in_byte & (1 << c)) setOutput(c, true);
else setOutput(c, false);
}
state = IDLE;
break;
case SETUP_CANBUS: //todo: validate checksum
switch(step)
{
case 0:
build_int = in_byte;
break;
case 1:
build_int |= in_byte << 8;
break;
case 2:
build_int |= in_byte << 16;
break;
case 3:
build_int |= in_byte << 24;
busSpeed = build_int & 0xFFFFF;
if(busSpeed > 1000000) busSpeed = 1000000;
if(build_int > 0)
{
if(build_int & 0x80000000ul) //signals that enabled and listen only status are also being passed
{
if(build_int & 0x40000000ul)
{
settings.CAN0_Enabled = true;
} else
{
settings.CAN0_Enabled = false;
}
if(build_int & 0x20000000ul)
{
settings.CAN0ListenOnly = true;
} else
{
settings.CAN0ListenOnly = false;
}
} else
{
//if not using extended status mode then just default to enabling - this was old behavior
settings.CAN0_Enabled = true;
}
//CAN0.set_baudrate(build_int);
settings.CAN0Speed = busSpeed;
} else { //disable first canbus
settings.CAN0_Enabled = false;
}
if (settings.CAN0_Enabled)
{
CAN0.begin(settings.CAN0Speed, 255);
if (settings.CAN0ListenOnly) CAN0.setListenOnlyMode(true);
else CAN0.setListenOnlyMode(false);
CAN0.watchFor();
}
else CAN0.disable();
break;
case 4:
build_int = in_byte;
break;
case 5:
build_int |= in_byte << 8;
break;
case 6:
build_int |= in_byte << 16;
break;
case 7:
build_int |= in_byte << 24;
busSpeed = build_int & 0xFFFFF;
if(busSpeed > 1000000) busSpeed = 1000000;
if(build_int > 0 && SysSettings.numBuses > 1)
{
if(build_int & 0x80000000ul) //signals that enabled and listen only status are also being passed
{
if(build_int & 0x40000000ul)
{
settings.CAN1_Enabled = true;
} else
{
settings.CAN1_Enabled = false;
}
if(build_int & 0x20000000ul)
{
settings.CAN1ListenOnly = true;
} else
{
settings.CAN1ListenOnly = false;
}
} else
{
//if not using extended status mode then just default to enabling - this was old behavior
settings.CAN1_Enabled = true;
}
//CAN1.set_baudrate(build_int);
settings.CAN1Speed = busSpeed;
} else { //disable first canbus
settings.CAN1_Enabled = false;
}
if (settings.CAN1_Enabled)
{
CAN1.begin(settings.CAN0Speed, 255);
if (settings.CAN1ListenOnly) CAN1.setListenOnlyMode(true);
else CAN1.setListenOnlyMode(false);
CAN1.watchFor();
}
else CAN1.disable();
state = IDLE;
//now, write out the new canbus settings to EEPROM
//EEPROM.writeBytes(0, &settings, sizeof(settings));
//EEPROM.commit();
//setPromiscuousMode();
break;
}
step++;
break;
case GET_CANBUS_PARAMS:
// nothing to do
break;
case GET_DEVICE_INFO:
// nothing to do
break;
case SET_SINGLEWIRE_MODE:
if(in_byte == 0x10){
} else {
}
//EEPROM.writeBytes(0, &settings, sizeof(settings));
//EEPROM.commit();
state = IDLE;
break;
case SET_SYSTYPE:
settings.systemType = in_byte;
//EEPROM.writeBytes(0, &settings, sizeof(settings));
//EEPROM.commit();
//loadSettings();
state = IDLE;
break;
case ECHO_CAN_FRAME:
buff[1 + step] = in_byte;
switch(step)
{
case 0:
build_out_frame.id = in_byte;
break;
case 1:
build_out_frame.id |= in_byte << 8;
break;
case 2:
build_out_frame.id |= in_byte << 16;
break;
case 3:
build_out_frame.id |= in_byte << 24;
if(build_out_frame.id & 1 << 31) {
build_out_frame.id &= 0x7FFFFFFF;
build_out_frame.extended = true;
} else build_out_frame.extended = false;
break;
case 4:
out_bus = in_byte & 1;
break;
case 5:
build_out_frame.length = in_byte & 0xF;
if(build_out_frame.length > 8) build_out_frame.length = 8;
break;
default:
if(step < build_out_frame.length + 6)
{
build_out_frame.data.bytes[step - 6] = in_byte;
}
else
{
state = IDLE;
//this would be the checksum byte. Compute and compare.
//temp8 = checksumCalc(buff, step);
//if (temp8 == in_byte)
//{
toggleRXLED();
//if(isConnected) {
canManager.displayFrame(build_out_frame, 0);
//}
//}
}
break;
}
step++;
break;
case SETUP_EXT_BUSES: //setup enable/listenonly/speed for SWCAN, Enable/Speed for LIN1, LIN2
switch(step)
{
case 0:
build_int = in_byte;
break;
case 1:
build_int |= in_byte << 8;
break;
case 2:
build_int |= in_byte << 16;
break;
case 3:
build_int |= in_byte << 24;
break;
case 4:
build_int = in_byte;
break;
case 5:
build_int |= in_byte << 8;
break;
case 6:
build_int |= in_byte << 16;
break;
case 7:
build_int |= in_byte << 24;
break;
case 8:
build_int = in_byte;
break;
case 9:
build_int |= in_byte << 8;
break;
case 10:
build_int |= in_byte << 16;
break;
case 11:
build_int |= in_byte << 24;
state = IDLE;
//now, write out the new canbus settings to EEPROM
//EEPROM.writeBytes(0, &settings, sizeof(settings));
//EEPROM.commit();
//setPromiscuousMode();
break;
}
step++;
break;
}
}
//Get the value of XOR'ing all the bytes together. This creates a reasonable checksum that can be used
//to make sure nothing too stupid has happened on the comm.
uint8_t GVRET_Comm_Handler::checksumCalc(uint8_t *buffer, int length)
{
uint8_t valu = 0;
for (int c = 0; c < length; c++) {
valu ^= buffer[c];
}
return valu;
}