-
Notifications
You must be signed in to change notification settings - Fork 0
/
can-232.cpp
556 lines (513 loc) · 18.3 KB
/
can-232.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*****************************************************************************************
* This is implementation of CAN BUS ASCII protocol based on LAWICEL v1.3 serial protocol
* of CAN232/CANUSB device (http://www.can232.com/docs/can232_v3.pdf)
*
* Made for Arduino with Seeduino/ElecFreaks CAN BUS Shield based on MCP2515
*
* Copyright (C) 2015 Anton Viktorov <[email protected]>
* https://github.com/latonita/can-ascii
*
* This library is free software. You may use/redistribute it under The MIT License terms.
*
*****************************************************************************************/
#include <SPI.h>
#include "mcp_can.h"
#include "can-232.h"
#define LOGGING_ENABLED
#ifdef LOGGING_ENABLED
// #define dbg_begin(x) debug.begin(x)
// #define dbg0(x) debug.print(x)
// #define dbg1(x) debug.println(x)
// #define dbg2(x,y) debug.print(x); debug.println(y)
// #define dbgH(x) debug.print(x,HEX)
#define DEBUG_RX_PIN 8
#define DEBUG_TX_PIN 9
#else
// #define dbg_begin(x)
// #define dbg0(x)
// #define dbg1(x)
// #define dbg2(x,y)
// #define dbgH(x)
#endif
#ifdef LOGGING_ENABLED
// software serial #2: TX = digital pin 8, RX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
#define debug NULL
// #define debug Serial
#endif
Can232* Can232::_instance = 0;
Can232* Can232::instance() {
if (_instance == 0)
_instance = new Can232();
return _instance;
}
void Can232::init(INT8U defaultCanSpeed, const INT8U clock) {
// dbg_begin(LW232_DEFAULT_BAUD_RATE); // logging through software serial
// dbg1("CAN ASCII. Welcome to debug");
instance()->lw232CanSpeedSelection = defaultCanSpeed;
instance()->lw232McpModuleClock = clock;
instance()->initFunc();
}
void Can232::setFilter(INT8U (*userFunc)(INT32U)) {
instance()->setFilterFunc(userFunc);
}
void Can232::loop() {
instance()->loopFunc();
}
void Can232::serialEvent() {
instance()->serialEventFunc();
}
void Can232::initFunc() {
if (!inputString.reserve(LW232_INPUT_STRING_BUFFER_SIZE)) {
// dbg0("inputString.reserve failed in initFunc. less optimal String work is expected");
}
// lw232AutoStart = true; //todo: read from eeprom
// lw232AutoPoll = false; //todo: read from eeprom
// lw232TimeStamp = //read from eeprom
// lw232Message[0] = 'Z'; lw232Message[1] = '1'; exec();
//if (lw232AutoStart) {
inputString = "O\0x0D";
stringComplete = true;
loopFunc();
//}
}
void Can232::setFilterFunc(INT8U (*userFunc)(INT32U)) {
instance()->userAddressFilterFunc = userFunc;
}
void Can232::loopFunc() {
if (stringComplete) {
int len = inputString.length();
if (len > 0 && len < LW232_FRAME_MAX_SIZE) {
strcpy((char*)lw232Message, inputString.c_str());
exec();
}
// clear the string:
inputString = "";
stringComplete = false;
}
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) {
int recv = 0;
while (CAN_MSGAVAIL == checkReceive() && recv++<5) {
// dbg0('+');
if (CAN_OK == receiveSingleFrame()) {
Serial.write(LW232_CR);
}
}
Serial.flush();
}
}
void Can232::serialEventFunc() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == LW232_CR) {
stringComplete = true;
}
}
}
INT8U Can232::exec() {
// dbg2("Command received:", inputString);
lw232LastErr = parseAndRunCommand();
switch (lw232LastErr) {
case LW232_OK:
Serial.write(LW232_RET_ASCII_OK);
break;
case LW232_OK_SMALL:
Serial.write(LW232_RET_ASCII_OK_SMALL);
Serial.write(LW232_RET_ASCII_OK);
break;
case LW232_OK_BIG:
Serial.write(LW232_RET_ASCII_OK_BIG);
Serial.write(LW232_RET_ASCII_OK);
break;
case LW232_ERR_NOT_IMPLEMENTED:
// Choose behavior: will it fail or not when not implemented command comes in. Some can monitors might be affected by this selection.
Serial.write(LW232_RET_ASCII_ERROR);
//Serial.write(LW232_RET_ASCII_OK);
break;
default:
Serial.write(LW232_RET_ASCII_ERROR);
}
return 0;
}
INT8U Can232::parseAndRunCommand() {
INT8U ret = LW232_OK;
INT8U idx = 0;
INT8U err = 0;
lw232LastErr = LW232_OK;
switch (lw232Message[0]) {
case LW232_CMD_SETUP:
// Sn[CR] Setup with standard CAN bit-rates where n is 0-9.
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
idx = HexHelper::parseNibbleWithLimit(lw232Message[1], LW232_CAN_BAUD_NUM);
lw232CanSpeedSelection = lw232CanBaudRates[idx];
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_SETUP_BTR:
// sxxyy[CR] Setup with BTR0/BTR1 CAN bit-rates where xx and yy is a hex value.
ret = LW232_ERR; break;
case LW232_CMD_OPEN:
// O[CR] Open the CAN channel in normal mode (sending & receiving).
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
ret = openCanBus();
if (ret == LW232_OK) {
lw232CanChannelMode = LW232_STATUS_CAN_OPEN_NORMAL;
}
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_LISTEN:
// L[CR] Open the CAN channel in listen only mode (receiving).
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
ret = openCanBus();
if (ret == LW232_OK) {
lw232CanChannelMode = LW232_STATUS_CAN_OPEN_LISTEN;
}
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_CLOSE:
// C[CR] Close the CAN channel.
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) {
lw232CanChannelMode = LW232_STATUS_CAN_CLOSED;
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_TX11:
// tiiildd...[CR] Transmit a standard (11bit) CAN frame.
if (lw232CanChannelMode == LW232_STATUS_CAN_OPEN_NORMAL) {
parseCanStdId();
lw232PacketLen = HexHelper::parseNibbleWithLimit(lw232Message[LW232_OFFSET_STD_PKT_LEN], LW232_FRAME_MAX_LENGTH + 1);
for (; idx < lw232PacketLen; idx++) {
lw232Buffer[idx] = HexHelper::parseFullByte(lw232Message[LW232_OFFSET_STD_PKT_DATA + idx * 2], lw232Message[LW232_OFFSET_STD_PKT_DATA + idx * 2 + 1]);
}
INT8U mcpErr = sendMsgBuf(lw232CanId, 0, 0, lw232PacketLen, lw232Buffer);
if (mcpErr != CAN_OK) {
ret = LW232_ERR;
} else if (lw232AutoPoll) {
ret = LW232_OK_SMALL;
}
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_TX29:
// Tiiiiiiiildd...[CR] Transmit an extended (29bit) CAN frame
if (lw232CanChannelMode == LW232_STATUS_CAN_OPEN_NORMAL) {
parseCanExtId();
lw232PacketLen = HexHelper::parseNibbleWithLimit(lw232Message[LW232_OFFSET_EXT_PKT_LEN], LW232_FRAME_MAX_LENGTH + 1);
for (; idx < lw232PacketLen; idx++) {
lw232Buffer[idx] = HexHelper::parseFullByte(lw232Message[LW232_OFFSET_EXT_PKT_DATA + idx * 2], lw232Message[LW232_OFFSET_EXT_PKT_DATA + idx * 2 + 1]);
}
if (CAN_OK != sendMsgBuf(lw232CanId, 1, 0, lw232PacketLen, lw232Buffer)) {
ret = LW232_ERR;
} else if (lw232AutoPoll) {
ret = LW232_OK_BIG;
} else {
ret = LW232_OK;
}
}
break;
case LW232_CMD_RTR11:
// riiil[CR] Transmit an standard RTR (11bit) CAN frame.
if (lw232CanChannelMode == LW232_STATUS_CAN_OPEN_NORMAL) {
parseCanStdId();
lw232PacketLen = HexHelper::parseNibbleWithLimit(lw232Message[LW232_OFFSET_STD_PKT_LEN], LW232_FRAME_MAX_LENGTH + 1);
if (CAN_OK != sendMsgBuf(lw232CanId, 0, 1, lw232PacketLen, lw232Buffer)) {
ret = LW232_ERR;
}
else if (lw232AutoPoll) {
ret = LW232_OK_SMALL;
}
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_RTR29:
// Riiiiiiiil[CR] Transmit an extended RTR (29bit) CAN frame.
if (lw232CanChannelMode == LW232_STATUS_CAN_OPEN_NORMAL) {
parseCanExtId();
lw232PacketLen = HexHelper::parseNibbleWithLimit(lw232Message[LW232_OFFSET_EXT_PKT_LEN], LW232_FRAME_MAX_LENGTH + 1);
if (CAN_OK != sendMsgBuf(lw232CanId, 1, 1, lw232PacketLen, lw232Buffer)) {
ret = LW232_ERR;
}
else if (lw232AutoPoll) {
ret = LW232_OK_SMALL; // not a typo. strangely can232_v3.pdf tells to return "z[CR]", not "Z[CR]" as in 29bit. todo: check if it is error in pdf???
}
} else {
ret = LW232_ERR;
}
break;
case LW232_CMD_POLL_ONE:
// P[CR] Poll incomming FIFO for CAN frames (single poll)
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED && lw232AutoPoll == LW232_AUTOPOLL_OFF) {
if (CAN_MSGAVAIL == checkReceive()) {
ret = receiveSingleFrame();
}
} else {
ret = LW232_ERR;
}
break;
case LW232_CMD_POLL_MANY:
// A[CR] Polls incomming FIFO for CAN frames (all pending frames)
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED && lw232AutoPoll == LW232_AUTOPOLL_OFF) {
while (CAN_MSGAVAIL == checkReceive()) {
ret = ret ^ receiveSingleFrame();
if (ret != CAN_OK)
break;
Serial.write(LW232_CR);
}
if (ret == CAN_OK)
Serial.print(LW232_ALL);
} else {
ret = LW232_ERR;
}
break;
case LW232_CMD_FLAGS:
// F[CR] Read Status Flags.
// LAWICEL CAN232 and CANUSB have some specific errors which differ from MCP2515/MCP2551 errors. We just return MCP2515 error.
Serial.print(LW232_FLAG);
if (lw232CAN.checkError(&err) == CAN_OK)
err = 0;
HexHelper::printFullByte(err & MCP_EFLG_ERRORMASK);
break;
case LW232_CMD_AUTOPOLL:
// Xn[CR] Sets Auto Poll/Send ON/OFF for received frames.
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
lw232AutoPoll = (lw232Message[1] == LW232_ON_ONE) ? LW232_AUTOPOLL_ON : LW232_AUTOPOLL_OFF;
//todo: save to eeprom
} else {
ret = LW232_ERR;
}
break;
case LW232_CMD_FILTER:
// Wn[CR] Filter mode setting. By default CAN232 works in dual filter mode (0) and is backwards compatible with previous CAN232 versions.
ret = LW232_ERR_NOT_IMPLEMENTED; break;
case LW232_CMD_ACC_CODE:
// Mxxxxxxxx[CR] Sets Acceptance Code Register (ACn Register of SJA1000). // we use MCP2515,
ret = LW232_ERR_NOT_IMPLEMENTED; break;
case LW232_CMD_ACC_MASK:
// mxxxxxxxx[CR] Sets Acceptance Mask Register (AMn Register of SJA1000).
ret = LW232_ERR_NOT_IMPLEMENTED; break;
case LW232_CMD_UART:
// Un[CR] Setup UART with a new baud rate where n is 0-6.
idx = HexHelper::parseNibbleWithLimit(lw232Message[1], LW232_UART_BAUD_NUM);
Serial.begin(lw232SerialBaudRates[idx]);
break;
case LW232_CMD_VERSION1:
case LW232_CMD_VERSION2:
// V[CR] Get Version number of both CAN232 hardware and software
Serial.print(LW232_LAWICEL_VERSION_STR);
break;
case LW232_CMD_SERIAL:
// N[CR] Get Serial number of the CAN232.
Serial.print(LW232_LAWICEL_SERIAL_NUM);
break;
case LW232_CMD_TIMESTAMP:
// Zn[CR] Sets Time Stamp ON/OFF for received frames only. Z0 - OFF, Z1 - Lawicel's timestamp 2 bytes, Z2 - arduino timestamp 4 bytes.
if (lw232CanChannelMode == LW232_STATUS_CAN_CLOSED) {
// lw232TimeStamp = (lw232Message[1] == LW232_ON_ONE);
if (lw232Message[1] == LW232_ON_ONE) {
lw232TimeStamp = LW232_TIMESTAMP_ON_NORMAL;
}
else if (lw232Message[1] == LW232_ON_TWO) {
lw232TimeStamp = LW232_TIMESTAMP_ON_EXTENDED;
}
else {
lw232TimeStamp = LW232_TIMESTAMP_OFF;
}
}
else {
ret = LW232_ERR;
}
break;
case LW232_CMD_AUTOSTART:
// Qn[CR] Auto Startup feature (from power on).
if (lw232CanChannelMode != LW232_STATUS_CAN_CLOSED) {
if (lw232Message[1] == LW232_ON_ONE) {
lw232AutoStart = LW232_AUTOSTART_ON_NORMAL;
}
else if (lw232Message[1] == LW232_ON_TWO) {
lw232AutoStart = LW232_AUTOSTART_ON_LISTEN;
}
else {
lw232AutoStart = LW232_AUTOSTART_OFF;
}
//todo: save to eeprom
}
else {
ret = LW232_ERR;
}
break;
default:
ret = LW232_ERR_UNKNOWN_CMD;
}
return ret;
}
INT8U Can232::checkReceive() {
#ifndef _MCP_FAKE_MODE_
return lw232CAN.checkReceive();
#else
return CAN_MSGAVAIL;
#endif
}
INT8U Can232::readMsgBufID(INT32U *ID, INT8U *len, INT8U buf[]) {
#ifndef _MCP_FAKE_MODE_
return lw232CAN.readMsgBufID(ID, len, buf);
#else
*ID = random(0x100, 0x110);
*len = 4;
buf[0] = random(0x01, 0x10);
buf[1] = random(0xa1, 0xf0);
buf[2] = 0x00;
buf[3] = 0x00;
return CAN_OK;
#endif
}
INT8U Can232::receiveSingleFrame() {
INT8U ret = LW232_OK;
INT8U idx = 0;
if (CAN_OK == readMsgBufID(&lw232CanId, &lw232PacketLen, lw232Buffer)) {
if (lw232CanId > 0x1FFFFFFF) {
ret = LW232_ERR; // address if totally wrong
}
else if (checkPassFilter(lw232CanId)) {// do we want to skip some addresses?
if (isExtendedFrame()) {
Serial.print(LW232_TR29);
HexHelper::printFullByte(HIGH_BYTE(HIGH_WORD(lw232CanId)));
HexHelper::printFullByte(LOW_BYTE(HIGH_WORD(lw232CanId)));
HexHelper::printFullByte(HIGH_BYTE(LOW_WORD(lw232CanId)));
HexHelper::printFullByte(LOW_BYTE(LOW_WORD(lw232CanId)));
}
else {
Serial.print(LW232_TR11);
HexHelper::printNibble(HIGH_BYTE(LOW_WORD(lw232CanId)));
HexHelper::printFullByte(LOW_BYTE(LOW_WORD(lw232CanId)));
}
//write data len
HexHelper::printNibble(lw232PacketLen);
//write data
for (idx = 0; idx < lw232PacketLen; idx++) {
HexHelper::printFullByte(lw232Buffer[idx]);
}
//write timestamp if needed
if (lw232TimeStamp != LW232_TIMESTAMP_OFF) {
INT32U time = millis();
if (lw232TimeStamp == LW232_TIMESTAMP_ON_NORMAL) {
// standard LAWICEL protocol. two bytes.
time %= 60000;
} else {
// non standard protocol - 4 bytes timestamp
HexHelper::printFullByte(HIGH_BYTE(HIGH_WORD(time)));
HexHelper::printFullByte(LOW_BYTE(HIGH_WORD(time)));
}
HexHelper::printFullByte(HIGH_BYTE(LOW_WORD(time)));
HexHelper::printFullByte(LOW_BYTE(LOW_WORD(time)));
}
}
}
else {
ret = LW232_ERR;
}
return ret;
}
INT8U Can232::isExtendedFrame() {
#ifndef _MCP_FAKE_MODE_
return lw232CAN.isExtendedFrame();
#else
return lw232CanId > 0x7FF ? 1 : 0; //simple hack for fake mode
#endif
}
INT8U Can232::checkPassFilter(INT32U addr) {
if (userAddressFilterFunc == 0)
return LW232_FILTER_PROCESS;
return (*userAddressFilterFunc)(addr);
}
INT8U Can232::openCanBus() {
INT8U ret = LW232_OK;
#ifndef _MCP_FAKE_MODE_
if (CAN_OK != lw232CAN.begin(lw232CanSpeedSelection, lw232McpModuleClock))
ret = LW232_ERR;
#endif
return ret;
}
INT8U Can232::sendMsgBuf(INT32U id, INT8U ext, INT8U rtr, INT8U len, INT8U *buf) {
#ifndef _MCP_FAKE_MODE_
return lw232CAN.sendMsgBuf(id, ext, rtr, len, buf);
#else
Serial.print("<sending:");
Serial.print(id, HEX);
Serial.print(',');
if (ext) Serial.print('+');
else Serial.print('-');
if (rtr) Serial.print('+');
else Serial.print('-');
Serial.print(',');
Serial.print(len, DEC);
Serial.print(',');
int i;
for (i = 0; i < len; i++) printFullByte(buf[i]);
return CAN_OK;
#endif
}
void Can232::parseCanStdId() {
lw232CanId = (((INT32U)HexHelper::parseNibble(lw232Message[1])) << 8)
+ (((INT32U)HexHelper::parseNibble(lw232Message[2])) << 4)
+ (((INT32U)HexHelper::parseNibble(lw232Message[3])));
lw232CanId &= 0x7FF;
}
void Can232::parseCanExtId() {
lw232CanId = (((INT32U)HexHelper::parseNibble(lw232Message[1])) << 28)
+ (((INT32U)HexHelper::parseNibble(lw232Message[2])) << 24)
+ (((INT32U)HexHelper::parseNibble(lw232Message[3])) << 20)
+ (((INT32U)HexHelper::parseNibble(lw232Message[4])) << 16)
+ (((INT32U)HexHelper::parseNibble(lw232Message[5])) << 12)
+ (((INT32U)HexHelper::parseNibble(lw232Message[6])) << 8)
+ (((INT32U)HexHelper::parseNibble(lw232Message[7])) << 4)
+ (((INT32U)HexHelper::parseNibble(lw232Message[8])));
lw232CanId &= 0x1FFFFFFF;
}
void HexHelper::printFullByte(INT8U b) {
if (b < 0x10) {
Serial.print('0');
// dbg0('0');
}
Serial.print(b, HEX);
//dbgH(b);
}
void HexHelper::printNibble(INT8U b) {
Serial.print(b & 0x0F, HEX);
//dbgH(b & 0x0F);
}
INT8U HexHelper::parseNibble(INT8U hex) {
INT8U ret = 0;
if (hex >= '0' && hex <= '9') {
ret = hex - '0';
} else if (hex >= 'a' && hex <= 'f') {
ret = hex - 'a' + 10;
} else if (hex >= 'A' && hex <= 'F') {
ret = hex - 'A' + 10;
} // else error, return 0
return ret;
}
INT8U HexHelper::parseFullByte(INT8U H, INT8U L) {
return (parseNibble(H) << 4) + parseNibble(L);
}
INT8U HexHelper::parseNibbleWithLimit(INT8U hex, INT8U limit) {
INT8U ret = parseNibble(hex);
if (ret < limit)
return ret;
else
return 0;
}