forked from audiohacked/python-pyBusPirate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_test_binmode.py
143 lines (121 loc) · 3.68 KB
/
spi_test_binmode.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
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Sean Nelson on 2009-10-14.
Copyright 2009-2013 Sean Nelson <audiohacked@gmail.com>
This file is part of pyBusPirate.
pyBusPirate 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.
pyBusPirate 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 pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import sys, optparse
from pyBusPirate.BinaryMode.SPI import *
def read_list_data(size):
data = []
for i in range(size+1):
data.append(0)
return data
def parse_prog_args():
parser = optparse.OptionParser(usage="%prog [options] filename",
version="%prog 1.0")
parser.set_defaults(command="read")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=True,
help="make lots of noise [default]")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose",
help="be mute")
parser.add_option("-r", "--read",
action="store_const", dest="command", const="read",
help="read from SPI to file [default]")
parser.add_option("-w", "--write",
action="store_const", dest="command", const="write",
help="write from file to SPI")
parser.add_option("-e", "--erase",
action="store_const", dest="command", const="erase",
help="erase SPI")
parser.add_option("-i", "--id",
action="store_const", dest="command", const="id",
help="print Chip ID")
parser.add_option("-s", "--size",
dest="flash_size", default=128,
help="Size of Flashchip in bytes", type="int")
(options, args) = parser.parse_args()
if options.command == "id":
return (options, args)
elif len(args) != 1:
parser.print_help()
print options
sys.exit(1)
else:
return (options, args)
""" enter binary mode """
if __name__ == '__main__':
data = ""
(opt, args) = parse_prog_args()
if opt.command == "read":
f=open(args[0], 'wb')
elif opt.command == "write":
f=open(args[0], 'rb')
spi = SPI("/dev/tty.usbserial-A7004qlY", 115200)
print "Entering binmode: ",
if spi.BBmode():
print "OK."
else:
print "failed."
sys.exit()
print "Entering raw SPI mode: ",
if spi.enter_SPI():
print "OK."
else:
print "failed."
sys.exit()
print "Configuring SPI."
if spi.cfg_pins(PinCfg.POWER | PinCfg.CS | PinCfg.AUX):
print "Failed to set SPI peripherals."
sys.exit()
if spi.set_speed(SPISpeed._2_6MHZ):
print "Failed to set SPI Speed."
sys.exit()
if spi.cfg_spi(SPICfg.CLK_EDGE | SPICfg.OUT_TYPE):
print "Failed to set SPI configuration.";
sys.exit()
spi.timeout(0.2)
if opt.command == "read":
print "Reading EEPROM."
spi.CS_Low()
spi.bulk_trans(5, [0xB, 0, 0, 0, 0])
for i in range((int(opt.flash_size)/16)):
data = spi.bulk_trans(16, read_list_data(16))
f.write(data)
spi.CS_High()
elif opt.command == "write":
print "Writing EEPROM."
spi.CS_Low()
spi.bulk_trans(4, [0xA, 0, 0, 0])
for i in range((int(opt.flash_size)/16)):
spi.bulk_trans(16, None)
spi.CS_High()
elif opt.command == "id":
print "Reading Chip ID: ",
spi.CS_Low()
d = spi.bulk_trans(4, [0x9F, 0, 0, 0])
spi.CS_High()
for each in d[1:]:
print "%02X " % ord(each),
print
elif opt.command == "erase":
pass
print "Reset Bus Pirate to user terminal: ",
if spi.resetBP():
print "OK."
else:
print "failed."
sys.exit()