-
Notifications
You must be signed in to change notification settings - Fork 179
/
slcan.c
220 lines (191 loc) · 4.95 KB
/
slcan.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
#include "stm32f0xx_hal.h"
#include "can.h"
#include "slcan.h"
static uint32_t current_filter_id = 0;
static uint32_t current_filter_mask = 0;
int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
uint8_t i = 0;
uint8_t id_len, j;
uint32_t tmp;
for (j=0; j < SLCAN_MTU; j++) {
buf[j] = '\0';
}
// add character for frame type
if (frame->RTR == CAN_RTR_DATA) {
buf[i] = 't';
} else if (frame->RTR == CAN_RTR_REMOTE) {
buf[i] = 'r';
}
// assume standard identifier
id_len = SLCAN_STD_ID_LEN;
tmp = frame->StdId;
// check if extended
if (frame->IDE == CAN_ID_EXT) {
// convert first char to upper case for extended frame
buf[i] -= 32;
id_len = SLCAN_EXT_ID_LEN;
tmp = frame->ExtId;
}
i++;
// add identifier to buffer
for(j=id_len; j > 0; j--) {
// add nybble to buffer
buf[j] = (tmp & 0xF);
tmp = tmp >> 4;
i++;
}
// add DLC to buffer
buf[i++] = frame->DLC;
// add data bytes
for (j = 0; j < frame->DLC; j++) {
buf[i++] = (frame->Data[j] >> 4);
buf[i++] = (frame->Data[j] & 0x0F);
}
// convert to ASCII (2nd character to end)
for (j = 1; j < i; j++) {
if (buf[j] < 0xA) {
buf[j] += 0x30;
} else {
buf[j] += 0x37;
}
}
// add carrage return (slcan EOL)
buf[i++] = '\r';
// return number of bytes in string
return i;
}
int8_t slcan_parse_str(uint8_t *buf, uint8_t len) {
CanTxMsgTypeDef frame;
uint8_t i;
// convert from ASCII (2nd character to end)
for (i = 1; i < len; i++) {
// lowercase letters
if(buf[i] >= 'a')
buf[i] = buf[i] - 'a' + 10;
// uppercase letters
else if(buf[i] >= 'A')
buf[i] = buf[i] - 'A' + 10;
// numbers
else
buf[i] = buf[i] - '0';
}
if (buf[0] == 'O') {
// open channel command
can_enable();
return 0;
} else if (buf[0] == 'C') {
// close channel command
can_disable();
return 0;
} else if (buf[0] == 'S') {
// set bitrate command
switch(buf[1]) {
case 0:
can_set_bitrate(CAN_BITRATE_10K);
break;
case 1:
can_set_bitrate(CAN_BITRATE_20K);
break;
case 2:
can_set_bitrate(CAN_BITRATE_50K);
break;
case 3:
can_set_bitrate(CAN_BITRATE_100K);
break;
case 4:
can_set_bitrate(CAN_BITRATE_125K);
break;
case 5:
can_set_bitrate(CAN_BITRATE_250K);
break;
case 6:
can_set_bitrate(CAN_BITRATE_500K);
break;
case 7:
can_set_bitrate(CAN_BITRATE_750K);
break;
case 8:
can_set_bitrate(CAN_BITRATE_1000K);
break;
default:
// invalid setting
return -1;
}
return 0;
} else if (buf[0] == 'm' || buf[0] == 'M') {
// set mode command
if (buf[1] == 1) {
// mode 1: silent
can_set_silent(1);
} else {
// default to normal mode
can_set_silent(0);
}
return 0;
} else if (buf[0] == 'F') {
// set filter command
uint32_t id = 0;
for (i = 1; i < len; i++) {
id *= 16;
id += buf[i];
}
current_filter_id = id;
can_set_filter(current_filter_id, current_filter_mask);
} else if (buf[0] == 'K') {
// set mask command
uint32_t mask = 0;
for (i = 1; i < len; i++) {
mask *= 16;
mask += buf[i];
}
current_filter_mask = mask;
can_set_filter(current_filter_id, current_filter_mask);
} else if (buf[0] == 't' || buf[0] == 'T') {
// transmit data frame command
frame.RTR = CAN_RTR_DATA;
} else if (buf[0] == 'r' || buf[0] == 'R') {
// transmit remote frame command
frame.RTR = CAN_RTR_REMOTE;
} else {
// error, unknown command
return -1;
}
if (buf[0] == 't' || buf[0] == 'r') {
frame.IDE = CAN_ID_STD;
} else if (buf[0] == 'T' || buf[0] == 'R') {
frame.IDE = CAN_ID_EXT;
} else {
// error
return -1;
}
frame.StdId = 0;
frame.ExtId = 0;
if (frame.IDE == CAN_ID_EXT) {
uint8_t id_len = SLCAN_EXT_ID_LEN;
i = 1;
while (i <= id_len) {
frame.ExtId *= 16;
frame.ExtId += buf[i++];
}
}
else {
uint8_t id_len = SLCAN_STD_ID_LEN;
i = 1;
while (i <= id_len) {
frame.StdId *= 16;
frame.StdId += buf[i++];
}
}
frame.DLC = buf[i++];
if (frame.DLC < 0 || frame.DLC > 8) {
return -1;
}
uint8_t j;
for (j = 0; j < frame.DLC; j++) {
frame.Data[j] = (buf[i] << 4) + buf[i+1];
i += 2;
}
// send the message
can_tx(&frame, 10);
return 0;
}