forked from metakevin/2can
/
2con.py
executable file
·1314 lines (1110 loc) · 38.1 KB
/
2con.py
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#******************************************************************************
# File: 2con.pl
# Author: Kevin Day
# Date: December, 2011
# Description:
# 2CAN bus analyzer debug console / web server
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (c) 2011 Kevin Day
# All rights reserved.
#******************************************************************************
import d2xx
import serial
from time import *
import os
import sys
import time
import traceback
import random
import exceptions
import traceback
from struct import *
import array
import threading
from Queue import Queue,Empty
import json
import pickle
# some constants from the C code
TASK_ID_COMMS = 0xC
TASK_ID_CAN = 8
TASK_ID_MCP = 4
MCP_MSG_READ = 4
MCP_MSG_WRITE = 5
COMMS_MSG_ECHO_REQUEST = 1
COMMS_MSG_ECHO_REPLY = 2
COMMS_MSG_STATS = 3
COMMS_MSG_SET_BAUD = 4
COMMS_MSG_NAK = 0xA
COMMS_MSG_BADTASK = 0xE
COMMS_MSG_CAN_RAW = 1
CAN_SEND_RAW_MSG = 2
CAN_STATS = 4
CAN_SET_BT = 0xB
CAN_SET_ACCEPT_FILTER = 0xA
CAN_SET_SID_FILTER = 0xC
def sendbytes(bytes):
# print "Sending " + str([hex(x) for x in bytes])
#usb.write(array.array('B', bytes).tostring())
ser.write(array.array('B', bytes).tostring())
tofile = None
msg = []
def send(task, code, payload=[]):
flags = 0
sender = 0
destination = 0
plen = len(payload)
if plen > 15:
print "Payload too long (%d)" % plen
raise BaseException
if task > 15:
print "Invalid task (%d)" % task
raise BaseException
if code > 15:
print "Invalid code (%d)" % code
raise BaseException
# note: gcc treats AVR as little endian, so 'flags' are the 4 lower bits
m = [((plen<<4)|flags), ((sender<<4)|destination), ((task<<4)|code)] + payload
csum = 0
for b in m:
csum += b
checksum = escape([(csum>>8)&0xFF, csum&0xFF])
me = escape(m)
msg = [0x7E] + me + checksum
sendbytes(msg)
def escape(m):
me = []
for b in m:
if b == 0x7D or b == 0x7E:
me.append(0x7D)
me.append(b^0x20)
else:
me.append(b)
return me
class TimeoutException(Exception):
def __init__(self,value):
self.value = value
def __str__(self):
return repr(self.value)
class NakException(Exception):
def __init__(self,value):
self.value = value
def __str__(self):
return repr(self.value)
# returns one integer representing one byte
def usbread(l):
tries = 0
maxtries = 5
while tries < maxtries:
s = ser.read(1)
if len(s) == 1:
break
tries = tries + 1
if tries == maxtries:
raise TimeoutException(tries)
#print "usb read returns",s
bs = [ord(x) for x in s]
# hs = [hex(x) for x in bs]
# print "Received",hs
return bs
def rd():
bs = usbread(256)
print [hex(x) for x in bs]
def usbread_esc(n):
bv = []
for i in range(0,n):
b = usbread(1)[0]
if b == 0x7D:
b = usbread(1)[0] ^ 0x20
bv.append(b)
return bv
def rcv_direct():
while 1:
b = usbread_esc(1)
if b[0] == 0x7E:
(m1, m2, m3) = usbread_esc(3)
csum = m1 + m2 + m3;
plen = (m1 >>4)
if (m1&1):
# big backet: plen is the number of octets of message size that
# preceed the actual payload
if plen == 1:
plen = usbread_esc(1)[0]
csum += plen
# print "Big packet payload length is %d" % plen
else:
print "FIXME: multi-octet big packet RX not supported (got %d)" % plen
raise BaseException
payload = usbread_esc(plen)
fromaddr = (m2&0xF)
task = (m3>>4)
code = (m3&0xF)
csum_v = usbread_esc(2)
csum_wire = ((csum_v[0]<<8) + csum_v[1])
for b in payload:
csum = csum + b
if csum != csum_wire:
print "Checksum error: calculated %X got %X" % (csum, csum_wire)
else:
return [task, code] + payload
else:
print "Junk character:",hex(b[0])
def rcvmsg():
while 1:
try:
m = rcv()
parse(m)
return
except KeyboardInterrupt:
print "Abort"
return
except TimeoutException as (attempts):
print "Timed out after %s attempts" % str(attempts)
return
except NakException:
print "Retransmitting"
sendbytes(msg)
def as_u32(bytes):
return ((bytes[3]<<24) | (bytes[2]<<16) | (bytes[1]<<8) | (bytes[0]))
def as_u16(bytes):
return ((bytes[1]<<8) | (bytes[0]))
def checkNak(msg):
task = msg[0]
code = msg[1]
if task == TASK_ID_COMMS and code == COMMS_MSG_NAK:
return 1
return 0
def parse(msg):
task = msg[0]
code = msg[1]
payload = msg[2:]
if checkNak(msg):
raise NakException(0)
print "Message task/code %X/%x %d bytes" % (task, code, len(payload))
if task == TASK_ID_COMMS and code == COMMS_MSG_STATS:
print "Packets RX: ", as_u32(payload[0:4])
print "Packets TX: ", as_u32(payload[4:8])
print "Phy bytes RX: ", as_u32(payload[8:12])
print "Phy bytes TX: ", as_u32(payload[12:16])
print "Junk bytes: ", as_u16(payload[16:18])
print "Error bytes: ", as_u16(payload[18:20])
print "Checksum errors: ", as_u16(payload[20:22])
print "Bad code packets: ", as_u16(payload[22:24])
print "RX ring overflow: ", as_u16(payload[24:26])
print "RX no-buffers: ", as_u16(payload[26:28])
print "RX state: ", payload[28]
print "In escape: ", payload[29]
print "Uptime: ", as_u32(payload[30:34])
print "TX no buffers: ", as_u16(payload[34:36])
print "Mailbox overflow: ", as_u16(payload[36:38])
print "UART RX overrun: ", as_u16(payload[38:40])
print "UART RX frame error: ", as_u16(payload[40:42])
print "UART RX parity error ", as_u16(payload[42:44])
print "UART RX multi-byte ", as_u16(payload[44:46])
print "UART TX drop (ISR) ", as_u16(payload[46:48])
print "UART TX ringfull wait", as_u16(payload[48:50])
print "Reset cause ", hex(payload[50]),
rstbits = {'PORF':0x1, 'EXTRF':0x2, 'BORF':0x4, 'WDRF':0x8, 'JTRF':0x10}
for n,m in rstbits.iteritems():
if (payload[50]&m):
print n,
print ""
elif task == TASK_ID_COMMS and code == COMMS_MSG_BADTASK:
print "Bad task: ", hex(payload[0])
print "Bad code: ", hex(payload[1])
elif task == TASK_ID_COMMS and code == COMMS_MSG_ECHO_REPLY:
print "Echo reply: ", [hex(x) for x in payload]
elif task == TASK_ID_MCP and code == MCP_MSG_READ:
print "MCP read:", [hex(x) for x in payload],[bin(x) for x in payload]
elif task == TASK_ID_MCP and code == COMMS_MSG_CAN_RAW:
# old format: parse_mcp_can(payload)
print_can("mcp", payload)
elif task == TASK_ID_CAN and code == COMMS_MSG_CAN_RAW:
print_can("avr", payload)
elif task == TASK_ID_CAN and code == CAN_STATS:
print "Bus Error ", as_u16(payload[0:2])
print "Over time ", as_u16(payload[2:4])
print "Stuff Error ", as_u16(payload[4:6])
print "CRC Error ", as_u16(payload[6:8])
print "Frame Error ", as_u16(payload[8:10])
print "Ack Error ", as_u16(payload[10:12])
print "Unhandled Interrupt ", as_u16(payload[12:14])
print "MOB RXOK ", as_u16(payload[14:16])
print "MOB TXOK ", as_u16(payload[16:18])
print "MOB DLCW ", as_u16(payload[18:20])
print "MOB Bit Err ", as_u16(payload[20:22])
print "MOB Stuff Error ", as_u16(payload[22:24])
print "MOB CRC Error ", as_u16(payload[24:26])
print "MOB Frame Error ", as_u16(payload[26:28])
print "MOB Ack Error ", as_u16(payload[28:30])
print "RX filtered ", as_u16(payload[30:32])
print "RX relayed ", as_u16(payload[32:34])
elif task == TASK_ID_CAN and code == CAN_SET_BT:
print "AVR CAN bit time set: CANBT[1..3] = ", [hex(x) for x in msg[2:5]];
else:
print "Unrecognized message", [hex(x) for x in msg]
def parse_mcp_can(msg):
#print [hex(x) for x in msg]
eid = -1
sid = (msg[0]<<3) | (msg[1]>>5)
if (msg[1] & (1<<3)):
eid = (sid<<18) | ((msg[1]&3)<<16) | (msg[2]<<8) | (msg[3])
plen = (msg[4]&0xF);
payload = msg[5:(5+plen)]
print "MCP2515 CAN frame: ",
if eid == -1:
print "SID %03X " % sid, bin(sid),
else:
print "EID %08X " % eid, bin(eid),
print [hex(x) for x in payload]
CAN_FLAG_EID = 1
def parse_can(msg):
ts = msg[1]<<8|msg[0]
flags = msg[2]
payload = msg[7:]
id = (msg[6]<<24)|(msg[5]<<16)|(msg[4]<<8)|msg[3]
return (id,flags,payload,ts)
def print_can(src, msg):
#typedef u16 can_20a_addr_t;
#typedef u32 can_20b_addr_t;
#typedef struct {
# u8 ver_20b : 1; /* 11 bit IDs if zero; 29 bit if one */
# u8 rtr : 1;
# union {
# can_20a_addr_t s;
# can_20b_addr_t e;
# } id;
#} can_addr_t;
#define CAN_MAX_PAYLOAD 8
(id, flags, payload, ts) = parse_can(msg)
print src, "CAN frame (flags %s) TS %d " % (bin(flags),ts),
if (flags & CAN_FLAG_EID): # first bit in field, i.e. ver_20b
# 29 bit
# eid = (msg[1]<<21)|(msg[2]<<13)|(msg[3]<<5)|(msg[4]>>3)
print "EID %08X " % id, bin(id),
else:
# sid = (msg[1]<<3)|(msg[2]>>5)
print "SID %03X " % id, bin(id),
print [hex(x) for x in payload]
def avrsend(sid, data, eid=0):
flags = 0
if eid:
flags = (flags | 1)
msg = [flags, (sid)&0xFF, (sid>>8)&0xFF, (sid>>16)&0xFF, (sid>>24)&0xFF] + data
send(TASK_ID_CAN, CAN_SEND_RAW_MSG, msg)
def mcpsend(sid, data, eid=0):
flags = 0
if eid:
flags = (flags | 1)
msg = [flags, (sid)&0xFF, (sid>>8)&0xFF, (sid>>16)&0xFF, (sid>>24)&0xFF] + data
send(TASK_ID_MCP, CAN_SEND_RAW_MSG, msg)
def cansend(interface, sid, data, eid=0):
if interface == 0:
avrsend(sid, data, eid)
else:
mcpsend(sid, data, eid)
def canrcv():
while 1:
m = rcv()
if len(m) >= 2:
task = m[0]
code = m[1]
payload = m[2:]
if (task == TASK_ID_MCP or task == TASK_ID_CAN) and code == COMMS_MSG_CAN_RAW:
(id, flags, payload, ts) = parse_can(payload)
rxif = 0
if task == TASK_ID_MCP:
rxif = 1
return (rxif, id, flags, payload, ts)
parse(m)
def canwalk(iter=1,start=0,end=40):
mcpmode("normal")
bad = 0
good = 0
j = 0
while j < iter:
for plen in range(0,9):
for txif in range(0,2):
for idbit in range(start,end):
if idbit < 11:
txeid = 0
txsid = (1<<idbit) + j
else:
txeid = 1
txsid = (1<<(idbit-11)) + j
txdata = []
for i in range(0,plen):
txdata.append(int(random.uniform(0,255)))
cansend(txif, txsid, txdata, txeid)
(rxif, rxsid, rxeid, rxdata) = canrcv()
mismatch = 0
if len(txdata) != len(rxdata):
mismatch = 1
else:
for td,rd in zip(txdata,rxdata):
if td != rd:
mismatch = 1
if mismatch == 0 and txif != rxif and txeid == rxeid and rxsid == txsid:
print "OK %d->%d eid=%d ID %X" % (txif,rxif,txeid,txsid), [hex(x) for x in txdata]
good=good+1
else:
print "BAD %d->%d: sent eid=%d ID %X received eid=%d %X" % (txif,rxif,txeid,txsid,rxeid,rxsid)
print "TX data", [hex(x) for x in txdata], "RX data", [hex(x) for x in rxdata]
bad=bad+1
j = j + 1
print "Good %d bad %d" % (good,bad)
def rcvraw():
while 1:
b = usbread_esc(1)[0]
print "Raw",b
def datatest(pl,c):
bad=0
naks=0
timeouts=0
j=0
lastgood=[]
while j < c:
p = [(j>>8)&0xFF,j&0xFF]
for i in range(2,pl):
p.append(int(random.uniform(0,255)))
while 1:
send(TASK_ID_COMMS, COMMS_MSG_ECHO_REQUEST, p)
# print "Sent: ",p
try:
r = rcv()
except TimeoutException:
print "Timeout in iteration %d" % j
timeouts = timeouts + 1
return;
# print "Received:",r
if checkNak(r):
# print "NAK"
naks = naks+1
else:
break
if r[0] == TASK_ID_COMMS and r[1] == COMMS_MSG_ECHO_REPLY:
mismatch = 0
for sx,rx in zip(p,r[2:]):
if sx != rx:
mismatch = 1
break
if mismatch:
print "Data mismatch: sent", [hex(x) for x in p]
print " received ", [hex(x) for x in r]
print " last good rx ", [hex(x) for x in lastgood]
bad=bad+1
rcvmsg() # resync
else:
# print "Data matched"
lastgood=r
else:
print "Code mismatch: sent %X/%X received %X/%X" % (TASK_ID_COMMS,COMMS_MSG_ECHO_REQUEST,r[0],r[1])
break
# print "Iteration %d complete" % j
j=j+1
print "Received %d bad packets of %d with %d retransmits and %d timeouts" % (bad,j,naks,timeouts)
def echo():
send(TASK_ID_COMMS, COMMS_MSG_ECHO_REQUEST, [])
parse(rcv())
def stats():
send(TASK_ID_COMMS, COMMS_MSG_STATS)
rcvmsg()
def canstats(clear=1):
send(TASK_ID_CAN, CAN_STATS, [clear])
rcvmsg()
def setbaud(bps=9600, dblspeed=0):
clkio = 16000000
if dblspeed == 0:
div = 16.0
else:
div = 8.0
ubrr = int((1.0*clkio / (div*bps)) + .5) - 1
efbps = clkio / (div*(ubrr+1))
err = -(1 - (1.0*efbps/bps)) * 100
print "Target BPS = %d Effective BPS = %d Error = %.1f%% UBRR = %02X (%d)" % (bps, efbps, err, ubrr, ubrr)
send(TASK_ID_COMMS, COMMS_MSG_SET_BAUD, [ubrr, dblspeed])
sleep(1)
ser.baudrate = bps
def modem():
ms = usb.getModemStatus()
print "Modem status: %X" % ms
bits = {'CTS':0x10, 'DSR':0x20, 'RI':0x40, 'DCD':0x80, 'OE':0x200, 'PE':0x400, 'FE':0x800, 'BI':0x1000}
for n,m in bits.iteritems():
if (ms&m):
print "%04s: SET" % n
else:
print "%04s: CLEAR" % n
(rx, tx, evt) = usb.getStatus()
print " txQ: %d" % tx
print " rxQ: %d" % rx
print " evt: %X" % evt
def mcpread(addr, len):
send(TASK_ID_MCP, MCP_MSG_READ, [addr, len])
rcvmsg()
def mcpwrite(addr, buf):
send(TASK_ID_MCP, MCP_MSG_WRITE, [addr] + buf)
def mcpmode(mode,bits=7):
if mode == "normal":
reqop = 0
elif mode == "sleep":
reqop = 1
elif mode == "loopback":
reqop = 2
elif mode == "listen":
reqop = 3
elif mode == "config":
reqop = 4
else:
print "invalid mode: options are normal,sleep,loopback,listen,config"
return
rval = reqop<<5 | bits
mcpwrite(0xF, [rval])
mcpread(0xF, 1)
# set baud rate 250kbps
# mcpwrite(0x28,[5,0xb8,1])
def mcpsend_reg(sid, data, eid=0, txbuf=0):
# registers: 0x30-0x3D + txbuf*0x10
# TXBnCTRL: set bit 3 to request TX, rest as zero
# TXBnSIDH: SID10:SID3
# TXBnSIDL: SID2:SID0-EXIDE-EID17:16 set bit 3 for extended ID
# TXBnEID8: EID15:EID8
# TXBnEID0: EID7:EID0
# TXBnDLC: bit 6 RTR bits 3-0 length
# TXBnDm: 0-8 bytes
tx = [1<<3, (sid>>3)&0xFF, (sid<<5)&0xFF, 0, 0, len(data)] + data
if eid != 0:
eidx = sid&0x3FFFF
sidx = sid>>18
tx = [1<<3, (sidx>>3)&0xFF, ((sidx<<5)&0xFF)| (1<<3) | ((eidx>>16)&3), ((eidx>>8)&0xFF), (eidx&0xFF), len(data)] + data
else:
tx = [1<<3, (sid>>3)&0xFF, (sid<<5)&0xFF, 0, 0, len(data)] + data
print [(hex(x), bin(x)) for x in tx]
basereg = 0x30 + txbuf*0x10
# address & data (the EID bytes could be skipped -- not sure if
# two writes is cheaper than one with two extra bytes)
mcpwrite(basereg+1, tx[1:])
# command (this could be done with the RTS pin)
mcpwrite(basereg, tx[0:1])
# note: must be run in config mode
MCP_REG_CNF1 = 0x2A
def mcpbaud(bps):
# This assumes CNF2/3 are programmed at startup to 0xB8 and 0x05 and Fosc is 16M
if bps == 500000:
brp = 0
else:
brp = ((((1000000000.0/bps)/16.0)/62.5)-2)/2 - 1
print "BRP for %d BPS is %f" % (bps, brp)
if int(brp) != brp:
print "Warning: prescaler is not an integer"
mcpwrite(MCP_REG_CNF1, [int(brp)])
mcpread(MCP_REG_CNF1, 3)
def avrbaud(bps):
canbt1 = {100000:18, 125000:14, 200000:8, 250000:6, 500000:2, 1000000:0}
if bps in canbt1:
send(TASK_ID_CAN, CAN_SET_BT, [canbt1[bps]])
rcvmsg()
else:
print "Invalid AVR baud rate %d" % bps
def canspeed(bps):
mcpbaud(bps)
avrbaud(bps)
def resetavr():
serclose()
usbopen()
# toggle CBUS2 (/RST)
usb.setBitMode(0x40,0x20)
usb.setBitMode(0x44,0x20)
usbclose()
print "reset opening serial"
seropen(9600)
print "serial opened"
def newspeed(bps=9600):
ser.baudrate = 9600
resetavr()
# wait for AVR to boot
sleep(1)
# switch to fast mode
setbaud(bps)
stats()
# main program
twocan = -1
usb = -1
ser = -1
# list devices by description, returns tuple of attached devices description strings
dl = d2xx.listDevices(d2xx.OPEN_BY_DESCRIPTION)
for num,name in enumerate(dl):
print "Found device " + str(num) + " named " + name
if name == "2CAN":
twocan = num
break
if twocan < 0:
print "2CAN not found"
raise BaseException
def usbopen():
global usb
usb = d2xx.open(twocan)
def usbclose():
global usb
usb.close()
usbopen()
print usb.getDeviceInfo()
port = usb.getComPortNumber()
print "Virtual COM port is COM%d " % port
usbclose()
rxq = Queue(50)
rxqctrl = Queue(1)
def seropen(bps):
global ser, port
timeout = 0.1
ser = serial.Serial(port-1,bps,serial.EIGHTBITS,serial.PARITY_NONE,serial.STOPBITS_ONE,timeout,False,True,None,False,None)
rxqctrl.put("go")
def serclose():
rxqctrl.put("wait")
rxq.get()
print "Sleep waiting for rxq stop"
sleep(1)
global ser
ser.close()
seropen(9600)
print "Opened serial port", ser.portstr
def rawread(n):
bv = []
for i in range(0,n):
b = usbread(1)[0]
bv.append(b)
return bv
def enter_bootloader():
serclose()
usbopen()
# assert CBUS2 (/RST)
# set CBUS3 high (enter bootloader mode)
usb.setBitMode(0xC8,0x20)
# deassert /RST, leave CBUS3 high
usb.setBitMode(0xCC,0x20)
usbclose()
print "bootloader opening serial"
seropen(38400)
print "serial opened, waiting for bootloader..."
try:
s = rawread(6);
print "got '%s'"%(s)
except TimeoutException:
print "Timed out waiting for bootloader"
def load_hex(filename):
import ihex
f = ihex.IHex();
f.read_file(filename);
for addr, data in f.areas.iteritems():
print "Area at %X for %X"%(addr, len(data))
def bootloader():
while 1:
try:
input("2CAN bootloader>")
except exceptions.KeyboardInterrupt:
break
except exceptions.EOFError:
break
except exceptions.ValueError:
break
except:
(t, v, tb) = sys.exc_info()
print "Exception:", t, v
print traceback.print_tb(tb)
sys.exit(1)
if len(sys.argv) > 1:
if sys.argv[1] == 'prog':
print "Entering bootloader mode"
bootloader()
else:
print "unrecognized argument"
def rxhandler():
cbs = []
while 1:
try:
m = rcv_direct()
havecb = 0
for c in filter(lambda x: x[0:2] == m[0:2], cbs):
# print "Callback for %X/%X" % (c[0], c[1])
havecb = 1
global rxtime
rxtime = time.time()
c[2](m)
if havecb == 0:
rxq.put(m)
except TimeoutException:
pass
if not rxqctrl.empty():
what = rxqctrl.get()
if what == "stop":
print "RX handler stopped"
break
elif what == "wait":
print "RX handler waiting"
rxq.put([])
block = rxqctrl.get()
elif what == "go":
print "RX handler: go"
pass
elif isinstance(what, (list,tuple)):
if what[0] == "addcb":
# print "Add callback for %X/%X" % (what[1],what[2])
cbs.append([what[1], what[2], what[3]])
elif what[0] == "delcb":
# for c in filter(lambda x: x[0:3] == what[1:4], cbs):
# print "Delete callback for %X/%X" % (what[1],what[2])
cbs.remove([what[1], what[2], what[3]])
elif what[0] == "resetcb":
cbs = []
else:
print "RX handler: unknown list command"
else:
print "RX handler: unknown command " + what
rxthread = threading.Thread(target=rxhandler)
rxthread.start()
def rcv():
try :
m = rxq.get(True,1)
#print "rxq.get: ", m
return m
except Empty:
raise TimeoutException(1)
def add_rcv_cb(task, code, func):
rxqctrl.put(["addcb", task, code, func])
def del_rcv_cb(task, code, func):
rxqctrl.put(["delcb", task, code, func])
tracing = 0
def tracemsg(prefix,ts,id,flags,data):
global tracing
if not tracing:
return
if flags&CAN_FLAG_EID:
type = "EID"
else:
type = "SID"
h = ""
for x in data:
h += "%02X " % (x)
print "%2d.%03d %s (%02X) %s %03X " % (ts/1000, ts%1000, prefix, flags, type, id), h
paused = 0
def canframecb(msg):
task = msg[0]
code = msg[1]
payload = msg[2:]
if checkNak(msg):
raise NakException(0)
rxif = -1
txif = -1
if task == TASK_ID_MCP and code == COMMS_MSG_CAN_RAW:
rxif = 1
txif = 0
elif task == TASK_ID_CAN and code == COMMS_MSG_CAN_RAW:
rxif = 0
txif = 1
else:
print "Unknown message received in canframecb: ", msg
return
(canid, flags, data, ts) = parse_can(payload)
if do_relay(canid, len(data)):
cansend(txif, canid, data)
canframe = {'canid': canid,
'flags': flags,
'timestamp':ts,
'interface':rxif,
'dlc': len(data),
'annotation': get_annotation(id, len(data)),
'data':data}
frameout(canframe)
def frameout(canframe):
global tofile
if tofile:
global rxtime
ts = strftime("%%H:%%M:%%S.%03d"%((rxtime-int(rxtime))*1000),time.localtime(rxtime))
log = "%s %03X %s\n"%(ts, canframe['canid'], ' '.join(["%02X"%(i) for i in canframe['data']]))
tofile.write(log)
#print log
return
logframe(canframe)
if not do_filter(canframe['canid'], canframe['dlc']):
tracemsg("RX%d"%(canframe['interface']), canframe['timestamp'],canframe['canid'],canframe['flags'],canframe['data'])
global paused
if not paused:
webout(json.dumps({'canframe':canframe}));
def webstatus(status):
webout(json.dumps({'statustext':status}));
logfilename = strftime("%Y-%M-%d-%H-%M.plog");
logging = 0
logpickles = []
def logframe(frame):
global logging
if logging:
global logpickles
logpickles.append(frame)
def unlog(filename):
f = open(filename, "rb")
log = pickle.load(f);
for frame in log:
frameout(frame)
def tofileonly(filename):
global tofile
tofile = open(filename, "w+")
def closetofile():
global tofile
tofile.close()
tofile = None
def statscb(msg):
task = msg[0]
code = msg[1]
payload = msg[2:]
js = {}
if task == TASK_ID_CAN and code == CAN_STATS:
js["Bus_Error"] = as_u16(payload[0:2])
# js["Over time"] = as_u16(payload[2:4])
js["Stuff_Error"] = as_u16(payload[4:6])
js["CRC_Error"] = as_u16(payload[6:8])
js["Frame_Error"] = as_u16(payload[8:10])
js["Ack_Error" ] = as_u16(payload[10:12])
js["Unhandled_Interrupt"] = as_u16(payload[12:14])
js["MOB_RXOK"] = as_u16(payload[14:16])
js["MOB_TXOK"] = as_u16(payload[16:18])
# js["MOB DLCW"] = as_u16(payload[18:20])
js["MOB_Bit_Err"] = as_u16(payload[20:22])
js["MOB_Stuff_Error"] = as_u16(payload[22:24])
js["MOB_CRC_Error"] = as_u16(payload[24:26])
js["MOB_Frame_Error"] = as_u16(payload[26:28])
js["MOB_Ack_Error"] = as_u16(payload[28:30])
js["RX_Filtered"] = as_u16(payload[30:32])
js["RX_Relayed"] = as_u16(payload[32:34])
if task == TASK_ID_COMMS and code == COMMS_MSG_STATS:
js["Junk_bytes"] = as_u16(payload[16:18])
js["Error_bytes"] = as_u16(payload[18:20])
js["Checksum_errors"] = as_u16(payload[20:22])
js["Bad_code_packets"] = as_u16(payload[22:24])
js["RX_ring_overflow"] = as_u16(payload[24:26])
js["RX_no-buffers"] = as_u16(payload[26:28])
js["Uptime"] = as_u32(payload[30:34])
js["TX_no_buffers"] = as_u16(payload[34:36])
js["Mailbox_overflow"] = as_u16(payload[36:38])
js["UART_RX_overrun"] = as_u16(payload[38:40])
js["UART_RX_frame error"] = as_u16(payload[40:42])
js["UART_RX_parity error"] = as_u16(payload[42:44])
js["UART_TX_drop_ISR"] = as_u16(payload[46:48])
webout(json.dumps({'stats':js}))
timerstop = None
def initbus():
webstatus("Resetting 2CAN")
newspeed(500000)
webstatus("Setting CAN speed to 500kbps")
canspeed(500000)
mcpmode("normal")
rxqctrl.put(["resetcb"]);
add_rcv_cb(TASK_ID_MCP, COMMS_MSG_CAN_RAW, canframecb)
add_rcv_cb(TASK_ID_CAN, COMMS_MSG_CAN_RAW, canframecb)
# add_rcv_cb(TASK_ID_CAN, CAN_STATS, statscb)
# add_rcv_cb(TASK_ID_COMMS, COMMS_MSG_STATS, statscb)
update_hwfilters()
webstatus("Monitoring both CAN busses @ 500kbps")
def stats():
#send(TASK_ID_CAN, CAN_STATS, [0])
#send(TASK_ID_COMMS, COMMS_MSG_STATS)
update_summary()
if timerstop == None:
threading.Timer(1, stats).start()
stats()
def acceptfilter(sid, mask):
# program hardware acceptance filter to only accept the given SID
idtidm = range(0,8)
idtidm[0] = (mask&0x7FF)>>3
idtidm[1] = (mask&7)<<5
idtidm[2] = 0
idtidm[3] = 0
idtidm[4] = (sid&0x7FF)>>3
idtidm[5] = (sid&7)<<5
idtidm[6] = 0
idtidm[7] = 0
send(TASK_ID_CAN, CAN_SET_ACCEPT_FILTER, idtidm)
def logone(sid):
webstatus("Resetting 2CAN")
newspeed(500000)
webstatus("Setting CAN speed")
canspeed(500000)
mcpmode("normal")
rxqctrl.put(["resetcb"]);
add_rcv_cb(TASK_ID_CAN, COMMS_MSG_CAN_RAW, canframecb)
acceptfilter(sid, 0x7FF)
filename = strftime("%m-%d_%H_%M")+"_%03X"%(sid)+".log"
print "Logging to %s"%(filename)
tofileonly(filename)
pcancelf = 0
def periodic(ms, iface, sid, data):
global pcancelf
pcancelf = 0