-
Notifications
You must be signed in to change notification settings - Fork 3
/
magboot.py
executable file
·185 lines (141 loc) · 3.59 KB
/
magboot.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
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
179
180
181
182
183
184
185
#! /usr/bin/env python
# Copyright (c) 2010-2011 Magnus Olsson (magnus@minimum.se)
# See LICENSE for details
"""magboot - AVR serial bootloader
usage: magboot <port> <device> <[command1] [command2] ... [commandN]>
commands:
-w <file> Write contents of <file> to beginning of flash (use - for stdin)
-i Verify device signature
-r Reset device (will bypass bootloader on next boot)
-z Wait for device to appear on <port>
supported devices:
atmega328p
atmega168p
"""
import serial
import getopt
import sys
from struct import *
from array import array
global ser
global devicelist
global dev
atmega328p = {
'signature': "\x1E\x95\x0F",
'pagesize': 128,
}
atmega168p = {
'signature': "\x1E\x94\x0B",
'pagesize': 128,
}
devicelist = {
'atmega328p': atmega328p,
'atmega168p': atmega168p,
}
def usage(*args):
sys.stdout = sys.stderr
print __doc__
for msg in args:
print msg
sys.exit(2)
def do_cmd(str, expect_reply=True):
ser.write(str)
if not expect_reply:
print "> OK"
return
reply = ser.read()
if (len(reply) == 0):
print "> FAILED (TIMEOUT)"
sys.exit(1)
if (reply == 'Y'):
print "> OK"
return
if (reply == 'N'):
print "> FAILED"
else:
print "> FAILED (UNKNOWN):"
while (len(reply) == 1):
print reply
reply = ser.read()
sys.exit(1)
def cmd_device_id():
print "ID"
do_cmd('I' + dev['signature'])
def cmd_load_addr(addr):
print "LOAD_ADDR"
# Little-endian, 16-bit uint load address
load_addr = pack('<H', addr)
do_cmd('A' + load_addr)
def checksum(data):
csum = 0
words = array('H', data)
for w in words:
csum = csum + w
# Fold overflow bits
while (csum >> 16):
csum = (csum & 0xFFFF) + (csum >> 16)
return pack('<H', csum)
def cmd_write_file(fname):
print "WRITE_FILE"
if (fname == "-"):
f = sys.stdin
else:
f = open(fname, "rb")
eof = False
cmd_load_addr(0)
while (not eof):
buf = array('c')
bytecount = 0
while (bytecount < dev['pagesize']):
data = f.read(1)
if (len(data) == 0):
eof = True
break
buf.append(data)
bytecount = bytecount + 1
# Zerofill remaining part of page
if (bytecount != dev['pagesize']):
while (bytecount < dev['pagesize']):
buf.append(chr(0))
bytecount = bytecount + 1
bufstr = buf.tostring()
csum = checksum(bufstr)
do_cmd('W' + csum + bufstr)
def cmd_reset():
print "RESET"
do_cmd('R', False)
def cmd_wait():
print "WAIT"
while True:
sys.stdout.flush()
ser.write('I' + dev['signature'])
data = ser.read()
if (len(data) == 1 and data == 'Y'):
break
sys.stdout.write('.')
sys.stdout.write('\n')
if __name__ == "__main__":
if len(sys.argv) < 4:
usage()
try:
dev = devicelist[sys.argv[2]]
except KeyError:
print "Unsupported device '" + sys.argv[2] + "'"
sys.exit(3)
ser = serial.Serial(port=sys.argv[1], baudrate=19200, timeout=1)
try:
opts, args = getopt.getopt(sys.argv[3:], 'a:w:ijrz')
except getopt.error, msg:
usage(msg)
ser.flushInput()
ser.flushOutput()
for o, a in opts:
if o == '-w':
cmd_write_file(a)
if o == '-i':
cmd_device_id()
if o == '-r':
cmd_reset()
if o == '-z':
cmd_wait()
ser.close()