-
Notifications
You must be signed in to change notification settings - Fork 3
/
canbootloaderutils.py
executable file
·64 lines (53 loc) · 1.49 KB
/
canbootloaderutils.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
#!/usr/bin/env python
'''
Utilities for communicating with CAN FLIP ISP
@author: Bob Jacobsen
@author: D.E. Goodman-Wilson
'''
'''
Check that a frame is of a given type
'''
def isFrameType(frame, CMD, CRIS) :
if not frame.startswith(":S"): return False
if (int(frame.split('S')[-1].split('N')[0], 16) == (CRIS<<3) + CMD): return True
return False
'''
Turn frame values into a string for sending to the interface.
header is the int value of the CAN frame header.
body is an array of 0 to 8 bytes of data for the frame body.
'''
def makeframestring(CRIS, command, body) :
retval = ":S"
retval += "%0.8X" % ((CRIS<<3)+command)
retval += "N"
if (body != None) :
for a in body :
retval += ("00"+(hex(a).upper()[2:]))[-2:]
retval += ";"
return retval
'''
Take a hex-byte-sequence string like 1.2.3.a3.4 and return
an array of ints, used for e.g. input of node and event IDs
'''
def splitSequence(seq) :
strings = seq.split('.')
result = []
for a in strings :
result = result+[int(a, 16)]
return result
'''
Pull body bytes from frame as array
'''
def bodyArray(frame) :
string = frame.split('N')[-1]
result = []
while (string[0] != ';') :
result = result+[int(string[:2],16)]
string = string[2:]
return result
def main():
print makeframestring(0xFF, 0xFF, [0x00])
print splitSequence("1.2.3.a.0a.10.4")
print bodyArray(":X1E00000N0F010203040506;")
if __name__ == '__main__':
main()