-
Notifications
You must be signed in to change notification settings - Fork 42
/
slcan.ino
322 lines (297 loc) · 6.91 KB
/
slcan.ino
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
#include <mcp2515_defs.h>
#include <mcp2515.h>
#include <defaults.h>
#include <global.h>
#include <Canbus.h>
#define LED_OPEN 7
#define LED_ERR 8
#define CMD_LEN (sizeof("T12345678811223344556677881234\r")+1)
int g_can_speed = CANSPEED_500; // default: 500k
int g_ts_en = 0;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(LED_OPEN, OUTPUT);
pinMode(LED_ERR, OUTPUT);
Serial.begin(1000000); // select from 115200,500000,1000000,2000000
if (Canbus.init(g_can_speed)) {
digitalWrite(LED_ERR, LOW);
} else {
digitalWrite(LED_ERR, HIGH);
}
}
int b2ahex(char *p, uint8_t s, uint8_t n, void *v)
{
const char *hex = "0123456789ABCDEF";
if (s == 1) {
uint8_t *tmp = (uint8_t *)v;
for (int i=0; i<n; i++) {
*p++ = hex[tmp[i] & 0x0f];
}
} else if (s == 2) {
uint8_t *tmp = (uint8_t *)v;
for (int i=0; i<n; i++) {
*p++ = hex[(tmp[i] >> 4) & 0x0f];
*p++ = hex[tmp[i] & 0x0f];
}
} else if (s == 3) {
uint16_t *tmp = (uint16_t *)v;
for (int i=0; i<n; i++) {
*p++ = hex[(tmp[i] >> 8) & 0x0f];
*p++ = hex[(tmp[i] >> 4) & 0x0f];
*p++ = hex[tmp[i] & 0x0f];
}
} else if (s == 4) {
uint16_t *tmp = (uint16_t *)v;
for (int i=0; i<n; i++) {
*p++ = hex[(tmp[i] >> 12) & 0x0f];
*p++ = hex[(tmp[i] >> 8) & 0x0f];
*p++ = hex[(tmp[i] >> 4) & 0x0f];
*p++ = hex[tmp[i] & 0x0f];
}
}
return s*n;
}
int a2bhex_sub(char a)
{
int val = 0;
if ('0' <= a && a <= '9') {
val = a - '0';
} else if ('A' <= a && a <= 'F') {
val = a - 'A' + 10;
} else if ('a' <= a && a <= 'f') {
val = a - 'a' + 10;
}
return val;
}
int a2bhex(char *p, uint8_t s, uint8_t n, void *v)
{
int i, j;
char buf[4+1];
int val;
if (s == 1 || s == 2) {
uint8_t *tmp = (uint8_t *)v;
for (i=0; i<n; i++) {
val = 0;
for (j=0; j<s; j++) {
val = (val << 4) | a2bhex_sub(*p++);
}
*tmp++ = val;
}
} else if (s == 3 || s == 4) {
uint16_t *tmp = (uint16_t *)v;
for (i=0; i<n; i++) {
val = 0;
for (j=0; j<s; j++) {
val = (val << 4) | a2bhex_sub(*p++);
}
*tmp++ = val;
}
}
return n;
}
// transfer messages from CAN bus to host
void xfer_can2tty()
{
tCAN msg;
char buf[CMD_LEN];
int i;
static uint16_t ts = 0;
char *p;
uint8_t len;
while (Canbus.message_rx(&msg)) {
p = buf;
if (msg.header.ide) {
if (msg.header.rtr) {
*p++ = 'R';
} else {
*p++ = 'T';
}
p += b2ahex(p, 4, 1, &msg.id);
p += b2ahex(p, 4, 1, &msg.ide);
len = msg.header.length % 10;
p += b2ahex(p, 1, 1, &len);
} else {
if (msg.header.rtr) {
*p++ = 'r';
} else {
*p++ = 't';
}
p += b2ahex(p, 3, 1, &msg.id);
len = msg.header.length;
p += b2ahex(p, 1, 1, &len);
}
p += b2ahex(p, 2, msg.header.length, msg.data);
// insert timestamp if needed
if (g_ts_en) {
p += b2ahex(p, 4, 1, &ts); // up to 60,000ms
ts++;
}
*p++ = '\r';
*p++ = '\0';
Serial.print(buf);
}
}
void slcan_ack()
{
Serial.write('\r'); // ACK
}
void slcan_nack()
{
Serial.write('\a'); // NACK
}
void send_canmsg(char *buf)
{
tCAN msg;
int len = strlen(buf) - 1;
uint16_t id[2];
uint8_t hlen;
int is_eff = buf[0] & 0x20 ? 0 : 1;
int is_rtr = buf[0] & 0x02 ? 1 : 0;
if (!is_eff && len >= 4) { // SFF
a2bhex(&buf[1], 3, 1, id);
msg.id = id[0];
msg.header.rtr = is_rtr;
msg.header.ide = 0;
a2bhex(&buf[4], 1, 1, &hlen);
msg.header.length = hlen;
if (len - 4 - 1 == msg.header.length * 2) {
a2bhex(&buf[5], 2, msg.header.length, msg.data);
while (!Canbus.message_tx(&msg)) ;
}
} else if (is_eff && len >= 9) { // EFF
a2bhex(&buf[1], 4, 2, id);
msg.id = id[0] & 0x1fff;
msg.ide = id[1];
msg.header.rtr = is_rtr;
msg.header.ide = 1;
a2bhex(&buf[9], 1, 1, &hlen);
msg.header.length = hlen;
if (len - 9 - 1 == msg.header.length * 2) {
a2bhex(&buf[10], 2, msg.header.length, msg.data);
while (!Canbus.message_tx(&msg)) ;
}
}
}
void pars_slcancmd(char *buf)
{
switch (buf[0]) {
// common commands
case 'O': // open channel
digitalWrite(LED_OPEN, HIGH);
if (Canbus.init(g_can_speed)) {
digitalWrite(LED_ERR, LOW);
} else {
digitalWrite(LED_ERR, HIGH);
}
slcan_ack();
break;
case 'C': // close channel
digitalWrite(LED_OPEN, LOW);
digitalWrite(LED_ERR, LOW);
slcan_ack();
break;
case 't': // SFF
case 'T': // EFF
case 'r': // RTR/SFF
case 'R': // RTR/EFF
send_canmsg(buf);
slcan_ack();
break;
case 'Z': // turn timestamp on/off
if (buf[1] == '0') {
g_ts_en = 0;
} else if (buf[1] == '1') {
g_ts_en = 1;
} else {
slcan_nack();
}
slcan_ack();
break;
case 'M': // acceptance mask
slcan_ack();
break;
case 'm': // acceptance value
slcan_ack();
break;
// non-standard commands
case 'S': // setup CAN bit-rates
switch (buf[1]) {
case '0': // 10k
case '1': // 20k
case '2': // 50k
slcan_nack();
break;
case '3': // 100k
g_can_speed = CANSPEED_100;
slcan_ack();
break;
case '4': // 125k
g_can_speed = CANSPEED_125;
slcan_ack();
break;
case '5': // 250k
g_can_speed = CANSPEED_250;
slcan_ack();
break;
case '6': // 500k
g_can_speed = CANSPEED_500;
slcan_ack();
break;
case '7': // 800k
slcan_nack();
break;
case '8': // 1000k
g_can_speed = CANSPEED_1000;
slcan_ack();
break;
default:
slcan_nack();
break;
}
break;
case 's': // directly set bitrate register of mcp2515
slcan_nack();
break;
case 'F': // status flag
Serial.print("F12");
slcan_ack();
break;
case 'V': // hw/sw version
Serial.print("V1234");
slcan_ack();
break;
case 'N': // serial number
Serial.print("N1234");
slcan_ack();
break;
default: // unknown command
slcan_nack();
break;
}
}
// transfer messages from host to CAN bus
void xfer_tty2can()
{
int length;
static char cmdbuf[CMD_LEN];
static int cmdidx = 0;
if ((length = Serial.available()) > 0) {
for (int i = 0; i < length; i++) {
char val = Serial.read();
cmdbuf[cmdidx++] = val;
if (cmdidx == CMD_LEN) { // command is too long
slcan_nack();
cmdidx = 0;
} else if (val == '\r') { // end of command
cmdbuf[cmdidx] = '\0';
pars_slcancmd(cmdbuf);
cmdidx = 0;
}
}
}
}
// the loop function runs over and over again forever
void loop() {
xfer_can2tty();
xfer_tty2can();
}