-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeprog.py
executable file
·73 lines (59 loc) · 1.6 KB
/
eeprog.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
#!/usr/bin/env python
import serial
import sys
import time
cmd = sys.argv[1]
port = sys.argv[2]
file = sys.argv[3]
# Open a new connection. This will reset the arduino.
with serial.Serial(port, 115200, timeout=1) as ser:
time.sleep(5)
ser.flush()
# Check th version number.
ser.write(b'v')
version = int(ser.readline())
if version != 1:
print('Invalid version.')
sys.exit(1)
# Read the block size.
ser.write(b'b')
bs = int(ser.readline())
if cmd == 'u':
with open(file, 'rb') as inf:
data = inf.read()
length = len(data)
if length > 0xFFFF:
print('File too large.')
sys.exit(1)
length = len(data)
if length % bs != 0:
print('Invalid file size')
sys.exit(1)
# Upload chunks.
print("Uploading {} bytes from {}".format(length, file))
ser.write(b'u')
ser.write(bytearray([length & 0xFF, length >> 8]))
chunks = int(length / bs) + (1 if length % bs != 0 else 0)
for i in range(0, chunks):
chunk = data[i * bs : (i + 1) * bs]
ser.write(chunk)
while True:
ch = ser.read(1)
if len(ch) == 0:
continue
if ch == b'.':
sys.stdout.write('.')
sys.stdout.flush()
elif ch == b'y':
sys.stdout.write('\n')
break
else:
sys.stdout.write('\nWrite failed: ' + str(ch) + '\n')
sys.exit(0)
if cmd == 'd':
length = 8192
with open(file, 'wb') as outf:
for i in range(0, int(length / bs)):
ser.write(b'd')
ser.write(bytearray([i & 0xFF, i >> 8]))
outf.write(ser.read(bs))