-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicroWire.py
More file actions
137 lines (93 loc) · 4.58 KB
/
MicroWire.py
File metadata and controls
137 lines (93 loc) · 4.58 KB
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
#!/usr/bin/env python
# encoding: utf-8
import sys,time
from optparse import OptionParser
from pyBusPirateLite.RAW_WIRE import *
def main():
# First of all parse the command line
parser = OptionParser()
parser.add_option("-c", "--capacity", dest="capacity", help="size of the memory chip.", type="int")
parser.add_option("-o", "--org", dest="org", help="specify the memory organization mode (8 or 16).", type="int")
parser.add_option("-a", "--addr", dest="addr", help="set the starting offset of the read or write procedure.", type="int", default=0)
parser.add_option("-n", "--number", dest="n", help="the number of data elements to read or write.", type="int", default=0)
parser.add_option("-f", "--file", dest="file", help="the input or output file.", metavar="FILE")
parser.add_option("-r", "--read", dest="action", help="read the memory chip.", default="read")
parser.add_option("-w", "--write", dest="action", help="write to the memory chip.")
parser.add_option("-d", "--device", dest="device", help="serial interface where bus pirate is in.[/dev/bus_pirate]", default="/dev/bus_pirate")
parser.add_option("-v", "--verbose", dest="verbose", help="don't be quiet.", action="store_true")
parser.add_option("-m", "--more", dest="more", help="only for testing: read more data elements", type="int", default=0)
(options,args) = parser.parse_args()
if (not options.capacity) or (not options.org) or (not options.file):
parser.print_help()
exit()
# Create an instance of the RAW_WIRE class as we are using the BitBang/RAW_WIRE mode
#rw = RAW_WIRE( '/dev/bus_pirate', 115200 )
rw = RAW_WIRE( options.device, 115200 )
if not rw.BBmode():
print "Can't enter into BitBang mode."
exit()
# We have succesfully activated the BitBang Mode, so we continue with
# the raw-wire mode.
if not rw.enter_rawwire():
print "Can't enable the raw-wire mode."
exit()
# Now we have raw-wire mode enabled, so first configure peripherals
# (Power, PullUps, AUX, CS)
if not rw.raw_cfg_pins( PinCfg.POWER | PinCfg.CS ):
print "Error enabling the internal voltage regulators."
# Configure the raw-wire mode
if not rw.cfg_raw_wire( (RAW_WIRECfg.BIT_ORDER & RAW_WIRE_BIT_ORDER_TYPE.MSB) | (RAW_WIRECfg.WIRES & RAW_WIRE_WIRES_TYPE.THREE) | (RAW_WIRECfg.OUT_TYPE & RAW_WIRE_OUT_TYPE._3V3) ):
print "Error configuring the raw-wire mode."
# Set raw-wire speed
if not rw.set_speed( RAW_WIRESpeed._5KHZ ):
print "Error setting raw-wire speed."
# Open the file for reading or writting
if options.action == "read":
f = file(options.file, "wb")
else:
f = file(options.file, "rb")
# How many elements to read or write?
if options.n != 0:
N = options.n + options.more
else:
N = options.capacity / options.org + options.more
# Opcodes for microwire memory devices
#
# Op Address Data
#Instruction SB Code x8 x16 x8 x16 Comments
# READ 1 10 A8 – A0 A7 – A0 Reads data stored in memory, at specified address
# EWEN 1 00 11XXXXXXX 11XXXXXX Write enable must precede all programming modes
#
# ....
#
if options.action == "read":
# Enable the Chip select signal
rw.CS_High()
rw.bulk_trans(1, [0x6])
rw.bulk_trans(1, [0x0])
# and read the items
if options.verbose:
print "Reading %d elements of %d bits" % (N, options.org)
if options.org == 8:
for i in range(0,N):
byte = rw.read_byte()
f.write(byte)
if options.verbose:
print "%02X" % (ord(byte),) ,
else:
for i in range(0,N):
byte = rw.read_byte()
f.write(byte)
if options.verbose:
print "%02X" % (ord(byte),) ,
byte = rw.read_byte()
f.write(byte)
if options.verbose:
print "%02X" % (ord(byte),) ,
f.close()
rw.CS_Low()
print "Done."
# Reset the bus pirate
rw.resetBP();
if __name__ == '__main__':
main()