@@ -19,9 +19,8 @@ def debug_response(command, response, fmt='STM BL RESPONSE TO 0x{}: {}'):
19
19
20
20
def send_bootloader_command (port , command , response_size = 1 ):
21
21
port .write ([command , 0xff - command ])
22
- port . flush ( )
22
+ time . sleep ( 0.01 )
23
23
response = port .read (response_size )
24
- debug_response (command , response )
25
24
return response
26
25
27
26
@@ -44,22 +43,32 @@ def compute_address_commandable(address):
44
43
45
44
46
45
def prepare_bootloader (port ):
47
- click .echo ('Preparing bootloader...' , nl = False )
48
- prosflasher .upload .configure_port (port , serial .PARITY_EVEN )
49
- port .flush ()
50
- port .write ([0x7f ])
51
- response = port .read (1 )
52
- debug_response (0x07f , response )
53
- if response is None or len (response ) != 1 or response [0 ] != ACK :
54
- click .echo ('failed' )
55
- return False
56
- click .echo ('complete' )
57
- if click .get_current_context ().obj .verbosity > 1 :
58
- click .echo ('Extra commands:' )
59
- send_bootloader_command (port , 0x00 , 15 )
60
- send_bootloader_command (port , 0x01 , 5 )
61
- send_bootloader_command (port , 0x02 , 5 )
62
- return True
46
+ click .echo ('Preparing bootloader... ' , nl = False )
47
+ # for _ in range(0, 3):
48
+ # response = send_bootloader_command(port, 0x00, 15)
49
+ # if response is not None and len(response) == 15 and response[0] == ACK and response[-1] == ACK:
50
+ # click.echo('complete')
51
+ # return True
52
+ time .sleep (0.01 )
53
+ port .rts = 0
54
+ time .sleep (0.01 )
55
+ for _ in range (0 ,3 ):
56
+ port .write ([0x7f ])
57
+ response = port .read (1 )
58
+ debug_response (0x7f , response )
59
+ if not (response is None or len (response ) != 1 or response [0 ] != ACK ):
60
+ time .sleep (0.01 )
61
+ response = send_bootloader_command (port , 0x00 , 15 )
62
+ debug_response (0x00 , response )
63
+ if response is None or len (response ) != 15 or response [0 ] != ACK or response [- 1 ] != ACK :
64
+ click .echo ('failed (couldn\' t verify commands)' )
65
+ return False
66
+ # send_bootloader_command(port, 0x01, 5)
67
+ # send_bootloader_command(port, 0x02, 5)
68
+ click .echo ('complete' )
69
+ return True
70
+ click .echo ('failed' )
71
+ return False
63
72
64
73
65
74
def read_memory (port , start_address , size = 0x100 ):
@@ -95,59 +104,73 @@ def read_memory(port, start_address, size=0x100):
95
104
96
105
97
106
def erase_flash (port ):
98
- click .echo ('Erasing user flash...' , nl = False )
99
- prosflasher . upload . configure_port ( port , serial . PARITY_EVEN )
107
+ click .echo ('Erasing user flash... ' , nl = False )
108
+ port . flush ( )
100
109
response = send_bootloader_command (port , 0x43 , 1 )
101
- if response is None or response [0 ] != 0x79 :
110
+ if response is None or len ( response ) < 1 or response [0 ] != 0x79 :
102
111
click .echo ('failed' )
103
112
return False
113
+ time .sleep (0.01 )
104
114
response = send_bootloader_command (port , 0xff , 1 )
105
- if response is None or response [0 ] != 0x79 :
106
- click .echo ('failed' )
115
+ debug_response (0xff , response )
116
+ if response is None or len (response ) < 1 or response [0 ] != 0x79 :
117
+ click .echo ('failed (address unacceptable)' )
107
118
return False
108
119
click .echo ('complete' )
109
120
return True
110
121
111
122
112
- def write_flash (port , start_address , data ):
123
+ def write_flash (port , start_address , data , retry = 2 ):
113
124
data = bytearray (data )
114
125
if len (data ) > 256 :
115
126
click .echo ('Tried writing too much data at once! ({} bytes)' .format (len (data )))
116
127
return False
117
128
port .read_all ()
118
- start_address = compute_address_commandable (start_address )
119
- debug ('Writing {} bytes to {}' .format (len (data ), adr_to_str (start_address )))
129
+ c_addr = compute_address_commandable (start_address )
130
+ debug ('Writing {} bytes to {}' .format (len (data ), adr_to_str (c_addr )))
120
131
response = send_bootloader_command (port , 0x31 )
121
- if response is None or response [0 ] != ACK :
132
+ if response is None or len ( response ) < 1 or response [0 ] != ACK :
122
133
click .echo ('failed (write command not accepted)' )
123
134
return False
124
- port .write (start_address )
135
+ port .write (c_addr )
125
136
port .flush ()
126
137
response = port .read (1 )
127
- debug_response (adr_to_str (start_address ), response )
128
- if response is None or response [0 ] != ACK :
129
- click .echo ('failed (address not accepted)' )
130
- return False
138
+ debug_response (adr_to_str (c_addr ), response )
139
+ if response is None or len (response ) < 1 or response [0 ] != ACK :
140
+ if retry > 0 :
141
+ debug ('RETRYING PACKET' )
142
+ write_flash (port , start_address , data , retry = retry - 1 )
143
+ else :
144
+ click .echo ('failed (address not accepted)' )
145
+ return False
131
146
checksum = len (data ) - 1
132
147
for x in data :
133
148
checksum ^= x
134
- data .insert (0 , len (data ) - 1 )
135
- data .append (checksum )
136
- port .write (data )
149
+ send_data = data
150
+ send_data .insert (0 , len (send_data ) - 1 )
151
+ send_data .append (checksum )
152
+ port .write (send_data )
137
153
time .sleep (0.005 )
138
154
response = port .read (1 )
139
- if response is None or response [0 ] != ACK :
140
- port .write (data )
141
- time .sleep (20 )
142
- response = port .read (1 )
143
- if response is None or response [0 ] != ACK :
155
+ debug ('STM BL RESPONSE TO WRITE: {}' .format (response ))
156
+ if response is None or len (response ) < 1 or response [0 ] != ACK :
157
+ if retry > 0 :
158
+ debug ('RETRYING PACKET' )
159
+ write_flash (port , start_address , data , retry = retry - 1 )
160
+ else :
144
161
click .echo ('failed (could not complete upload)' )
145
162
return False
146
163
port .flush ()
147
164
port .reset_input_buffer ()
148
165
return True
149
166
150
167
168
+ def chunks (l , n ):
169
+ """Yield successive n-sized chunks from l."""
170
+ for i in range (0 , len (l ), n ):
171
+ yield l [i :i + n ]
172
+
173
+
151
174
def upload_binary (port , file ):
152
175
address = 0x08000000
153
176
with open (file , 'rb' ) as f :
@@ -161,18 +184,23 @@ def upload_binary(port, file):
161
184
return True
162
185
163
186
164
- def send_go_command (port , address ):
165
- click .echo ('Executing binary ...' , nl = False )
166
- address = compute_address_commandable (address )
167
- debug ('Executing binary at {}' .format (adr_to_str (address )))
187
+ def send_go_command (port , address , retry = 3 ):
188
+ click .echo ('Executing user code ... ' , nl = False )
189
+ c_addr = compute_address_commandable (address )
190
+ debug ('Executing binary at {}' .format (adr_to_str (c_addr )))
168
191
169
192
response = send_bootloader_command (port , 0x21 , 1 )
170
- if response is None or response [0 ] != ACK :
193
+ debug_response (0x21 , response )
194
+ if response is None or len (response ) < 1 or response [0 ] != ACK :
171
195
click .echo ('failed (execute command not accepted)' )
172
196
return False
173
- port .write (address )
174
- port .flush ()
175
- click .echo ('complete' )
197
+ time .sleep (0.01 )
198
+ port .write (c_addr )
199
+ time .sleep (0.01 )
200
+ response = port .read (1 )
201
+ debug_response (adr_to_str (c_addr ), response )
202
+ if response is None or len (response ) < 1 or response [0 ] != ACK :
203
+ click .echo ('user code might not have started properly. May need to restart Cortex' )
204
+ else :
205
+ click .echo ('complete' )
176
206
return True
177
-
178
-
0 commit comments