-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfile2eeprom.py
More file actions
117 lines (91 loc) · 2.89 KB
/
Copy pathfile2eeprom.py
File metadata and controls
117 lines (91 loc) · 2.89 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
# Read or write a full serial EEPROM like 24xx512/128/64 etc
import time
import EasyMCP2221
import argparse
import textwrap
def hexint(x):
return int(x, 16)
parser = argparse.ArgumentParser(
description = 'Write or clear an EEPROM.',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog = textwrap.dedent("""
Delete a 24LC512:
file2eeprom.py -k 512 -p 128 -r 400 -w 5 -d
Write file to a 24LC128:
file2eeprom.py -k 128 -p 64 -r 400 -w 5 -f /tmp/eeprom
24AA16 uses a different protocol.
""")
)
parser.add_argument(
'-a','--address',
type=hexint,
required=False,
default=0x50,
help = "I2C slave 7 bit address in hex. E.g.: 50 for a common 24xx.")
parser.add_argument(
'-r','--rate',
type=int,
required=False,
default=400,
help = "I2C clock rate in kHz (default 400kHz).")
parser.add_argument(
'-k','--kbits',
type=int,
required=True,
help = "Memory size in kbits. E.g.: 128 for a 24xx128.")
parser.add_argument(
'-p','--page',
type=int,
required=True,
help = "Page size (EEPROM's internal buffer) in bytes. E.g.: 64 for a 24xx128, 128 for a 24xx512.")
parser.add_argument(
'-f','--file',
type=str,
required=False,
help = "File to save EEPROM content.")
parser.add_argument(
'-w','--wait',
type=int,
default=5,
required=False,
help = "Wait time for write cycle in ms. Usually 5.")
parser.add_argument(
'-d', '--delete',
action='store_true',
help = "Clear memory contents instead of reading a file.")
args = parser.parse_args()
if not args.file and not args.delete:
print("Error: File is needed if not delete.")
parser.print_help()
exit()
if args.file and args.delete:
print("Error: File and delete are mutually exclusive.")
parser.print_help()
exit()
mcp = EasyMCP2221.Device()
eeprom = mcp.I2C_Slave(args.address, speed = args.rate * 1000)
memsize = int(1024 * args.kbits / 8)
print("Writing EEPROM...")
start = time.perf_counter()
if args.delete:
for i in range(memsize//args.page):
print("Bytes %d/%d." %((i+1) * args.page, memsize))
buffer = b'\xff' * args.page
eeprom.write_register(i * args.page, buffer, reg_bytes=2)
until = time.perf_counter() + args.wait / 1000
while time.perf_counter() < until:
pass
else:
with open(args.file, 'rb') as f:
for i in range(memsize//args.page):
print("Bytes %d/%d." %((i+1) * args.page, memsize))
buffer = f.read(args.page)
if not buffer:
print("End of file.")
break
eeprom.write_register(i * args.page, buffer, reg_bytes=2)
until = time.perf_counter() + args.wait / 1000
while time.perf_counter() < until:
pass
end = time.perf_counter()
print("Elapsed time: %.1fs" % (end - start))