-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSTK500.py
209 lines (169 loc) · 4.88 KB
/
STK500.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
#import libraries
import time
import bluetooth as bt
# define a rfcomm bluetooth connection
sock = bt.BluetoothSocket(bt.RFCOMM)
# connect to the bluetooth device
sock.connect(("98:D3:31:B1:CA:E9", 1))
# get in sync with the AVR
for i in range(5):
print "syncing"
sock.send('\x30') # STK_GET_SYNC
sock.send('\x20') # STK_CRC_EOP
time.sleep(0.05)
# receive sync ack
print "receiving sync ack"
insync = sock.recv(1) # STK_INSYNC
ok = sock.recv(1) # STK_OK
# check received ack
if insync == '\x14' and ok == '\x10':
print "insync"
# get the MAJOR version of the bootloader
print "getting the MAJOR version of the bootloader"
sock.send('\x41') # STK_GET_PARAMETER
sock.send('\x81') # STK_SW_MAJOR
sock.send('\x20') # SYNC_CRC_EOP
time.sleep(0.05)
# receive bootlader MAJOR version
print "receiving bootloader MAJOR version"
insync = sock.recv(1) # STK_INSYNC
major = sock.recv(1) # STK_SW_MJAOR
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
# get the MINOR version of the bootloader
print "getting the MINOR version of the bootloader"
sock.send('\x41') # STK_GET_PARAMETER
sock.send('\x82') # STK_SW_MINOR
sock.send('\x20') # SYNC_CRC_EOP
time.sleep(0.05)
# receive bootlader MINOR version
print "receiving bootloader MINOR version"
insync = sock.recv(1) # STK_INSYNC
minor = sock.recv(1) # STK_SW_MINOR
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
print "bootloader version %s.%s" % (ord(major), ord(minor))
# enter programming mode
print "entering programming mode"
sock.send('\x50') # STK_ENTER_PROGMODE
sock.send('\x20') # SYNC_CRC_EOP
time.sleep(0.05)
# receive sync ack
print "receiving sync ack"
insync = sock.recv(1) # STK_INSYNC
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
# get device signature
print "getting device signature"
sock.send('\x75') # STK_READ_SIGN
sock.send('\x20') # SYNC_CRC_EOP
# receive device signature
print "receiving device signature"
insync = sock.recv(1) # STK_INSYNC
signature = sock.recv(3) # device
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
print "device signature %s-%s-%s" % (ord(signature[0]), ord(signature[1]), ord(signature[2]))
# start with page address 0
address = 0
# open the hex file
program = open("main.hex", "rb")
while True:
# calculate page address
laddress = chr(address % 256)
haddress = chr(address / 256)
address += 64
# load page address
print "loading page address"
sock.send('\x55') # STK_LOAD_ADDRESS
sock.send(laddress)
sock.send(haddress)
sock.send('\x20') # SYNC_CRC_EOP
#time.sleep(0.01)
# receive sync ack
print "receiving sync ack"
insync = sock.recv(1) # STK_INSYNC
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
data = ""
# the hex in the file is represented in char
# so we have to merge 2 chars into one byte
# 16 bytes in a line, 16 * 8 = 128
for i in range(8):
# just take the program data
data += program.readline()[9:][:-4]
# half the size
size = chr(len(data)/2)
print "sending program page to write", ord(size), "laddress", ord(laddress), "haddress", ord(haddress)
sock.send('\x64') # STK_PROGRAM_PAGE
sock.send('\x00') # page size
sock.send(size) # page size
sock.send('\x46') # flash memory, 'F'
# while data left
while data:
# assemble a byte and send it
sock.send(chr(int(data[:2], 16)))
# chop of sent data
data = data[2:]
sock.send('\x20') # SYNC_CRC_EOP
#time.sleep(0.01)
# receive sync ack
print "receiving sync ack"
insync = sock.recv(1) # STK_INSYNC
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
# when the whole program was uploaded
if size != '\x80':
break
# close the hex file
program.close()
# load page address
#print "loading page address"
#sock.send('\x55') # STK_LOAD_ADDRESS
#sock.send('\x00')
#sock.send('\x00')
#sock.send('\x20') # SYNC_CRC_EOP
#time.sleep(0.05)
# receive sync ack
#print "receiving sync ack"
#insync = sock.recv(1) # STK_INSYNC
#ok = sock.recv(1) # STK_OK
# check received sync ack
#if insync == '\x14' and ok == '\x10':
# print "insync"
# send read program page
#print "sending program page to read"
#sock.send('\x74') # STK_READ_PAGE
#sock.send('\x00') # page size
#sock.send('\x80') # page size
#sock.send('\x46') # flash memory, 'F'
#sock.send('\x20') # SYNC_CRC_EOP
#time.sleep(0.05)
#print len(sock.recv(128))
# leave programming mode
print "leaving programming mode"
sock.send('\x51') # STK_LEAVE_PROGMODE
sock.send('\x20') # SYNC_CRC_EOP
time.sleep(0.05)
# receive sync ack
print "receiving sync ack"
insync = sock.recv(1) # STK_INSYNC
ok = sock.recv(1) # STK_OK
# check received sync ack
if insync == '\x14' and ok == '\x10':
print "insync"
# close the bluetooth connection
sock.close()