-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcan232.cpp
More file actions
275 lines (242 loc) · 11.4 KB
/
can232.cpp
File metadata and controls
275 lines (242 loc) · 11.4 KB
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
/*******************************************************************************
can232.cpp
(c) 2010 by Sophie Dexter
portions (c) 2009, 2010 by Janis Silins (johnc)
Lawicel CAN232 type functions for Just4Trionic by Just4pLeisure
********************************************************************************
WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.
*******************************************************************************/
#include "can232.h"
#include "interfaces.h"
#include "sizedefs.h"
// constants
#define CMD_BUF_LENGTH 32 ///< command buffer size
// command characters
#define CMD_CLOSE 'C' ///< Close the CAN device
#define CMD_OPEN 'O' ///< Open the CAN device (do this before an S/s command)
#define CMD_PRESET_SPEED 'S' ///< Sn: set preconfigured speeds
#define CMD_SPEED_0 '0' ///< 10 kbits (or 47,619 bits with 's')
#define CMD_SPEED_1 '1' ///< 20 kbits (or 500 kbits with 's')
#define CMD_SPEED_2 '2' ///< 50 kbits (or 615 kbits with 's')
#define CMD_SPEED_3 '3' ///< 100 kbits
#define CMD_SPEED_4 '4' ///< 125 kbits
#define CMD_SPEED_5 '5' ///< 250 kbits
#define CMD_SPEED_6 '6' ///< 500 kbits
#define CMD_SPEED_7 '7' ///< 800 kbits
#define CMD_SPEED_8 '8' ///< 1 mbits
#define CMD_DIRECT_SPEED 's' ///< sxxyy: set the CAN bus speed registers directly
///< xx: BTR0 register setting
///< yy: BTR1 register setting
#define CMD_SEND_11BIT 't' ///< tiiildd..: send 11 bit id CAN frame
///< iii: identfier 0x0..0x7ff
///< l: Number of data bytes in CAN frame 0..8
///< dd..: data byte values 0x0..0xff (l pairs)
#define CMD_SEND_29BIT 'T' ///< Tiiiiiiiildd..: send 29 bit id CAN frame
///< iiiiiiii: identifier 0x0..0x1fffffff
///< l: Number of data bytes in CAN frame 0..8
///< dd..: data byte values 0x0..0xff (l pairs)
#define CMD_READ_FLAGS 'F' ///< Read flags !?!
#define CMD_FILTER 'f' ///< Filter which CAN message types to allow
#define CMD_FILTER_NONE '0' ///< Allow all CAN message types
#define CMD_FILTER_T5 '5' ///< Allow only Trionic 5 CAN message types
#define CMD_FILTER_T7 '7' ///< Allow only Trionic 5 CAN message types
#define CMD_FILTER_T8 '8' ///< Allow only Trionic 5 CAN message types
#define CMD_ACCEPT_CODE 'M' ///< Mxxxxxxxx: Acceptance code e.g. 0x00000000 } accept
#define CMD_ACCEPT_MASK 'm' ///< mxxxxxxxx: Acceptance mask e.g. 0xffffffff } all
#define CMD_VERSION 'V' ///< Replies with Firmware and hardware version numbers; 2 bytes each
#define CMD_SERIAL_NUMBER 'N' ///< Replies with serial number; 4 bytes
#define CMD_TIMESTAMP 'Z' ///< Zn: n=0 means timestamp off, n=1 means timestamp is on
///< Replies a value in milliseconds with two bytes 0x0..0xfa5f
///< equivalent to 0..60 seconds
// static variables
static char cmd_buffer[CMD_BUF_LENGTH]; ///< command string buffer
static uint32_t can_id; ///< can message id
static uint32_t can_len; ///< can message length
static uint8_t can_msg[8]; ///< can message frame - up to 8 bytes
// private functions
uint8_t execute_can_cmd();
// command argument macros
#define CHECK_ARGLENGTH(len) \
if (cmd_length != len + 2) \
return TERM_ERR
#define GET_NUMBER(target, offset, len) \
if (!ascii2int(target, cmd_buffer + 2 + offset, len)) \
return TERM_ERR
void can232() {
// main loop
*cmd_buffer = '\0';
char ret;
char rx_char;
while (true) {
// read chars from USB
if (pc.readable()) {
// turn Error LED off for next command
led4 = 0;
rx_char = pc.getc();
switch (rx_char) {
// 'ESC' key to go back to mbed Just4Trionic 'home' menu
case '\e':
can_close();
can.attach(NULL);
return;
// end-of-command reached
case TERM_OK :
// execute command and return flag via USB
ret = execute_can_cmd();
pc.putc(ret);
// reset command buffer
*cmd_buffer = '\0';
ret == TERM_OK ? led3 = 1 : led4 = 1;
break;
// another command char
default:
// store in buffer if space permits
if (StrLen(cmd_buffer) < CMD_BUF_LENGTH - 1) {
StrAddc(cmd_buffer, rx_char);
}
break;
}
}
}
}
//-----------------------------------------------------------------------------
/**
Executes a command and returns result flag (does not transmit the flag
itself).
@return command flag (success / failure)
*/
uint8_t execute_can_cmd() {
uint8_t cmd_length = StrLen(cmd_buffer);
char cmd = *(cmd_buffer + 1);
// command groups
switch (*cmd_buffer) {
// adapter commands
case CMD_SEND_11BIT:
if (cmd_length < 7) return TERM_ERR;
GET_NUMBER(&can_id, -1, 3);
GET_NUMBER(&can_len, 2, 1);
if (cmd_length < (4 + (can_len * 2))) return TERM_ERR;
for (uint8_t i = 0; i < (uint8_t)can_len; i++) {
uint32_t result;
GET_NUMBER(&result, (3 + (i * 2)), 2);
can_msg[i] = (uint8_t)result;
}
return (can_send_timeout (can_id, (char*)can_msg, (uint8_t)can_len, 500)) ? TERM_OK : TERM_ERR;
case CMD_SEND_29BIT:
break;
case CMD_CLOSE:
can_close();
return TERM_OK;
case CMD_OPEN:
can_open();
return TERM_OK;
case CMD_PRESET_SPEED:
CHECK_ARGLENGTH(0);
switch (cmd) {
case CMD_SPEED_0:
can_configure(2, 10000, 0);
break;
case CMD_SPEED_1:
can_configure(2, 20000, 0);
break;
case CMD_SPEED_2:
can_configure(2, 50000, 0);
break;
case CMD_SPEED_3:
can_configure(2, 100000, 0);
break;
case CMD_SPEED_4:
can_configure(2, 125000, 0);
break;
case CMD_SPEED_5:
can_configure(2, 250000, 0);
break;
case CMD_SPEED_6:
can_configure(2, 500000, 0);
break;
case CMD_SPEED_7:
can_configure(2, 800000, 0);
break;
case CMD_SPEED_8:
can_configure(2, 1000000, 0);
break;
default:
return TERM_ERR;
}
can.attach(&show_can_message);
return TERM_OK;
case CMD_DIRECT_SPEED:
CHECK_ARGLENGTH(0);
switch (cmd) {
case CMD_SPEED_0:
can_configure(2, 47619, 0);
break;
case CMD_SPEED_1:
can_configure(2, 500000, 0);
break;
case CMD_SPEED_2:
can_configure(2, 615000, 0);
break;
default:
return TERM_ERR;
}
can.attach(&show_can_message);
return TERM_OK;
case CMD_FILTER:
CHECK_ARGLENGTH(0);
switch (cmd) {
case CMD_FILTER_NONE:
can_use_filters(false); // Accept all messages (Acceptance Filters disabled)
return TERM_OK;
case CMD_FILTER_T5:
can_reset_filters();
can_add_filter(2, 0x005); //005h -
can_add_filter(2, 0x006); //006h -
can_add_filter(2, 0x00C); //00Ch -
can_add_filter(2, 0x008); //008h -
return TERM_OK;
case CMD_FILTER_T7:
can_reset_filters();
can_add_filter(2, 0x220); //220h
can_add_filter(2, 0x238); //238h
can_add_filter(2, 0x240); //240h
can_add_filter(2, 0x258); //258h
can_add_filter(2, 0x266); //266h - Ack
return TERM_OK;
can_add_filter(2, 0x1A0); //1A0h - Engine information
can_add_filter(2, 0x280); //280h - Pedals, reverse gear
can_add_filter(2, 0x290); //290h - Steering wheel and SID buttons
can_add_filter(2, 0x2F0); //2F0h - Vehicle speed
can_add_filter(2, 0x320); //320h - Doors, central locking and seat belts
can_add_filter(2, 0x370); //370h - Mileage
can_add_filter(2, 0x3A0); //3A0h - Vehicle speed
can_add_filter(2, 0x3B0); //3B0h - Head lights
can_add_filter(2, 0x3E0); //3E0h - Automatic Gearbox
can_add_filter(2, 0x410); //410h - Light dimmer and light sensor
can_add_filter(2, 0x430); //430h - SID beep request (interesting for Knock indicator?)
can_add_filter(2, 0x460); //460h - Engine rpm and speed
can_add_filter(2, 0x4A0); //4A0h - Steering wheel, Vehicle Identification Number
can_add_filter(2, 0x520); //520h - ACC, inside temperature
can_add_filter(2, 0x530); //530h - ACC
can_add_filter(2, 0x5C0); //5C0h - Coolant temperature, air pressure
can_add_filter(2, 0x630); //630h - Fuel usage
can_add_filter(2, 0x640); //640h - Mileage
can_add_filter(2, 0x7A0); //7A0h - Outside temperature
return TERM_OK;
case CMD_FILTER_T8:
can_reset_filters();
can_add_filter(2, 0x645); //645h - CIM
can_add_filter(2, 0x7E0); //7E0h -
can_add_filter(2, 0x7E8); //7E8h -
can_add_filter(2, 0x311); //311h -
can_add_filter(2, 0x5E8); //5E8h -
//can_add_filter(2, 0x101); //101h -
return TERM_OK;
}
break;
}
// unknown command
return TERM_ERR;
}