@@ -68,7 +68,7 @@ def STM32_sendCommand(cmd: bytes):
68
68
uart .write (_cmd )
69
69
70
70
71
- def STM32_readResponse () -> [bytearray , int ]:
71
+ def STM32_readResponse () -> [bytearray , bytes ]:
72
72
"""
73
73
Blocking read to get the STM32 response to command, according to AN3155
74
74
:return: returns a response bytearray dropping leading and trailing ACKs. returns -1 if NACK
@@ -81,7 +81,7 @@ def STM32_readResponse() -> [bytearray, int]:
81
81
if b is None :
82
82
continue
83
83
if b == STM32_NACK :
84
- return - 1
84
+ return STM32_NACK
85
85
elif b == STM32_ACK :
86
86
if acks == 1 :
87
87
break
@@ -113,7 +113,7 @@ def STM32_getID() -> bytearray:
113
113
"""
114
114
STM32_sendCommand (STM32_GET_ID )
115
115
res = STM32_readResponse ()
116
- if res == - 1 :
116
+ if res == STM32_NACK :
117
117
print ("GET_ID: STM32 responded with NACK" )
118
118
return bytearray (0 )
119
119
return res [1 :]
@@ -126,7 +126,7 @@ def STM32_getVER() -> bytearray:
126
126
"""
127
127
STM32_sendCommand (STM32_GET_VERSION )
128
128
res = STM32_readResponse ()
129
- if res == - 1 :
129
+ if res == STM32_NACK :
130
130
print ("GET VER: STM32 responded with NACK" )
131
131
return bytearray (0 )
132
132
return res
@@ -239,8 +239,13 @@ def STM32_readMEM(pages: int):
239
239
"""
240
240
241
241
for i in range (0 , pages ):
242
- _STM32_readMode ()
243
- _STM32_sendAddress (readAddress )
242
+ if _STM32_readMode () != STM32_ACK :
243
+ print ("COULD NOT ENTER READ MODE" )
244
+ return
245
+
246
+ if _STM32_sendAddress (readAddress ) != STM32_ACK :
247
+ print ("STM32 ERROR ON ADDRESS SENT" )
248
+ return
244
249
245
250
page = _STM32_readPage ()
246
251
print (f"Page { i + 1 } content:\n " )
@@ -252,24 +257,28 @@ def STM32_readMEM(pages: int):
252
257
def STM32_writeMEM (file_path : str ):
253
258
254
259
with open (file_path , 'rb' ) as f :
255
-
260
+ print ( f"Flashing { file_path } \n " )
256
261
while True :
257
262
data = bytearray (f .read (256 ))
258
263
read_bytes = len (data )
259
264
if read_bytes == 0 :
260
265
break
261
266
data .extend (bytearray ([255 ]* (256 - read_bytes ))) # 0xFF padding
262
267
263
- _STM32_writeMode ()
264
- _STM32_sendAddress (writeAddress )
268
+ if _STM32_writeMode () != STM32_ACK :
269
+ print ("COULD NOT ENTER WRITE MODE" )
270
+ return
271
+
272
+ if _STM32_sendAddress (writeAddress ) != STM32_ACK :
273
+ print ("STM32 ERROR ON ADDRESS SENT" )
274
+ return
275
+
276
+ if _STM32_flashPage (data ) != STM32_ACK :
277
+ print (f"STM32 ERROR FLASHING PAGE: { writeAddress } " )
278
+ return
265
279
266
- _STM32_flashPage (data )
267
- print ("\n Wrote\n " )
268
- print (data .hex ())
269
- print ("\n in\n " )
270
- print (writeAddress .hex ())
280
+ print ("." )
271
281
_incrementAddress (writeAddress )
272
- sleep_ms (100 )
273
282
274
283
275
284
def _STM32_standardEraseMEM (pages : int , page_list : bytearray = None ):
0 commit comments