-
Notifications
You must be signed in to change notification settings - Fork 1
/
dumpi2c.py
97 lines (83 loc) · 2.86 KB
/
dumpi2c.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
#!/usr/bin/env python
# encoding: utf-8
"""
Adapted from i2c-test.py from Peter Huewe
"""
import sys
import argparse
import binascii
try:
from pyBusPirateLite.I2C import *
except ImportError:
sys.stderr.write("Could not find pyBusPirateLite library!\n")
sys.stderr.write("See: http://dangerousprototypes.com/docs/Bus_Pirate_Scripting_in_Python\n")
sys.exit(1)
def i2c_write_data(data):
i2c.send_start_bit()
i2c.bulk_trans(len(data),data)
i2c.send_stop_bit()
def i2c_read_bytes(address, numbytes, ret=False):
data_out=[]
i2c.send_start_bit()
i2c.bulk_trans(len(address),address)
while numbytes > 0:
if not ret:
print ord(i2c.read_byte())
else:
data_out.append(ord(i2c.read_byte()))
if numbytes > 1:
i2c.send_ack()
numbytes-=1
i2c.send_nack()
i2c.send_stop_bit()
if ret:
return data_out
if __name__ == '__main__':
parser = argparse.ArgumentParser(sys.argv[0])
parser.add_argument("-o", "--output", dest="outfile", metavar="OUTFILE", type=argparse.FileType('wb'),
required=True)
parser.add_argument("-p", "--serial-port", dest="bp", default="/dev/ttyUSB0")
parser.add_argument("-s", "--size", dest="size", type=int, required=True)
parser.add_argument("-S", "--serial-speed", dest="speed", default=115200, type=int)
parser.add_argument("-b", "--block-size", dest="bsize", default=256, type=int)
args = parser.parse_args(sys.argv[1:])
i2c = I2C(args.bp, args.speed)
print "Entering binmode: ",
if i2c.BBmode():
print "OK."
else:
print "failed."
sys.exit()
print "Entering raw I2C mode: ",
if i2c.enter_I2C():
print "OK."
else:
print "failed."
sys.exit()
print "Configuring I2C."
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS):
print "Failed to set I2C peripherals."
sys.exit()
if not i2c.set_speed(I2CSpeed._400KHZ):
print "Failed to set I2C Speed."
sys.exit()
i2c.timeout(0.2)
print "Dumping %d bytes out of the EEPROM." % args.size
# Start dumping
for block in range(0, args.size, args.bsize):
# Reset the address
i2c_write_data([0xa0 + ((block / args.bsize) << 1), 0])
print 0xa0 + ((block / args.bsize) << 1)
data = " ".join([hex(x) for x in i2c_read_bytes([0xa1 + ((block / args.bsize) << 1)], args.bsize, True)])
args.outfile.write((data))
if args.size % 16 != 0:
end = 16 * (args.size / args.bsize)
data = " ".join([hex(x) for x in i2c_read_bytes([0xa1 + ((args.size / args.bsize) << 1)], args.size % args.bsize, True)])
args.outfile.write((data))
args.outfile.close()
print "Reset Bus Pirate to user terminal: "
if i2c.resetBP():
print "OK."
else:
print "failed."
sys.exit()