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
|
#!/usr/bin/python -S
# Copyright 2004-present Facebook. All rights reserved.
from argparse import ArgumentParser
from at93c46 import AT93C46, AT93C46SPI
import sys
def get_raw(args):
return AT93C46SPI(args.bus_width, args.cs, args.clk, args.mosi, args.miso,
args.verbose)
def get_chip(args):
return AT93C46(args.bus_width, args.cs, args.clk, args.mosi, args.miso,
args.byte_swap, args.verbose)
def access_parser(ap):
# Default, based on currenct HW configuration
SPI_CS_DEFAULT = 68
SPI_CLK_DEFAULT = 69
SPI_MOSI_DEFAULT = 70
SPI_MISO_DEFAULT = 71
spi_group = ap.add_argument_group('SPI Access')
spi_group.add_argument('--cs', type=int, default=SPI_CS_DEFAULT,
help='The GPIO number for SPI CS pin (default: %s)'
% SPI_CS_DEFAULT)
spi_group.add_argument('--clk', type=int, default=SPI_CLK_DEFAULT,
help='The GPIO number for SPI CLK pin (default: %s)'
% SPI_CLK_DEFAULT)
spi_group.add_argument('--mosi', type=int, default=SPI_MOSI_DEFAULT,
help='The GPIO number for SPI MOSI pin (default: %s)'
% SPI_MOSI_DEFAULT)
spi_group.add_argument('--miso', type=int, default=SPI_MISO_DEFAULT,
help='The GPIO number for SPI MISO pin (default: %s)'
% SPI_MISO_DEFAULT)
def bus_width_parser(ap):
# Default, based on currenct HW configuration
AT83C46_BUS_WIDTH = 16
bus_group = ap.add_argument_group('Bus Width')
bus_group.add_argument('--bus-width', type=int, default=AT83C46_BUS_WIDTH,
help='The configured bus width (default: %s)'
% AT83C46_BUS_WIDTH)
def read_raw(args):
raw = get_raw(args)
val = raw.read(args.address)
if args.int:
print "{}".format(val)
else:
if args.bus_width == 16:
print "0x{:04X}".format(val)
else:
print "0x{:02X}".format(val)
def write_raw(args):
if args.value[:2] == "0x":
value = int(args.value, 16)
else:
value = int(args.value)
raw = get_raw(args)
raw.ewen()
raw.erase(args.address)
raw.write(args.address, value)
raw.ewds()
def erase_raw(args):
raw = get_raw(args)
raw.ewen()
raw.erase(args.address)
raw.ewds()
def raw_subparser(subparsers):
raw_parser = subparsers.add_parser('raw', help='Raw memory access')
raw_sub = raw_parser.add_subparsers()
read_parser = raw_sub.add_parser('read', help='Read a single memory address')
read_parser.add_argument('address', type=int, help='The memory address')
read_parser.add_argument('--int', action='store_true',
help='Display output as an integer')
read_parser.set_defaults(func=read_raw)
write_parser = raw_sub.add_parser('write', help='Write a single memory address')
write_parser.add_argument('address', type=int, help='The memory address')
write_parser.add_argument('value', type=str, help='The value to write, either integer or hex')
write_parser.set_defaults(func=write_raw)
erase_parser = raw_sub.add_parser('erase', help='Erase a single memory address')
erase_parser.add_argument('address', type=int, help='The memory address')
erase_parser.set_defaults(func=erase_raw)
def read_chip(args):
chip = get_chip(args)
data = chip.read(args.start, args.length)
if args.file is None:
sys.stdout.write(data)
else:
fp = open(args.file, "wb")
fp.write(data)
def write_chip(args):
chip = get_chip(args)
# Either way, limit reads to the size of the chip
if args.file is None:
data = sys.stdin.read(AT93C46.AT93C46_MEMORY_SIZE)
else:
fp = open(args.file, "rb")
data = fp.read(AT93C46.AT93C46_MEMORY_SIZE)
if args.length is not None:
# Make sure length is correct
if len(data) < args.length:
data = data + '\x00' * (args.length - len(data))
if len(data) > args.length:
data = data[:args.length]
chip.write(data, args.start)
def erase_chip(args):
chip = get_chip(args)
chip.erase(args.start, args.length)
def chip_subparser(subparsers):
chip_parser = subparsers.add_parser('chip', help='Chip-level access')
chip_sub = chip_parser.add_subparsers()
read_parser = chip_sub.add_parser('read', help='Read from the chip')
read_parser.add_argument('--start', type=int,
help='The memory address to start at (default: 0)')
read_parser.add_argument('--length', type=int,
help='The number of bytes to read (default: whole chip)')
read_parser.add_argument('--file', type=str,
help='File to operate on (default: stdout)')
read_parser.add_argument('--byte-swap', action='store_true',
help='Byte swap values for 16-bit reads/writes')
read_parser.set_defaults(func=read_chip)
write_parser = chip_sub.add_parser('write', help='Write to the chip')
write_parser.add_argument('--start', type=int,
help='The memory address to start at (default: 0)')
write_parser.add_argument('--length', type=int,
help='The number of bytes to write (default: file length)')
write_parser.add_argument('--file', type=str,
help='File to operate on (default: stdin)')
write_parser.add_argument('--byte-swap', action='store_true',
help='Byte swap values for 16-bit reads/writes')
write_parser.set_defaults(func=write_chip)
erase_parser = chip_sub.add_parser('erase', help='Erase the chip')
erase_parser.add_argument('--start', type=int,
help='The memory address to start at (default: 0)')
erase_parser.add_argument('--length', type=int,
help='The number of bytes to erase (default: whole chip)')
erase_parser.set_defaults(func=erase_chip)
if __name__ == "__main__":
# General arguments
ap = ArgumentParser()
ap.add_argument('--verbose', action='store_true',
help='Print verbose debugging information')
# SPI and bus width arguments
access_parser(ap)
bus_width_parser(ap)
# Functionality
subparsers = ap.add_subparsers()
raw_subparser(subparsers)
chip_subparser(subparsers)
# Command runner
args = ap.parse_args()
args.func(args)
|