-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
367 lines (327 loc) · 10 KB
/
main.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
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
/*
* main.c
*
* Created by SQ8KFH on 2014-08-09.
*
* Copyright (C) 2014-2020 Kamil Palkowski. All rights reserved.
*/
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "ft220x.h"
#include "can.h"
#define CAN_RECV_DDR DDRC
#define CAN_RECV_PORT PORTC
#define CAN_RECV PC0
uint8_t can_bitrate = 4;
char itoh(uint8_t i) {
char h[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
return h[i & 0x0F];
}
uint8_t htoi(char c) {
if ('0' <= c && c <= '9')
return ((uint8_t)(c - '0') & 0x0F);
switch (c) {
case 'a':
case 'A':
return 0x0A;
case 'b':
case 'B':
return 0x0B;
case 'c':
case 'C':
return 0x0C;
case 'd':
case 'D':
return 0x0D;
case 'e':
case 'E':
return 0x0E;
case 'f':
case 'F':
return 0x0F;
}
return 0x00;
}
uint8_t slcan_command_t(char *data) { //TX Frame Format SFF tiiildddddddddddddddd
can_buf_t frame;
uint8_t id2 = htoi(data[1]);
uint8_t id1 = htoi(data[2]);
uint8_t id0 = htoi(data[3]);
frame.canidt1 = id0;
frame.canidt1 <<= 4;
frame.canidt1 |= id1;
frame.canidt1 <<= 1;
frame.canidt1 |= (id2 >> 3);
frame.canidt2 = id2;
frame.canidt2 <<= 5;
frame.canidt3 = 0;
frame.canidt4 = 0;
uint8_t dlc = htoi(data[4]);
if (dlc > 8)
return 0;
frame.cancdmob = dlc;
for (int i = 5, i2 = 0; data[i] != '\r' && i2 < 8; i+=2, ++i2) {
frame.data[i2] = htoi(data[i]);
frame.data[i2] <<= 4;
frame.data[i2] |= htoi(data[i+1]);
}
return CAN_tx(&frame);
}
uint8_t slcan_command_T(char *data) { //TX Frame Format EFF Tiiiiiiiildddddddd...
can_buf_t frame;
uint8_t id7 = htoi(data[1]);
uint8_t id6 = htoi(data[2]);
uint8_t id5 = htoi(data[3]);
uint8_t id4 = htoi(data[4]);
uint8_t id3 = htoi(data[5]);
uint8_t id2 = htoi(data[6]);
uint8_t id1 = htoi(data[7]);
uint8_t id0 = htoi(data[8]);
frame.canidt4 = id1;
frame.canidt4 <<= 4;
frame.canidt4 |= id0;
frame.canidt4 <<= 3;
frame.canidt3 = id3;
frame.canidt3 <<= 4;
frame.canidt3 |= id2;
frame.canidt3 <<= 3;
frame.canidt3 |= (id1 >> 1);
frame.canidt2 = id5;
frame.canidt2 <<= 4;
frame.canidt2 |= id4;
frame.canidt2 <<= 3;
frame.canidt2 |= (id3 >> 1);
frame.canidt1 = id7;
frame.canidt1 <<= 4;
frame.canidt1 |= id6;
frame.canidt1 <<= 3;
frame.canidt1 |= (id5 >> 1);
uint8_t dlc = htoi(data[9]);
if (dlc > 8)
return 0;
frame.cancdmob = dlc | (1 << IDE);
for (int i = 10, i2 = 0; data[i] != '\r' && i2 < 8; i+=2, ++i2) {
frame.data[i2] = htoi(data[i]);
frame.data[i2] <<= 4;
frame.data[i2] |= htoi(data[i+1]);
}
return CAN_tx(&frame);
}
uint8_t slcan_command_r(char *data) { //TX Frame Format RTR/SFF riiil
can_buf_t frame;
uint8_t id2 = htoi(data[1]);
uint8_t id1 = htoi(data[2]);
uint8_t id0 = htoi(data[3]);
frame.canidt1 = id0;
frame.canidt1 <<= 4;
frame.canidt1 |= id1;
frame.canidt1 <<= 1;
frame.canidt1 |= (id2 >> 3);
frame.canidt2 = (id2 << 5);
frame.canidt3 = 0;
frame.canidt4 = (1 << RTRTAG);
uint8_t dlc = htoi(data[4]);
if (dlc > 8)
return 0;
frame.cancdmob = dlc;
return CAN_tx(&frame);
}
uint8_t slcan_command_R(char *data) { //TX Frame Format RTR/EFF Riiiiiiiil
can_buf_t frame;
uint8_t id7 = htoi(data[1]);
uint8_t id6 = htoi(data[2]);
uint8_t id5 = htoi(data[3]);
uint8_t id4 = htoi(data[4]);
uint8_t id3 = htoi(data[5]);
uint8_t id2 = htoi(data[6]);
uint8_t id1 = htoi(data[7]);
uint8_t id0 = htoi(data[8]);
frame.canidt4 = id1;
frame.canidt4 <<= 4;
frame.canidt4 |= id0;
frame.canidt4 <<= 3;
frame.canidt4 |= (1 << RTRTAG);
frame.canidt3 = id3;
frame.canidt3 <<= 4;
frame.canidt3 |= id2;
frame.canidt3 <<= 3;
frame.canidt3 |= (id1 >> 1);
frame.canidt2 = id5;
frame.canidt2 <<= 4;
frame.canidt2 |= id4;
frame.canidt2 <<= 3;
frame.canidt2 |= (id3 >> 1);
frame.canidt1 = id7;
frame.canidt1 <<= 4;
frame.canidt1 |= id6;
frame.canidt1 <<= 3;
frame.canidt1 |= (id5 >> 1);
uint8_t dlc = htoi(data[9]);
if (dlc > 8)
return 0;
frame.cancdmob = dlc | (1 << IDE);
return CAN_tx(&frame);
}
void slcan_interpreter(char *command) {
if (command[0] == 'T') { //TX Frame Format EFF Tiiiiiiiildddddddd...
if (slcan_command_T(command))
FT220X_write('\r');
else
goto error;
}
else if (command[0] == 't') { //TX Frame Format SFF tiiildddddddddd...
if (slcan_command_t(command))
FT220X_write('\r');
else
goto error;
}
else if (command[0] == 'R') { //TX Frame Format RTR/EFF Riiiiiiiil
if (slcan_command_R(command))
FT220X_write('\r');
else
goto error;
}
else if (command[0] == 'r') { //TX Frame Format RTR/SFF riiil
if (slcan_command_r(command))
FT220X_write('\r');
else
goto error;
}
else if (command[0] == 'F') { //Read Status Flags
if (command[1] != '\r')
goto error;
char tmp[5];
sprintf(tmp, "F%02hhx\r", CANGSTA);
FT220X_write_s(tmp);
}
else if (command[0] == 'O') { //Open Channel
if (command[1] != '\r')
goto error;
CAN_init(can_bitrate);
FT220X_write('\r');
}
else if (command[0] == 'C') { //Close Channel
if (command[1] != '\r')
goto error;
CAN_disable();
FT220X_write('\r');
}
else if (command[0] == 'S') { //Set bitrate
if (command[2] != '\r')
goto error;
if ('0' <= command[1] && command[1] <= '8') {
can_bitrate = ((uint8_t) (command[1] - '0') & 0x0Fu);
FT220X_write('\r');
}
else {
goto error;
}
}
// else if (command[0] == 'M') { //Acceptance Mask Mxxxxxxxx
// }
// else if (command[0] == 'm') { //Acceptance Value mxxxxxxxx
// }
else if (command[0] == 'V') { //HW/SW Version
if (command[1] != '\r')
goto error;
char tmp[9];
sprintf(tmp, "V01%02hhu\r", FTCAN_VERSION_MAJOR);
FT220X_write_s(tmp);
}
else if (command[0] == 'v') { //Major/Minor Version
if (command[1] != '\r')
goto error;
char tmp[9];
sprintf(tmp, "v%02hhu%02hhu\r", FTCAN_VERSION_MAJOR, FTCAN_VERSION_MINOR);
FT220X_write_s(tmp);
}
else {
goto error;
}
return;
error:
FT220X_write('\a');
return;
}
int main(void) {
CAN_RECV_DDR = (1 << CAN_RECV);
CAN_RECV_PORT &= ~(1 << CAN_RECV);
FT220X_init();
//CAN_init(can_bitrate);
sei();
FT220X_flush();
uint16_t led_counter = 0;
uint8_t read_idx = 0;
char read_buf[51];
uint8_t send_bell = 0;
while (1) {
if (led_counter > 100) {
CAN_RECV_PORT |= (1 << CAN_RECV);
}
else {
++led_counter;
}
read_idx += FT220X_read_line(&read_buf[read_idx],50-read_idx);
if (read_buf[read_idx-1] == '\r') {
read_buf[read_idx] = '\0';
read_idx=0;
slcan_interpreter(read_buf);
}
else if (read_idx >= 50) {
read_idx=0;
FT220X_write('\a');
}
if (send_bell) {
FT220X_write('\a');
send_bell=0;
}
if (can_rx_buf_top != can_rx_buf_bottom) {
CAN_RECV_PORT &= ~(1 << CAN_RECV);
led_counter = 0;
char buf[30];
uint8_t i, dlc;
if (can_rx_buf[can_rx_buf_bottom].cancdmob & (1 << IDE)) { //29-bits
if (can_rx_buf[can_rx_buf_bottom].canidt4 & (1 << RTRTAG)) //remote frame
buf[0] = 'R';
else
buf[0] = 'T';
buf[1] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 >> 7);
buf[2] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 >> 3);
buf[3] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 << 1 | can_rx_buf[can_rx_buf_bottom].canidt2 >> 7);
buf[4] = itoh(can_rx_buf[can_rx_buf_bottom].canidt2 >> 3);
buf[5] = itoh(can_rx_buf[can_rx_buf_bottom].canidt2 << 1 | can_rx_buf[can_rx_buf_bottom].canidt3 >> 7);
buf[6] = itoh(can_rx_buf[can_rx_buf_bottom].canidt3 >> 3);
buf[7] = itoh(can_rx_buf[can_rx_buf_bottom].canidt3 << 1 | can_rx_buf[can_rx_buf_bottom].canidt4 >> 7);
buf[8] = itoh(can_rx_buf[can_rx_buf_bottom].canidt4 >> 3);
dlc = can_rx_buf[can_rx_buf_bottom].cancdmob & 0x0F;
buf[9] = itoh(dlc);
i = 10;
}
else { //11-bits
if (can_rx_buf[can_rx_buf_bottom].canidt4 & (1 << RTRTAG)) //remote frame
buf[0] = 'r';
else
buf[0] = 't';
buf[1] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 >> 5);
buf[2] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 >> 1);
buf[3] = itoh(can_rx_buf[can_rx_buf_bottom].canidt1 << 3 | can_rx_buf[can_rx_buf_bottom].canidt2 >> 5);
dlc = can_rx_buf[can_rx_buf_bottom].cancdmob & 0x0F;
buf[4] = itoh(dlc);
i = 5;
}
if (!(can_rx_buf[can_rx_buf_bottom].canidt4 & (1 << RTRTAG))) {
for (uint8_t op = 0; op < dlc; ++op) {
buf[i++] = itoh(can_rx_buf[can_rx_buf_bottom].data[op] >> 4);
buf[i++] = itoh(can_rx_buf[can_rx_buf_bottom].data[op]);
}
}
buf[i++] = '\r';
buf[i++] = '\0';
send_bell = FT220X_write_s(buf); // == 1 -> fail
can_rx_buf_bottom = (uint8_t)((can_rx_buf_bottom + 1) & CAN_RX_BUF_INDEX_MASK);
}
}
}