forked from collin80/ESP32RET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lawicel.cpp
346 lines (331 loc) · 11.8 KB
/
lawicel.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
/*
Implements the lawicel protocol.
*/
#include "lawicel.h"
#include "config.h"
#include <esp32_can.h>
#include "utility.h"
void LAWICELHandler::handleShortCmd(char cmd)
{
switch (cmd)
{
case 'O': //LAWICEL open canbus port
CAN0.setListenOnlyMode(false);
CAN0.begin(settings.CAN0Speed, 255);
CAN0.enable();
Serial.write(13); //send CR to mean "ok"
SysSettings.lawicelMode = true;
break;
case 'C': //LAWICEL close canbus port (First one)
CAN0.disable();
Serial.write(13); //send CR to mean "ok"
break;
case 'L': //LAWICEL open canbus port in listen only mode
CAN0.setListenOnlyMode(true);
CAN0.begin(settings.CAN0Speed, 255);
CAN0.enable();
Serial.write(13); //send CR to mean "ok"
SysSettings.lawicelMode = true;
break;
case 'P': //LAWICEL - poll for one waiting frame. Or, just CR if no frames
if (CAN0.available()) SysSettings.lawicelPollCounter = 1;
else Serial.write(13); //no waiting frames
break;
case 'A': //LAWICEL - poll for all waiting frames - CR if no frames
SysSettings.lawicelPollCounter = CAN0.available();
if (SysSettings.lawicelPollCounter == 0) Serial.write(13);
break;
case 'F': //LAWICEL - read status bits
Serial.print("F00"); //bit 0 = RX Fifo Full, 1 = TX Fifo Full, 2 = Error warning, 3 = Data overrun, 5= Error passive, 6 = Arb. Lost, 7 = Bus Error
Serial.write(13);
break;
case 'V': //LAWICEL - get version number
Serial.print("V1013\n");
SysSettings.lawicelMode = true;
break;
case 'N': //LAWICEL - get serial number
Serial.print("ESP32RET\n");
SysSettings.lawicelMode = true;
break;
case 'x':
SysSettings.lawicellExtendedMode = !SysSettings.lawicellExtendedMode;
if (SysSettings.lawicellExtendedMode) {
Serial.print("V2\n");
}
else {
Serial.print("LAWICEL\n");
}
break;
case 'B': //LAWICEL V2 - Output list of supported buses
if (SysSettings.lawicellExtendedMode) {
for (int i = 0; i < NUM_BUSES; i++) {
printBusName(i);
Serial.print("\n");
}
}
break;
case 'X':
if (SysSettings.lawicellExtendedMode) {
}
break;
}
}
void LAWICELHandler::handleLongCmd(char *buffer)
{
CAN_FRAME outFrame;
char buff[80];
int val;
tokenizeCmdString(buffer);
switch (buffer[0]) {
case 't': //transmit standard frame
outFrame.id = Utility::parseHexString(buffer + 1, 3);
outFrame.length = buffer[4] - '0';
outFrame.extended = false;
if (outFrame.length < 0) outFrame.length = 0;
if (outFrame.length > 8) outFrame.length = 8;
for (int data = 0; data < outFrame.length; data++) {
outFrame.data.bytes[data] = Utility::parseHexString(buffer + 5 + (2 * data), 2);
}
CAN0.sendFrame(outFrame);
if (SysSettings.lawicelAutoPoll) Serial.print("z");
break;
case 'T': //transmit extended frame
outFrame.id = Utility::parseHexString(buffer + 1, 8);
outFrame.length = buffer[9] - '0';
outFrame.extended = false;
if (outFrame.length < 0) outFrame.length = 0;
if (outFrame.length > 8) outFrame.length = 8;
for (int data = 0; data < outFrame.length; data++) {
outFrame.data.bytes[data] = Utility::parseHexString(buffer + 10 + (2 * data), 2);
}
CAN0.sendFrame(outFrame);
if (SysSettings.lawicelAutoPoll) Serial.print("Z");
break;
case 'S':
if (!SysSettings.lawicellExtendedMode) {
//setup canbus baud via predefined speeds
val = Utility::parseHexCharacter(buffer[1]);
switch (val) {
case 0:
settings.CAN0Speed = 10000;
break;
case 1:
settings.CAN0Speed = 20000;
break;
case 2:
settings.CAN0Speed = 50000;
break;
case 3:
settings.CAN0Speed = 100000;
break;
case 4:
settings.CAN0Speed = 125000;
break;
case 5:
settings.CAN0Speed = 250000;
break;
case 6:
settings.CAN0Speed = 500000;
break;
case 7:
settings.CAN0Speed = 800000;
break;
case 8:
settings.CAN0Speed = 1000000;
break;
}
}
else { //LAWICEL V2 - Send packet out of specified bus - S <Bus> <ID> <Data0> <Data1> <...>
uint8_t bytes[8];
uint32_t id;
int numBytes = 0;
id = strtol(tokens[2], nullptr, 16);
for (int b = 0; b < 8; b++) {
if (tokens[3 + b][0] != 0) {
bytes[b] = strtol(tokens[3 + b], nullptr, 16);
numBytes++;
}
else break; //break for loop because we're obviously done.
}
if (!strcasecmp(tokens[1], "CAN0")) {
CAN_FRAME outFrame;
outFrame.id = id;
outFrame.length = numBytes;
outFrame.extended = false;
for (int b = 0; b < numBytes; b++) outFrame.data.bytes[b] = bytes[b];
CAN0.sendFrame(outFrame);
}
if (!strcasecmp(tokens[1], "CAN1")) {
CAN_FRAME outFrame;
outFrame.id = id;
outFrame.length = numBytes;
outFrame.extended = false;
for (int b = 0; b < numBytes; b++) outFrame.data.bytes[b] = bytes[b];
CAN1.sendFrame(outFrame);
}
}
case 's': //setup canbus baud via register writes (we can't really do that...)
//settings.CAN0Speed = 250000;
break;
case 'r': //send a standard RTR frame (don't really... that's so deprecated its not even funny)
break;
case 'R':
if (SysSettings.lawicellExtendedMode) { //Lawicel V2 - Set that we want to receive traffic from the given bus - R <BUSID>
if (!strcasecmp(tokens[1], "CAN0")) SysSettings.lawicelBusReception[0] = true;
if (!strcasecmp(tokens[1], "CAN1")) SysSettings.lawicelBusReception[1] = true;
}
else { //Lawicel V1 - send extended RTR frame (NO! DON'T DO IT!)
}
break;
case 'X': //Set autopoll off/on
if (buffer[1] == '1') SysSettings.lawicelAutoPoll = true;
else SysSettings.lawicelAutoPoll = false;
break;
case 'W': //Dual or single filter mode
break; //don't actually support this mode
case 'm': //set acceptance mask - these things seem to be odd and aren't actually implemented yet
case 'M':
if (SysSettings.lawicellExtendedMode) { //Lawicel V2 - Set filter mask - M <busid> <Mask> <FilterID> <Ext?>
int mask = strtol(tokens[2], nullptr, 16);
int filt = strtol(tokens[3], nullptr, 16);
if (!strcasecmp(tokens[1], "CAN0"))
{
if (!strcasecmp(tokens[4], "X")) CAN0.setRXFilter(0, filt, mask, true);
else CAN0.setRXFilter(0, filt, mask, false);
}
if (!strcasecmp(tokens[1], "CAN1"))
{
if (!strcasecmp(tokens[4], "X")) CAN1.setRXFilter(0, filt, mask, true);
else CAN1.setRXFilter(0, filt, mask, false);
}
}
else { //Lawicel V1 - set acceptance code
}
break;
case 'H':
if (SysSettings.lawicellExtendedMode) { //Lawicel V2 - Halt reception of traffic from given bus - H <busid>
if (!strcasecmp(tokens[1], "CAN0")) SysSettings.lawicelBusReception[0] = false;
if (!strcasecmp(tokens[1], "CAN1")) SysSettings.lawicelBusReception[1] = false;
}
break;
case 'U': //set uart speed. We just ignore this. You can't set a baud rate on a USB CDC port
break; //also no action here
case 'Z': //Turn timestamp off/on
if (buffer[1] == '1') SysSettings.lawicelTimestamping = true;
else SysSettings.lawicelTimestamping = false;
break;
case 'Q': //turn auto start up on/off - probably don't need to actually implement this at the moment.
break; //no action yet or maybe ever
case 'C': //Lawicel V2 - configure one of the buses - C <busid> <speed> <any additional needed params>
if (SysSettings.lawicellExtendedMode) {
//at least two parameters separated by spaces. First BUS ID (CAN0, CAN1, SWCAN, etc) then speed (or more params separated by #'s)
int speed = atoi(tokens[2]);
if (!strcasecmp(tokens[1], "CAN0")) {
CAN0.begin(speed, 255);
}
if (!strcasecmp(tokens[1], "CAN1")) {
CAN1.begin(speed, 255);
}
}
break;
}
Serial.write(13);
}
//Tokenize cmdBuffer on space boundaries - up to 10 tokens supported
void LAWICELHandler::tokenizeCmdString(char *buff) {
int idx = 0;
char *tok;
for (int i = 0; i < 13; i++) tokens[i][0] = 0;
tok = strtok(buff, " ");
if (tok != nullptr) strcpy(tokens[idx], tok);
else tokens[idx][0] = 0;
while (tokens[idx] != nullptr && idx < 13) {
idx++;
tok = strtok(nullptr, " ");
if (tok != nullptr) strcpy(tokens[idx], tok);
else tokens[idx][0] = 0;
}
}
void LAWICELHandler::uppercaseToken(char *token) {
int idx = 0;
while (token[idx] != 0 && idx < 9) {
token[idx] = toupper(token[idx]);
idx++;
}
token[idx] = 0;
}
void LAWICELHandler::printBusName(int bus) {
switch (bus) {
case 0:
Serial.print("CAN0");
break;
case 1:
Serial.print("CAN1");
break;
default:
Serial.print("UNKNOWN");
break;
}
}
//Expecting to find ID in tokens[2] then zero or more data bytes
bool LAWICELHandler::parseLawicelCANCmd(CAN_FRAME &frame) {
if (tokens[2] == nullptr) return false;
frame.id = strtol(tokens[2], nullptr, 16);
int idx = 3;
int dataLen = 0;
while (tokens[idx] != nullptr) {
frame.data.bytes[dataLen++] = strtol(tokens[idx], nullptr, 16);
idx++;
}
frame.length = dataLen;
return true;
}
void LAWICELHandler::sendFrameToBuffer(CAN_FRAME &frame, int whichBus)
{
uint8_t buff[40];
uint8_t writtenBytes;
uint8_t temp;
uint32_t now = micros();
if (SysSettings.lawicellExtendedMode)
{
Serial.print(micros());
Serial.print(" - ");
Serial.print(frame.id, HEX);
if (frame.extended) Serial.print(" X ");
else Serial.print(" S ");
printBusName(whichBus);
for (int d = 0; d < frame.length; d++)
{
Serial.print(" ");
Serial.print(frame.data.uint8[d], HEX);
}
}
else
{
if (frame.extended)
{
Serial.print("T");
sprintf((char *)buff, "%08x", frame.id);
Serial.print((char *)buff);
}
else
{
Serial.print("t");
sprintf((char *)buff, "%03x", frame.id);
Serial.print((char *)buff);
}
Serial.print(frame.length);
for (int i = 0; i < frame.length; i++)
{
sprintf((char *)buff, "%02x", frame.data.uint8[i]);
Serial.print((char *)buff);
}
if (SysSettings.lawicelTimestamping)
{
uint16_t timestamp = (uint16_t)millis();
sprintf((char *)buff, "%04x", timestamp);
Serial.print((char *)buff);
}
}
Serial.write(13);
}