-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecu-rc5.go
328 lines (264 loc) · 8.56 KB
/
ecu-rc5.go
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
package main
import (
"fmt"
"time"
"errors"
// "encoding/hex"
"github.com/distributed/sers"
"strconv"
)
var (
rc5PingCommand = []byte {0x82, 0x00, 0x7D}
rc5RequestFaultsCommand = []byte {0x82, 0x33, 0x4A}
rc5ClearFaultsCommand = []byte {0x82, 0xC3, 0xBA}
rc5WokeResponse = []byte {0x55, 0x06, 0x3B}
rc5PongResponse = []byte {0xC2, 0x00, 0x3D}
rc5FaultsResponse = []byte {0x33}
rc5FaultsClearedResponse = []byte {0xC2, 0xC3, 0x7A}
rc5UserCommands = map[string] []byte{
"clearfaults": rc5ClearFaultsCommand,
}
rc5Faults = map[int]string{
0x150A: "Driver airbag shorted to battery positive",
0x150B: "Driver airbag shorted to battery negative",
0x150C: "Driver airbag high resistance",
0x150D: "Driver airbag low resistance",
0x150E: "Driver airbag squib circuit",
0x1512: "Passenger airbag squib short to battery positive",
0x1513: "Passenger airbag 1 short to battery negative",
0x1514: "Passenger airbag 1 high resistance",
0x1515: "Passenger airbag 1 low resistance",
0x1516: "Passenger airbag 1 squib circuit",
0x151A: "Pretensioner short to battery positive",
0x151B: "Pretensioner short to battery negative",
0x151C: "Passenger airbag 2 high resistance",
0x151D: "Passenger airbag 2 low resistance",
0x151E: "Passenger airbag 2 squib circuit",
0x1524: "Right pretensioner high resistance",
0x1525: "Right pretensioner low resistance",
0x1526: "Right pretensioner squib circuit",
0x152C: "Left pretensioner high resistance",
0x152D: "Left pretensioner low resistance",
0x152E: "Left pretensioner squib circuit",
0x160C: "SRS warning lamp short circuit",
0x160D: "SRS warning lamp open circuit",
0x160E: "SRS warning lamp driver",
0x250A: "(Historic) Driver airbag shorted to battery positive",
0x250B: "(Historic) Driver airbag shorted to battery negative",
0x250C: "(Historic) Driver airbag high resistance",
0x250D: "(Historic) Driver airbag low resistance",
0x250E: "(Historic) Driver airbag squib circuit",
0x2512: "(Historic) Passenger airbag squib short to battery positive",
0x2513: "(Historic) Passenger airbag 1 short to battery negative",
0x2514: "(Historic) Passenger airbag 1 high resistance",
0x2515: "(Historic) Passenger airbag 1 low resistance",
0x2516: "(Historic) Passenger airbag 1 squib circuit",
0x251A: "(Historic) Pretensioner short to battery positive",
0x251B: "(Historic) Pretensioner short to battery negative",
0x251C: "(Historic) Passenger airbag 2 high resistance",
0x251D: "(Historic) Passenger airbag 2 low resistance",
0x251E: "(Historic) Passenger airbag 2 squib circuit",
0x2524: "(Historic) Right pretensioner high resistance",
0x2525: "(Historic) Right pretensioner low resistance",
0x2526: "(Historic) Right pretensioner squib circuit",
0x252C: "(Historic) Left pretensioner high resistance",
0x252D: "(Historic) Left pretensioner low resistance",
0x252E: "(Historic) Left pretensioner squib circuit",
0x260C: "(Historic) SRS warning lamp short circuit",
0x260D: "(Historic) SRS warning lamp open circuit",
0x260E: "(Historic) SRS warning lamp driver",
0x0000: "0x0000 Unknown fault, power cycle and try again",
}
)
func rc5SendNextCommand(sp sers.SerialPort, previousResponse []byte) {
if globalUserCommand != "" {
command, ok := rc5UserCommands[globalUserCommand];
if ok {
globalUserCommand = ""
sp.Write(command)
return
} else {
fmt.Println("Asked to perform a user command but don't understand it")
}
}
globalUserCommand = ""
if slicesEqual(previousResponse, rc5PongResponse) {
sp.Write(rc5RequestFaultsCommand)
} else if slicesEqual(previousResponse, rc5WokeResponse) || slicesEqual(previousResponse, rc5FaultsResponse) {
sp.Write(rc5PingCommand)
} else if slicesEqual(previousResponse, rc5FaultsClearedResponse) {
sp.Write(rc5RequestFaultsCommand)
} else { // fall back to ping
sp.Write(rc5PingCommand)
}
}
func readFirstBytesFromPortRc5(fn string) ([]byte, error) {
fmt.Println("Connecting to RC5 ECU")
globalConnected = false
sp, err := sers.Open(fn)
if err != nil {
return nil, err
}
defer sp.Close()
err = sp.SetMode(2400, 8, sers.N, 1, sers.NO_HANDSHAKE)
if err != nil {
return nil, err
}
// setting:
// minread = 0: minimal buffering on read, return characters as early as possible
// timeout = 1.0: time out if after 1.0 seconds nothing is received
err = sp.SetReadParams(0, 0.001)
if err != nil {
return nil, err
}
mode, err := sp.GetMode()
fmt.Println("Serial cable set to:")
fmt.Println(mode)
sp.SetBreak(false)
time.Sleep(2000 * time.Millisecond)
sp.SetBreak(true)
time.Sleep(200 * time.Millisecond)
sp.SetBreak(false)
time.Sleep(400 * time.Millisecond)
sp.SetBreak(true)
time.Sleep(400 * time.Millisecond)
sp.SetBreak(false)
time.Sleep(400 * time.Millisecond)
sp.SetBreak(true)
time.Sleep(400 * time.Millisecond)
sp.SetBreak(false)
time.Sleep(200 * time.Millisecond)
// TODO: get rid of this
// time.Sleep(1000 * time.Millisecond)
// buffer to read into
initBuffer := make([]byte, 0)
initLoops := 0
initLoopsLimit := 100
for initLoops < initLoopsLimit {
initLoops++
if initLoops > 1 {
time.Sleep(10 * time.Millisecond)
}
var rb []byte
rb = make([]byte, 128)
// read
n, err := sp.Read(rb[:])
if err != nil { continue }
if n == 0 { continue }
// fmt.Println("got some bytes:")
// fmt.Println(n)
// fmt.Printf("got %d bytes \n%s", len(initBuffer), hex.Dump(initBuffer))
// chop down to actual data size
rb = rb[0:n]
initBuffer = append(initBuffer, rb...)
// strip leading zeros (wake breaks causing them)
for (len(initBuffer) > 0 && initBuffer[0] == 0x00) {
// fmt.Println("Stripping leading zero")
initBuffer = initBuffer[1:]
}
if len(initBuffer) < 3 {
continue
}
if slicesEqual(initBuffer[0:3], rc5WokeResponse) {
fmt.Println("RC5 woke up")
globalConnected = true
break
} else {
return nil, errors.New("Unsure what RC5 sent back, aborting")
}
}
if initLoops >= initLoopsLimit {
return nil, errors.New("Timed out waiting for RC5 to wake up")
}
// wait 700ms after first connect
// normall sleep 200ms, so 500 extra this time
time.Sleep(500 * time.Millisecond)
// go into proper read/write loop here, with ping as first command
time.Sleep(200 * time.Millisecond)
rc5SendNextCommand(sp, rc5WokeResponse)
buffer := make([]byte, 0)
readLoops := 0
for readLoops < 100 {
readLoops++
if readLoops > 1 {
time.Sleep(10 * time.Millisecond)
}
rb := make([]byte, 128)
n, _ := sp.Read(rb[:])
rb = rb[0:n] // chop down to actual data size
buffer = append(buffer, rb...)
if n > 0 {
readLoops = 0 // reset timeout
}
if len(buffer) == 0 { continue }
if len(buffer) >= 3 {
// check for our echos and throw them away
if slicesEqual(buffer[0:3], rc5PingCommand) {
buffer = buffer[3:]
continue;
} else if slicesEqual(buffer[0:3], rc5RequestFaultsCommand) {
buffer = buffer[3:]
continue;
} else if slicesEqual(buffer[0:3], rc5ClearFaultsCommand) {
buffer = buffer[3:]
continue;
}
if slicesEqual(buffer[0:3], rc5PongResponse) {
fmt.Println("< PONG from ECU")
buffer = buffer[3:]
time.Sleep(200 * time.Millisecond)
rc5SendNextCommand(sp, rc5PongResponse)
continue
}
if slicesEqual(buffer[0:3], rc5FaultsClearedResponse) {
fmt.Println("< FAULT CODES CLEARED")
globalAlert = "ECU reports faults cleared"
buffer = buffer[3:]
time.Sleep(200 * time.Millisecond)
rc5SendNextCommand(sp, rc5FaultsClearedResponse)
continue
}
// faults returned
if len(buffer) > 2 && buffer[1] == rc5FaultsResponse[0] {
expectedLength := buffer[0]
expectedLength = expectedLength - 0xC0 + 1
if len(buffer) < int(expectedLength) {
continue
}
fmt.Println("< FAULTS Got fault codes!")
rc5ParseFaults(buffer)
buffer = buffer[expectedLength:]
time.Sleep(200 * time.Millisecond)
rc5SendNextCommand(sp, rc5FaultsResponse)
continue
}
}
}
if readLoops == 100 {
return nil, errors.New("readloop timed out")
}
fmt.Println("fell out of readloop")
return nil, err
}
func rc5ParseFaults(buffer []byte) {
// fmt.Printf("got %d bytes \n%s", len(buffer), hex.Dump(buffer))
// remove first 2 bytes which are length/type
buffer = buffer[2:]
numFaults := len(buffer)/2
fmt.Println("num faults:")
fmt.Println(numFaults)
faults := []string {}
i := 0
for i < numFaults {
fault := int(buffer[i*2]) << 8
fault += int(buffer[(i*2)+1])
faultText, ok := rc5Faults[fault];
if !ok {
faultText = "unknown fault: "+strconv.Itoa(fault);
}
// fmt.Println(faultText)
faults = append(faults, faultText)
i++
}
globalFaults = faults
}